CakePHP登录无限重定向

时间:2012-12-10 10:48:24

标签: php cakephp authentication infinite-loop

我在SO上看了很多类似的问题,但没有人回答我的问题,或者能够帮我解决这个问题......基本上当我评论出$ this-> auth->允许行NewsController(因为我只希望经过身份验证的人访问除登录/注册之外的所有操作),它会导致登录无限循环。当我允许所有用户访问新闻控制器中的索引操作时,它工作正常。任何想法为什么会在登录时循环?

AppController的

<?php
App::uses('Controller', 'Controller');

class AppController extends Controller {
    public $components = array(
        'Session',
        'Auth' => array(
            'loginAction' => array('controller' => 'users', 'action' => 'login'),
            'loginRedirect' => array('controller' => 'news', 'action' => 'index'),
            'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
            'authorize' => array('Controller')
        )
    );

UsersController

<?php
class UsersController extends AppController {

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

    public function login() {
        $this->layout = 'eprime_empty';
        if ($this->request->is('post')) {
            if ($this->Auth->login()) {
                $this->redirect($this->Auth->redirect());
            } else {
                $this->Session->setFlash('Invalid username or password, try again', 'default', array('class' => 'warning'));
            }
        }
    }

    public function logout() {
        $this->redirect($this->Auth->logout());
    }

NewsController

<?php
class NewsController extends AppController {

    public $helpers = array('Html', 'Form', 'Session');

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

    public function index() {
         $this->set('news', $this->News->find('all'));
    }

2 个答案:

答案 0 :(得分:1)

如果您只希望经过身份验证的人可以访问除登录和注销之外的所有操作,则无需定义键值对

'authorize' => array('Controller') 
在AppCOntroller中

。因为如果指定此键,则必须指定函数isAuthorized(),它将返回true或false(基于您指定的条件,允许用户/用户组访问该操作)。

    public function isAuthorized(){
     return true;//or false 
   } 

并且无需重新定义

public $helpers = array('Html', 'Form', 'Session');
public $components = array('Session');
正如您在AppController中定义的那样

答案 1 :(得分:0)

在Elements中使用请求操作时可能会发生另一个问题, 因此,您必须在其主控制器中允许请求操作,如下所示:

 --------[app\View\view.ctp]------------
 $this->Element('comments');

 --------[app\View\Elements\comments.ctp]----------
 $comments = $this->requestAction('comments/grab');

 --------[app\Controller\CommentsController]-----------
 function beforeFilter() {
     parent::beforeFilter();
     $this->Auth->allow('grab');
 }
相关问题