Advertisement
Guest User

model users

a guest
May 8th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. <?php
  2.  
  3. namespace app\models;
  4.  
  5. class User extends \yii\base\BaseObject implements \yii\web\IdentityInterface
  6. {
  7. public $id;
  8. public $username;
  9. public $password;
  10. public $authKey;
  11. public $accessToken;
  12.  
  13. private static $users = [];
  14.  
  15. public static function setUsers($user=[]){
  16. self::$users = $user;
  17. }
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public static function findIdentity($id)
  22. {
  23. return isset(self::$users[$id]) ? new static(self::$users[$id]) : null;
  24. }
  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.  
  37. return null;
  38. }
  39.  
  40. /**
  41. * Finds user by username
  42. *
  43. * @param string $username
  44. * @return static|null
  45. */
  46. public static function findByUsername($username)
  47. {
  48. foreach (self::$users as $user) {
  49. if (strcasecmp($user['username'], $username) === 0) {
  50. return new static($user);
  51. }
  52. }
  53.  
  54. return null;
  55. }
  56.  
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function getId()
  61. {
  62. return $this->id;
  63. }
  64.  
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function getAuthKey()
  69. {
  70. return $this->authKey;
  71. }
  72.  
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function validateAuthKey($authKey)
  77. {
  78. return $this->authKey === $authKey;
  79. }
  80.  
  81. /**
  82. * Validates password
  83. *
  84. * @param string $password password to validate
  85. * @return bool if password provided is valid for current user
  86. */
  87. public function validatePassword($password)
  88. {
  89. return $this->password === $password;
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement