Yii2 - 登录后用户被注销
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Yii2 - 登录后用户被注销相关的知识,希望对你有一定的参考价值。
我使用标准方式登录,但由于某种原因,会话/登录未按预期工作。一旦用户被重定向,会话将被销毁,用户将不会保持登录状态。
登录:
public function actionLogin()
{
$model = new LoginForm();
if ($model->load(Yii::$app->request->post()) && $model->login()) {
var_dump('<pre>', Yii::$app->user->identity , '</pre>');die;
// $this->redirect(Yii::$app->user->returnUrl);
} else {
return $this->render('login', [
'model' => $model,
]);
}
}
(var_dump为用户提供正确的信息,这意味着用户已成功登录)。
LoginForm模型:
<?php
namespace appmodels;
use appmodels;
use Yii;
use yiiaseModel;
/**
* LoginForm is the model behind the login form.
*/
class LoginForm extends Model
{
public $email;
public $password;
public $rememberMe = true;
public $verifyCode;
private $_user = false;
/**
* @return array the validation rules.
*/
public function rules()
{
return [
// email and password are both required
[['email', 'password'], 'required'],
// rememberMe must be a boolean value
['rememberMe', 'boolean'],
// password is validated by validatePassword()
['password', 'validatePassword'],
[['verifyCode'], 'required', 'when' => function () {
return $this->captchaNeeded;
}],
[['verifyCode'], 'captcha', 'when' => function () {
return $this->captchaNeeded;
}],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'verifyCode' => 'Security code'
];
}
/**
* 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->password)) {
//handle password retries and set captcha checker
Yii::$app->session->set('_try_login', Yii::$app->session->get('_try_login', 0) + 1);
//return error
$this->addError($attribute, 'Incorrect email or password.');
}
}
}
/**
* Check if captcha is needed.
*
* @return boolean whether the user try to login more than 2 times
*/
public function getCaptchaNeeded()
{
return Yii::$app->session->get('_try_login', 0) > 2;
}
/**
* Logs in a user using the provided email 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 [[email]]
*
* @return User|null
*/
public function getUser()
{
if ($this->_user === false)
return User::findByEmail($this->email);
return $this->_user;
}
}
用户模型是标准的(在此处发布它非常大),但它正在实现IdentityInterface
class User extends yiidbActiveRecord implements IdentityInterface
Session正在使用db,它在web.php(配置文件)中定义:
'session' => [
'class' => 'yiiwebDbSession'
],
整体用户模型工作正常,因为如果我尝试使用错误信息登录,我将收到正确的通知,如果我只是在登录后呈现页面(经过验证的登录,我将获得正确的标题)。有些东西在某个地方失败了,我无法弄清楚到底是什么。
我不知道这有关系,我使用的是PHP版本7.0.15(PHP 7.0.15-0ubuntu0.16.04.2(cli)(NTS)),Yii2是2.0.11.2。
答案
你检查过$user->status = 10
我遇到过这个问题吗?我在数据库上将用户状态从0更改为10并且工作正常。
以上是关于Yii2 - 登录后用户被注销的主要内容,如果未能解决你的问题,请参考以下文章