Ivin
Ivin

Reputation: 4815

admin_logout issue in cakephp

I have enabled admin_actions in my cakePHP project. I have a logout() for normal employees and admin_logout() for admin logout. Both actions belong to EmployeesController.

The code inside both logout actions is the same,except for the flash message.

admin_logout():

$this->Session->destroy();
$this->Session->setFlash('You have been logged out of admin dashboard!','flash_success');
$this->redirect('/employees/login');

logout()

$this->Session->destroy();
$this->Session->setFlash('You have been logged out!','flash_success');
$this->redirect('/employees/login');

The logout is working perfectly and destroys the session. But not the admin_logout().

For debugging, i tried this in admin_logout():

$this->Session->delete('Admin');
$this->Session->setFlash('You have been logged out of admin dashboard!','flash_success');
$this->redirect('/employees/login');

It is also working. But it still wont destroy the complete session variable. I could fix this by using normal logout() for both admin and employee. But out of curiosity, whats going wrong here?

EDIT: Needed behavior is the destruction of session variable, then redirection with flash message. But what happens is redirection and flash message is getting displayed BUT session var is not getting destroyed!

Upvotes: 0

Views: 480

Answers (3)

Big D
Big D

Reputation: 23

If you want to have separate flash messages for admins, you could simply use an if statement in the logout method to check whatever variable determines if the user is an admin.

    if($this->Auth->user('is_admin')){
        $this->Session->setFlash(__('You have been logged out of admin dashboard!','flash_success'));
    }
    else{
        $this->Session->setFlash(__('You have been logged out!','flash_success'));

I may be missing something here but doesn't

    $this->redirect($this->Auth->logout());

destroy the session?

Upvotes: 0

Bart Gloudemans
Bart Gloudemans

Reputation: 1221

Not a fix for your problem, but a slightly more elegant approach to the logout process:
Create only one logout method in your class, one without prefix. Like:

public function logout(){
}

Now, in all your views create a logout link explicitly nullifying the admin prefix:

echo $this->Html->link('logout',
    array('admin'=>false,'controller'=>'employees','action'=>'logout')
);

Upvotes: 1

ilinsky
ilinsky

Reputation: 11

If you use 2.0 you should do

 $this->redirect($this->Auth->logout()); 

or

$this->Auth->logout();

for your redirects

Upvotes: 0

Related Questions