Yii2没有邮件确认用户不能登录

ruyhziif  于 2022-11-09  发布在  其他
关注(0)|答案(5)|浏览(158)

当用户注册,系统发送一个确认邮件给用户它的工作很好,但没有任何电子邮件确认系统自动登录或用户可以登录.我该如何解决这个问题,用户应该确认电子邮件登录前,如果用户没有确认一个电子邮件用户不能登录?
我正在使用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')
    ];
}
}
c3frrgcw

c3frrgcw1#

这是因为默认情况下电子邮件状态为非活动状态,要更改此状态,您可以转到
common/models/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]],
        ];
    }

祝你好运

q35jwt9p

q35jwt9p2#

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

希望这对你有帮助

qlfbtfca

qlfbtfca3#

将后端(用户表)的状态更改为10非常有效

ztyzrc3y

ztyzrc3y4#

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

  • 在“@frontend/runtime/mail”目录中有一个类似 * 2021 XXXX-162725-6907-8769.eml* 的文件名。
  • 打开它,点击其中的链接,然后你在你的网络浏览器中收到一条消息“你的电子邮件已经确认!”
  • 现在,您的电子邮件已通过验证,您可以继续登录用户。
at0kjp5o

at0kjp5o5#

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

相关问题