mithlesh
(Mithlesh1007)
October 19, 2012, 7:23pm
1
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.
alirz23
(Alirz23)
October 19, 2012, 7:34pm
2
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:
You can avoid the second query we doing to check if user is active or not
In order for this to work you have to have a field in your database that represent your active state
mithlesh
(Mithlesh1007)
October 21, 2012, 9:09am
3
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.
seenivasan
(Chellamnivas)
October 21, 2012, 9:36am
4
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.
alirz23
(Alirz23)
October 21, 2012, 1:05pm
5
Mithlesh please change the following
$user = User::model()->findByPk(Yii::app()->user);
to
$user = User::model()->findByPk(Yii::app()->user->id);
mithlesh
(Mithlesh1007)
October 21, 2012, 3:29pm
6
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.
Bianca
(Biancajsen)
October 21, 2012, 3:47pm
7
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
seenivasan
(Chellamnivas)
October 21, 2012, 4:14pm
8
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"));
}
alirz23
(Alirz23)
October 21, 2012, 5:40pm
9
if($model->validate() && $model->login()) {
$user = User::model()->findByPk(Yii::app()->user->Id);
if($user->status === active) { // where this active const is comming from
$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',
);
}
mithlesh
(Mithlesh1007)
October 21, 2012, 5:56pm
10
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
mithlesh
(Mithlesh1007)
October 21, 2012, 6:18pm
11
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')
Bianca
(Biancajsen)
October 21, 2012, 6:19pm
12
Mithlesh:
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
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’
mithlesh
(Mithlesh1007)
October 22, 2012, 3:49am
14
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’
Hi Jimlam,
Yes it is working…
We posted at the same time.
Thanks for looking into this issue.