【问题标题】:CakePHP 3 - Authorization for pages on PagesControllerCakePHP 3 - PagesController 上的页面授权
【发布时间】:2017-06-13 12:29:01
【问题描述】:

我的管理中心页面当前位于 PagesController(称为 admin)中。但是,非登录用户和非管理员用户都可以访问此中心页面,即使他们无法访问该中心的所有链接。

编辑:我刚刚意识到它可能不起作用,因为“admin”不是 PagesController 中的函数,而是属于“display”。

我的AppController如下:

public function initialize()
{
    parent::initialize();

    $this->loadComponent('RequestHandler');
    $this->loadComponent('Flash');
    $this->loadComponent('Auth',[
        'authorize' => 'Controller',
    ]);

    $this->Auth->allow(['display']);
}

public function beforeFilter(Event $event)
{
    $this->Auth->deny(['admin']);

}

我的PagesController如下:

public function initialize()
{
    parent::initialize();
    $this->Auth->deny(['admin']);
}

public function isAuthorized($user)
{
    if (in_array($this->request->action,['admin'])) {
        return (bool)($user['role_id'] === 1); //where role_id = 1 refers to an admin
    }
    return parent::isAuthorized($user);
}

【问题讨论】:

    标签: php cakephp authorization cakephp-3.0 cakephp-3.x


    【解决方案1】:

    Auth->deny() 函数是为了防止未经授权的用户访问操作。另一方面,Auth->allow() 功能是让公众可以访问特定(或所有)操作。

    请在此处阅读文档:https://book.cakephp.org/3.0/en/controllers/components/authentication.html#making-actions-require-authorization

    要使 Auth 组件按您的需要工作,请阅读以下内容:假设您有一个不同的管理员用户数据库表,并且您想向访问者询问访问受限页面的凭据,您可以执行以下操作:

    在 AppController 中,当用户访问受限页面时,或者在您的情况下,当用户访问 PagesController 时,加载 Auth 组件并确保在 Auth 组件中定义 admin users 表,或者在命名时遵循 Cakephp 约定表。

    在 AppController 中

     public function initialize()
     {
        parent::initialize();
        if ($this->request->params["controller"] == 'Pages') {
            $this->loadComponent('Auth', [
                    'loginAction' => [
                        'controller' => 'Access',
                        'action' => 'admin_login',
                        'plugin' => 'Access'
                    ],
                    'loginRedirect' => [
                        'controller' => 'Access',
                        'action' => 'admin_index',
                        'plugin' => 'Access'
                    ],
                    'authenticate' => [
                        'Form' => [
                        'userModel' => 'your-admin-database-table'
                    ]
                ]
               ]);
        }
    }
    

    在 PagesController 中,您需要以下内容:

    public function initialize()
    {
        parent::initialize();
    }
    

    loginAction - 用于管理员登录。 loginRedirect - 登录后登录管理员用户的位置。 authenticate - 用于定义表单详细信息,例如数据库名称和字段。

    可以在此处找到非常详细的文档:https://book.cakephp.org/3.0/en/controllers/components/authentication.html

    编辑:请注意该代码尚未经过测试

    【讨论】:

      【解决方案2】:

      您检查的是动作,而不是前缀。 Documentation 建议你想要的是

      if ($this->request->getParam('prefix') === 'admin')
      

      【讨论】:

      • 请注意,another bit of documentation 表示if ($this->request->getParam('admin')) 是要走的路。我不使用管理员路由,所以我无法轻易确认哪个是正确的,或者它们是否都可以接受。
      猜你喜欢
      • 2016-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多