简体   繁体   English

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

[英]Manage Roles and Assign Permissions to Roles - Symfony

I'm building an Admin panel in Symfony 3 based on Roles & Permissions. 我正在基于角色和权限在Symfony 3中构建一个Admin面板。 Each admin will be assigned a role(or multiple roles) and then he will be able to do things based on the permissions assigned to that role. 将为每个管理员分配一个角色(或多个角色),然后他将能够根据分配给该角色的权限执行操作。


To give you an idea, here's an example: 为了给你一个想法,这是一个例子:

  • Admin panel has the functionality to add users, edit users and delete users. 管理面板具有添加用户,编辑用户和删除用户的功能。
  • I created a role: USER_MANAGEMENT_WITHOUT_DELETE which has permission to user_create and user_edit . 我创建了一个角色: USER_MANAGEMENT_WITHOUT_DELETE ,它具有user_createuser_edit权限。
  • I created USER_MANAGEMENT_WITH_DELETE role which has permission to user_create , user_edit and user_delete 我创建了USER_MANAGEMENT_WITH_DELETE角色,该角色具有user_createuser_edituser_delete
  • So now, admin with role USER_MANAGEMENT_WITH_DELETE can add , edit and delete the users where as admin with role USER_MANAGEMENT_WITHOUT_DELETE can only add and edit users but cannot delete them. 现在,具有角色USER_MANAGEMENT_WITH_DELETE管理员可以addeditdelete用户,其中具有角色USER_MANAGEMENT_WITHOUT_DELETE管理员只能addedit用户但不能删除它们。

I searched and found out about FOSUserBundle and ACL . 我搜索并发现了有关FOSUserBundleACL的信息 Some recommended ACL while others say it's better to use FOSUserBunder 一些推荐的ACL,而其他人说最好使用FOSUserBunder

I also read the documentation of FOSUserBunder and how it store Roles in roles column, something like a:1:{i:0;s:10:"ROLE_ADMIN";} , but there is nothing mentioned about permissions. 我还阅读了FOSUserBunder的文档以及它如何在roles列中存储角色,类似于a:1:{i:0;s:10:"ROLE_ADMIN";} ,但没有提到任何关于权限的内容。 So here are my queries: 所以这是我的疑问:

  1. I'm confused between the two. 我在两者之间感到困惑。 Which one should i use? 我应该使用哪一个?
  2. If i use FOSUserBunder , how to manage permissions? 如果我使用FOSUserBunder ,如何管理权限?

Roles are not specific tu FOSUserBundle. 角色不是特定的tu FOSUserBundle。 They are in Symfony. 他们在Symfony。

ACLs are more complex than using roles. ACL比使用角色更复杂。 So I would suggest to use roles. 所以我建议使用角色。

From the Symfony documentation : Alternatives to ACLs 来自Symfony文档:ACL的替代方案

Using ACL's isn't trivial, and for simpler use cases, it may be overkill. 使用ACL并非易事,对于更简单的用例,它可能有点过分。 If your permission logic could be described by just writing some code (eg 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. 选民将被传递给被投票的对象,您可以使用该对象做出复杂的决策并有效地实施您自己的ACL。 Enforcing authorization (eg 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. 执行授权(例如isGranted部分)看起来与您在此条目中看到的类似,但您的选民类将处理幕后逻辑,而不是ACL系统。

To deal with 'permissions', I would sugget to use Voters : 为了处理'权限',我建议使用选民

First of all create a voter like this : 首先创建一个这样的选民:

Configuration : 配置:

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

And the 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;
    }
}

Then you can check for permission in your controller : 然后,您可以检查控制器中的权限:

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

    // next steps ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM