protected\components\UserIdentity.php文件: ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
| classUserIdentityextendsCUserIdentity
{
private$_id;
publicfunctionauthenticate()
{
$record=User::model()->findByAttributes(array('username'=>$this->username));
if($record===null)
$this->errorCode=self::ERROR_USERNAME_INVALID;
elseif($record->password!==md5($this->password))
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else{
$this->_id=$record->id;
$this->setState('title',$record->title);
$this->errorCode=self::ERROR_NONE;
}
return!$this->errorCode;
}
publicfunctiongetId()
{
return$this->_id;
}
}
|
Model: ?
1
2
3
4
5
6
7
8
9
10
11
12
13
| $identity=newUserIdentity($this->username,$this->password);
if($identity->authenticate())
Yii::app()->user->login($identity);
else
echo$identity->errorMessage;
// 注销当前用户
Yii::app()->user->logout();
// 保留用户登陆状态时间7天
// 确保用户部件的allowAutoLogin被设置为true。
//Yii::app()->user->login($identity,3600*24*7);
|
|