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