以多层前缀授权用户

问题描述 投票:0回答:1

我使用cakephp建立一个新的网站,对于管理部分,我使用的是多层前缀,例如。 (管理/网络)

所以在这种情况下,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),给我一条消息,说我需要先登录才能看到该页面。

有什么建议?

谢谢。

cakephp cakephp-3.0
1个回答
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;
    }
© www.soinside.com 2019 - 2024. All rights reserved.