【问题标题】:How to check the pages that are set to allowed in controller CakePhp 3?如何检查控制器 CakePhp 3 中设置为允许的页面?
【发布时间】:2015-10-21 17:04:55
【问题描述】:

我正在尝试创建一个注销功能,该功能将检查当前页面是否允许注销的用户使用。如果是,我将留在页面上,如果不是,我将重定向到主页。我想知道如何检查当前页面是否被允许。我可以检查他们是否已获得此代码的授权:

public function logout()
{
    if($this->isAuthorized($this->Auth->user())) {
        $this->Auth->logout();
        $redirect = $this->redirect($this->referer());
    } else {
        $this->Auth->logout();
        $redirect = $this->redirect(['controller' => 'pages', 'action' => 'home']);
    }
    return $redirect;
}

但我无法检查当前页面是否允许:

public function logout()
{
    if(in_array($this->request->here, $this->Auth->allow())) {
        $this->Auth->logout();
        $redirect = $this->redirect($this->referer());
    } else {
        $this->Auth->logout();
        $redirect = $this->redirect(['controller' => 'pages', 'action' => 'home']);
    }
    return $redirect;
}

【问题讨论】:

    标签: php cakephp-3.0 cakephp-3.1


    【解决方案1】:

    最佳实践是使用$this->Auth->allowedActions,而Auth 组件将允许的操作存储在allowedAction 属性中。 你可以这样称呼它们:

    $actions = $this->Auth->allowedActions;
    

    【讨论】:

      【解决方案2】:

      我想出了一个答案,但如果有更好的方法,我仍然想知道。我将隐藏输入中的动作传递给控制器​​方法并检查是否在 allowedPages 数组中。

      public $allowedPages = [];
      public function beforeFilter(Event $event)
      {
          parent::beforeFilter($event);
          $this->allowedPages = ['checkout', 'charge', 'logout'];
          $this->Auth->allow($this->allowedPages);
      }
      
      public function logout()
      {
          if(in_array($this->request->data('action'), $this->allowedPages)) {
              $this->Auth->logout();
              $redirect = $this->redirect($this->referer());
          } else {
              $this->Auth->logout();
              $redirect = $this->redirect(['controller' => 'pages', 'action' => 'home']);
          }
          return $redirect;
      }
      

      【讨论】:

        猜你喜欢
        • 2011-12-02
        • 1970-01-01
        • 1970-01-01
        • 2018-04-28
        • 1970-01-01
        • 2014-10-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多