gpt4 book ai didi

php - 管理角色并为角色分配权限 - Symfony

转载 作者:可可西里 更新时间:2023-10-31 22:40:46 25 4
gpt4 key购买 nike

我正在 Symfony 3 中基于角色和权限构建一个管理面板。每个管理员将被分配一个角色(或多个角色),然后他将能够根据分配给该角色的权限执行操作。


给你一个想法,这里有一个例子:

  • 管理面板具有添加用户、编辑用户和删除用户的功能。
  • 我创建了一个角色:USER_MANAGEMENT_WITHOUT_DELETE,它具有user_createuser_edit 的权限。
  • 我创建了 USER_MANAGEMENT_WITH_DELETE 角色,该角色有权执行 user_createuser_edituser_delete
  • 现在,具有 USER_MANAGEMENT_WITH_DELETE 角色的管理员可以添加编辑删除 用户角色 USER_MANAGEMENT_WITHOUT_DELETE 只能添加编辑 用户,但不能删除他们。

我搜索并找到了关于 FOSUserBundle 的信息和 ACL .一些recommended ACL而其他人则说 use FOSUserBunder 更好

我还阅读了 FOSUserBunder 的文档以及它如何在 roles 列中存储角色,例如 a:1:{i:0;s:10:"ROLE_ADMIN";},但没有提到权限。所以这是我的查询:

  1. 我对两者感到困惑。我应该使用哪一个?
  2. 如果我使用FOSUserBunder,如何管理权限?

最佳答案

角色不是特定于 FOSUserBundle。它们在 Symfony 中。

ACLs比使用角色更复杂。所以我建议使用角色。

来自 Symfony 文档:ACL 的替代品

Using ACL's isn't trivial, and for simpler use cases, it may be overkill. If your permission logic could be described by just writing some code (e.g. to check if a Blog is owned by the current User), then consider using voters. A voter is passed the object being voted on, which you can use to make complex decisions and effectively implement your own ACL. Enforcing authorization (e.g. the isGranted part) will look similar to what you see in this entry, but your voter class will handle the logic behind the scenes, instead of the ACL system.

为了处理“权限”,我建议使用 Voters :

首先像这样创建一个选民:

配置:

# app/config/services.yml
services:
app.user_permissions:
class: AppBundle\Voters\UserPermissionsVoter
arguments: ['@security.access.decision_manager']
tags:
- { name: security.voter }
public: false

类(class):

namespace AppBundle\Voters;

use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\User\UserInterface;

class UserPermissionsVoter extends Voter
{
const USER_CREATE = 'user_create';
const USER_EDIT = 'user_edit';
const USER_DELETE = 'user_delete';

private $decisionManager;

public function __construct($decisionManager)
{
$this->decisionManager = $decisionManager;
}

protected function supports($attribute, $object)
{
if (!in_array($attribute, array(self::USER_CREATE,self::USER_EDIT,self::USER_DELETE))) {
return false;
}

return true;
}

protected function voteOnAttribute($attribute, $object, TokenInterface $token)
{
$user = $token->getUser();

if (!$user instanceof UserInterface) {
return false;
}

switch($attribute) {
case self::USER_CREATE:
if ($this->decisionManager->decide($token, array('ROLE_USER_MANAGEMENT_WITH_DELETE'))
|| $this->decisionManager->decide($token, array('USER_MANAGEMENT_WITHOUT_DELETE'))
){
return true;
}
break;
case self::USER_EDIT:
// ...
break;
case self::USER_DELETE:
// ...
break;
}

return false;
}
}

然后你可以在你的 Controller 中检查权限:

userCreateAction()
{
if(!$this->isGranted('user_create')){throw $this->createAccessDeniedException('You are not allowed to create an user.');}

// next steps ...
}

关于php - 管理角色并为角色分配权限 - Symfony,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38565572/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com