Yii2 RBAC基于权限

时间:2017-11-11 21:14:37

标签: yii yii2 yii2-rbac

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

目前在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的这个包呢?

1 个答案:

答案 0 :(得分:0)

您正在使用的AccessControl过滤器允许您通过"权限"领域。

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

检查文档: http://www.yiiframework.com/doc-2.0/yii-filters-accessrule.html# $权限细节