【问题标题】:Yii2 RBAC based on permissionsYii2 RBAC 基于权限
【发布时间】:2017-11-11 21:14:36
【问题描述】:

我正在设计一个系统,但我需要授权管理员用户创建角色并为其分配一组权限。

目前在 RBAC 中

public function behaviors()
{
    return [
        'access' => [
            'class' => AccessControl::className(),
            'rules' => [
                [
                    'actions' => ['index','view'], // these action are accessible 
                                                   //only the yourRole1 and yourRole2
                    'allow' => true,
                    'roles' => ['yourRole1', 'yourRole2'],
                ],
                [    // all the action are accessible to superadmin, admin and manager
                    'allow' => true,  
                    'roles' => ['superAdmin', 'admin', 'manager'],
                ],   
            ],
        ],
    ];
}

不过我最需要的是

public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['index','view'], 
                        'allow' => true,
                        'permission' => ['canView'],
                    ],
                    [    
                        'actions' => ['update','delete'], // these action are accessible 
                        'allow' => true,  
                        'permission' => ['canDelete', 'canUpdate'],
                    ],   
                ],
            ],
        ];
    }

通过这样做并创建一组权限,管理员用户可以创建角色、分配权限并为用户分配角色。

有没有人知道 yii2 的包可以做到这一点?

【问题讨论】:

  • 你试过Yii2中默认的rbac组件吗???

标签: yii yii2 yii2-rbac


【解决方案1】:

您使用的 AccessControl 过滤器已经允许您通过“权限”字段执行此操作。

[
    'actions' => ['index','view'], 
    'allow' => true,
    'permissions' => ['canView'],
],

查看文档: http://www.yiiframework.com/doc-2.0/yii-filters-accessrule.html#$permissions-detail

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-22
    • 2019-01-13
    • 2014-01-25
    相关资源
    最近更新 更多