【问题标题】:CakePHP Auth how to allow specific controller and actionsCakePHP Auth 如何允许特定的控制器和动作
【发布时间】:2010-05-08 10:03:25
【问题描述】:

我有一个“帖子”和一个“用户”控制器。我使用 Auth 组件,我希望所有用户都可以访问“Post.index”,但只有登录用户才能访问“User.index”。

在我的 app_controller.php 我有这个

$this->Auth->allow('signup', 'confirm', 'index');

但是所有用户都可以访问 post.index 和 user.index。如何在允许方法中指定控制器?

这对我不起作用:

$this->Auth->allow('signup', 'confirm', 'Post.index');

更新 我从 app_controller.php 中删除了“索引”,而是将其设置在后控制器的 beforeFilter 方法中:

function beforeFilter() 
{
    parent::beforeFilter();
    $this->Auth->allow('index');
}

我还在 app_controller 中设置了一个变量“loggedIn”,而没有调用“parent::beforeFilter();”我收到“未定义变量”通知。

谢谢sibidiba

【问题讨论】:

    标签: authentication cakephp controller


    【解决方案1】:

    该期间将不起作用。你可以试试 '/' 代替。如果同样失败,您应该在 PostController 和 UserController 的 ::beforeFilter() 中分别设置 $this->Auth->allow('index')。不要忘记调用 parent::beforeFilter()。

    【讨论】:

    • 完美答案!它不适用于 '/' 所以我从 app_controller 中删除了 'index' 并允许它在后控制器中使用: function beforeFilter() { parent::beforeFilter(); $this->Auth->allow('index'); }
    【解决方案2】:

    取决于您正在使用的版本。如果是 cakephp 2.x,请将此代码放入具有您想要授予访问权限而无需登录的操作的控制器中。作为您的问题,您应该将此代码放入 Posts 控制器:

    function beforeFilter(){
         $this->Auth->allow(array('index','another action'));}
    

    allow(array('acction you want to allow')) 而不是allow('acction you want to allow')

    【讨论】:

      【解决方案3】:

      我正在使用 CakePHP 2.x。斜线技巧不起作用。

      如果你想允许用户在没有登录的情况下访问“myController.myAction”,你应该将 beforeFilter() 添加到 myController.php 而不是 AppController.php

      这是添加到 myController.php 中的代码:

      function beforeFilter() {
          parent::beforeFilter();
          $this->Auth->allow('myAction');
      }
      

      【讨论】:

        【解决方案4】:

        对于 Cakephp 2.x,有几种方法(取决于 cakephp 版本)。

        来自文档 (http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html):

        // Allow all actions. CakePHP 2.0
        $this->Auth->allow('*');
        
        // Allow all actions. CakePHP 2.1
        $this->Auth->allow();
        
        // Allow only the view and index actions.
        $this->Auth->allow('view', 'index');
        
        // Allow only the view and index actions.
        $this->Auth->allow(array('view', 'index'));
        

        【讨论】:

          【解决方案5】:

          CakePHP 开发人员的常见问题是授权特定控制器的特定操作

          https://blog.sohelrana.me/cakephp-auth-allow-specific-actions-specific-controllers/

          【讨论】:

            【解决方案6】:

            在 cake 3.x 中,您可以使用以下代码行来允许所有操作。

                public function beforeFilter(Event $event) {
                  parent::beforeFilter($event);
                  $this->Auth->allow();
                }
            

            【讨论】:

              【解决方案7】:

              $this->name 返回当前请求的控制器。

              在 AppController::beforeFilter() 中试试这个

              public function beforeFilter()
              {
              
                  // ... Basic configs 
              
                  switch ($this->name) {
                      case 'Posts':
                          $this->Auth->allow('add');
                          break;              
                      case 'Test':
                          $this->Auth->allow('test');
                          break;
                  }
              }
              

              对不起,我的英文不好

              【讨论】:

                【解决方案8】:

                CakePHP 3.*允许特定方法特定控制器

                //put this line after namespace
                use Cake\Event\Event;
                
                // in your specific controller call this function to allow specific methods
                public function beforeFilter(Event $event) {
                        parent::beforeFilter($event);
                        $this->Auth->allow(['index','view']); //<-- here you put your methods
                }
                

                【讨论】:

                  猜你喜欢
                  • 2014-03-15
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2011-11-28
                  • 1970-01-01
                  相关资源
                  最近更新 更多