1
votes

I have two types of users and right now i dont want to use ACL. With Auth Component i want to achieve the following

login() -> allows users to login and access the general part of the site admin_login -> allows the admin to access the admin_{actions} part of the website.

When i do a Admin Login -> i want to check if group_id = 1 in the Users Module and only allow them to login to the admin section of the website.

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'));
        }
    }
}

How to check if the group_id = 1 when the user logs in ?

1

1 Answers

4
votes

I would do something like this:

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'));
        }
    }
}