2012-12-12 31 views
0

我不想讓我的用戶控制的login()logout()add()行動Auth授予訪問權限,但如果我用$this->Auth->allow('logout');與否我得到的消息也沒關係:You are not authorized to access that location.login()add()做工精細,雖然。爲什麼註銷操作無法訪問?

這是我AppContoller.php:

class AppController extends Controller { 

    public $components = array(

     'Auth' => array(
      'authenticate' => array(
       'Form' => array(
        'userModel' => 'User', 
        'fields' => array(
        'username' => 'email', 'password' => 'password') 
        ) 

      ), 

      'loginRedirect' => array('controller' => 'users', 'action' => 'index'), 
      'logoutRedirect' => array('controller' => 'pages', 'action' => 'display', 'landing') 
     ), 'Session' 
    ); 

    public function beforeFilter() { 
     $this->Auth->allow('add', 'login'); 
    } 

} 

這是我UsersController.php的相關部分:

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


    public function beforeFilter() { 

     parent::beforeFilter(); 
     $this->Auth->allow('logout'); 

    } 
    public function logout() { 
     $this->set('title_for_layout', 'Logout'); 
     $this->redirect($this->Auth->logout()); 
    } 

有誰看到這裏的問題?我感謝您的幫助。

+0

快速瀏覽一下,我可以看到一個小問題,在重定向之前沒有必要設置標題,儘管它不應該成爲問題的一個因素。您要重定向到的頁面是Auth->允許? – Daniel

+0

@Daniel,是的,你可以在UserController.php的beforeFilter函數中看到它是auth允許的。 – Loolooii

+0

對不起,我的意思是logoutRedirect操作 -/pages/display/landing。 – Daniel

回答

0

看起來您正在訪問註銷操作而沒有問題,但註銷重定向會破壞您的會話並將您重定向到page視圖。

看來你沒有登錄就無法訪問該頁面。 (你可以嘗試訪問URL不被記錄)

PagesController添加beforeFilter功能:

public function beforeFilter(){ 
    parent::beforeFilter(); 

    $this->Auth->allow(); 
} 

PagesController自帶默認使用CakePHP 2.2,如果沒有它,只是複製並粘貼任何其他控制器,並添加此功能刪除所有其餘的。

編輯: 如果PagesController已經存在,只需添加beforeFilter函數。

+0

哦,太好了,我確定了這個問題後,我明顯地得到了答案。謝謝。 – Daniel

+0

請您將答案標記爲有效單擊答案左側的票證? – Alvaro

+0

不要,至少在編輯之前 - 史蒂夫建議從PagesController中刪除函數是不好的,它會消除提問者明顯依賴使用的顯示操作。 – Daniel