Reputation: 812
I have a site running in production which is working fine and I am making a few changes to code so I'm copying it to localhost. The site has a user management system and requires login. Once user logs in, they are redirected to mysite.com/admin/index.
The user validation is taking place and authenticate class in UserIdentity class return errorcode as 0. However, when user goes to WebUser class, it returns an empty value when I check $this->id. Also, Yi::app()->user->id does not exist.
Following are relevant sections of my code:
In main.php
'components'=>array(
'user'=>array(
// enable cookie-based authentication
// 'allowAutoLogin'=>true,
'class' => 'WebUser',
'loginUrl'=>array('site/login'),
),
// uncomment the following to enable URLs in path-format
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
In UserIdentity.php
private $_group;
private $_id;
public function getId(){
return $this->_id;
}
public function authenticate($seeker = false, $queryid = false){
//Retrieve seeker Login
$record=User::model()->findByAttributes(array('login_name'=>$this->username, 'status' => 0));
//Invalid username
if($record===null){
$this->errorCode=self::ERROR_USERNAME_INVALID;
}
//Invalid password
else if($record->password !== $record->encryptPassword($this->password) && $record->password !== $record->encryptPassword($this->password, true)){
$this->errorCode=self::ERROR_PASSWORD_INVALID;
}
//Valid login
else{
//Use new encryption
if($record->password === $record->encryptPassword($this->password, true))
$record->resetPassword($this->password);
$this->_id = $record->id;
$this->_group = $record->group;
$this->errorCode = self::ERROR_NONE;
}
if($this->errorCode == self::ERROR_NONE){
$auth=Yii::app()->authManager;
try{ $role=$auth->createRole($this->_group); } catch(Exception $e){}
if(!$auth->isAssigned($this->_group,$this->_id)){
if($auth->assign($this->_group,$this->_id)){
Yii::app()->authManager->save();
}
}
}
return !$this->errorCode;
}
In my SiteController.php, when I check for userid after login, it returns correct value but after the user is redirected to admin/index and I check value for userid, it doesn't show any value.
Can someone suggest what I am missing?
Upvotes: 0
Views: 185
Reputation: 915
If deciding only from your code and nothing else matters, it looks like this validation
Yii::app()->user->returnUrl == '/index.php'
is always false. You can simply debug this by stopping your code at this pint in debugger, or by simply putting „dump'n'stop code“ :)
var_dump(
Yii::app()->user->returnUrl == '/index.php',
Yii::app()->user->returnUrl
);
die();
before this line
$this->redirect(Yii::app()->user->returnUrl == '/index.php'? '/admin/index': Yii::app()->user->returnUrl);
Upvotes: 0
Reputation: 11829
code below save url state,
request admin/edit/3
-> redirect to login page
-> redirect to admin/edit/3
request login page
-> redirect admin/index
$this->redirect(Yii::app()->getUser()->getReturnUrl('admin/index'));
Upvotes: 1