wCMF  3.6
 All Classes Namespaces Files Functions Variables Groups Pages
class.UserManagerXML.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.UserManagerXML.php 1462 2014-02-04 23:52:27Z iherwig $
18  */
19 require_once(BASE."wcmf/lib/security/class.UserManager.php");
20 require_once(BASE."wcmf/lib/security/class.User.php");
21 require_once(BASE."wcmf/lib/security/class.Role.php");
22 require_once(BASE."wcmf/lib/util/class.XMLUtil.php");
23 
24 /**
25  * @class UserManagerXML
26  * @ingroup Security
27  * @brief UserManagerXML is a UserManager that stores user and role information in an XML file.
28  *
29  * @author ingo herwig <ingo@wemove.com>
30  */
32 {
33  var $_xml = null; // XML database
34  var $_old_error_handler = null;
35 
36  var $_initParams = null;
37  var $_fileOpened = false;
38 
39  /**
40  * Open the XML database.
41  * @param lock True/False wether a lock is required or not
42  */
43  function openConnection($lock=true)
44  {
45  $this->_old_error_handler = set_error_handler("XMLUtilErrorHandler");
46 
47  // setup xml database
48  $this->_xml = new XMLUtil();
49  $this->_xml->SetOptions(array('TimeStampFlag' => 0, 'XmlOptions' => array(XML_OPTION_SKIP_WHITE => 1)));
50 
51  // Security settings
52  $aDbPermissions = array(
53  );
54  // Pass down the security mode settings.
55  $this->_xml->bSecureMode = TRUE;
56  foreach ($aDbPermissions as $MethodName => $Permission)
57  $this->_xml->aPermissions[$MethodName] = $Permission;
58 
59  if (!$this->_xml->Open($this->_initParams['filename'], FALSE, $lock))
60  WCMFException::throwEx("Could not open XML input: ".$this->_initParams['filename'], __FILE__, __LINE__);
61  $this->_xml->XmlDb->setVerbose(0);
62 
63  $this->_fileOpened = true;
64 
65  // call commitTransaction() on shutdown for automatic transaction end
66  register_shutdown_function(array(&$this, 'closeConnection'));
67  }
68 
69  /**
70  * Close the XML database.
71  */
72  function closeConnection()
73  {
74  if ($this->_old_error_handler != null)
75  set_error_handler($this->_old_error_handler);
76  $this->_xml->Close();
77  $this->_fileOpened = false;
78  }
79 
80  /**
81  * Set Modification flag if no error occured.
82  */
83  function validateChanges()
84  {
85  if (($err = $this->_xml->XmlDb->getLastError()) != '')
86  {
87  // on error discard all changes
88  $this->_xml->bModifyFlag = false;
90  }
91  else
92  $this->_xml->bModifyFlag = true;
93  }
94 
95  /**
96  * @see UserManager::startTransaction()
97  */
98  function startTransaction()
99  {
100  if (!$this->_fileOpened)
101  $this->openConnection();
102  }
103 
104  /**
105  * @see UserManager::commitTransaction()
106  */
107  function commitTransaction()
108  {
109  if ($this->_fileOpened)
110  $this->closeConnection();
111  }
112 
113  /**
114  * @see UserManager::rollbackTransaction()
115  */
117  {
118  if ($this->_fileOpened)
119  {
120  $this->_xml->bModifyFlag = FALSE;
121  $this->closeConnection();
122  }
123  }
124 
125  /**
126  * @see UserManager::initialize()
127  *
128  * @note This class relies on the following dtd
129  * @verbatim
130  <!ELEMENT XmlDatabase (#PCDATA | nextID | users | roles)*>
131 
132  <!ELEMENT nextID (#PCDATA)>
133 
134  <!ELEMENT users (#PCDATA | user)*>
135  <!ELEMENT user (#PCDATA | name | firstname | login | password | config | userroles)*>
136  <!ATTLIST user id CDATA #IMPLIED>
137  <!ELEMENT firstname (#PCDATA)>
138  <!ELEMENT login (#PCDATA)>
139  <!ELEMENT password (#PCDATA)>
140  <!ELEMENT config (#PCDATA)>
141  <!ELEMENT userroles (#PCDATA | roleid)*>
142  <!ELEMENT roleid (#PCDATA)>
143 
144  <!ELEMENT roles (#PCDATA | role)*>
145  <!ELEMENT role (#PCDATA | name)*>
146  <!ATTLIST role id CDATA #IMPLIED>
147 
148  <!ELEMENT name (#PCDATA)>
149  @endverbatim
150  *
151  * @note Initialization data given in the constructor require the following keys:
152  * filename
153  */
154  function initialize($params)
155  {
156  $userRepository = array();
157  $userRepository['users'] = array();
158  $userRepository['roles'] = array();
159 
160  // connect to database
161  $this->_initParams = $params;
162  $this->openConnection(false);
163 
164  // query database
165  // users
166  $userQuery = "*/users/*";
167  $userPathArray = $this->_xml->XmlDb->evaluate($userQuery);
168  foreach ($userPathArray as $userPath)
169  {
170  $curUserID = $this->_xml->XmlDb->getAttributes($userPath, 'id');
171  $user = &$this->createUserInstance();
172  $user->setDBID($this->_xml->XmlDb->getAttributes($userPath, 'id'));
173  $user->setLogin($this->_xml->XmlDb->getData($userPath."/login"));
174  $user->setPassword($this->_xml->XmlDb->getData($userPath."/password"));
175  $user->setName($this->_xml->XmlDb->getData($userPath."/name"));
176  $user->setFirstname($this->_xml->XmlDb->getData($userPath."/firstname"));
177  $user->setConfig($this->_xml->XmlDb->getData($userPath."/config"));
178  $userRolesPathArray = $this->_xml->XmlDb->evaluate($userPath.'/userroles/roleid');
179  foreach ($userRolesPathArray as $userRolePath)
180  {
181  $roleId = $this->_xml->XmlDb->getData($userRolePath);
182  $rolePathArray = $this->_xml->XmlDb->evaluate('*/roles/role[@id="'.$roleId.'"]/name');
183  $role = &$this->createRoleInstance();
184  $role->setDBID($roleId);
185  $role->setName($this->_xml->XmlDb->getData($rolePathArray[0]));
186  $user->addChild($role);
187  }
188  $userRepository['users'][sizeof($userRepository['users'])] = &$user;
189  }
190  // roles
191  $roleQuery = "*/roles/*";
192  $rolePathArray = $this->_xml->XmlDb->evaluate($roleQuery);
193  foreach ($rolePathArray as $rolePath)
194  {
195  $curRoleID = $this->_xml->XmlDb->getAttributes($rolePath, 'id');
196  $role = &$this->createRoleInstance();
197  $role->setDBID($curRoleID);
198  $role->setName($this->_xml->XmlDb->getData($rolePath."/name"));
199  $userRepository['roles'][sizeof($userRepository['roles'])] = $role;
200  }
201 
202  return $userRepository;
203  }
204 
205  /**
206  * Create a user instance
207  */
208  function &createUserInstance()
209  {
210  $className = UserManager::getUserClassName();
211  return new $className;
212  }
213 
214  /**
215  * Create a role instance
216  */
217  function &createRoleInstance()
218  {
219  $className = UserManager::getRoleClassName();
220  return new $className;
221  }
222 
223  /**
224  * @see UserManager::createUserImpl()
225  */
226  function &createUserImpl($name, $firstname, $login, $password)
227  {
228  $newID = $this->_xml->GetNextInsertId();
229 
230  // find parent
231  $parentPathArray = $this->_xml->XmlDb->evaluate('*/users');
232 
233  // add user nodes
234  $userPath = $this->_xml->XmlDb->appendChild($parentPathArray[0], '<user/>');
235  $this->_xml->XmlDb->setAttributes($userPath, array('id' => $newID));
236 
237  $namePath = $this->_xml->XmlDb->appendChild($userPath, '<name/>');
238  $this->_xml->XmlDb->insertData($namePath, $name);
239 
240  $firstnamePath = $this->_xml->XmlDb->appendChild($userPath, '<firstname/>');
241  $this->_xml->XmlDb->insertData($firstnamePath, $firstname);
242 
243  $loginPath = $this->_xml->XmlDb->appendChild($userPath, '<login/>');
244  $this->_xml->XmlDb->insertData($loginPath, $login);
245 
246  $passwordPath = $this->_xml->XmlDb->appendChild($userPath, '<password/>');
247  $this->_xml->XmlDb->insertData($passwordPath, $password);
248 
249  $configPath = $this->_xml->XmlDb->appendChild($userPath, '<config/>');
250 
251  $rolesPath = $this->_xml->XmlDb->appendChild($userPath, '<userroles/>');
252 
253  // save if no error
254  $this->validateChanges();
255 
256  $className = UserManager::getUserClassName();
257  $user = new $className;
258  $user->setDBID($newID);
259  $user->setLogin($login);
260  $user->setPassword($password);
261  $user->setName($name);
262  $user->setFirstname($firstname);
263  return $user;
264  }
265 
266  /**
267  * @see UserManager::removeUserImpl()
268  */
269  function removeUserImpl(&$user)
270  {
271  // find user
272  $userQuery = '*/users/user[@id="'.$user->getDBID().'"]';
273  $userPathArray = $this->_xml->XmlDb->evaluate($userQuery);
274 
275  // remove user
276  $this->_xml->_RemoveRecord($userPathArray);
277 
278  // save if no error
279  $this->validateChanges();
280  }
281 
282  /**
283  * @see UserManager::setUserPropertyImpl()
284  */
285  function setUserPropertyImpl(&$user, $property, $value)
286  {
287  // find property
288  $propertyQuery = '*/users/user[@id="'.$user->getDBID().'"]/'.$property;
289  $propertyPathArray = $this->_xml->XmlDb->evaluate($propertyQuery);
290 
291  $this->_xml->XmlDb->replaceData($propertyPathArray[0], $value);
292 
293  // save if no error
294  $this->validateChanges();
295  }
296 
297  /**
298  * @see UserManager::createRoleImpl()
299  */
300  function &createRoleImpl($name)
301  {
302  $newID = $this->_xml->GetNextInsertId();
303 
304  // find parent
305  $parentPathArray = $this->_xml->XmlDb->evaluate('*/roles');
306 
307  // add role node
308  $rolePath = $this->_xml->XmlDb->appendChild($parentPathArray[0], '<role/>');
309  $this->_xml->XmlDb->setAttributes($rolePath, array('id' => $newID));
310  $namePath = $this->_xml->XmlDb->appendChild($rolePath, '<name/>');
311  $this->_xml->XmlDb->insertData($namePath, $name);
312 
313  // save if no error
314  $this->validateChanges();
315 
316  return new Role($newID, $name);
317  }
318 
319  /**
320  * @see UserManager::removeRoleImpl()
321  */
322  function removeRoleImpl(&$role)
323  {
324  // find user roles
325  $rolesQuery = '*/users/user/userroles/roleid[.="'.$role->getDBID().'"]';
326  $rolesPathArray = $this->_xml->XmlDb->evaluate($rolesQuery);
327 
328  // remove role from users
329  $this->_xml->_RemoveRecord($rolesPathArray);
330 
331  // find role
332  $roleQuery = '*/roles/role[@id="'.$role->getDBID().'"]';
333  $rolePathArray = $this->_xml->XmlDb->evaluate($roleQuery);
334 
335  // remove role
336  $this->_xml->_RemoveRecord($rolePathArray);
337 
338  // save if no error
339  $this->validateChanges();
340  }
341 
342  /**
343  * @see UserManager::setRolePropertyImpl()
344  */
345  function setRolePropertyImpl(&$role, $property, $value)
346  {
347  // find property
348  $propertyQuery = '*/roles/role[@id="'.$role->getDBID().'"]/'.$property;
349  $propertyPathArray = $this->_xml->XmlDb->evaluate($propertyQuery);
350 
351  $this->_xml->XmlDb->replaceData($propertyPathArray[0], $value);
352 
353  // save if no error
354  $this->validateChanges();
355  }
356 
357  /**
358  * @see UserManager::addUserToRoleImpl()
359  */
360  function addUserToRoleImpl(&$role, &$user)
361  {
362  // find user roles
363  $rolesQuery = '*/users/user[@id="'.$user->getDBID().'"]/userroles';
364  $rolesPathArray = $this->_xml->XmlDb->evaluate($rolesQuery);
365 
366  // add role
367  $rolePath = $this->_xml->XmlDb->appendChild($rolesPathArray[0], '<roleid/>');
368  $this->_xml->XmlDb->insertData($rolePath, $role->getDBID());
369 
370  // save if no error
371  $this->validateChanges();
372  }
373 
374  /**
375  * @see UserManager::removeUserFromRoleImpl()
376  */
377  function removeUserFromRoleImpl(&$role, &$user)
378  {
379  // find user roles
380  $rolesQuery = '*/users/user[@id="'.$user->getDBID().'"]/userroles/roleid[.="'.$role->getDBID().'"]';
381  $rolesPathArray = $this->_xml->XmlDb->evaluate($rolesQuery);
382 
383  // remove role
384  $this->_xml->_RemoveRecord($rolesPathArray);
385 
386  // save if no error
387  $this->validateChanges();
388  }
389 }
390 ?>
Abstract base class for role classes that represent a user role.
Definition: class.Role.php:28
throwEx($message, $file='', $line='')
XMLUtil helps in using XML files as storage. XMLUtil is a subclass of CXmlDb that is customized for u...
UserManagerXML is a UserManager that stores user and role information in an XML file.
addUserToRoleImpl(&$role, &$user)
removeUserFromRoleImpl(&$role, &$user)
UserManager is used to edit users and roles. UserManager supports the following operations: ...
& createUserImpl($name, $firstname, $login, $password)
openConnection($lock=true)
setUserPropertyImpl(&$user, $property, $value)
setRolePropertyImpl(&$role, $property, $value)