Yii2 基于权限的 RBAC

Yii2 RBAC based on permissions

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

目前在 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 的软件包可以做到这一点吗?

您正在使用的 AccessControl 过滤器已经允许您通过 "permissions" 字段执行此操作。

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

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