LoginSignup
32
31

More than 5 years have passed since last update.

CakephpのAuthで管理者と担当の権限分け

Posted at

ユーザーテーブルにroleというカラムを作って、adminかuserを入れておく。
以下をAppControllerの方に書く。

class AppController extends Controller {
    public $components = array(
        'Session',
        'Auth' => array(
            'loginRedirect' => array('controller' => 'pages', 'action' => 'display', 'home'),
            'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
            'authorize' => array('Controller') 
        )
    );

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

        return false;
    }

}

個別のコントローラに以下を書く。

    public function isAuthorized($user) {
        if ($this->action === 'index') {
            return true;
        }

        return parent::isAuthorized($user);
    }

許されたページ以外のリンクをたたいても移動しない。

32
31
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
32
31