2016-03-28 74 views
-1

當用戶註冊時,系統發送確認郵件給用戶其工作正常,但沒有任何電子郵件確認系統自動登錄或用戶可以登錄。我如何解決這個問題,該用戶應該在登錄前確認電子郵件,如果用戶未確認電子郵件用戶不能登錄?Yii2沒有電子郵件確認用戶不能登錄

我使用this project: Yii 2 Advanced Template With Rbac User Managment

我LoginForm的型號代碼

namespace common\models; 
use Yii; 
use yii\base\Model; 

/** 
* Login form 
*/ 

class LoginForm extends Model 
{ 
public $email; 
public $password; 
public $rememberMe = true; 

protected $_user = false; 


/** 
* @inheritdoc 
*/ 
public function rules() 
{ 
    return [ 
     // username and password are both required 
     ['email', 'filter', 'filter' => 'trim'], 
     [['email','password'], 'required'], 
     ['email', 'email'], 
     // rememberMe must be a boolean value 
     ['rememberMe', 'boolean'], 
     // password is validated by validatePassword() 
     ['password', 'validatePassword','skipOnEmpty'=>false], 
    ]; 
} 

/** 
* 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->$attribute)) { 
      $this->addError('email', Yii::t('messages','Incorrect password or email.')); 
      $this->addError('password', Yii::t('messages','Incorrect password or email.')); 
     } 
    } 
} 

/** 
* 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::findByEmail($this->email); 
    } 

    return $this->_user; 
} 

public function attributeLabels() 
{ 
    return [ 
     'email' => Yii::t('app','Email'), 
     'password' => Yii::t('app','Password') 
    ]; 
} 
} 

回答

1

發現以下共同點/型號功能/ user.php的

public static function findByEmail($email) 
    { 
     return static::findOne(['email'=>$email,'status'=>self::STATUS_ACTIVE]); 
    } 

,並用以下

public static function findByEmail($email) 
{ 
    return static::findOne(['email'=>$email,'status'=>self::STATUS_ACTIVE,'email_verification_status'=>self::EMAIL_VERIFIED]); 
} 
更換

希望這會幫助你