Yii2 Ошибка авторизации. Неверный логин и пароль
Ситуация следующая. Достался в работу сайт на yii2. Через некоторое время перестала работать авторизация. Логин и пароль 100% верны, но не пускает. Пишет логин и пароль. В чем может быть ошибка. Помогите, сутки бьюсь. Код ниже.
model - loginForm
<?php
namespace app\models;
use Yii;
use yii\base\Model;
class LoginForm extends Model
{
public $username;
public $password;
public $rememberMe = true;
private $_user = false;
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 validatePassword($attribute, $params)
{
if (!$this->hasErrors()) {
$user = $this->getUser();
if (!$user || !$user->validatePassword($this->password)) {
$this->addError($attribute, 'имя пользователя или пароль не верны');
}
}
}
/**
* 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()) {
if($this->rememberMe){
$u = $this->getUser();
$u->generateAuthKey();
$u->save();
}
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0);
}
return false;
}
public function getUser()
{
if ($this->_user === false) {
$this->_user = User::findByUsername($this->username);
}
return $this->_user;
}
}
Контроллер Site
<?php
namespace app\controllers;
use Yii;
use yii\filters\AccessControl;
use yii\web\Response;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
use yii\web\Controller;
use yii\data\ActiveDataProvider;
class SiteController extends Controller
{
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'actions' => ['login', 'error', 'signup'],
'allow' => true,
],
[
'allow' => true,
'roles' => ['@'],
],
[
'allow' => true,
'controllers' => ['site/signup'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post', 'get'],
],
],
];
}
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];
}
public function actionLogin()
{
$this->layout = 'login';
if (!Yii::$app->user->isGuest) {
return $this->goHome();
}
$model = new LoginForm();
if ($model->load(Yii::$app->request->post()) && $model->login()) {
// echo '<pre>'; print_r($model);
return $this->goBack();
}
else {
$model->password = '';
// echo '<pre>'; print_r($model);
return $this->render('login', [
'model' => $model,
]);
}
}
public function actionLogout()
{
Yii::$app->user->logout();
return $this->goHome();
}
Модель - User
<?php
namespace app\models;
use yii\db\ActiveRecord;
class User extends ActiveRecord implements \yii\web\IdentityInterface
{
public static function tableName()
{
return 'user';
}
public static function findIdentity($id)
{
return static::findOne($id);
}
public static function findIdentityByAccessToken($token, $type = null)
{
}
public static function findByUsername($username)
{
return static::findOne(['username' => $username]);
}
public function getId()
{
return $this->id;
}
public function getAuthKey()
{
return $this->auth_key;
}
public function validateAuthKey($authKey)
{
return $this->auth_key === $authKey;
}
public function validatePassword($password)
{
return \Yii::$app->getSecurity()->validatePassword($password, $this->password);
}
public function generateAuthKey()
{
$this->auth_key = \Yii::$app->getSecurity()->generateRandomString();
}
public function attributeLabels()
{
return [
'id' => 'ID',
'username' => 'Логин',
'password' => 'Пароль',
'name' => 'Имя',
'mail' => 'Почта',
'role' => 'Роль',
'crm' => 'Доступ к системе учета'
];
}
}
И возможно потребуется, создание пользователей через UserController
<?php
namespace app\controllers;
use Yii;
use app\models\User;
use app\models\UserSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use app\models\Sales;
use yii\data\ActiveDataProvider;
class UserController extends Controller
{
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'allow' => true,
'roles' => ['?'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
public function actionCreate()
{
$model = new User();
$hash = Yii::$app->getSecurity()->generatePasswordHash('qwerty123456');
if ($model->load(Yii::$app->request->post())) {
$post = Yii::$app->request->post();
//debug($post);
$hash = Yii::$app->getSecurity()->generatePasswordHash($post['User']['password']);
$model->username = $post['User']['username'];
$model->password = $hash;
$model->name = $post['User']['name'];
$model->mail = $post['User']['mail'];
$model->role = $post['User']['role'];
$model->crm = $post['User']['crm'];
$model->save();
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('create', [
'model' => $model,
]);
}
protected function findModel($id)
{
if (($model = User::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
}