AdminIdentity.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace wechat\models;
  3. use yii\base\Model;
  4. use yii\web\IdentityInterface;
  5. class AdminIdentity extends Model implements IdentityInterface
  6. {
  7. public $id;
  8. public $username;
  9. public $password;
  10. /**
  11. * @inheritdoc
  12. */
  13. public function rules()
  14. {
  15. return [
  16. [['username', 'password'], 'required'],
  17. [['username', 'password'], 'string'],
  18. [['id'], 'integer']
  19. ];
  20. }
  21. /**
  22. * Finds an identity by the given ID.
  23. * @param string|int $id the ID to be looked for
  24. * @return IdentityInterface the identity object that matches the given ID.
  25. * Null should be returned if such an identity cannot be found
  26. * or the identity is not in an active state (disabled, deleted, etc.)
  27. */
  28. public static function findIdentity($id)
  29. {
  30. $api = new AdminApi();
  31. $result = $api->getAdminById($id);
  32. if ($result['code'] == 1) {
  33. $identity = new static();
  34. $identity->setAttributes($result['data']);
  35. return $identity;
  36. } else {
  37. return null;
  38. }
  39. }
  40. /**
  41. * Finds an identity by the given token.
  42. * @param mixed $token the token to be looked for
  43. * @param mixed $type the type of the token. The value of this parameter depends on the implementation.
  44. * For example, [[\yii\filters\auth\HttpBearerAuth]] will set this parameter to be `yii\filters\auth\HttpBearerAuth`.
  45. * @return IdentityInterface the identity object that matches the given token.
  46. * Null should be returned if such an identity cannot be found
  47. * or the identity is not in an active state (disabled, deleted, etc.)
  48. */
  49. public static function findIdentityByAccessToken($token, $type = null)
  50. {
  51. return null;
  52. }
  53. /**
  54. * Returns an ID that can uniquely identify a user identity.
  55. * @return string|int an ID that uniquely identifies a user identity.
  56. */
  57. public function getId()
  58. {
  59. return $this->id;
  60. }
  61. /**
  62. * Returns a key that can be used to check the validity of a given identity ID.
  63. *
  64. * The key should be unique for each individual user, and should be persistent
  65. * so that it can be used to check the validity of the user identity.
  66. *
  67. * The space of such keys should be big enough to defeat potential identity attacks.
  68. *
  69. * This is required if [[User::enableAutoLogin]] is enabled.
  70. * @return string a key that is used to check the validity of a given identity ID.
  71. * @see validateAuthKey()
  72. */
  73. public function getAuthKey()
  74. {
  75. return '';
  76. }
  77. /**
  78. * Validates the given auth key.
  79. *
  80. * This is required if [[User::enableAutoLogin]] is enabled.
  81. * @param string $authKey the given auth key
  82. * @return bool whether the given auth key is valid.
  83. * @see getAuthKey()
  84. */
  85. public function validateAuthKey($authKey)
  86. {
  87. return $this->getAuthKey() === $authKey;
  88. }
  89. }