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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
<?php
/**
* TUser class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link https://github.com/pradosoft/prado
* @copyright Copyright © 2005-2015 The PRADO Group
* @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
* @package System.Security
*/
/**
* Using IUserManager interface
*/
Prado::using('System.Security.IUserManager');
/**
* TUser class
*
* TUser implements basic user functionality for a Prado application.
* To get the name of the user, use {@link getName Name} property.
* The property {@link getIsGuest IsGuest} tells if the user a guest/anonymous user.
* To obtain or test the roles that the user is in, use property
* {@link getRoles Roles} and call {@link isInRole()}, respectively.
*
* TUser is meant to be used together with {@link IUserManager}.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @package System.Security
* @since 3.0
*/
class TUser extends TComponent implements IUser
{
/**
* @var array persistent state
*/
private $_state;
/**
* @var boolean whether user state is changed
*/
private $_stateChanged=false;
/**
* @var IUserManager user manager
*/
private $_manager;
/**
* Constructor.
* @param IUserManager user manager
*/
public function __construct(IUserManager $manager)
{
$this->_state=array();
$this->_manager=$manager;
$this->setName($manager->getGuestName());
}
/**
* @return IUserManager user manager
*/
public function getManager()
{
return $this->_manager;
}
/**
* @return string username, defaults to empty string.
*/
public function getName()
{
return $this->getState('Name','');
}
/**
* @param string username
*/
public function setName($value)
{
$this->setState('Name',$value,'');
}
/**
* @return boolean if the user is a guest, defaults to true.
*/
public function getIsGuest()
{
return $this->getState('IsGuest',true);
}
/**
* @param boolean if the user is a guest
*/
public function setIsGuest($value)
{
if($isGuest=TPropertyValue::ensureBoolean($value))
{
$this->setName($this->_manager->getGuestName());
$this->setRoles(array());
}
$this->setState('IsGuest',$isGuest);
}
/**
* @return array list of roles that the user is of
*/
public function getRoles()
{
return $this->getState('Roles',array());
}
/**
* @return array|string list of roles that the user is of. If it is a string, roles are assumed by separated by comma
*/
public function setRoles($value)
{
if(is_array($value))
$this->setState('Roles',$value,array());
else
{
$roles=array();
foreach(explode(',',$value) as $role)
{
if(($role=trim($role))!=='')
$roles[]=$role;
}
$this->setState('Roles',$roles,array());
}
}
/**
* @param string role to be tested. Note, role is case-insensitive.
* @return boolean whether the user is of this role
*/
public function isInRole($role)
{
foreach($this->getRoles() as $r)
if(strcasecmp($role,$r)===0)
return true;
return false;
}
/**
* @return string user data that is serialized and will be stored in session
*/
public function saveToString()
{
return serialize($this->_state);
}
/**
* @param string user data that is serialized and restored from session
* @return IUser the user object
*/
public function loadFromString($data)
{
if(!empty($data))
$this->_state=unserialize($data);
if(!is_array($this->_state))
$this->_state=array();
return $this;
}
/**
* Returns the value of a variable that is stored in user session.
*
* This function is designed to be used by TUser descendant classes
* who want to store additional user information in user session.
* A variable, if stored in user session using {@link setState} can be
* retrieved back using this function.
*
* @param string variable name
* @param mixed default value
* @return mixed the value of the variable. If it doesn't exist, the provided default value will be returned
* @see setState
*/
protected function getState($key,$defaultValue=null)
{
return isset($this->_state[$key])?$this->_state[$key]:$defaultValue;
}
/**
* Stores a variable in user session.
*
* This function is designed to be used by TUser descendant classes
* who want to store additional user information in user session.
* By storing a variable using this function, the variable may be retrieved
* back later using {@link getState}. The variable will be persistent
* across page requests during a user session.
*
* @param string variable name
* @param mixed variable value
* @param mixed default value. If $value===$defaultValue, the variable will be removed from persistent storage.
* @see getState
*/
protected function setState($key,$value,$defaultValue=null)
{
if($value===$defaultValue)
unset($this->_state[$key]);
else
$this->_state[$key]=$value;
$this->_stateChanged=true;
}
/**
* @return boolean whether user session state is changed (i.e., setState() is called)
*/
public function getStateChanged()
{
return $this->_stateChanged;
}
/**
* @param boolean whether user session state is changed
*/
public function setStateChanged($value)
{
$this->_stateChanged=TPropertyValue::ensureBoolean($value);
}
}
|