User.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace app\models;
  3. class User extends \yii\base\BaseObject implements \yii\web\IdentityInterface
  4. {
  5. public $id;
  6. public $username;
  7. public $password;
  8. public $authKey;
  9. public $accessToken;
  10. private static $users = [
  11. '102' => [
  12. 'id' => '102',
  13. 'username' => 'longyi',
  14. 'password' => 'longyi123',
  15. 'authKey' => 'test102key',
  16. 'accessToken' => '102-token',
  17. ],
  18. ];
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public static function findIdentity($id)
  23. {
  24. return isset(self::$users[$id]) ? new static(self::$users[$id]) : null;
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public static function findIdentityByAccessToken($token, $type = null)
  30. {
  31. foreach (self::$users as $user) {
  32. if ($user['accessToken'] === $token) {
  33. return new static($user);
  34. }
  35. }
  36. return null;
  37. }
  38. /**
  39. * Finds user by username
  40. *
  41. * @param string $username
  42. * @return static|null
  43. */
  44. public static function findByUsername($username)
  45. {
  46. foreach (self::$users as $user) {
  47. if (strcasecmp($user['username'], $username) === 0) {
  48. return new static($user);
  49. }
  50. }
  51. return null;
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function getId()
  57. {
  58. return $this->id;
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function getAuthKey()
  64. {
  65. return $this->authKey;
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function validateAuthKey($authKey)
  71. {
  72. return $this->authKey === $authKey;
  73. }
  74. /**
  75. * Validates password
  76. *
  77. * @param string $password password to validate
  78. * @return bool if password provided is valid for current user
  79. */
  80. public function validatePassword($password)
  81. {
  82. return $this->password === $password;
  83. }
  84. }