2017-05-27 74 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; 
} 
相关问题