yii2 中的自定义 userIdentity 类 [英] Custom userIdentity class in yii2

查看:47
本文介绍了yii2 中的自定义 userIdentity 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据我的具体要求创建自定义 userIdentity 类.这里的代码是

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

<?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.');
    }



}

?>

基本上我有两个表角色和用户,我想在 yii::$app->user->identity 中设置两个表的特定属性

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

当我调用上面的代码时,findIdentity($id) 函数返回错误,原因很明显,说明我不能在静态函数中调用 $this .如何在函数中设置所需的属性并从中返回 userIdentity 类的实例?

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

推荐答案

我推荐阅读:什么时候在 $this 上使用 self ? 你真的把 2 搞混了.

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

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

你在一个对象上调用 :: ,你不能那样做.

You are calling :: on an object, you cannot do that.

我说删除你所做的并重新开始,这应该比你写的要容易得多.向您展示如何正确执行需要很长时间,只需查看 yii2 Advance 模板,看看他们是如何做的.您可以使用自己的身份类并在那里设置任何特殊属性.研究yii2代码就行了.

I say delete what you have done and start again, it should be much easier then what you wrote. It would take a long time to show you how to do it properly, just look in the yii2 advance template and see how they are doing it. You can use your own identity class and set up any special attributes there. Just study the yii2 code.

这篇关于yii2 中的自定义 userIdentity 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆