2012-12-10 72 views
0

我看了很多类似的问题,所以没有人回答我的问题或能够帮助我解决这个问题...基本上,当我注释$ this-> auth-> allow行在NewsController中(因为我只希望通过身份验证的人访问除登录/注册以外的所有操作),它会导致登录无限循环。当我允许所有用户访问newscontroller中的索引操作时,它工作正常。任何想法为什么这将循环登录?CakePHP登录无限重定向

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')); 
    } 
+0

不要在控制器重新定义你的会话组件。您已经在应用程序控制器中声明了它。您正在覆盖该变量。我不知道这是否是你的问题,但你可以通过重新声明'public $ components = array('Session');' –

+0

我已经从newscontroller中删除了该行(有道理),但是这样做可以排除AuthComponent不会影响我不幸的结果。 – medoix

+1

我认为你在你的appcontroller中滥用''authorize'=>数组('Controller')'。如果你想在控制器的beforeFilter中使用动作授权,你必须将其设置为false。 'authorize'是用于控制器中的'isAuthorized'功能。 http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#using-no-authorization –

回答

1

如果只想验证的人民以访问除了登录的所有行动和注销则没有必要定义键值对

'authorize' => array('Controller') 

in AppCOntroller。因为如果你指定了这个键,你必须指定函数isAuthorized(),它将返回true或false(根据你指定的允许用户/用户组访问该动作的条件)。

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

,无需重新定义

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

因为你已经在AppController的定义。

0

有当元素使用要求采取行动, 所以你必须让他们的主控制器,要求动作如下可能出现另一个问题:

--------[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'); 
}