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