0
votes

I'm fairly new to cakePHP and have read all the tutorials on the cake site. I'm building a small sample app using cake 2.1 and have hit a problem. Basically, I want admin users redirected to a different page on login than what a normal user gets redirected to. I'm sure there's an easy way to do this but I'm struggling!

I have Admin routing enabled and am using the auth component (and the ACL component though that's not affecting my issue). I have 2 logins one for admin - admin_login() and a login for normal users - login() - the normal non admin login works fine.

In my AppController.php I have this:

class AppController extends Controller {

public $components = array(
    'Acl',
    'Auth' => array(
        'authorize' => array(
            'Actions' => array('actionPath' => 'controllers')
        )
    ),
    'Session'
);
public $helpers = array('Html', 'Form', 'Session');

public function beforeFilter() {
    //Configure AuthComponent
    $this->Auth->allow('display'); // Allows access to the homepage of the app
    $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
    $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login');
    $this->Auth->loginRedirect = array('controller' => 'posts', 'action' => 'add');
}

}

So as you can see the default is for users to be redirected on login to the posts controllers add() function, which works fine. However, how can I set a different login redirect for my admin_login() function?

I've read a number of posts on here but most relate to older versions of cake and not cake 2.

If it helps, this is my admin_login function in the UsersController.php

public function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow('logout');
}

public function admin_login() {
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            if ($this->Session->read('Auth.User')) {
                $this->Session->setFlash('You are logged in!');
                //$this->redirect($this->Auth->redirect());

                // This doesnt work! Grrrrrr
                //$this->redirect(array('controller'=>'pages', 'action'=>'admin_index'));

                // This nearly works...
                if($this->Auth->user())$this->redirect(array('controller' => 'pages', 'action' => 'admin_index'));
            }            
        }
        else {
            $this->Session->setFlash('Your username or password was incorrect.');
        }
    }

Could anyone point me in the right direction?

2

2 Answers

2
votes

If you have role -> "admin, user" in your database, put in your AppController - beforeFilter():

if($this->Auth->user('role') == 'admin'){
     $this->Auth->loginRedirect = array('controller' => 'controller1', 'action' => 'action1');
}else{
     $this->Auth->loginRedirect = array('controller' => 'controller2', 'action' => 'action2');
}

If you want to know what is in your $this->Auth->user() just put:

debug($this->Auth->user());
0
votes

Maybe this works:

if($this->request->params['admin']) {
    $this->Auth->loginRedirect = array('controller' => 'pages', 'action' => 'index', 'admin' => true);
}
else {
    $this->Auth->loginRedirect = array('controller' => 'posts', 'action' => 'add');
}

Replace your loginRedirect setting in beforeFilter() with these lines