後製定或修改密碼的用戶我已經在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