我希望可以根据几种角色授权用户访问权限。所有来访者都应该能够访问show方法。因此我在AppController中编写了如下代码:
public function beforeFilter(Event $event) {
$this->Auth->allow(['show']);
}
它能工作。
在AppController的initialize()方法中,我还有以下代码:
$this->loadComponent('Auth', [
'authorize' => 'Controller'
]);
我想允许角色为“用户”的已登录用户访问所有的“index”和“add”方法,因此我在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;
}
管理员可以按预期访问所有方法。以“用户”角色登录的用户无法访问“index”或“add”方法。我该怎么解决这个问题?