2

当用户注册时,系统会向用户发送确认电子邮件,其工作正常但没有任何电子邮件确认系统自动登录或用户可以登录。我该如何解决这个问题,该用户应在登录前确认电子邮件,如果用户未确认电子邮件用户无法登录?

我正在使用这个项目:Yii 2 Advanced Template With Rbac User Management

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

5 回答 5

4

发生这种情况是因为默认情况下电子邮件状态为非活动状态,要更改此状态,您可以转到

常见/模型/user.php

并将规则功能

public function rules()
    {
        return [
            ['status', 'default', 'value' => self::STATUS_INACTIVE],
            ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_INACTIVE, self::STATUS_DELETED]],
        ];
    }

public function rules()
    {
        return [
            ['status', 'default', 'value' => self::STATUS_ACTIVE],
            ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_INACTIVE, self::STATUS_DELETED]],
        ];
    }

祝你好运

于 2019-03-29T04:40:02.953 回答
2

在 common/models/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]);
}

希望对你有帮助

于 2016-03-28T07:23:21.297 回答
0

将后端(用户表)的状态更改为 10 就像魅力一样

于 2021-02-20T07:43:11.770 回答
0

您需要为电子邮件验证设置 SMTP,但对于 localhost,您可以使用以下步骤。

  • 在'@frontend/runtime/mail'目录下有一个类似2021XXXX-162725-6907-8769.eml的文件名。
  • 打开它并单击其中的链接,然后您会收到一条消息“您的电子邮件已被确认!” 在您的网络浏览器中。
  • 现在,您的电子邮件已通过验证,您可以继续登录用户。
于 2021-07-22T19:29:14.310 回答
-1

在数据库的用户表中使用 sataus = 10 它对我有用。

于 2020-06-22T06:23:24.330 回答