CBaseUserIdentity.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * CBaseUserIdentity class file
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CBaseUserIdentity is a base class implementing {@link IUserIdentity}.
  12. *
  13. * CBaseUserIdentity implements the scheme for representing identity
  14. * information that needs to be persisted. It also provides the way
  15. * to represent the authentication errors.
  16. *
  17. * Derived classes should implement {@link IUserIdentity::authenticate}
  18. * and {@link IUserIdentity::getId} that are required by the {@link IUserIdentity}
  19. * interface.
  20. *
  21. * @property mixed $id A value that uniquely represents the identity (e.g. primary key value).
  22. * The default implementation simply returns {@link name}.
  23. * @property string $name The display name for the identity.
  24. * The default implementation simply returns empty string.
  25. * @property array $persistentStates The identity states that should be persisted.
  26. * @property boolean $isAuthenticated Whether the authentication is successful.
  27. *
  28. * @author Qiang Xue <qiang.xue@gmail.com>
  29. * @package system.web.auth
  30. * @since 1.0
  31. */
  32. abstract class CBaseUserIdentity extends CComponent implements IUserIdentity
  33. {
  34. const ERROR_NONE=0;
  35. const ERROR_USERNAME_INVALID=1;
  36. const ERROR_PASSWORD_INVALID=2;
  37. const ERROR_UNKNOWN_IDENTITY=100;
  38. /**
  39. * @var integer the authentication error code. If there is an error, the error code will be non-zero.
  40. * Defaults to 100, meaning unknown identity. Calling {@link authenticate} will change this value.
  41. */
  42. public $errorCode=self::ERROR_UNKNOWN_IDENTITY;
  43. /**
  44. * @var string the authentication error message. Defaults to empty.
  45. */
  46. public $errorMessage='';
  47. private $_state=array();
  48. /**
  49. * Returns a value that uniquely represents the identity.
  50. * @return mixed a value that uniquely represents the identity (e.g. primary key value).
  51. * The default implementation simply returns {@link name}.
  52. */
  53. public function getId()
  54. {
  55. return $this->getName();
  56. }
  57. /**
  58. * Returns the display name for the identity (e.g. username).
  59. * @return string the display name for the identity.
  60. * The default implementation simply returns empty string.
  61. */
  62. public function getName()
  63. {
  64. return '';
  65. }
  66. /**
  67. * Returns the identity states that should be persisted.
  68. * This method is required by {@link IUserIdentity}.
  69. * @return array the identity states that should be persisted.
  70. */
  71. public function getPersistentStates()
  72. {
  73. return $this->_state;
  74. }
  75. /**
  76. * Sets an array of persistent states.
  77. *
  78. * @param array $states the identity states that should be persisted.
  79. */
  80. public function setPersistentStates($states)
  81. {
  82. $this->_state = $states;
  83. }
  84. /**
  85. * Returns a value indicating whether the identity is authenticated.
  86. * This method is required by {@link IUserIdentity}.
  87. * @return boolean whether the authentication is successful.
  88. */
  89. public function getIsAuthenticated()
  90. {
  91. return $this->errorCode==self::ERROR_NONE;
  92. }
  93. /**
  94. * Gets the persisted state by the specified name.
  95. * @param string $name the name of the state
  96. * @param mixed $defaultValue the default value to be returned if the named state does not exist
  97. * @return mixed the value of the named state
  98. */
  99. public function getState($name,$defaultValue=null)
  100. {
  101. return isset($this->_state[$name])?$this->_state[$name]:$defaultValue;
  102. }
  103. /**
  104. * Sets the named state with a given value.
  105. * @param string $name the name of the state
  106. * @param mixed $value the value of the named state
  107. */
  108. public function setState($name,$value)
  109. {
  110. $this->_state[$name]=$value;
  111. }
  112. /**
  113. * Removes the specified state.
  114. * @param string $name the name of the state
  115. */
  116. public function clearState($name)
  117. {
  118. unset($this->_state[$name]);
  119. }
  120. }