2017-05-27 76 views
1

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

小的變化要保存模型兩次,爲什麼呢?你也沒有檢查第二次保存的結果。在actionCreate1你不檢查保存的結果和手工指定這是由負載的方法自動完成的屬性。這是不好的編碼。 – Bizley

+0

in action創建我需要插入後的記錄ID。如何解決這個問題?在actionCreate1讓用戶沒有問題,良好的登錄。 – user3770797

回答

0

您正在使用相同的名稱屬性保管密碼哈希和原始密碼。

將密碼哈希屬性更改爲類似passwod_hash或原始密碼屬性類似於raw_password之類的內容,並相應地修改代碼以處理它。

+0

所以爲什麼我登錄與通:12345用戶:admin,請檢查圖像:http://i.imgur.com/Xy78066.jpg – user3770797

+0

顯示你'User'和''LoginForm'規則()'方法。 – Bizley

+0

用戶:公共職能規則() { 回報[ [ 'AUTH_KEY', '用戶名', '密碼', '角色', '姓名', '姓氏', 'mahal_tavalod', 'mahal_sodor',「name_pedar ','tarikh_tavalod','telephon','mobile','address','code_posti','sherkat_bime','shomare_gharardad'],'string'], [['code','created_at','status' ,'codemeli','shomare_shenasname','sex','darsad_porsant','total_sod'],'integer'], [[''username','code','codemeli'],'unique'], ] ; } – user3770797

0

解決此問題。

第一步:保存模型之前用戶 變化

public function beforeSave($insert) 
    { 
     if($insert) 
     { 
      if($this->password) { 
       $this->setPassword($this->password); 
      } 

      $this->generateAuthKey(); 
     } 

     return parent::beforeSave($insert); 
    } 

第二步:在actionCreate在更新用戶

In the first code 
    $password = $model->password; 

    if(Yii::$app->request->post('User')['password'] != null) { 
$model->password = Yii::$app->security->generatePasswordHash(Yii::$app->request->post('User')['password']); 
} 
else 
{ 
$model->password = $password; 
} 
相關問題