wCMF  3.6
 All Classes Namespaces Files Functions Variables Groups Pages
class.UserManagerRDB.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.UserManagerRDB.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.InifileParser.php");
23 require_once(BASE."wcmf/3rdparty/adodb/adodb.inc.php");
24 
25 /**
26  * @class UserManagerRDB
27  * @ingroup Security
28  * @brief UserManagerRDB is a UserManager that stores user and role information in a database.
29  * @deprecated Use UserManagerRDB instead
30  *
31  * @author ingo herwig <ingo@wemove.com>
32  */
33 class UserManagerRDB extends UserManager
34 {
35  var $_conn = null; // database connection
36  var $_dbPrefix = ''; // database prefix (if given in the configuration file)
37 
38  /**
39  * Open the database connection.
40  * @param params Assoziative array with the following keys: dbType, dbHostName, dbUserName, dbPassword, dbName
41  * if dbPrefix is given it will be appended to every table string, which is
42  * usefull if different cms operate on the same database
43  */
44  function openConnection($params)
45  {
46  // create new connection
47  $this->_conn = &ADONewConnection($params['dbType']);
48  $connected = $this->_conn->PConnect($params['dbHostName'],$params['dbUserName'],$params['dbPassword'],$params['dbName']);
49  if (!$connected)
50  WCMFException::throwEx($this->_conn->ErrorMsg(), __FILE__, __LINE__);
51 
52  $this->_conn->replaceQuote = "\'";
53  $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
54  define(ADODB_OUTP, "gError");
55 
56  // get database prefix if defined
57  $this->_dbPrefix = $params['dbPrefix'];
58 
59  // log sql if requested
60  $parser = &InifileParser::getInstance();
61  if (($logSQL = $parser->getValue('logSQL', 'cms')) === false)
62  $logSQL = 0;
63  $this->_conn->LogSQL($logSQL);
64  }
65 
66  /**
67  * @see UserManager::initialize()
68  *
69  * @note This class relies on the following database tables
70  * @verbatim
71  CREATE TABLE nm_user_role (
72  fk_user_id int(11) NOT NULL default '0',
73  fk_role_id int(11) NOT NULL default '0',
74  KEY fk_user_id (fk_user_id,fk_role_id)
75  ) TYPE=MyISAM;
76 
77  CREATE TABLE role (
78  id int(11) NOT NULL auto_increment,
79  name varchar(50) default NULL,
80  PRIMARY KEY (id)
81  ) TYPE=MyISAM;
82 
83  CREATE TABLE user (
84  id int(11) NOT NULL auto_increment,
85  name varchar(50) default NULL,
86  firstname varchar(50) default NULL,
87  login varchar(50) default NULL,
88  password varchar(50) default NULL,
89  config varchar(255) default NULL,
90  PRIMARY KEY (id)
91  ) TYPE=MyISAM;
92  @endverbatim
93  *
94  * @note Initialization data given in the constructor require the following keys:
95  * dbType, dbHostName, dbUserName, dbPassword, dbName
96  */
97  function initialize($params)
98  {
99  $userRepository = array();
100  $userRepository['users'] = array();
101  $userRepository['roles'] = array();
102 
103  // connect to database
104  $this->openConnection($params);
105 
106  // query database
107  // users
108  $sqlStr = "SELECT ".$this->_dbPrefix."user.id, ".$this->_dbPrefix."user.name, ".$this->_dbPrefix."user.login, ".$this->_dbPrefix."user.password, ".$this->_dbPrefix."user.firstname, ".$this->_dbPrefix."user.config, ".$this->_dbPrefix."role.name AS rolename
109  FROM ".$this->_dbPrefix."user LEFT JOIN ".$this->_dbPrefix."nm_user_role ON ".$this->_dbPrefix."user.id=".$this->_dbPrefix."nm_user_role.fk_user_id LEFT JOIN ".$this->_dbPrefix."role
110  ON ".$this->_dbPrefix."nm_user_role.fk_role_id=".$this->_dbPrefix."role.id ORDER BY user.id;";
111  $rs = &$this->_conn->Execute($sqlStr);
112  $curUserID = '';
113  while ($rs && $row = $rs->FetchRow())
114  {
115  if ($row['id'] != $curUserID)
116  {
117  $curUserID = $row['id'];
118  $user = new User($curUserID, $row['login'], $row['password'], $row['name'], $row['firstname'], $row['config'], array());
119  $userRepository['users'][$curUserID] = $user;
120  }
121  if ($row['rolename'] != '')
122  $userRepository['users'][$curUserID]->addRole($row['rolename']);
123  }
124  // roles
125  $sqlStr = "SELECT ".$this->_dbPrefix."role.id, ".$this->_dbPrefix."role.name FROM ".$this->_dbPrefix."role;";
126  $rs = &$this->_conn->Execute($sqlStr);
127  while ($rs && $row = $rs->FetchRow())
128  $userRepository['roles'][$row['id']] = new Role($row['id'], $row['name']);
129 
130  return $userRepository;
131  }
132 
133  /**
134  * @see UserManager::createUserImpl()
135  */
136  function createUserImpl($name, $firstname, $login, $password)
137  {
138  $newID = $this->_conn->GenID();
139  $sqlStr = "INSERT INTO ".$this->_dbPrefix."user (id, name, firstname, login, password) VALUES (".$this->_conn->qstr($newID).", ".
140  $this->_conn->qstr($name).", ".$this->_conn->qstr($firstname).", ".$this->_conn->qstr($login).", ".$this->_conn->qstr($password).");";
141  if ($this->_conn->Execute($sqlStr) === false)
142  {
143  Log::error($this->_conn->ErrorMsg().". Your query was: ".$sqlStr, __CLASS__);
144  WCMFException::throwEx("Error creating user '".$login."'. See log file for details.", __FILE__, __LINE__);
145  }
146 
147  return $newID;
148  }
149 
150  /**
151  * @see UserManager::removeUserImpl()
152  */
153  function removeUserImpl($user)
154  {
155  // remove user from all rows
156  $sqlStr = "DELETE FROM ".$this->_dbPrefix."nm_user_role WHERE fk_user_id=".$this->_conn->qstr($user->getID()).";";
157  if ($this->_conn->Execute($sqlStr) === false)
158  {
159  Log::error($this->_conn->ErrorMsg().". Your query was: ".$sqlStr, __CLASS__);
160  WCMFException::throwEx("Error removing user '".$user->getLogin()."' from his roles. See log file for details.", __FILE__, __LINE__);
161  }
162  // remove user
163  $sqlStr = "DELETE FROM ".$this->_dbPrefix."user WHERE id=".$this->_conn->qstr($user->getID()).";";
164  if ($this->_conn->Execute($sqlStr) === false)
165  {
166  Log::error($this->_conn->ErrorMsg().". Your query was: ".$sqlStr, __CLASS__);
167  WCMFException::throwEx("Error removing user '".$user->getLogin()."'. See log file for details.", __FILE__, __LINE__);
168  }
169  }
170 
171  /**
172  * @see UserManager::setUserPropertyImpl()
173  */
174  function setUserPropertyImpl($user, $property, $value)
175  {
176  $sqlStr = "UPDATE ".$this->_dbPrefix."user SET ".$property."=".$this->_conn->qstr($value)." WHERE id=".$this->_conn->qstr($user->getID()).";";
177  if ($this->_conn->Execute($sqlStr) === false)
178  {
179  Log::error($this->_conn->ErrorMsg().". Your query was: ".$sqlStr, __CLASS__);
180  WCMFException::throwEx("Error changing property '".$property."' for user '".$user->getLogin()."'. See log file for details.", __FILE__, __LINE__);
181  }
182  }
183 
184  /**
185  * @see UserManager::createRoleImpl()
186  */
187  function createRoleImpl($name)
188  {
189  $newID = $this->_conn->GenID();
190  $sqlStr = "INSERT INTO ".$this->_dbPrefix."role (id, name) VALUES (".$this->_conn->qstr($newID).", ".$this->_conn->qstr($name).");";
191  if ($this->_conn->Execute($sqlStr) === false)
192  {
193  Log::error($this->_conn->ErrorMsg().". Your query was: ".$sqlStr, __CLASS__);
194  WCMFException::throwEx("Error creating role '".$name."'. See log file for details.", __FILE__, __LINE__);
195  }
196 
197  return $newID;
198  }
199 
200  /**
201  * @see UserManager::removeRoleImpl()
202  */
203  function removeRoleImpl($role)
204  {
205  // remove role from all users
206  $sqlStr = "DELETE FROM ".$this->_dbPrefix."nm_user_role WHERE fk_role_id=".$this->_conn->qstr($role->getID()).";";
207  if ($this->_conn->Execute($sqlStr) === false)
208  {
209  Log::error($this->_conn->ErrorMsg().". Your query was: ".$sqlStr, __CLASS__);
210  WCMFException::throwEx("Error removing role '".$role->getName()."' from her users. See log file for details.", __FILE__, __LINE__);
211  }
212  // remove role
213  $sqlStr = "DELETE FROM ".$this->_dbPrefix."role WHERE id=".$this->_conn->qstr($role->getID()).";";
214  if ($this->_conn->Execute($sqlStr) === false)
215  {
216  Log::error($this->_conn->ErrorMsg().". Your query was: ".$sqlStr, __CLASS__);
217  WCMFException::throwEx("Error removing role '".$role->getName()."'. See log file for details.", __FILE__, __LINE__);
218  }
219  }
220 
221  /**
222  * @see UserManager::setRolePropertyImpl()
223  */
224  function setRolePropertyImpl($role, $property, $value)
225  {
226  $sqlStr = "UPDATE ".$this->_dbPrefix."role SET ".$property."=".$this->_conn->qstr($value)." WHERE id=".$this->_conn->qstr($role->getID()).";";
227  if ($this->_conn->Execute($sqlStr) === false)
228  {
229  Log::error($this->_conn->ErrorMsg().". Your query was: ".$sqlStr, __CLASS__);
230  WCMFException::throwEx("Error changing property '".$property."' for role '".$role->getName()."'. See log file for details.", __FILE__, __LINE__);
231  }
232  }
233 
234  /**
235  * @see UserManager::addUserToRoleImpl()
236  */
237  function addUserToRoleImpl($role, $user)
238  {
239  $sqlStr = "INSERT INTO ".$this->_dbPrefix."nm_user_role (fk_user_id, fk_role_id) VALUES (".$this->_conn->qstr($user->getID()).", ".$this->_conn->qstr($role->getID()).");";
240  if ($this->_conn->Execute($sqlStr) === false)
241  {
242  Log::error($this->_conn->ErrorMsg().". Your query was: ".$sqlStr, __CLASS__);
243  WCMFException::throwEx("Error adding user '".$user->getLogin()."' to role '".$role->getName()."'. See log file for details.", __FILE__, __LINE__);
244  }
245  }
246 
247  /**
248  * @see UserManager::removeUserFromRoleImpl()
249  */
250  function removeUserFromRoleImpl($role, $user)
251  {
252  $sqlStr = "DELETE FROM ".$this->_dbPrefix."nm_user_role WHERE fk_user_id=".$this->_conn->qstr($user->getID())." AND fk_role_id=".$this->_conn->qstr($role->getID()).";";
253  if ($this->_conn->Execute($sqlStr) === false)
254  {
255  Log::error($this->_conn->ErrorMsg().". Your query was: ".$sqlStr, __CLASS__);
256  WCMFException::throwEx("Error removing user '".$user->getLogin()."' from role '".$role->getName()."'. See log file for details.", __FILE__, __LINE__);
257  }
258  }
259 }
260 ?>
error($message, $category)
Definition: class.Log.php:69
Abstract base class for user classes that represent a system user.
Definition: class.User.php:31
Abstract base class for role classes that represent a user role.
Definition: class.Role.php:28
setRolePropertyImpl($role, $property, $value)
throwEx($message, $file='', $line='')
setUserPropertyImpl($user, $property, $value)
UserManager is used to edit users and roles. UserManager supports the following operations: ...
createUserImpl($name, $firstname, $login, $password)
removeUserFromRoleImpl($role, $user)
addUserToRoleImpl($role, $user)
UserManagerRDB is a UserManager that stores user and role information in a database using RDBMappers...