后制定或修改密码的用户我已经在Yii2登录错误“错误的用户名或密码”化妆后或更改密码,用户不登录在Yii2
与用户“管理员”的登录是工作(我就与actionCreate1管理员用户)
auth_key: kYm0pvYAXY4IzuV7eYgGgtjSqoxxMNUL
password: $2y$13$QqsbMW3ErXwWOPad3abDYOPzh5XLwuEvQKBhZGEEDoT0Av5l0bE2S
,但我使用户或修改密码,在登录页面,我有错误:“不正确的用户名或密码”
我认为问题来自beforeSave在型号USER
用户表:
id int
auth_key text
username text
password text
actionCreate:它不工作
public function actionCreate()
{
$model = new User();
if ($model->load(Yii::$app->request->post())) {
if($model->save())
{
$model->img = UploadedFile::getInstance($model, 'img');
if($model->img !== null) $model->upload('img',$model->id);
$model->save();
}
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
actionCreate1:这是很好的工作
public function actionCreate()
{
$model = new User();
if ($model->load(Yii::$app->request->post())) {
$model->username = Yii::$app->request->post('User')['username'];
$model->password = Yii::$app->request->post('User')['password'];
$model->save();
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
型号用户:
public function beforeSave($insert)
{
if($this->password) {
$this->setPassword($this->password);
}
if($insert)
{
$this->generateAuthKey();
}
return parent::beforeSave($insert);
}
public function setPassword($password) {
$this->password = Yii::$app->security->generatePasswordHash($password);
}
public function generateAuthKey() {
$this->auth_key = Yii::$app->security->generateRandomString();
}
/**
* @param $password
* @return bool
*/
public function validatePassword($password) {
return Yii::$app->security->validatePassword($password, $this->password);
}
/**
* @inheritdoc
*/
public static function findIdentity($id)
{
return static::findOne([
'id' => $id
]);
}
public static function findIdentityByAccessToken($token, $type = null) {}
/**
* @param $username
* @return null|static
*/
public static function findByUsername($username)
{
return static::findOne([
'username' => $username,
]);
}
/**
* @inheritdoc
*/
public function getId()
{
return $this->id;
}
/**
* @inheritdoc
*/
public function getAuthKey()
{
return $this->auth_key;
}
/**
* @inheritdoc
*/
public function validateAuthKey($authKey)
{
return $this->auth_key === $authKey;
}
小的变化要保存模型两次,为什么呢?你也没有检查第二次保存的结果。在actionCreate1你不检查保存的结果和手工指定这是由负载的方法自动完成的属性。这是不好的编码。 – Bizley
in action创建我需要插入后的记录ID。如何解决这个问题?在actionCreate1让用户没有问题,良好的登录。 – user3770797