IdentityInterface.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\web;
  8. /**
  9. * IdentityInterface is the interface that should be implemented by a class providing identity information.
  10. *
  11. * This interface can typically be implemented by a user model class. For example, the following
  12. * code shows how to implement this interface by a User ActiveRecord class:
  13. *
  14. * ```php
  15. * class User extends ActiveRecord implements IdentityInterface
  16. * {
  17. * public static function findIdentity($id)
  18. * {
  19. * return static::findOne($id);
  20. * }
  21. *
  22. * public static function findIdentityByAccessToken($token, $type = null)
  23. * {
  24. * return static::findOne(['access_token' => $token]);
  25. * }
  26. *
  27. * public function getId()
  28. * {
  29. * return $this->id;
  30. * }
  31. *
  32. * public function getAuthKey()
  33. * {
  34. * return $this->authKey;
  35. * }
  36. *
  37. * public function validateAuthKey($authKey)
  38. * {
  39. * return $this->authKey === $authKey;
  40. * }
  41. * }
  42. * ```
  43. *
  44. * @author Qiang Xue <qiang.xue@gmail.com>
  45. * @since 2.0
  46. */
  47. interface IdentityInterface
  48. {
  49. /**
  50. * Finds an identity by the given ID.
  51. * @param string|int $id the ID to be looked for
  52. * @return IdentityInterface|null the identity object that matches the given ID.
  53. * Null should be returned if such an identity cannot be found
  54. * or the identity is not in an active state (disabled, deleted, etc.)
  55. */
  56. public static function findIdentity($id);
  57. /**
  58. * Finds an identity by the given token.
  59. * @param mixed $token the token to be looked for
  60. * @param mixed $type the type of the token. The value of this parameter depends on the implementation.
  61. * For example, [[\yii\filters\auth\HttpBearerAuth]] will set this parameter to be `yii\filters\auth\HttpBearerAuth`.
  62. * @return IdentityInterface|null the identity object that matches the given token.
  63. * Null should be returned if such an identity cannot be found
  64. * or the identity is not in an active state (disabled, deleted, etc.)
  65. */
  66. public static function findIdentityByAccessToken($token, $type = null);
  67. /**
  68. * Returns an ID that can uniquely identify a user identity.
  69. * @return string|int an ID that uniquely identifies a user identity.
  70. */
  71. public function getId();
  72. /**
  73. * Returns a key that can be used to check the validity of a given identity ID.
  74. *
  75. * The key should be unique for each individual user, and should be persistent
  76. * so that it can be used to check the validity of the user identity.
  77. *
  78. * The space of such keys should be big enough to defeat potential identity attacks.
  79. *
  80. * This is required if [[User::enableAutoLogin]] is enabled. The returned key will be stored on the
  81. * client side as a cookie and will be used to authenticate user even if PHP session has been expired.
  82. *
  83. * Make sure to invalidate earlier issued authKeys when you implement force user logout, password change and
  84. * other scenarios, that require forceful access revocation for old sessions.
  85. *
  86. * @return string a key that is used to check the validity of a given identity ID.
  87. * @see validateAuthKey()
  88. */
  89. public function getAuthKey();
  90. /**
  91. * Validates the given auth key.
  92. *
  93. * This is required if [[User::enableAutoLogin]] is enabled.
  94. * @param string $authKey the given auth key
  95. * @return bool whether the given auth key is valid.
  96. * @see getAuthKey()
  97. */
  98. public function validateAuthKey($authKey);
  99. }