5 * @author Qiang Xue <qiang.xue@gmail.com>
6 * @link https://github.com/pradosoft/prado
7 * @copyright Copyright © 2005-2016 The PRADO Group
8 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
9 * @package System.Security
13 * Using IUserManager interface
15 Prado::using('System.Security.IUserManager');
20 * TUser implements basic user functionality for a Prado application.
21 * To get the name of the user, use {@link getName Name} property.
22 * The property {@link getIsGuest IsGuest} tells if the user a guest/anonymous user.
23 * To obtain or test the roles that the user is in, use property
24 * {@link getRoles Roles} and call {@link isInRole()}, respectively.
26 * TUser is meant to be used together with {@link IUserManager}.
28 * @author Qiang Xue <qiang.xue@gmail.com>
29 * @package System.Security
32 class TUser extends TComponent implements IUser
35 * @var array persistent state
39 * @var boolean whether user state is changed
41 private $_stateChanged=false;
43 * @var IUserManager user manager
49 * @param IUserManager user manager
51 public function __construct(IUserManager $manager)
53 $this->_state=array();
54 $this->_manager=$manager;
55 $this->setName($manager->getGuestName());
59 * @return IUserManager user manager
61 public function getManager()
63 return $this->_manager;
67 * @return string username, defaults to empty string.
69 public function getName()
71 return $this->getState('Name','');
75 * @param string username
77 public function setName($value)
79 $this->setState('Name',$value,'');
83 * @return boolean if the user is a guest, defaults to true.
85 public function getIsGuest()
87 return $this->getState('IsGuest',true);
91 * @param boolean if the user is a guest
93 public function setIsGuest($value)
95 if($isGuest=TPropertyValue::ensureBoolean($value))
97 $this->setName($this->_manager->getGuestName());
98 $this->setRoles(array());
100 $this->setState('IsGuest',$isGuest);
104 * @return array list of roles that the user is of
106 public function getRoles()
108 return $this->getState('Roles',array());
112 * @return array|string list of roles that the user is of. If it is a string, roles are assumed by separated by comma
114 public function setRoles($value)
117 $this->setState('Roles',$value,array());
121 foreach(explode(',',$value) as $role)
123 if(($role=trim($role))!=='')
126 $this->setState('Roles',$roles,array());
131 * @param string role to be tested. Note, role is case-insensitive.
132 * @return boolean whether the user is of this role
134 public function isInRole($role)
136 foreach($this->getRoles() as $r)
137 if(strcasecmp($role,$r)===0)
143 * @return string user data that is serialized and will be stored in session
145 public function saveToString()
147 return serialize($this->_state);
151 * @param string user data that is serialized and restored from session
152 * @return IUser the user object
154 public function loadFromString($data)
157 $this->_state=unserialize($data);
158 if(!is_array($this->_state))
159 $this->_state=array();
164 * Returns the value of a variable that is stored in user session.
166 * This function is designed to be used by TUser descendant classes
167 * who want to store additional user information in user session.
168 * A variable, if stored in user session using {@link setState} can be
169 * retrieved back using this function.
171 * @param string variable name
172 * @param mixed default value
173 * @return mixed the value of the variable. If it doesn't exist, the provided default value will be returned
176 protected function getState($key,$defaultValue=null)
178 return isset($this->_state[$key])?$this->_state[$key]:$defaultValue;
182 * Stores a variable in user session.
184 * This function is designed to be used by TUser descendant classes
185 * who want to store additional user information in user session.
186 * By storing a variable using this function, the variable may be retrieved
187 * back later using {@link getState}. The variable will be persistent
188 * across page requests during a user session.
190 * @param string variable name
191 * @param mixed variable value
192 * @param mixed default value. If $value===$defaultValue, the variable will be removed from persistent storage.
195 protected function setState($key,$value,$defaultValue=null)
197 if($value===$defaultValue)
198 unset($this->_state[$key]);
200 $this->_state[$key]=$value;
201 $this->_stateChanged=true;
205 * @return boolean whether user session state is changed (i.e., setState() is called)
207 public function getStateChanged()
209 return $this->_stateChanged;
213 * @param boolean whether user session state is changed
215 public function setStateChanged($value)
217 $this->_stateChanged=TPropertyValue::ensureBoolean($value);