0
votes

i have made a simple cakephp application . at the moment i am just working with auth component to send user to their respective pages according to their. for ex if role =1 send to admin page and else if role = 2 send it to moderator page . i am using both session and auth component to see how they work and save data in them. below is the code for usercontroller login action

public function login(){

$this->Session->setFlash($this->Auth->user('role'));//checks for data in auth component if any



    if($this->request->is('post') ){


         $results = $this->User->findByEmail($this->request->data['User']['username']);
        if($results &&$results['User']['password']== md5($this->request->data['User']['password']))
        {
            $this->Session->write('user',$results['User']);
            $this->Auth->login($results['User']);
            $this->Session->setFlash('User logged in successfully'.$this->Auth->user('role'));
            return $this->redirect($this->Auth->redirect());
        }
        else
        {
            $this->Session->setFlash('Login is incorrect');
        }
    }


}

The problem is the login works fine all the data is stored in session and auth variable but loginredirect behave weird. in my chrome browser . it always redirects to admin page no matter what the role is , but it is flashing correct message which i set in flash. the code of beforefilter in appcontroller

public function beforeFilter(){

    $this->Auth->allow('display'); 

    $this->Auth->loginAction = array('controller' => 'Users', 'action' => 'login');

    $this->Auth->logoutRedirect = array('controller' => 'Users', 'action' => 'login');

    if($this->Auth->user('role') == '1'){
            $this->Session->setFlash($this->Auth->user('role').'adminnnnnnnnnnnnnnnnnnnnn');
    $this->Auth->loginRedirect = '/admins/index';
    }

    if($this->Auth->user('role') == '2'){
        $this->Session->setFlash('moderatorrrrrrrrrrrrrrrrr');
        $this->Auth->loginRedirect = '/users/index';
    }
}

so the problem is the loop runs fine in before filter , the setflash display whether user is admin or moderator , but for some reason it redirects to only single page either admins/index page or users/index page no matter who logs in . This is behavior on chrome browser. On firefox the loginredirects sends user to webroot/index page but again the flash messages are correct.

I am not sure what i am doing wrong is there a problem in my code or cakephp 2.0 auth component has measure bugs.

1
for the time being i am using loginredirect to a function called dashboard which take care of routing So after user logs in it gets redirected via Auth->loginRedirect to dashboard() and here i check users role and use redirect to send particular user to the exact location. I am not sure whether its correct way to do things but it works. Can anyone tell if this solution is safe enough as i am not using auth redirects ? Thanks - Ravi Jadhav
this is the code function dashboard() { //$role = $this->Session->read('user.role'); $role=$this->Auth->user('role'); //user selection logic here if($role== '1'){ $this->redirect(array('controller' => 'Admins','action' => 'index','admin' => true)); } else if($role == '2'){ $this->redirect(array('controller' => 'users','action' => 'index', 'admin' => true)); } else if($role == '9'){ $this->redirect(array('controller' => 'users', 'action' => 'index', 'admin' => true)); $this->Session->setFlash('3'); } } - Ravi Jadhav

1 Answers

1
votes

after user logs in it gets redirected via Auth->loginRedirect to dashboard() and here i check users role and use redirect to send particular user to the exact location

  function dashboard() {
  //get user's group (role)
    //$role = $this->Session->read('user.role');
    $role=$this->Auth->user('role');
        //user selection logic here
    if($role== '1'){
        $this->redirect(array('controller' => 'users','action' => 'admin_index','admin' => false));

    }
    else if($role == '2'){
        $this->redirect(array('controller' => 'users','action' => 'admin_index', 'admin' => false));

    }
    else if($role == '9'){
        $this->redirect(array('controller' => 'users', 'action' => 'index', 'admin' => false));
        $this->Session->setFlash('3');
    }

   }

This is just another way to work things out i included the dashboard function in my users controller and did auth login redirect to this function from appcontroller. Hope it solves problem for others who are facing the issue. Thanks