自动使用多层前缀的用户

时间:2019-03-26 08:07:20

标签: cakephp cakephp-3.0

我正在使用cakephp建立一个新网站,对于管理部分,我正在使用多层前缀。 (管理员/网络)

因此,在这种情况下,admin是前缀,web是前缀。

我一直在尝试使用authorize =>控制器并设置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);
}

但是只有第一个前缀(管理员)有效,另一个(网络)有效,给我一条消息,说我需要登录才能看到该页面。

有什么建议吗?

谢谢。

1 个答案:

答案 0 :(得分:1)

正如Documentation所说,您可以在“管理”范围内移动“管理”操作:

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;
    }