User.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 = array(
  10. '100' => array(
  11. 'id' => '100',
  12. 'username' => 'admin',
  13. 'password' => 'admin',
  14. 'authKey' => 'test100key',
  15. ),
  16. '101' => array(
  17. 'id' => '101',
  18. 'username' => 'demo',
  19. 'password' => 'demo',
  20. 'authKey' => 'test101key',
  21. ),
  22. );
  23. public static function findIdentity($id)
  24. {
  25. return isset(self::$users[$id]) ? new self(self::$users[$id]) : null;
  26. }
  27. public static function findByUsername($username)
  28. {
  29. foreach (self::$users as $user) {
  30. if (strcasecmp($user['username'], $username) === 0) {
  31. return new self($user);
  32. }
  33. }
  34. return null;
  35. }
  36. public function getId()
  37. {
  38. return $this->id;
  39. }
  40. public function getAuthKey()
  41. {
  42. return $this->authKey;
  43. }
  44. public function validateAuthKey($authKey)
  45. {
  46. return $this->authKey === $authKey;
  47. }
  48. public function validatePassword($password)
  49. {
  50. return $this->password === $password;
  51. }
  52. }