关于登陆后右上角么有登录用户名的问题。 [ 2.0 版本 ]
登录能够成功,但是就是登陆后右上角logout(admin)括号中的名称为空,登录的对象是自己创建的User对象
对象代码如下
不知道哪里错了,求各位大神帮助QAQ。
/**
* @inheritdoc
*/
class User extends \app\core\base\BaseActiveRecord implements \yii\web\IdentityInterface
{
public $username;
public $password;
public $rememberMe;
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%user}}';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['user_name', 'user_pw'], 'required'],
[['user_name', 'user_pw'], 'string', 'max' => 64]
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'user_name' => 'User Name',
'user_pw' => 'User Pw',
'Email' => 'Email',
'regtime' => 'Regtime',
];
}
public function scenarios()
{
$parent = parent::scenarios();
$parent['login'] = ['username','password'];
$parent['register'] = ['user_name','user_pw','Email'];
return $parent;
}
/**
* @return \yii\db\ActiveQuery
*/
public function getAssignments()
{
return $this->hasMany(Assignment::className(), ['user_id' => 'id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getItemNames()
{
return $this->hasMany(Item::className(), ['name' => 'item_name'])->viaTable('{{%assignment}}', ['user_id' => 'id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getLeavewords()
{
return $this->hasMany(Leaveword::className(), ['userid' => 'id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getReplywords()
{
return $this->hasMany(Replyword::className(), ['userid' => 'id']);
}
public static function findIdentity($id)
{
return User::findOne(['id'=>$id]);
}
public static function findIdentityByAccessToken($token, $type = null)
{
foreach (self::$users as $user) {
if ($user['accessToken'] === $token) {
return new static($user);
}
}
return null;
}
public function getId()
{
return $this->id;
}
public function getAuthKey()
{
return $this->auth_key;
}
public function validateAuthKey($authKey)
{
return $this->auth_key === $authKey;
}
public function validatePassword($password,$password_hash)
{
return Yii::$app->security->validatePassword($password, $password_hash);
}
public function login(){
if (!$this->validate()) {
return false;
}
$user = User::findOne(['user_name'=>$this->username]);
if($user!==null)
{
if ($this->password === $user->user_pw) {
return Yii::$app->user->login($user, $this->rememberMe ? 3600*24*30 : 0);
}
return false;
}
else {
return false;
}
}
}
最佳答案
其他 1 个回答
zkh90644
注册时间:2015-05-11
最后登录:2015-12-23
在线时长:2小时34分
最后登录:2015-12-23
在线时长:2小时34分
- 粉丝0
- 金钱150
- 威望0
- 积分170