I stumbled upon a simple post with the implementation of a simple admin . yii2 installed advanced. I did everything as described, but the problems did not pass by. At the exit: if you check the status by 10 (STATUS_ACTIVE) - login is fine in bacend, if you change it to 20 (STATUS_ADMIN) - wrong login or password! My code: commons \ modelsUser.php

public function rules() { return [ ['status', 'default', 'value' => self::STATUS_ADMIN], ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED, self::STATUS_ADMIN]], ]; } public static function isUserAdmin($username) { return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]); } 

common \ models \ LoginForm.php

 public function loginAdmin() { if ($this->validate() && User::isUserAdmin($this->username)) { return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0); } else { return false; } } 

backend \ controllers \ SiteController.php

 public function actionLogin() { if (!Yii::$app->user->isGuest) { return $this->goHome(); } $model = new LoginForm(); if ($model->load(Yii::$app->request->post()) && $model->loginAdmin()) { return $this->goBack(); } else { return $this->render('login', [ 'model' => $model, ]); } } 
  • one
    well, right, in the database, then you probably have status = 10 - Blacknife
  • Not, not everything is so simple, not so much I’m stupid so that I don’t fix it in the DB value))) I found a solution, a little later published ... - Oleg Shleif

1 answer 1

The decision may not be the most correct, but if we do everything simply, it will do! The User class (common \ models \ User.php) has a function:

 public static function findByUsername($username) { return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]); } 

because of this parameter 'status' => self :: STATUS_ACTIVE] our code could not be validated. I just deleted it .

Also in this controller backend \ controllers \ SiteController.php there is a code upon successful authorization:

 return $this->goBack(); 

It doesn't work for me, so I replaced it with

 return $this->render('index'); 

It seems everything is fine ... If someone knows how to do better - please write back , always happy to new knowledge!