nex*_*equ 3 php cakephp cakephp-3.x
我想基于几个角色授权用户.所有访客都应该能够到达方法节目.所以我在AppController中写道:
public function beforeFilter(Event $event) {
$this->Auth->allow(['show']);
}
Run Code Online (Sandbox Code Playgroud)
有用.
在AppController的initialize()方法中,我还得到了:
$this->loadComponent('Auth', [
'authorize' => 'Controller'
]);
Run Code Online (Sandbox Code Playgroud)
我想允许具有角色"user"的登录用户访问所有"索引"和"添加"方法,所以我在AppController中写道:
public function isAuthorized($user) {
if (isset($user['role']) && $user['role'] === 'admin') {
return true;
}
if (isset($user['role']) && $user['role'] === 'user') {
$this->Auth->allow(['index', 'logout', 'add']);
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
管理员可以按预期访问所有方法.用角色"user"登录的用户无法达到"index"或"add"方法.我怎样才能解决这个问题?
Dav*_*ave 11
而不是使用您的逻辑添加额外的Auth允许,只需使用逻辑来确定它们是否在允许的操作中,通过检查操作,并true在它们被授权时返回.
public function isAuthorized($user) {
// Admin allowed anywhere
if (isset($user['role']) && $user['role'] === 'admin') {
return true;
}
// 'user' allowed in specific actions
if (isset($user['role']) && $user['role'] === 'user') {
$allowedActions = ['index', 'logout', 'add'];
if(in_array($this->request->action, $allowedActions)) {
return true;
}
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
(显然这段代码可以根据自己的喜好缩短,但它显示了这个概念)
| 归档时间: |
|
| 查看次数: |
1531 次 |
| 最近记录: |