[英]Yii2 roles and users
Can I statically define roles in authManager (in defaultRoles
array in config) and assign them to users so behavior rules define access to actions? 是否可以在authManager中静态定义角色(在config中的
defaultRoles
数组中)并将其分配给用户,以便行为规则定义对操作的访问权限?
As i have certain roles, I don't want to use auth_assignment
and auth_item
and ... 由于我具有某些角色,因此我不想使用
auth_assignment
和auth_item
以及...
Assuming I create column in user table for role and every user has one role and roles are define in config file. 假设我在用户表中为角色创建了一个列,并且每个用户都有一个角色,并且角色在配置文件中定义。
In fact I want to build access rules like 'admin' for users who are admin (Where yii says '@' for authenticated user and '?' for guest). 实际上,我想为管理员用户建立访问规则,例如“ admin”(其中yii对已认证用户说“ @”,对来宾说“?”)。
First create your roles somewhere like params then behaviors function can manage authentication easily 首先在诸如params之类的地方创建角色,然后使用行为功能轻松管理身份验证
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'except' => [''],//or only
'rules' => [
[
'allow' => true,
'actions' => ['deletepic', 'regenerate'],
'matchCallback' => function ($rule, $action) {
return (myAuth(['root','admin']));
}
],
],
],
];
}
myAuth() will check current user role and return true if they role match requested action. myAuth()将检查当前用户角色,如果他们的角色与请求的操作匹配,则返回true。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.