From 216c44de7fd10b6ff2f1ced71cc7d670d4f46d97 Mon Sep 17 00:00:00 2001 From: xue <> Date: Sat, 27 Jan 2007 20:07:09 +0000 Subject: Added TUser.getState() and setState() for storing user session data --- framework/Security/TUser.php | 98 +++++++++++++++++++++++++++++--------------- 1 file changed, 65 insertions(+), 33 deletions(-) (limited to 'framework/Security') diff --git a/framework/Security/TUser.php b/framework/Security/TUser.php index 3ff41104..7ed76c04 100644 --- a/framework/Security/TUser.php +++ b/framework/Security/TUser.php @@ -34,21 +34,13 @@ Prado::using('System.Security.IUserManager'); class TUser extends TComponent implements IUser { /** - * @var IUserManager user manager - */ - private $_manager; - /** - * @var boolean if the user is a guest - */ - private $_isGuest=true; - /** - * @var string username + * @var array persistent state */ - private $_name=''; + private $_state; /** - * @var array user roles + * @var IUserManager user manager */ - private $_roles=array(); + private $_manager; /** * Constructor. @@ -56,8 +48,9 @@ class TUser extends TComponent implements IUser */ public function __construct(IUserManager $manager) { + $this->_state=array(); $this->_manager=$manager; - $this->_name=$manager->getGuestName(); + $this->setName($manager->getGuestName()); } /** @@ -69,11 +62,11 @@ class TUser extends TComponent implements IUser } /** - * @return string username + * @return string username, defaults to empty string. */ public function getName() { - return $this->_name; + return $this->getState('Name',''); } /** @@ -81,15 +74,15 @@ class TUser extends TComponent implements IUser */ public function setName($value) { - $this->_name=$value; + $this->setState('Name',$value,''); } /** - * @return boolean if the user is a guest + * @return boolean if the user is a guest, defaults to true. */ public function getIsGuest() { - return $this->_isGuest; + return $this->getState('IsGuest',true); } /** @@ -97,11 +90,12 @@ class TUser extends TComponent implements IUser */ public function setIsGuest($value) { - if($this->_isGuest=TPropertyValue::ensureBoolean($value)) + if($isGuest=TPropertyValue::ensureBoolean($value)) { - $this->_name=$this->_manager->getGuestName(); - $this->_roles=array(); + $this->setName($this->_manager->getGuestName()); + $this->setRoles(array()); } + $this->setState('IsGuest',$isGuest); } /** @@ -109,7 +103,7 @@ class TUser extends TComponent implements IUser */ public function getRoles() { - return $this->_roles; + return $this->getState('Roles',array()); } /** @@ -118,15 +112,16 @@ class TUser extends TComponent implements IUser public function setRoles($value) { if(is_array($value)) - $this->_roles=$value; + $this->setState('Roles',$value,array()); else { - $this->_roles=array(); + $roles=array(); foreach(explode(',',$value) as $role) { if(($role=trim($role))!=='') - $this->_roles[]=$role; + $roles[]=$role; } + $this->setState('Roles',$roles,array()); } } @@ -136,7 +131,7 @@ class TUser extends TComponent implements IUser */ public function isInRole($role) { - foreach($this->_roles as $r) + foreach($this->getRoles() as $r) if(strcasecmp($role,$r)===0) return true; return false; @@ -147,7 +142,7 @@ class TUser extends TComponent implements IUser */ public function saveToString() { - return serialize(array($this->_name,$this->_roles,$this->_isGuest)); + return serialize($this->_state); } /** @@ -157,14 +152,51 @@ class TUser extends TComponent implements IUser public function loadFromString($data) { if(!empty($data)) - { - $array=unserialize($data); - $this->_name=$array[0]; - $this->_roles=$array[1]; - $this->_isGuest=$array[2]; - } + $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; + } } ?> \ No newline at end of file -- cgit v1.2.3