Jimothey
Jimothey

Reputation: 2434

CakePHP - Auth logoutRedirect not working for Admin users

In my cake 2.2 app I have the following beforeFilter() set up in my App Controller:

public function beforeFilter() {

    //Configure AuthComponent
    // Admin
    if($this->Auth->user('group_id') == '12') {
        $this->Auth->allow('admin_index'); 
        $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login', 'admin' => FALSE);
        $this->Auth->loginRedirect = array('controller' => 'pages', 'action' => 'index', 'admin' => TRUE);
        $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login', 'admin' => FALSE);

        $this->set("group", "admin");

    // Staff
    }

    if($this->Auth->user('group_id') == '13') {
        $this->Auth->allow('admin_index'); 
        $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login', 'admin' => FALSE);
        $this->Auth->loginRedirect = array('controller' => 'pages', 'action' => 'index', 'admin' => TRUE);
        $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login', 'admin' => FALSE);

        $this->set("group", "staff");

So basically I want all users regardles of user group to be sent to /users/login when the session expires. This works for users but any admin users get redirected to admin/users/login and presented with a Missing method in users controller error (because this isnt an admin method). For some reason the 'admin' => FALSE isnt working.

So, how can I get all users regardless of user type to get redirected to the NON admin method/url of /users/login

    // Users
    } 

    if($this->Auth->user('group_id') == '14') {
        $this->Auth->allow(array('controller' => 'pages', 'action' => 'index', 'admin' => FALSE));
        $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login', 'admin' => FALSE);
        $this->Auth->loginRedirect = array('controller' => 'pages', 'action' => 'index', 'admin' => FALSE);
        $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login', 'admin' => FALSE);

        $this->set("group", "user");
    }

    // General logout redirect (including expired session redirect)
    $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login', 'admin' => FALSE);
}

Upvotes: 1

Views: 2619

Answers (2)

Ashish Pathak
Ashish Pathak

Reputation: 824

public function admin_logout() {
    $this->Session->setFlash(__('Thanks for using Applired.com!'), 'default', array('class' => 'alert alert-success'));
    $this->Session->delete('user_to_register');
    $this->Session->destroy();
    $this->Auth->logout();
    return $this->redirect(array('controller' => 'dashboard', 'action' => 'login'));
}

Upvotes: 1

Nunser
Nunser

Reputation: 4522

What I guess is happening is that the user is not actually login out when the session expires. Unless the user explicitely logs out (executing a lougout action in your UsersController, I'm assuming), like this for example

public function logout() {
    ... some code here...
    $this->Session->destroy();
    $this->redirect($this->Auth->logout());
}

that logoutRedirect is probably not going to work.
If the session expires, the user will be unauthorized to view the page, and the redirect is going to go to the Auth->unauthorizedRedirect.

For what you're trying to do, I'd use a method checking if the user is logged in beforeFilter of the AppController

public function beforeFilter() {
    if (!$this->Auth->loggedIn() && $this->action != 'login') {
        $this->redirect(array('controller'=>'users', 'action'=>'login', 'admin'=>false));
    }
}

or if you want

public function beforeFilter() {
    if (!$this->Auth->loggedIn() && $this->action != 'login') {
        $this->redirect($this->Auth->logoutRedirect);
    }
}

Upvotes: 1

Related Questions