[英]How can I deny access for users withe role member from access the backend in yii2?
I want to restrict the users of role member from accessing the backend. 我想限制角色成员的用户访问后端。 I am using the default
RBAC
of yii2
advanced for authorization, but I can't add any roles in access rules for rules: 我使用的是
yii2
Advanced的默认RBAC
进行授权,但无法在规则的访问规则中添加任何角色:
rules => [
[
'actions' => ['login', 'error'],
'allow' => TRUE,
]
in siteController of the backend. 在后端的siteController中。 If there is any way to do this, I will be thankful.
如果有任何办法,我将很感激。
If you want only deny access to member and allow to your_role you can 如果您只想拒绝成员访问并允许your_role,则可以
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'allow' => false,
'roles' => ['member']
],
[
'allow' => true,
'roles' => ['your_role'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}
It's no need to code every backend controllers behaviors. 无需编写每个后端控制器行为的代码。 You can add same code to end of $config['components'] array backend/config/main.php:
您可以在$ config ['components']数组backend / config / main.php的末尾添加相同的代码:
'as beforeRequest' => [
'class' => \yii\filters\AccessControl::className(),
'rules' => [
[
'actions' => ['login', 'error'],
'allow' => true,
],
[
'allow' => false,
'roles' => ['member'],
],
],
'denyCallback' => function () {
return Yii::$app->response->redirect(['frontend']);
},
],
Try this , 尝试这个 ,
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
'access' => [
'class' => AccessControl::className(),
// We will override the default rule config with the new AccessRule class
'ruleConfig' => [
'class' => AccessRule::className(),
],
'only' => ['index','create', 'update', 'delete'],
'rules' => [
[
'actions' => ['index','create'],
'allow' => true,
// Allow users, moderators and admins to create
'roles' => [
User::ROLE_USER,
User::ROLE_MODERATOR,
User::ROLE_ADMIN
],
],
[
'actions' => ['update'],
'allow' => true,
// Allow moderators and admins to update
'roles' => [
User::ROLE_MODERATOR,
User::ROLE_ADMIN
],
],
[
'actions' => ['delete'],
'allow' => true,
// Allow admins to delete
'roles' => [
User::ROLE_ADMIN
],
],
],
],
];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.