2012-12-10 73 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'); 
}