2017-09-15 99 views
1

我想在Yii2中定製登錄失敗消息。 現在,當我輸入錯誤username或錯誤password它將顯示兩個相同的消息。Yii2:根據確切原因定製登錄失敗消息

現在我有一列user表即is_verified 如果用戶的電子郵件驗證,然後我把這個字段的值爲1,否則0

如果用戶註冊和不檢驗他的電子郵件的is_verified=0的價值

現在,如果用戶想要登錄則表明Incorrect username or password.

,但我想告訴大家,他的電子郵件未驗證用戶。

LoginForm.php功能validatePassword將通過這個錯誤

public function validatePassword($attribute, $params) { 
     if (!$this->hasErrors()) { 
      $user = $this->getUser(); 
      if (!$user || !$user->validatePassword($this->password)) { 
       $this->addError($attribute, 'Incorrect username or password.'); 
      } 
     } 
    } 

請在此幫助或提供任何想法我怎麼能覆蓋默認登錄功能

我使用yii2-basic-app

一件事$user = $this->getUser();這將只返回那些默認情況下我的status=10 用戶設置用戶status=0直到他無法確認他的電子郵件

什麼想做的事:

if(username not exist in db){ 
     echo "username not found in db"; 
    }elseif(password not valid){ 
     echo "password not valid"; 
    }elseif(is_verified=0){ 
     echo "email not verified"; 
    }elseif(status = 0){ 
     echo "account is deactive"; 
    } 

代碼app\models\User.php

/** 
    * @inheritdoc 
    */ 
    public static function findIdentity($id) { 
     return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]); 
    } 

    /** 
    * @inheritdoc 
    */ 
    public static function findIdentityByAccessToken($token, $type = null) { 
     throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.'); 
    } 

    /** 
    * Finds user by username 
    * 
    * @param string $username 
    * @return static|null 
    */ 
    public static function findByUsername($username) { 
     return static::findOne(['email' => $username, 'status' => self::STATUS_ACTIVE]); 
    } 

    /** 
    * Finds user by password reset token 
    * 
    * @param string $token password reset token 
    * @return static|null 
    */ 
    public static function findByPasswordResetToken($token) { 
     if (!static::isPasswordResetTokenValid($token)) { 
      return null; 
     } 

     return static::findOne([ 
      'password_reset_token' => $token, 
      'status' => self::STATUS_ACTIVE, 
     ]); 
    } 

    /** 
    * Finds out if password reset token is valid 
    * 
    * @param string $token password reset token 
    * @return boolean 
    */ 
    public static function isPasswordResetTokenValid($token) { 
     if (empty($token)) { 
      return false; 
     } 
     $expire = Yii::$app->params['user.passwordResetTokenExpire']; 
     $parts = explode('_', $token); 
     $timestamp = (int) end($parts); 
     return $timestamp + $expire >= time(); 
    } 

    /** 
    * @inheritdoc 
    */ 
    public function getId() { 
     return $this->getPrimaryKey(); 
    } 

    /** 
    * @inheritdoc 
    */ 
    public function getAuthKey() { 
     return $this->auth_key; 
    } 

    /** 
    * @inheritdoc 
    */ 
    public function validateAuthKey($authKey) { 
     return $this->getAuthKey() === $authKey; 
    } 

    /** 
    * Validates password 
    * 
    * @param string $password password to validate 
    * @return boolean if password provided is valid for current user 
    */ 
    public function validatePassword($password) { 
     return Yii::$app->security->validatePassword($password, $this->password_hash); 
    } 

    /** 
    * Generates password hash from password and sets it to the model 
    * 
    * @param string $password 
    */ 
    public function setPassword($password) { 
     $this->password_hash = Yii::$app->security->generatePasswordHash($password); 
    } 

    /** 
    * Generates "remember me" authentication key 
    */ 
    public function generateAuthKey() { 
     $this->auth_key = Yii::$app->security->generateRandomString(); 
    } 

    /** 
    * Generates new password reset token 
    */ 
    public function generatePasswordResetToken() { 
     $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time(); 
    } 

    /** 
    * Removes password reset token 
    */ 
    public function removePasswordResetToken() { 
     $this->password_reset_token = null; 
    } 

代碼從app\models\LoginForm.php

/** 
    * Validates the password. 
    * This method serves as the inline validation for password. 
    * 
    * @param string $attribute the attribute currently being validated 
    * @param array $params the additional name-value pairs given in the rule 
    */ 
    public function validatePassword($attribute, $params) { 
     if (!$this->hasErrors()) { 
      $user = $this->getUser(); 
      if (!$user || !$user->validatePassword($this->password)) { 
       $this->addError($attribute, 'Incorrect username or password.'); 
      } 
     } 
    } 

    /** 
    * Logs in a user using the provided username and password. 
    * 
    * @return boolean whether the user is logged in successfully 
    */ 
    public function login() { 
     if ($this->validate()) { 
      return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0); 
     } else { 
      return false; 
     } 
    } 

    /** 
    * Finds user by [[username]] 
    * 
    * @return User|null 
    */ 
    public function getUser() { 
     if ($this->_user === false) { 
      $this->_user = User::findByusername($this->username); 
     } 

     return $this->_user; 
    } 

謝謝。

+0

你使用yii2用戶擴展嗎? – Kalu

+0

不,我沒有使用任何擴展 –

回答

1

,你可以檢查車況!$user!$user->validatePassword($this->password) separatly

public function validatePassword($attribute, $params) { 
     if (!$this->hasErrors()) { 
      $user = $this->getUser(); 
      if (!$user |) { 
       $this->addError($attribute, 'Invalid username.'); 
       return; 
      } 
      if (!$user->validatePassword($this->password)) { 
       $this->addError($attribute, 'Incorrect password.'); 
       return; 
      } 
     } 
    } 

,你可以檢查用戶是否正確設置好的這樣

假設你有一個用戶模型類

$checkUser = User::find()->where('username' => $your_userName)->one(); 
    if isset($checkUser){ 
     if($checkUser->status = 0){ 
     // User not activated 
    } 
    } else { 
    // user not present 
} 

尋找你的代碼,你可以檢查輸入的用戶名是否已經啓用,例如:

public function login() { 
    if ($this->validate()) { 
     if ( User::find()->where(['username' => $this->username , 'status' => 0 ) { 
      // mean the user is not validated .. 
      $this->addError($attribute, 'User not validated .. '); 
      return false 
     } else { 
      return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0); 

     } 
    } else { 
     return false; 
    } 
} 
+0

然後用戶名無效 – scaisEdge

+0

'$ user = $ this-> getUser();'這將只返回那些'status = 10'的用戶默認情況下我設置用戶'狀態= 0'直到他沒有驗證他的電子郵件,所以直到他的狀態= 10,我總是會在'$ this-> getUser()中出錯;' –

+0

用戶名有效,但狀態爲無效 –

0

覆蓋beforeValidate()函數並添加您的支票。 即:

public function beforeValidate() 
{ 
    if (parent::beforeValidate()) { 
     $this->user = findUserByUsernameOrEmail(trim($this->username)); 
     if ($this->user !== null && $this->user->is_verified === 0) { 
      $this->addError('username' , ' Email not verified....etc'); 
     } 
    } 

} 
1

據我理解你,功能:

public function validatePassword($attribute, $params) { 
    if (!$this->hasErrors()) { 
     $user = $this->getUser(); 
     if (!$user || !$user->validatePassword($this->password)) { 
      $this->addError($attribute, 'Incorrect username or password.'); 
      return false; 
     } 
     if(!$user->is_verified){ 
      $this->addError('username', 'Email is not verified.'); 
      return false; 
     }  
    } 
} 

當然,如果你從數據庫獲取你的用戶,在其他情況下,你必須寫在用戶或LoginForm的模型多了一個方法獲得電子郵件狀態表格數據庫,使用ActiveRecords,例如:

public function validatePassword($attribute, $params) { 
    if (!$this->hasErrors()) { 
     $user = $this->getUser(); 
     if (!$user || !$user->validatePassword($this->password)) { 
      $this->addError($attribute, 'Incorrect username or password.'); 
      return false; 
     } 
     if(!$user->getUserEmailStatus()){ 
      $this->addError('username', 'Email is not verified.'); 
      return false; 
     }  
    } 
} 
+0

'$ user = $ this-> getUser();'這將只返回那些'status = 10'的用戶,默認情況下我設置用戶'status = 0'直到他沒有驗證他的電子郵件地址 –

+0

在這種情況下你需要如果($ user-> status!= 10)$ this->覆蓋,這個函數將返回給用戶狀態,然後檢查他的狀態,在'validatePassword()' '... addError('用戶名','電子郵件未驗證。'); 返回false; } ...' – vityapro

0

我已經找到了解決方案,現在它的工作如我所料。 所有積分均爲@scaisEdge。 我從他的建議中得到了啓發。現在我發佈的實際代碼是:

public function validatePassword($attribute, $params) { 

    if (!$this->hasErrors()) { 
     $user = $this->getUser(); 
     if (!$user) { 
      $this->addError("username", 'Invalid username.'); 
      return; 
     } 
     if (!$user->validatePassword($this->password)) { 
      $this->addError($attribute, 'Incorrect password.'); 
      return; 
     } 
     if (User::find()->where(['email' => $this->username])->andwhere(['verified' => 0])->one()) { 

      $this->addError("username", 'email not varified '); 
      return; 
     } 
     if (User::find()->where(['username' => $this->username])->andwhere(['status' => 0])->one()) { 

      $this->addError($attribute, 'account is not active'); 
      return; 
     } 
    } 
} 

謝謝所有回答我的人和善待我的人。

我發佈了此答案,以便其他人可以從中獲得幫助。

相關問題