S
S
Sergey Zhukov2015-10-09 07:39:56
Yii
Sergey Zhukov, 2015-10-09 07:39:56

How to login in Yii2 without DB involvement?

Lord, please help.
I googled everything, the same manual walks both here and over the hill.
I can not get to the idea of ​​​​how to log in simply forcibly.
The maximum I got to:
\Yii::$app->getUser()->login(User::findByRoot());
And in the User model :

public static function findByRoot()
  {
    $model = new User();
    $model->id = '1000';
    $model->username = 'vasya';
    $model->status = 10;
    return $model;
  }

In Yii::$app->getUser()->login(); it’s just impossible to slip a bare User object , Yii2 complains that UserIdentity must be implemented.
There are no errors. login method returns TRUE and isGuest shows true
too Tried another implementation of findByRoot()
public static function findByRoot()
  {
    $arr = [
      'id' => 100,
      'username' => 'vasya',
      'status' => 10,
    ];
    return new static($arr);
  }

The result is the same. There are suspicions that something is wrong with the fields.
Decided to make a full copy of all fields,
$arr = [
      'id' => 100,
      'created_at' => 1444322024,
      'updated_at' => 1444322024,
      'username' => 'vasya',
      'auth_key' => 'aagsdghfgukfyrtweri',
      'email_confirm_token' => NULL,
      'password_hash' => 'aa2gsdg123hfgukfyrtweri',
      'password_reset_token' => NULL,
      'email' => '[email protected]',
      'status' => 10,
    ];
    return new static($arr);

Yii::$app->user->identity is still NULL

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey Zhukov, 2015-10-09
@Adobe

Resolved the issue. The problem was that Yii2 rechecks UserIdendity and makes a new request, in which case it is necessary to return fake data again with the findIdentity method

public static function findIdentity($id)
  {
    if($id == 1385)
      return self::findByRoot();
    else
      return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
  }

L
Lander, 2015-10-09
@usdglander

I didn’t understand the question very well, but I suppose that you need something like a fixed set of accounts right in the code and a login under them. This is done in the basic application. See what it says there. The point is that IdentityInterface should be hung on the class that is responsible for authorization.

class User implements IdentityInterface
{
...
}

N
Nikita, 2015-10-09
@bitver

Your example is the most correct thing you can do, you just mistyped/underwritten somewhere. Yii::$ap->user->isGuest == falseimplies that there is Yii::$ap->user->identity, pointing to the user model.
And the standard solution will help you solve your problem: https://github.com/yiisoft/yii2-app-basic/blob/mas...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question