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

WBOY
Release: 2016-06-06 20:35:40
Original
1081 people have browsed it

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>
Copy after login

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

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

AccessControl 支持配置 role

例如:

<code>[
    'actions' => ['edit'],
    'allow' => true,
    'roles' => ['@', 'author'],
],
</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!