Home  >  Article  >  Backend Development  >  yii2 rbac 每次走一个方法都要自己验证一次有没有权限吗?如果是配置的话怎么配置?

yii2 rbac 每次走一个方法都要自己验证一次有没有权限吗?如果是配置的话怎么配置?

WBOY
WBOYOriginal
2016-06-06 20:35:401120browse

yii2 rbac 每次走一个方法都要自己验证一次用户有没有该控制器与Action的权限吗?如果是配置的话怎么配置?

回复内容:

yii2 rbac 每次走一个方法都要自己验证一次用户有没有该控制器与Action的权限吗?如果是配置的话怎么配置?

yii\filters\AccessRule::matchCalllback. 注意传递的参数 $rule, $action

<code>[
    'actions' => ['special-callback'],
    'allow' => true,
    'matchCallback' => function ($rule, $action) {
        # write arbitrary access check logic
        # permission = (rule [xxoo] action)
        # return user->can(permission);
    }
]
</code>

或者使用下面的方式, 见 yii2-admin

<code>return [
    'components' => [
        'authManager' => [
            'class' => 'yii\rbac\PhpManager', // or use 'yii\rbac\DbManager'
        ]
    ],
    'as access' => [
        'class' => 'mdm\admin\components\AccessControl',
    ],
];
</code>

AccessControl 支持配置 role

例如:

<code>[
    'actions' => ['edit'],
    'allow' => true,
    'roles' => ['@', 'author'],
],
</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn