2012-04-05 8 views
2

私のCakeアプリケーションでは、基本認証を行っています。私はそれをシンプルでセマンティックなものに保つことを好みます(ACLが嫌いです)ので、単にユーザーの役割をチェックし、それに応じて許可または拒否します。今CakePHP:許可された動作でさえも認証エラー

、予想通り承認すべての機能が、私は認証のエラーメッセージにかかわらず、ユーザーが許可されるアクションを試行するかどうかの示し奇妙な問題を取得しています。ログアウトしても表示されたままです。

ここでのAppControllerです:

public $components = array(
    'Session', 
    'Password', 
    'Auth' => array(
     'loginRedirect' => array('controller' => 'users', 'action' => 'index'), 
     'logoutRedirect' => array('controller' => 'pages', 'action' => 'display', 'home'), 
     'authError' => "Sorry, you're not allowed to do that.", 
     'authorize' => array('Controller') 
    ), 
    'RequestHandler' 
); 

public function beforeFilter() { 
    $this->set('loggedIn', $this->Auth->loggedIn()); 
    $this->set('current_user', $this->Auth->user()); 
    $this->set('admin', $this->_isAdmin()); 
    $this->set('coach', $this->_isCoach()); 
    $this->Auth->allow('login', 'logout', 'display'); 
} 

public function isAuthorized($user) { 
    if (isset($user['role']) && $user['role'] === 'admin') { 
     return true; 
    } 
    return false; 
} 

のbeforeFilterとのisAuthorized別のコントローラから:

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

public function isAuthorized($user) { 
    if ($user['role'] === 'coach') { 
     if ($this->action === 'index') { 
      return true; 
     } 
     if (in_array($this->action, array('view', 'edit', 'delete'))) { 
      $id = $this->request->params['pass'][0]; 
      $this->User->id = $id; 
      if ($this->User->field('client_id') === $user['client_id']) 
       return true; 
      } else { 
       return false; 
      } 
     } 
     return false; 
    } 
    return parent::isAuthorized($user); 
} 

答えて

1

私が代わりに私のユーザーコントローラでこれを行うことを決めた、とすべてがうまく動作しているようだ、プラスそれはクリーナー/もっと読みやすいです:

public function isAuthorized($user = null) { 
    switch($this->action) { 
     case "index": 
     case "add": 
      if ($user['role'] == 'coach') { 
       return true; 
      } 
      break; 

     case "view": 
     case "edit": 
     case "delete": 
      $id = $this->request->params['pass'][0]; 
      $this->User->id = $id; 
      if ($user['role'] == 'coach' && $this->User->field('client_id') == $user['client_id']) { 
       return true; 
      } 
      break; 
    } 
    return parent::isAuthorized($user); 
} 
関連する問題