Drupal investigation

User.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. public $accessToken;
  10. public $forename;
  11. public $surname;
  12. public $email;
  13. public $phone;
  14. private static $users = [
  15. '100' => [
  16. 'id' => '100',
  17. 'username' => 'admin',
  18. 'password' => 'test',
  19. 'authKey' => 'test100key',
  20. 'accessToken' => '100-token',
  21. //'role' => 'admin',
  22. ],
  23. '101' => [
  24. 'id' => '101',
  25. 'username' => 'hr',
  26. 'password' => 'test',
  27. 'authKey' => 'test101key',
  28. 'accessToken' => '101-token',
  29. //'role' => 'hr',
  30. ],
  31. '102' => [
  32. 'id' => '102',
  33. 'username' => 'employee',
  34. 'password' => 'test',
  35. 'authKey' => 'test102key',
  36. 'accessToken' => '102-token',
  37. //'role' => 'employee',
  38. ],
  39. ];
  40. public static function tableName()
  41. {
  42. return 'testUsers';
  43. }
  44. /**
  45. * @inheritdoc
  46. */
  47. public static function findIdentity($id)
  48. {
  49. return isset(self::$users[$id]) ? new static(self::$users[$id]) : null;/*
  50. $dbUser = DbUser::find()
  51. ->where([
  52. "id" => $id
  53. ])
  54. ->one();
  55. if (!count($dbUser)) {
  56. return null;
  57. }
  58. return new static($dbUser);*/
  59. }
  60. /**
  61. * @inheritdoc
  62. */
  63. public static function findIdentityByAccessToken($token, $type = null)
  64. {
  65. foreach (self::$users as $user) {
  66. if ($user['accessToken'] === $token) {
  67. return new static($user);
  68. }
  69. }
  70. return null;
  71. }
  72. /**
  73. * Finds user by username
  74. *
  75. * @param string $username
  76. * @return static|null
  77. */
  78. public static function findByUsername($username)
  79. {
  80. foreach (self::$users as $user) {
  81. if (strcasecmp($user['username'], $username) === 0) {
  82. return new static($user);
  83. }
  84. }
  85. return null;
  86. }
  87. /**
  88. * @inheritdoc
  89. */
  90. public function getId()
  91. {
  92. return $this->id;
  93. }
  94. /**
  95. * @inheritdoc
  96. */
  97. public function getAuthKey()
  98. {
  99. return $this->authKey;
  100. }
  101. /**
  102. * @inheritdoc
  103. */
  104. public function validateAuthKey($authKey)
  105. {
  106. return $this->authKey === $authKey;
  107. }
  108. /**
  109. * Validates password
  110. *
  111. * @param string $password password to validate
  112. * @return bool if password provided is valid for current user
  113. */
  114. public function validatePassword($password)
  115. {
  116. return $this->password === $password;
  117. }
  118. }