Wednesday, November 5, 2008

Changing the Auth->loginRedirect based on value in user model

Hi all,

I have a simple app that needs to have 2 types of users, normal users
and admins. I have got the Auth component working great and has the
entire site requiring log in etc, but I want to redirect users that
are admins to the admin route sections upon login.

I have this in my app_controller:

function beforeFilter() {
$this->Auth->allow('display');
$this->Auth->logoutRedirect =
array(Configure::read('Routing.admin') => false, 'controller' =>
'users', 'action' => 'logout');
$user = $this->Auth->user();
//debug($user);
if($user['User']['admin'] == 'n') {
$this->Auth->loginRedirect = array('controller'=>'orders',
'action'=>'index');
} else {
$this->Auth->loginRedirect = array('controller'=>'orders',
'action'=>'index', 'admin' => true);
}

}

And while it doesnt throw errors it seems to behave wierdly - it
redirects to the /admin route regardless, but if I go back in my
browser and login again it redirects to the correct page!

Has anyone tried to do this before? Any suggestions what I may be
missing?
TIA,
d.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: