Login Redirect To Particular Page

Hi,

I am creating a page for active users…

I am having a field called status in my table having active,start and inactive status of the user

I want it something like after user logs in, it will check the status of the user if status is active or start that particular page should be displayed and if inactive that particular page should not be displayed instead he should see his profile page.

Thanks in advance.

Hi Mithlesh

In your SiteController you have an action name Login in that action there is redirect call


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

  $this->redirect(Yii::app()->user->returnUrl);

}

right before the call you can do your check if user is active something as following




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

$user = User::model()->findByPk(Yii::app()->user); 

if($user->active === 1) {

  $this->redirect(array("/controller/action")); redirect to your active page / profile page

} else {

    $this->redirect(array("/controller/action")); redirect to your inactive page

}

}



NOTE:

  1. You can avoid the second query we doing to check if user is active or not

  2. In order for this to work you have to have a field in your database that represent your active state

Hi alirz,

After using the given code it is showing error "Object of class CWebUser could not be converted to int"

I am not using RBAC.

Yes i am having a status field in my table consisting active and inactive status.

Please suggest.

Dear Friend

The error occurs because Yii::app()->user is an object, an instance of CWebUser.

Kindly check the value of the following.




echo Yii::app()->user->id;



If it gives username (it is the default thing in Yii) then




$user=User::model()->findByAttributes(array('username'=>Yii::app()->user->id));



If it gives an integer value then




$user=User::model()->findByPk(Yii::app()->user->id);



Regards.

Mithlesh please change the following


$user = User::model()->findByPk(Yii::app()->user); 

to


$user = User::model()->findByPk(Yii::app()->user->id); 

Hi alirz/seenivasan,

if i use the below code the else condition gets executed and user is shown his profile page

Note:- (status === 1)




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

				$user = User::model()->findByPk(Yii::app()->user->Id);  

				if($user->status === 1) { 

				  $this->redirect(array("/user/news"));  

				} else { 

					$this->redirect(array("/site/profile"));

				} 

				}}



if i use the below code i get error “Use of undefined constant active - assumed ‘active’”

Note:- (status === active)




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

				$user = User::model()->findByPk(Yii::app()->user->Id);  

				if($user->status === active) { 

				  $this->redirect(array("/user/news"));  

				} else { 

					$this->redirect(array("/site/profile"));

				} 

				}}



I have already defined constant in user model




class User extends CActiveRecord

{


	const STATUS_ACTIVE='Active';

	const STATUS_INACTIVE='Inactive';


	public function getStatusOptions()

		{

			return array(

				self::STATUS_ACTIVE=>'Active',

				self::STATUS_INACTIVE=>'Inactive',

				);

		}	



Kindly suggest.

Thanks in advance.

if i use the below code the else condition gets executed and user is shown his profile page

Note:- (status === 1)




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

				$user = User::model()->findByPk(Yii::app()->user->Id);  

				if($user->status === 1) { 

				  $this->redirect(array("/user/news"));  

				} else { 

					$this->redirect(array("/site/profile"));

				} 

				}} // this closing brace is not needed



Don’t you have a closing brace (}) which is not needed in the above code

Dear Friend

Kindly check after replacing the idendical operator(===) with equal operator(==).




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

{ 

     $user = User::model()->findByPk(Yii::app()->user->Id); 

 

     if($user->status == 1)                           //'===' replced with '=='

         $this->redirect(array("/user/news"));  


     else  $this->redirect(array("/site/profile"));

                                

                                

}



the error is undefined const active question is where that active const is coming from you dont have const active in your code please double check. you only have two constants defined in the following code STATUS_ACTIVE and STATUS_INACTIVE


class User extends CActiveRecord

{


        const STATUS_ACTIVE='Active';

        const STATUS_INACTIVE='Inactive';


        public function getStatusOptions()

                {

                        return array(

                                self::STATUS_ACTIVE=>'Active',

                                self::STATUS_INACTIVE=>'Inactive',

                                );

                }    

Hi alirz,

I am having a table with status field which contains the value either active and inactive.

If the status field contains active page1 is displayed and if the status field contains inactive page2 is displayed.

I am not sure where i am doing wrong.

Thanks in advance

Hi All,

Thank you all for the support and your valuable time, It is now working.

There was no requirement of constant

I have put the active in single quote and after that it started working.




if($user->status == 'active') 



What is the type of your status fields in your table? Is it a boolean? If it is mysql, it should then return 0 or 1. You should then compare it to 0 or 1. If it is a string, then you should compare to ‘active’ or ‘inactive’

he said its working now

Hi Jimlam,

Yes it is working…

We posted at the same time.

Thanks for looking into this issue.