User.php 1.4 KB

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