0
votes

I have different users groups e.g. admin, author,publisher and have separately controllers for them I want to set default path after login on base of

$this->Auth->User('group_id')

like this in appcontroller in beforefilter() method

if ($this->Auth->User('group_id') == '1')
        {
            Router::connect('/', array('controller' => 'admin', 'action' => 'index'));
        } 
elseif($this->Auth->User('group_id') == '2')
        {
            Router::connect('/', array('controller' => 'author', 'action' => 'index'));
        } 
else {
            Router::connect('/', array('controller' => 'publisher', 'action' => 'index'));
        }

I tried this in

routes.php

in Config using $_SESSION Variable because in that file couldnt use $this. Main purpose is that when user login it will take to their controller and so i can have clean URL I dont want to same controller i have to use check for group than ACL library's power will be in waste. Any Help will be appreciated to accomplish this. Thanks in advance

2

2 Answers

0
votes

Based on your comment on Isaac's answer, you can do something like this. Say, in routes.php you can have:

Route::connect('/', array('controller' => 'some_controller', 'action' => 'index'));

Then in your redirected controller's index() method:

public function index() 
{
    $group = $this->User->Group->find('first', array( //assuming User belongsTo Group
        'conditions' => array(
            'id' => $this->Auth->User('group_id')
        ),
        'fields' => array('name')
        )
    )); //getting the name of the group the user belongs to

    call_user_func(array($this, strtolower($group['Group']['name']).'_index'));

}

And then in your controller you can have something like:

protected function admin_index()
{
    //code for admins
}

protected function publisher_index()
{
    //code for publishers
}

protected function author_index()
{
    //code for authors
}

So you have all code on the same controller but separated on different methods.

0
votes

//Don't miss the app_controller for this small kind of thing. bear in mind always keep the app controller clean and tidy.

function login(){
    //here you can say after login what's happen next
    if ($this->Auth->login()){
          $this->redirectUser();
    }

}

//redirect user groups
function redirectUser(){
    $role = $this->Auth->User('group_id');

    switch ($role) {
        case 1:
              return $this->redirect(array('controllers'=>'admin_controller', 'action'=>'action_name')); 
                 break;
        case 2:
              return $this->redirect(array('controllers'=>'author_controller', 'action'=>'action_name'));
        default:
              return $this->redirect(array('controllers'=>'user_controller', 'action'=>'action_name'));
                 break;
         }
}

If you want to use custom url You also need to name it into routes.php

Route::connect('/admin', array('controller' => 'admin_controller', 'action' => 'index'));

the rest of links