CakePHP 3.2:防止在用户未登录时显示 authError
CakePHP 3.2: Prevent authError showing when user is not logged in
即使用户未登录并尝试打开主页,在被重定向到登录页面后,authError displayed.Is有没有一种优雅的方法可以防止这种情况发生,而无需修改 Auth 组件?这是我加载 Auth 组件的方式(我使用 TinyAuth 作为授权适配器):
$this->loadComponent('Auth', [
'loginAction' => [
'controller' => 'Users',
'action' => 'login'
],
'loginRedirect' => [
'controller' => 'Home',
'action' => 'index'
],
'authError' => 'You dont have permissions for that action',
'authenticate' => [
'Form' => [
'fields' => [
'username' => 'email',
'password' => 'password'
],
'scope' => ['Users.active' => true],
'contain' => ['Roles']
]
],
'authorize' => [
'TinyAuth.Tiny' => [
'roleColumn' => 'role_id',
'rolesTable' => 'Roles',
'multiRole' => true,
'pivotTable' => 'roles_users',
'superAdminRole' => null,
'authorizeByPrefix' => false,
'prefixes' => [],
'allowUser' => false,
'adminPrefix' => null,
'autoClearCache' => true
]
]
]
);
根据 CakePHP's documentation,您可以通过将 authError
设置为 false
来防止显示错误消息。
Sometimes, you want to display the authorization error only after the
user has already logged-in. You can suppress this message by setting
its value to boolean false.
这应该禁用错误消息:
if (!$this->Auth->user()) {
$this->Auth->config('authError', false);
}
即使用户未登录并尝试打开主页,在被重定向到登录页面后,authError displayed.Is有没有一种优雅的方法可以防止这种情况发生,而无需修改 Auth 组件?这是我加载 Auth 组件的方式(我使用 TinyAuth 作为授权适配器):
$this->loadComponent('Auth', [
'loginAction' => [
'controller' => 'Users',
'action' => 'login'
],
'loginRedirect' => [
'controller' => 'Home',
'action' => 'index'
],
'authError' => 'You dont have permissions for that action',
'authenticate' => [
'Form' => [
'fields' => [
'username' => 'email',
'password' => 'password'
],
'scope' => ['Users.active' => true],
'contain' => ['Roles']
]
],
'authorize' => [
'TinyAuth.Tiny' => [
'roleColumn' => 'role_id',
'rolesTable' => 'Roles',
'multiRole' => true,
'pivotTable' => 'roles_users',
'superAdminRole' => null,
'authorizeByPrefix' => false,
'prefixes' => [],
'allowUser' => false,
'adminPrefix' => null,
'autoClearCache' => true
]
]
]
);
根据 CakePHP's documentation,您可以通过将 authError
设置为 false
来防止显示错误消息。
Sometimes, you want to display the authorization error only after the user has already logged-in. You can suppress this message by setting its value to boolean false.
这应该禁用错误消息:
if (!$this->Auth->user()) {
$this->Auth->config('authError', false);
}