【发布时间】:2014-11-26 18:02:53
【问题描述】:
我有一个正在生产中运行的站点,它运行良好,我正在对代码进行一些更改,因此我将其复制到本地主机。该站点有一个用户管理系统,需要登录。用户登录后,他们将被重定向到 mysite.com/admin/index。
正在进行用户验证,并且 UserIdentity 类中的身份验证类返回错误代码为 0。但是,当用户转到 WebUser 类时,当我检查 $this->id 时它返回一个空值。另外,Yi::app()->user->id 也不存在。
以下是我的代码的相关部分:
在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>',
),
),
在 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;
}
在我的 SiteController.php 中,当我在登录后检查用户 ID 时,它返回正确的值,但在用户被重定向到 admin/index 并且我检查用户 ID 的值后,它没有显示任何值。
有人可以建议我缺少什么吗?
【问题讨论】: