Юзер из базы достается без проблем, валидацию пароля и логина проходит, далее объект юзера передается в метод login и тут начинается самое интересное. Появляется приветственное имя, типа привет admin, далее стоит сделать переход по ссылке, как тут же разлогинивается. И приходится логинится по новой. Потом опять тоже самое - переход по ссылке, и опять становишься гостем. Не понимаю что за хрень.
LoginForm
Код: Выделить всё
<?php
namespace app\models;
use Yii;
use yii\base\Model;
/**
* LoginForm is the model behind the login form.
*
* @property User|null $user This property is read-only.
*
*/
class LoginForm extends Model
{
public $username;
public $password;
public $rememberMe = true;
private $_user = false;
/**
* @return array the validation rules.
*/
public function rules()
{
return [
// username and password are both required
[['username', 'password'], 'required'],
// rememberMe must be a boolean value
['rememberMe', 'boolean'],
// password is validated by validatePassword()
['password', 'validatePassword'],
];
}
//лэйблы
public function attributeLabels()
{
return [
'username' => 'Логин',
'password' => 'Пароль',
'rememberMe' => 'Запомнить меня',
];
}
/**
* 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)) {
$this->addError($attribute, 'Incorrect username or password.');
}
}
}
/**
* Logs in a user using the provided username and password.
* @return bool 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);
//Yii::$app->user->login($this->getUser());
}
return false;
}
/**
* Finds user by [[username]]
*
* @return User|null
*/
public function getUser()
{
if ($this->_user === false) {
$this->_user = UserIdentity::findByUsername($this->username);
}
return $this->_user;
}
}
Users
Код: Выделить всё
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "users".
*
* @property integer $user_id
* @property string $username
* @property string $password
* @property integer $role
* @property string $created_at
* @property string $update_at
*/
class Users extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'users';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['username', 'password'], 'required'],
[['role'], 'integer'],
[['created_at', 'update_at'], 'safe'],
[['username'], 'string', 'max' => 50],
[['password'], 'string', 'max' => 255],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'user_id' => 'User ID',
'username' => 'Username',
'password' => 'Password',
'role' => 'Role',
'created_at' => 'Created At',
'update_at' => 'Update At',
];
}
}
UserIdentity
Код: Выделить всё
<?php
namespace app\models;
class UserIdentity extends Users implements \yii\web\IdentityInterface
{
/**
* @inheritdoc
*/
public static function findIdentity($id)
{
return static::findOne($id);
}
/**
* @inheritdoc
*/
public static function findIdentityByAccessToken($token, $type = null)
{
return static::findOne(['access_token' => $token]);
}
/**
* Finds user by username
*
* @param string $username
* @return static|null
*/
public static function findByUsername($username)
{
return static::findOne(['username' => $username]);
}
/**
* @inheritdoc
*/
public function getId()
{
return $this->id;
}
/**
* @inheritdoc
*/
public function getAuthKey()
{
return $this->auth_key;
}
/**
* @inheritdoc
*/
public function validateAuthKey($authKey)
{
return $this->auth_key === $authKey;
}
/**
* Validates password
*
* @param string $password password to validate
* @return bool if password provided is valid for current user
*/
public function validatePassword($password)
{
return $this->password === $password;
}
}