2012-03-01 147 views
1

我正在使用cakePHP的isAuthorized()函數來檢查用戶是否被授權執行控制器操作,但是我遇到的奇怪問題是我的其他控制器操作的權限被阻止來自用戶。我只是這樣做:奇怪的cakePHP isAuthorized()問題

function isAuthorized() 
{ 
    $user_id_logged_in = $this->Auth->user('id'); 

    switch($this->action) 
    { 
     case: 'my_action': 

     if($user_id_logged_in) 
     { 
      // check if user has access to execute controller action 
      return $this->Acl->check(array(
           'model' => 'MyModel', 'foreign_key' => $my_foreign_key), 
           'controllers/MyController/'.$this->action); 
     } 
     else 
     { 
      return false; 
     } 

     break; 
    } 
} 

我要檢查,如果登錄用戶被授權執行的動作my_action。但是當我這樣做時,用戶也失去了我控制器中其他操作的權限。任何想法這有什麼問題?

謝謝

+0

你在哪裏設置'$ user_id_logged_in'? – 2012-03-01 23:25:36

+0

$ user_id_logged_in就等於'$ this-> Auth-> user('id');' – user765368 2012-03-01 23:26:50

+0

從我可以從文檔中得知,'check()'方法需要3個字符串參數。 '檢查($ aro,$ aco,$ action =「*」)'。你可以看到[這裏](http://api20.cakephp.org/class/acl-component#method-AclComponentcheck)。 – 2012-03-01 23:36:19

回答

0

我認爲這個問題是這樣一個事實:你不回來在isAuthorized()到底有什麼價值,所以蛋糕假定它是false,因此拒絕訪問一切。

如果您不想授權訪問其他功能(或者將它們添加到Auth中的'允許'列表中),請嘗試在函數的末尾添加一個return true;