CakePHP管理员登录

时间:2012-04-09 06:43:52

标签: cakephp cakephp-2.0

我有两种类型的用户,现在我不想使用ACL。使用Auth Component我想实现以下

login() - >允许用户登录和访问网站的常规部分 admin_login - >允许管理员访问网站的admin_ {actions}部分。

当我进行管理员登录时 - >我想检查用户模块中的group_id = 1,并且只允许他们登录网站的管理部分。

function admin_login(){
    $this->layout = 'admin_login';

    if($this->request->is('post')) {
        if($this->Auth->login()) {
            $this->redirect($this->Auth->redirect());
        } else {
            $this->Session->setFlash(__('Invalid username and password, try again'));
        }
    }
}

如何在用户登录时检查group_id = 1?

1 个答案:

答案 0 :(得分:4)

我会做这样的事情:

function admin_login(){
    $this->layout = 'admin_login';

    if($this->request->is('post')) {
        if($this->Auth->login()) {
            // If here because user is logged in
            // Check to see if group_id is 1
            if($this->Auth->user('group_id') == 1){
                //$this->redirect($this->Auth->redirect());
                $this->redirect('/admin/dashboards'); //Example
            }else{
                // In case a user tries to login thru admin_login
                // You should log them in anyway and send them to where they belond
                $this->redirect('/users/account'); 
            }
        } else {
            $this->Session->setFlash(__('Invalid username and password, try again'));
        }
    }
}