Yii2 - 登录后用户被注销

时间:2017-02-26 16:02:17

标签: php yii yii2

我使用标准方式登录,但由于某种原因,会话/登录无法按预期工作。一旦用户被重定向,会话将被销毁,并且用户不会继续登录。

登录:

std::condition_variable::wait

(var_dump为用户提供了正确的信息,这意味着用户已成功登录)。

LoginForm模型

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

用户模型是标准的(在此处发布它非常大),但它正在实现IdentityInterface

<?php

namespace app\models;

use app\models;
use Yii;
use yii\base\Model;

/**
 * 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;
    }
}

Session正在使用db,它在web.php(配置文件)中定义:

class User extends \yii\db\ActiveRecord implements IdentityInterface

整体用户模型工作正常,因为如果我尝试使用错误信息登录,我将收到正确的通知,如果我只是在登录后呈现页面(经过验证的登录,我将获得正确的标题)。某些事情在某个方面失败了,我无法弄明白究竟是什么。

我不知道这有关系,我使用的是PHP版本7.0.15( PHP 7.0.15-0ubuntu0.16.04.2(cli)(NTS)),以及Yii2是2.0.11.2。

1 个答案:

答案 0 :(得分:0)

您检查过$user->status = 10 我遇到过这个问题。我在数据库上将用户状态从0更改为10并且有效。

相关问题