Login redirect depending on user role (RBAC)

Hey Guys,

I’ve searched around and can’t seem to find a solution to the problem. I’m a rookie developer, so apologies if this is straight forward.

I’m wanting to have a simple re-direct depending on the user role. I have a “role” row within my “Users” table, and I want them to be directed to the “Index.php” page if they are a “user”, and the “Dashboard” page if they are an “administrator”.

I understand that it has something to do with the “SiteController”, I’m just not sure of the exact code. For a reference, I currently have the following under the “ActionLogin” function -

public function actionLogin()

{


	$model=new LoginForm;





	// if it is ajax validation request


	if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')


	{


		echo CActiveForm::validate($model);


		Yii::app()->end();


	}





	// collect user input data


	if(isset($_POST['LoginForm']))


	{


		$model->attributes=$_POST['LoginForm'];


		// validate user input and redirect to the previous page if valid


		if($model->validate() && $model->login())


			$this->redirect(array("Site/Dashboard"));


	}


	// display the login form


	$this->render('login',array('model'=>$model));


	


}

Does anybody know how to do this?

Thanks a lot, I’m slowly learning!

This should be simple

after this line :

if($model->validate() && $model->login())




   if ($model->role == 'value for user role')

        $this->redirect('url for user role')

   else if ($model->role == 'value for admin role')

         $this->redirect('url for admin role')



1 Like