The username and password entered by the user are sent to the remote server for verification via soap. If a positive answer comes, I need to authorize the user on the site (Yii2 advanced). Tell me how to do this? I do not have a local database and there is no complete list of users to fit it into an array in User models as it is done in Yii2 basic.

  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

After a positive response, you need to authorize the user, then call the yii\web\User::login() method, which in turn takes two parameters - the object implementing the yii \ web \ IdentityInterface interface and the user authorization time (cookie lifetime).

About authentication can read here . An example implementation without a database can be viewed in the basic template .

Depending on the implementation, the key will be the implementation of the two getId() and findIdentity() methods. The first will return a unique user key using which the yii\web\User::login() method generates a session parameter and a cookie. The second is used to verify authorization using a session or cookie.

As an example of interface implementation:

 class User extends \yii\base\Object implements \yii\web\IdentityInterface { public $id; public function getId() { return $this->id; } public static function findIdentity($id) { //можно реализовать запрос на удаленный сервер проверив авторизацию $isAuth = Soap::isAuth($id); if($isAuth){ return new static(['id'=>$id]); }else{ return null; } } .... } 

This can be a full-fledged user model, you can add public properties, and fill in with not only id but other data as well.

Well, an example for authorization:

 public function actionLogin($login, $password){ $id = Soap::auth($login, $password); $user = new User(['id'=>$id]); Yii::$app->user->login($user, 100); ... } 

I hope the idea is about clear. Examples are not true, it all depends on your implementation and approach to the solution.

  • And it is possible to do the authorization in this way: I check the data on the soap server, if everything is ok, in response I do not receive an id, but immediately a data set for the authenticated user. For example, mail, full name, etc. And after that I will authorize the user with already received data. In Yii1 it was possible to do it, but here I do not understand how to achieve this. those. you need something like this: $ data = array (); $ data = Soap :: auth ($ login, $ password); $ user = new User ($ data); Yii :: $ app-> user-> login ($ user, 100); at the same time in the class User there should not be a call to soap - Alex
  • Yes, why not, just an id, for example, you can calculate a certain hash from the obtained data. In the model that implements the interface, add public properties, and create an object as you specified. $data=['username'=>'test', 'email'=>'email@email.com']; $data['id']= md5(...); $user=new User($data); , class User ... { public $id; public $username; public $email; ...} class User ... { public $id; public $username; public $email; ...} class User ... { public $id; public $username; public $email; ...} - Bookin
  • Why, that's the point, that doesn't work. public static function findIdentity ($ id) {...} should return, as I understand it, return new static (['id' => '...', ..]) with all the properties. Ie the very value of these properties must be obtained from somewhere in this method. And it is static and out of nowhere, except for the database, prescription by hand or in my case through soap, I can not get it. I understand correctly? - Alexey
  • Well, all that except the id is necessary because you somehow get it, so yes. I do not remember exactly all the data he keeps in the session or not, it is worth experimenting, watching. As a crutch example, it is generally possible to drive an array with data into a string and present it as id ( example ). I do not know all the details, so it's up to you to decide which path to take. - Bookin