在多层前缀中自动化用户
Autorize users in a multiple layer prefix
我正在使用 cakephp 构建一个新网站,对于管理部分,我正在使用多层前缀,例如。 (admin/web)
所以在这种情况下,admin 是一个前缀,web 是一个前缀。
我一直在尝试使用 authorize => controller 并设置 isAuthorized 函数,如下所示:
public function isAuthorized($user = null)
{
if (!$this->request->getParam('prefix')) {
return true;
}
// Only admins or specific roles can access admin functions
if ($this->request->getParam('prefix') === 'admin') {
if ($this->request->getParam('prefix') === 'web') {
return (bool)($user['role'] === 'admin');
}
return (bool)($user['role'] === 'admin');
}
return false;
}
并且在我添加的任何控制器中:
public function beforeFilter(Event $event)
{
parent::beforeFilter($event);
}
但只有第一个前缀 (admin) 有效,另一个 (web) 给我一条消息,说我需要登录才能看到该页面。
有什么建议吗?
谢谢。
正如Documentation所说,您可以在管理员范围内移动"admin"个操作:
Router::prefix('admin', function ($routes) {
// All routes here will be prefixed with `/admin`
// And have the prefix => admin route element added.
$routes->fallbacks(DashedRoute::class);
});
然后把你的管理方法放在让我们说
src/Controller/Admin/UsersController.php
或者您可以像现在这样使用两个前缀,比方说:page/admin/web/page
但在这种情况下
// $this->request->getParam('prefix') returns admin/web
public function isAuthorized($user = null)
{
$prefix =$this->request->getParam('prefix');
if (!$prefix ) {
return true; //sure?
}
// Only admins or specific roles can access admin functions
if ($prefix==='web/admin' || $prefix==='admin') {
return (bool)($user['role'] === 'admin');
}
return false;
}
我正在使用 cakephp 构建一个新网站,对于管理部分,我正在使用多层前缀,例如。 (admin/web)
所以在这种情况下,admin 是一个前缀,web 是一个前缀。
我一直在尝试使用 authorize => controller 并设置 isAuthorized 函数,如下所示:
public function isAuthorized($user = null)
{
if (!$this->request->getParam('prefix')) {
return true;
}
// Only admins or specific roles can access admin functions
if ($this->request->getParam('prefix') === 'admin') {
if ($this->request->getParam('prefix') === 'web') {
return (bool)($user['role'] === 'admin');
}
return (bool)($user['role'] === 'admin');
}
return false;
}
并且在我添加的任何控制器中:
public function beforeFilter(Event $event)
{
parent::beforeFilter($event);
}
但只有第一个前缀 (admin) 有效,另一个 (web) 给我一条消息,说我需要登录才能看到该页面。
有什么建议吗?
谢谢。
正如Documentation所说,您可以在管理员范围内移动"admin"个操作:
Router::prefix('admin', function ($routes) {
// All routes here will be prefixed with `/admin`
// And have the prefix => admin route element added.
$routes->fallbacks(DashedRoute::class);
});
然后把你的管理方法放在让我们说
src/Controller/Admin/UsersController.php
或者您可以像现在这样使用两个前缀,比方说:page/admin/web/page
但在这种情况下
// $this->request->getParam('prefix') returns admin/web
public function isAuthorized($user = null)
{
$prefix =$this->request->getParam('prefix');
if (!$prefix ) {
return true; //sure?
}
// Only admins or specific roles can access admin functions
if ($prefix==='web/admin' || $prefix==='admin') {
return (bool)($user['role'] === 'admin');
}
return false;
}