wCMF  3.6
 All Classes Namespaces Files Functions Variables Groups Pages
class.User.php
Go to the documentation of this file.
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2014 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of any of the following licenses
7  * at your choice:
8  *
9  * - GNU Lesser General Public License (LGPL)
10  * http://www.gnu.org/licenses/lgpl.html
11  * - Eclipse Public License (EPL)
12  * http://www.eclipse.org/org/documents/epl-v10.php
13  *
14  * See the license.txt file distributed with this work for
15  * additional information.
16  *
17  * $Id: class.User.php 1462 2014-02-04 23:52:27Z iherwig $
18  */
19 require_once(BASE."wcmf/lib/model/class.Node.php");
20 require_once(BASE."wcmf/lib/persistence/class.PersistenceFacade.php");
21 require_once(BASE."wcmf/lib/model/class.NodeIterator.php");
22 require_once(BASE."wcmf/lib/visitor/class.CommitVisitor.php");
23 
24 /**
25  * @class User
26  * @ingroup Security
27  * @brief Abstract base class for user classes that represent a system user.
28  *
29  * @author ingo herwig <ingo@wemove.com>
30  */
31 class User extends Node
32 {
33  var $_cachedRoles = array();
34  var $_hasOwnRolesLoaded = false;
35 
36  /**
37  * Default constructor.
38  */
39  function User($oid=null, $type='User')
40  {
41  parent::Node($type, $oid);
42  }
43 
44  /**
45  * Get the user instance by login and password.
46  * The default implementation searches the user using the PersistenceFacade.
47  * This method may be called in a static way.
48  * Subclasses will override this to implement special retrieval.
49  * @return A reference to the instance or null if not found.
50  */
51  function &getUser($login, $password)
52  {
53  $persistenceFacade = &PersistenceFacade::getInstance();
54  $oid = $persistenceFacade->getFirstOID(UserManager::getUserClassName(),
55  array('login' => $login, 'password' => $password), null);
56  if ($oid != null)
57  {
58  $user = &$persistenceFacade->load($oid, BUILDDEPTH_SINGLE);
59  // initially load roles
60  $user->getRoles();
61  return $user;
62  }
63  return null;
64  }
65 
66  /**
67  * Get the id of the user.
68  * @return The id.
69  */
70  function getUserId()
71  {
72  return $this->getDBID();
73  }
74 
75  /**
76  * Set the login of the user.
77  * @param login The login of the user.
78  */
79  function setLogin($login)
80  {
81  WCMFException::throwEx("setLogin() must be implemented by derived class: ".get_class($this), __FILE__, __LINE__);
82  }
83 
84  /**
85  * Get the login of the user.
86  * @return The login of the user.
87  */
88  function getLogin()
89  {
90  WCMFException::throwEx("getLogin() must be implemented by derived class: ".get_class($this), __FILE__, __LINE__);
91  }
92 
93  /**
94  * Set the password of the user.
95  * @param password The unencrypted password of the user.
96  */
97  function setPassword($password)
98  {
99  WCMFException::throwEx("setPassword() must be implemented by derived class: ".get_class($this), __FILE__, __LINE__);
100  }
101 
102  /**
103  * Get the password of the user.
104  * @return The unencrypted password of the user.
105  */
106  function getPassword()
107  {
108  WCMFException::throwEx("getPassword() must be implemented by derived class: ".get_class($this), __FILE__, __LINE__);
109  }
110 
111  /**
112  * Set the name of the user.
113  * @param name The name of the user.
114  */
115  function setName($name)
116  {
117  WCMFException::throwEx("setName() must be implemented by derived class: ".get_class($this), __FILE__, __LINE__);
118  }
119 
120  /**
121  * Get name of the user.
122  * @return The name of the user.
123  */
124  function getName()
125  {
126  WCMFException::throwEx("getName() must be implemented by derived class: ".get_class($this), __FILE__, __LINE__);
127  }
128 
129  /**
130  * Set the firstname of the user.
131  * @param firstname The firstname of the user.
132  */
133  function setFirstname($firstname)
134  {
135  WCMFException::throwEx("setFirstname() must be implemented by derived class: ".get_class($this), __FILE__, __LINE__);
136  }
137 
138  /**
139  * Get the firstname of the user.
140  * @return The firstname of the user.
141  */
142  function getFirstname()
143  {
144  WCMFException::throwEx("getFirstname() must be implemented by derived class: ".get_class($this), __FILE__, __LINE__);
145  }
146 
147  /**
148  * Set the configuration file of the user.
149  * @param config The configuration file of the user.
150  */
151  function setConfig($config)
152  {
153  WCMFException::throwEx("setConfig() must be implemented by derived class: ".get_class($this), __FILE__, __LINE__);
154  }
155 
156  /**
157  * Get the configuration file of the user.
158  * @return The configuration file of the user.
159  */
160  function getConfig()
161  {
162  WCMFException::throwEx("getConfig() must be implemented by derived class: ".get_class($this), __FILE__, __LINE__);
163  }
164 
165  /**
166  * Assign a role to the user.
167  * @param rolename The role name. e.g. "administrators"
168  * @param commit True/False wether to commit the changes or not [default: false].
169  */
170  function addRole($rolename, $commit=false)
171  {
172  if ($this->hasRole($rolename))
173  return;
174 
175  // add the role if existing
176  $role = &$this->getRoleByName($rolename);
177  if ($role != null)
178  $this->addChild($role);
179 
180  // commit the changes if requested
181  if ($commit)
182  {
183  $nIter = new NodeIterator($this);
184  $cv = new CommitVisitor();
185  $cv->startIterator($nIter);
186  }
187  }
188 
189  /**
190  * Remove a role from the user.
191  * @param rolename The role name. e.g. "administrators"
192  * @param commit True/False wether to commit the changes or not [default: false].
193  */
194  function removeRole($rolename, $commit=false)
195  {
196  if (!$this->hasRole($rolename))
197  return;
198 
199  // remove the role if existing
200  $role = &$this->getRoleByName($rolename);
201  if ($role != null)
202  {
203  // commit the changes if requested
204  if ($commit)
205  {
206  // mark the child deleted first
207  $this->deleteChild($role->getOID());
208  $nIter = new NodeIterator($this);
209  $cv = new CommitVisitor();
210  $cv->startIterator($nIter);
211  }
212 
213  $this->deleteChild($role->getOID(), true);
214  }
215  }
216 
217  /**
218  * Check for a certain role in the user roles.
219  * @param rolename The role name to check for. e.g. "administrators"
220  * @return True/False whether the user has the role
221  */
222  function hasRole($rolename)
223  {
224  $roles = $this->getRoles();
225  for ($i=0; $i<sizeof($roles); $i++)
226  if ($roles[$i]->getName() == $rolename)
227  return true;
228  return false;
229  }
230 
231  /**
232  * Get the roles of a user.
233  * @return An array holding the role names
234  */
235  function getRoles()
236  {
237  if (!$this->_hasOwnRolesLoaded)
238  {
239  // make sure that the roles are loaded
240 
241  // allow this in any case (prevent infinite loops when trying to authorize)
242  $rightsManager = &RightsManager::getInstance();
243  $isAnonymous = $rightsManager->isAnonymous();
244  if (!$isAnonymous)
245  $rightsManager->deactivate();
246 
248 
249  // reactivate the RightsManager if necessary
250  if (!$isAnonymous)
251  $rightsManager->activate();
252 
253  $this->_hasOwnRolesLoaded = true;
254  }
255  return $this->getChildrenEx(null, UserManager::getRoleClassName(), null, null);
256  }
257 
258  /**
259  * Get a Role instance whose name is given
260  * @param rolename The name of the role
261  * @return A reference to the role or null if nor existing
262  */
263  function &getRoleByName($rolename)
264  {
265  if (!isset($this->_cachedRoles[$rolename]))
266  {
267  // load the role
268  $persistenceFacade = &PersistenceFacade::getInstance();
269  $role = &$persistenceFacade->loadFirstObject(UserManager::getRoleClassName(), BUILDDEPTH_SINGLE, array('name' => $rolename));
270  if ($role != null)
271  $this->_cachedRoles[$rolename] = &$role;
272  else
273  return null;
274  }
275  return $this->_cachedRoles[$rolename];
276  }
277 
278  /**
279  * This class caches loaded roles for performance reasons. After retrieving
280  * an instance from the session, the cache is invalid and must be reseted using
281  * this method.
282  */
283  function resetRoleCache()
284  {
285  $this->_cachedRoles = array();
286  $this->_hasOwnRolesLoaded = false;
287  }
288 }
289 ?>
setPassword($password)
Definition: class.User.php:97
The CommitVisitor is used to commit the object's changes to the object storage. The objects must impl...
Node is the basic component for building trees (although a Node can have one than more parents)...
Definition: class.Node.php:118
getPassword()
Definition: class.User.php:106
& getUser($login, $password)
Definition: class.User.php:51
$_hasOwnRolesLoaded
Definition: class.User.php:34
loadChildren($type, $buildDepth=BUILDDEPTH_SINGLE, $forceUpdate=false)
Definition: class.Node.php:255
User($oid=null, $type='User')
Definition: class.User.php:39
& getRoleByName($rolename)
Definition: class.User.php:263
Abstract base class for user classes that represent a system user.
Definition: class.User.php:31
addChild(&$child, $addtype=ADDCHILD_BACK)
Definition: class.Node.php:166
$_cachedRoles
Definition: class.User.php:33
NodeIterator is used to iterate over a tree/list build of objects using a Depth-First-Algorithm. Classes used with the NodeIterator must implement the getChildren() and getOID() methods. NodeIterator implements the 'Iterator Pattern'. The base class NodeIterator defines the interface for all specialized Iterator classes.
setFirstname($firstname)
Definition: class.User.php:133
setName($name)
Definition: class.User.php:115
getFirstname()
Definition: class.User.php:142
throwEx($message, $file='', $line='')
removeRole($rolename, $commit=false)
Definition: class.User.php:194
setConfig($config)
Definition: class.User.php:151
getLogin()
Definition: class.User.php:88
getRoles()
Definition: class.User.php:235
resetRoleCache()
Definition: class.User.php:283
getName()
Definition: class.User.php:124
getChildrenEx($oid, $type, $values, $properties, $useRegExp=true)
Definition: class.Node.php:325
getUserId()
Definition: class.User.php:70
getConfig()
Definition: class.User.php:160
deleteChild($childOID, $reallyDelete=false)
Definition: class.Node.php:223
const BUILDDEPTH_SINGLE
setLogin($login)
Definition: class.User.php:79
hasRole($rolename)
Definition: class.User.php:222
addRole($rolename, $commit=false)
Definition: class.User.php:170