Usuario.php 1.7 KB

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