Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * TUser class file.
4
 *
5
 * @author Qiang Xue <qiang.xue@gmail.com>
6
 * @link http://www.pradosoft.com/
7
 * @copyright Copyright &copy; 2005-2008 PradoSoft
8
 * @license http://www.pradosoft.com/license/
9
 * @version $Id: TUser.php 2541 2008-10-21 15:05:13Z qiang.xue $
10
 * @package System.Security
11
 */
12
 
13
/**
14
 * Using IUserManager interface
15
 */
16
Prado::using('System.Security.IUserManager');
17
 
18
/**
19
 * TUser class
20
 *
21
 * TUser implements basic user functionality for a Prado application.
22
 * To get the name of the user, use {@link getName Name} property.
23
 * The property {@link getIsGuest IsGuest} tells if the user a guest/anonymous user.
24
 * To obtain or test the roles that the user is in, use property
25
 * {@link getRoles Roles} and call {@link isInRole()}, respectively.
26
 *
27
 * TUser is meant to be used together with {@link IUserManager}.
28
 *
29
 * @author Qiang Xue <qiang.xue@gmail.com>
30
 * @version $Id: TUser.php 2541 2008-10-21 15:05:13Z qiang.xue $
31
 * @package System.Security
32
 * @since 3.0
33
 */
34
class TUser extends TComponent implements IUser
35
{
36
	/**
37
	 * @var array persistent state
38
	 */
39
	private $_state;
40
	/**
41
	 * @var boolean whether user state is changed
42
	 */
43
	private $_stateChanged=false;
44
	/**
45
	 * @var IUserManager user manager
46
	 */
47
	private $_manager;
48
 
49
	/**
50
	 * Constructor.
51
	 * @param IUserManager user manager
52
	 */
53
	public function __construct(IUserManager $manager)
54
	{
55
		$this->_state=array();
56
		$this->_manager=$manager;
57
		$this->setName($manager->getGuestName());
58
	}
59
 
60
	/**
61
	 * @return IUserManager user manager
62
	 */
63
	public function getManager()
64
	{
65
		return $this->_manager;
66
	}
67
 
68
	/**
69
	 * @return string username, defaults to empty string.
70
	 */
71
	public function getName()
72
	{
73
		return $this->getState('Name','');
74
	}
75
 
76
	/**
77
	 * @param string username
78
	 */
79
	public function setName($value)
80
	{
81
		$this->setState('Name',$value,'');
82
	}
83
 
84
	/**
85
	 * @return boolean if the user is a guest, defaults to true.
86
	 */
87
	public function getIsGuest()
88
	{
89
		return $this->getState('IsGuest',true);
90
	}
91
 
92
	/**
93
	 * @param boolean if the user is a guest
94
	 */
95
	public function setIsGuest($value)
96
	{
97
		if($isGuest=TPropertyValue::ensureBoolean($value))
98
		{
99
			$this->setName($this->_manager->getGuestName());
100
			$this->setRoles(array());
101
		}
102
		$this->setState('IsGuest',$isGuest);
103
	}
104
 
105
	/**
106
	 * @return array list of roles that the user is of
107
	 */
108
	public function getRoles()
109
	{
110
		return $this->getState('Roles',array());
111
	}
112
 
113
	/**
114
	 * @return array|string list of roles that the user is of. If it is a string, roles are assumed by separated by comma
115
	 */
116
	public function setRoles($value)
117
	{
118
		if(is_array($value))
119
			$this->setState('Roles',$value,array());
120
		else
121
		{
122
			$roles=array();
123
			foreach(explode(',',$value) as $role)
124
			{
125
				if(($role=trim($role))!=='')
126
					$roles[]=$role;
127
			}
128
			$this->setState('Roles',$roles,array());
129
		}
130
	}
131
 
132
	/**
133
	 * @param string role to be tested. Note, role is case-insensitive.
134
	 * @return boolean whether the user is of this role
135
	 */
136
	public function isInRole($role)
137
	{
138
		foreach($this->getRoles() as $r)
139
			if(strcasecmp($role,$r)===0)
140
				return true;
141
		return false;
142
	}
143
 
144
	/**
145
	 * @return string user data that is serialized and will be stored in session
146
	 */
147
	public function saveToString()
148
	{
149
		return serialize($this->_state);
150
	}
151
 
152
	/**
153
	 * @param string user data that is serialized and restored from session
154
	 * @return IUser the user object
155
	 */
156
	public function loadFromString($data)
157
	{
158
		if(!empty($data))
159
			$this->_state=unserialize($data);
160
		if(!is_array($this->_state))
161
			$this->_state=array();
162
		return $this;
163
	}
164
 
165
	/**
166
	 * Returns the value of a variable that is stored in user session.
167
	 *
168
	 * This function is designed to be used by TUser descendant classes
169
	 * who want to store additional user information in user session.
170
	 * A variable, if stored in user session using {@link setState} can be
171
	 * retrieved back using this function.
172
	 *
173
	 * @param string variable name
174
	 * @param mixed default value
175
	 * @return mixed the value of the variable. If it doesn't exist, the provided default value will be returned
176
	 * @see setState
177
	 */
178
	protected function getState($key,$defaultValue=null)
179
	{
180
		return isset($this->_state[$key])?$this->_state[$key]:$defaultValue;
181
	}
182
 
183
	/**
184
	 * Stores a variable in user session.
185
	 *
186
	 * This function is designed to be used by TUser descendant classes
187
	 * who want to store additional user information in user session.
188
	 * By storing a variable using this function, the variable may be retrieved
189
	 * back later using {@link getState}. The variable will be persistent
190
	 * across page requests during a user session.
191
	 *
192
	 * @param string variable name
193
	 * @param mixed variable value
194
	 * @param mixed default value. If $value===$defaultValue, the variable will be removed from persistent storage.
195
	 * @see getState
196
	 */
197
	protected function setState($key,$value,$defaultValue=null)
198
	{
199
		if($value===$defaultValue)
200
			unset($this->_state[$key]);
201
		else
202
			$this->_state[$key]=$value;
203
		$this->_stateChanged=true;
204
	}
205
 
206
	/**
207
	 * @return boolean whether user session state is changed (i.e., setState() is called)
208
	 */
209
	public function getStateChanged()
210
	{
211
		return $this->_stateChanged;
212
	}
213
 
214
	/**
215
	 * @param boolean whether user session state is changed
216
	 */
217
	public function setStateChanged($value)
218
	{
219
		$this->_stateChanged=TPropertyValue::ensureBoolean($value);
220
	}
221
}
222