当用户注册,系统发送一个确认邮件给用户它的工作很好,但没有任何电子邮件确认系统自动登录或用户可以登录.我该如何解决这个问题,用户应该确认电子邮件登录前,如果用户没有确认一个电子邮件用户不能登录?
我正在使用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')
];
}
}
5条答案
按热度按时间c3frrgcw1#
这是因为默认情况下电子邮件状态为非活动状态,要更改此状态,您可以转到
common/models/user.php
并将规则函数从
至
祝你好运
q35jwt9p2#
在common/models/User.php中找到以下函数
并将其替换为以下内容
希望这对你有帮助
qlfbtfca3#
将后端(用户表)的状态更改为10非常有效
ztyzrc3y4#
您需要为电子邮件验证设置SMTP,但对于本地主机,您可以使用以下步骤。
at0kjp5o5#
在数据库的用户表中使用sataus = 10,它对我很有效。