User class userIdentity in yii2

I want to create a userIdentity custom class according to my specific requirements. Here is the code

<?php namespace app\models; use yii\web\IdentityInterface; use app\models\dbTables\Users; class UserIdentity implements IdentityInterface{ const ERROR_USERNAME_INVALID=3; const ERROR_PASSWORD_INVALID=4; const ERROR_NONE=0; public $errorCode; private $_id; private $_email; private $_role; private $_name; public function findIdentityById($id){ $objUserMdl = new Users; $user = $objUserMdl::findOne($id); $userRole = $objUserMdl->getUserRole($user->user_id); $this->_id = $user->user_id; $this->_email = $user->email_address; $this->_role = $userRole; $this->_name = $user->full_name; return $this; } public function getId() { return $this->_id; } public function getName(){ return $this->_name; } public function getEmail(){ return $this->_email; } public function getRole(){ return $this->_role; } public static function findIdentity($id) { return self::findIdentityById($id); } public function getAuthKey() { throw new NotSupportedException('"getAuthKey" is not implemented.'); } public function validateAuthKey($authKey) { throw new NotSupportedException('"validateAuthKey" is not implemented.'); } public static function findIdentityByAccessToken($token, $type = null) { throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.'); } } ?> 

Basically, I have two roles of tables and users, and I want to set specific properties from both tables in yii :: $ app-> user-> identity

When I call the above code, the findIdentity($id) function returns an error for obvious reasons, stating that I cannot call $this in a static funtion. How to set the required properties in a function and return an instance of the userIdentity class from it ?

0
source share
1 answer

I recommend reading this: When to use self over $ this? you really confuse 2.

  $objUserMdl = new Users; $user = $objUserMdl::findOne($id); $userRole = $objUserMdl->getUserRole($user->user_id); 

You call :: object, you cannot do this.

I say, delete what you have done, and start over, it should be much easier than what you wrote. It takes a long time to show you how to do it right, just look into the yii2 promotion template and see how they do it. You can use your own identification class and configure any special attributes there. Just learn the yii2 code.

0
source

Source: https://habr.com/ru/post/1241294/


All Articles