2016-03-28 6 views
-1

Wenn Benutzer anmelden, senden System eine Bestätigungs-E-Mail an den Benutzer seine Arbeit gut, aber ohne E-Mail-Bestätigungssystem automatische Anmeldung oder Benutzer kann anmelden. Wie kann ich dies lösen, dass Benutzer E-Mails vor dem Login bestätigen sollten und wenn der Benutzer nicht bestätigt hat, dass ein E-Mail-Benutzer nicht eingeloggt werden kann?Yii2 ohne E-Mail-Bestätigung Benutzer kann nicht anmelden

i this project: Yii 2 Advanced Template With Rbac User Managment bin mit

mein Loginform Modellcode

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') 
    ]; 
} 
} 

Antwort

1

Unten finden Sie Funktion gemeinsam/models/User.php

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

und ersetzen Sie es durch folgende

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

Hoffe, das hilft dir

Verwandte Themen