]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/framework/Security/TUser.php
baculum: New Baculum API and Baculum Web
[bacula/bacula] / gui / baculum / framework / Security / TUser.php
1 <?php
2 /**
3  * TUser class file.
4  *
5  * @author Qiang Xue <qiang.xue@gmail.com>
6  * @link https://github.com/pradosoft/prado
7  * @copyright Copyright &copy; 2005-2016 The PRADO Group
8  * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
9  * @package System.Security
10  */
11
12 /**
13  * Using IUserManager interface
14  */
15 Prado::using('System.Security.IUserManager');
16
17 /**
18  * TUser class
19  *
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.
25  *
26  * TUser is meant to be used together with {@link IUserManager}.
27  *
28  * @author Qiang Xue <qiang.xue@gmail.com>
29  * @package System.Security
30  * @since 3.0
31  */
32 class TUser extends TComponent implements IUser
33 {
34         /**
35          * @var array persistent state
36          */
37         private $_state;
38         /**
39          * @var boolean whether user state is changed
40          */
41         private $_stateChanged=false;
42         /**
43          * @var IUserManager user manager
44          */
45         private $_manager;
46
47         /**
48          * Constructor.
49          * @param IUserManager user manager
50          */
51         public function __construct(IUserManager $manager)
52         {
53                 $this->_state=array();
54                 $this->_manager=$manager;
55                 $this->setName($manager->getGuestName());
56         }
57
58         /**
59          * @return IUserManager user manager
60          */
61         public function getManager()
62         {
63                 return $this->_manager;
64         }
65
66         /**
67          * @return string username, defaults to empty string.
68          */
69         public function getName()
70         {
71                 return $this->getState('Name','');
72         }
73
74         /**
75          * @param string username
76          */
77         public function setName($value)
78         {
79                 $this->setState('Name',$value,'');
80         }
81
82         /**
83          * @return boolean if the user is a guest, defaults to true.
84          */
85         public function getIsGuest()
86         {
87                 return $this->getState('IsGuest',true);
88         }
89
90         /**
91          * @param boolean if the user is a guest
92          */
93         public function setIsGuest($value)
94         {
95                 if($isGuest=TPropertyValue::ensureBoolean($value))
96                 {
97                         $this->setName($this->_manager->getGuestName());
98                         $this->setRoles(array());
99                 }
100                 $this->setState('IsGuest',$isGuest);
101         }
102
103         /**
104          * @return array list of roles that the user is of
105          */
106         public function getRoles()
107         {
108                 return $this->getState('Roles',array());
109         }
110
111         /**
112          * @return array|string list of roles that the user is of. If it is a string, roles are assumed by separated by comma
113          */
114         public function setRoles($value)
115         {
116                 if(is_array($value))
117                         $this->setState('Roles',$value,array());
118                 else
119                 {
120                         $roles=array();
121                         foreach(explode(',',$value) as $role)
122                         {
123                                 if(($role=trim($role))!=='')
124                                         $roles[]=$role;
125                         }
126                         $this->setState('Roles',$roles,array());
127                 }
128         }
129
130         /**
131          * @param string role to be tested. Note, role is case-insensitive.
132          * @return boolean whether the user is of this role
133          */
134         public function isInRole($role)
135         {
136                 foreach($this->getRoles() as $r)
137                         if(strcasecmp($role,$r)===0)
138                                 return true;
139                 return false;
140         }
141
142         /**
143          * @return string user data that is serialized and will be stored in session
144          */
145         public function saveToString()
146         {
147                 return serialize($this->_state);
148         }
149
150         /**
151          * @param string user data that is serialized and restored from session
152          * @return IUser the user object
153          */
154         public function loadFromString($data)
155         {
156                 if(!empty($data))
157                         $this->_state=unserialize($data);
158                 if(!is_array($this->_state))
159                         $this->_state=array();
160                 return $this;
161         }
162
163         /**
164          * Returns the value of a variable that is stored in user session.
165          *
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.
170          *
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
174          * @see setState
175          */
176         protected function getState($key,$defaultValue=null)
177         {
178                 return isset($this->_state[$key])?$this->_state[$key]:$defaultValue;
179         }
180
181         /**
182          * Stores a variable in user session.
183          *
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.
189          *
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.
193          * @see getState
194          */
195         protected function setState($key,$value,$defaultValue=null)
196         {
197                 if($value===$defaultValue)
198                         unset($this->_state[$key]);
199                 else
200                         $this->_state[$key]=$value;
201                 $this->_stateChanged=true;
202         }
203
204         /**
205          * @return boolean whether user session state is changed (i.e., setState() is called)
206          */
207         public function getStateChanged()
208         {
209                 return $this->_stateChanged;
210         }
211
212         /**
213          * @param boolean whether user session state is changed
214          */
215         public function setStateChanged($value)
216         {
217                 $this->_stateChanged=TPropertyValue::ensureBoolean($value);
218         }
219 }
220