| | 1 | <?php |
|---|
| | 2 | |
|---|
| | 3 | require_once SGL_MOD_DIR . '/user/classes/DA_UserTemplate.php'; |
|---|
| | 4 | require_once 'DB/DataObject.php'; |
|---|
| | 5 | |
|---|
| | 6 | // role sync constants |
|---|
| | 7 | //define('SGL_ROLESYNC_ADD', 1); |
|---|
| | 8 | //define('SGL_ROLESYNC_REMOVE', 2); |
|---|
| | 9 | //define('SGL_ROLESYNC_ADDREMOVE', 3); |
|---|
| | 10 | //define('SGL_ROLESYNC_VIEWONLY', 4); |
|---|
| | 11 | |
|---|
| | 12 | class DA_User2 extends DA_UserTemplate |
|---|
| | 13 | { |
|---|
| | 14 | function DA_User2() |
|---|
| | 15 | { |
|---|
| | 16 | parent::DA_UserTemplate(); |
|---|
| | 17 | $this->dbh = &SGL_DB::singleton(); |
|---|
| | 18 | } |
|---|
| | 19 | |
|---|
| | 20 | function &singleton() |
|---|
| | 21 | { |
|---|
| | 22 | static $instance; |
|---|
| | 23 | |
|---|
| | 24 | // If the instance is not there, create one |
|---|
| | 25 | if (!isset($instance)) { |
|---|
| | 26 | $instance = new DA_User2(); |
|---|
| | 27 | } |
|---|
| | 28 | return $instance; |
|---|
| | 29 | } |
|---|
| | 30 | |
|---|
| | 31 | function _startTransaction() |
|---|
| | 32 | { |
|---|
| | 33 | SGL_DB::setConnection(); |
|---|
| | 34 | $this->dbh->autocommit(false); |
|---|
| | 35 | } |
|---|
| | 36 | |
|---|
| | 37 | function _rollback() |
|---|
| | 38 | { |
|---|
| | 39 | $this->dbh->rollback(); |
|---|
| | 40 | } |
|---|
| | 41 | |
|---|
| | 42 | function _commit() |
|---|
| | 43 | { |
|---|
| | 44 | $this->dbh->commit(); |
|---|
| | 45 | } |
|---|
| | 46 | |
|---|
| | 47 | function _addUser($oUser) |
|---|
| | 48 | { |
|---|
| | 49 | $userId = $this->dbh->nextId($this->conf['table']['user']); |
|---|
| | 50 | if (PEAR::isError($userId)) { |
|---|
| | 51 | return $userId; |
|---|
| | 52 | } |
|---|
| | 53 | |
|---|
| | 54 | $oUser->usr_id = $userId; |
|---|
| | 55 | $ok = $oUser->insert(); |
|---|
| | 56 | // according to PEAR Manual |
|---|
| | 57 | // insert() throws PEAR_Error on failure |
|---|
| | 58 | if (PEAR::isError($ok)) { |
|---|
| | 59 | return $ok; |
|---|
| | 60 | } |
|---|
| | 61 | |
|---|
| | 62 | // assign permissions associated with role user belongs to |
|---|
| | 63 | // first get all perms associated with user's role |
|---|
| | 64 | $aRolePerms = $this->_getPermsByRoleId($oUser->role_id); |
|---|
| | 65 | if (PEAR::isError($aRolePerms)) { |
|---|
| | 66 | return $aRolePerms; |
|---|
| | 67 | } |
|---|
| | 68 | |
|---|
| | 69 | // then assign them to the user_permission table |
|---|
| | 70 | $ok = $this->_addPermsByUserId($aRolePerms, $oUser->usr_id); |
|---|
| | 71 | if (PEAR::isError($ok)) { |
|---|
| | 72 | return $ok; |
|---|
| | 73 | } |
|---|
| | 74 | |
|---|
| | 75 | // assign preferences associated with org user belongs to |
|---|
| | 76 | // first get all prefs associated with user's org or default |
|---|
| | 77 | // prefs if orgs are disabled |
|---|
| | 78 | $aPrefs = (@$this->conf['OrgMgr']['enabled']) |
|---|
| | 79 | ? $this->getUserPrefsByOrgId($oUser->organisation_id, SGL_RET_ID_VALUE) |
|---|
| | 80 | : $this->_getMasterPrefs(SGL_RET_ID_VALUE); |
|---|
| | 81 | |
|---|
| | 82 | if (PEAR::isError($aPrefs)) { |
|---|
| | 83 | return $aPrefs; |
|---|
| | 84 | } |
|---|
| | 85 | |
|---|
| | 86 | // then assign them to the user_preference table |
|---|
| | 87 | $ok = $this->_addPrefsByUserId($aPrefs, $oUser->usr_id); |
|---|
| | 88 | if (PEAR::isError($ok)) { |
|---|
| | 89 | return $ok; |
|---|
| | 90 | } |
|---|
| | 91 | |
|---|
| | 92 | return $userId; |
|---|
| | 93 | } |
|---|
| | 94 | |
|---|
| | 95 | function _updateUser($oUser) |
|---|
| | 96 | { |
|---|
| | 97 | // get existing user object |
|---|
| | 98 | $oUserBackup = $this->_getUserById($oUser->usr_id); |
|---|
| | 99 | |
|---|
| | 100 | $ok = $oUser->update(); |
|---|
| | 101 | // according to PEAR Manual |
|---|
| | 102 | // update() returns false or throws PEAR_Error on failure |
|---|
| | 103 | if ($ok === false) { |
|---|
| | 104 | return PEAR::raiseError('Problem updating user DataObject'); |
|---|
| | 105 | } elseif (PEAR::isError($ok)) { |
|---|
| | 106 | return $ok; |
|---|
| | 107 | } |
|---|
| | 108 | |
|---|
| | 109 | // change perms if role is modified |
|---|
| | 110 | if ($oUser->role_id != $oUserBackup->role_id) { |
|---|
| | 111 | |
|---|
| | 112 | // disallow usr_id(1) admin from changing role |
|---|
| | 113 | if ($oUser->usr_id == SGL_ADMIN) { |
|---|
| | 114 | return PEAR::raiseError('User with ID = 1 (SGL_ADMIN) cannot change role'); |
|---|
| | 115 | } |
|---|
| | 116 | |
|---|
| | 117 | // first delete old perms |
|---|
| | 118 | $aDeleteOptions = array( |
|---|
| | 119 | 'userId' => $oUser->usr_id |
|---|
| | 120 | ); |
|---|
| | 121 | $ok = $this->_deleteUserPermissions($aDeleteOptions); |
|---|
| | 122 | if (PEAR::isError($ok)) { |
|---|
| | 123 | return $ok; |
|---|
| | 124 | } |
|---|
| | 125 | |
|---|
| | 126 | // assign permissions associated with role user has been moved to |
|---|
| | 127 | // first get all perms associated with user's new role |
|---|
| | 128 | $aRolePerms = $this->_getPermsByRoleId($oUser->role_id); |
|---|
| | 129 | if (PEAR::isError($aRolePerms)) { |
|---|
| | 130 | return $aRolePerms; |
|---|
| | 131 | } |
|---|
| | 132 | |
|---|
| | 133 | // then assign them to the user_permission table |
|---|
| | 134 | $ok = $this->_addPermsByUserId($aRolePerms, $oUser->usr_id); |
|---|
| | 135 | if (PEAR::isError($ok)) { |
|---|
| | 136 | return $ok; |
|---|
| | 137 | } |
|---|
| | 138 | } |
|---|
| | 139 | |
|---|
| | 140 | // change prefs if org is modified |
|---|
| | 141 | if ($oUser->organisation_id != $oUserBackup->organisation_id) { |
|---|
| | 142 | |
|---|
| | 143 | // first delete old preferences |
|---|
| | 144 | $ok = $this->deletePrefsByUserId($oUser->usr_id); |
|---|
| | 145 | if (PEAR::isError($ok)) { |
|---|
| | 146 | return $ok; |
|---|
| | 147 | } |
|---|
| | 148 | |
|---|
| | 149 | // assign preferences associated with org user belongs to |
|---|
| | 150 | // first get all prefs associated with user's org |
|---|
| | 151 | $aOrgPrefs = $this->getUserPrefsByOrgId($oUser->organisation_id, SGL_RET_ID_VALUE); |
|---|
| | 152 | if (PEAR::isError($aOrgPrefs)) { |
|---|
| | 153 | return $aOrgPrefs; |
|---|
| | 154 | } |
|---|
| | 155 | |
|---|
| | 156 | // then assign them to the user_preference table |
|---|
| | 157 | $ok = $this->addPrefsByUserId($aOrgPrefs, $oUser->usr_id); |
|---|
| | 158 | if (PEAR::isError($ok)) { |
|---|
| | 159 | return $ok; |
|---|
| | 160 | } |
|---|
| | 161 | } |
|---|
| | 162 | |
|---|
| | 163 | return true; |
|---|
| | 164 | } |
|---|
| | 165 | |
|---|
| | 166 | function _getRoles($bExcludeRoot) |
|---|
| | 167 | { |
|---|
| | 168 | SGL::logMessage(null, PEAR_LOG_DEBUG); |
|---|
| | 169 | |
|---|
| | 170 | $whereClause = ($bExcludeRoot) ? ' AND role_id <> ' . SGL_ADMIN : ''; |
|---|
| | 171 | |
|---|
| | 172 | $query = " |
|---|
| | 173 | SELECT role_id, name |
|---|
| | 174 | FROM " . $this->conf['table']['role'] . " |
|---|
| | 175 | WHERE role_id <> " . SGL_GUEST . " |
|---|
| | 176 | AND role_id <> " . SGL_UNASSIGNED . |
|---|
| | 177 | $whereClause; |
|---|
| | 178 | $aAllRoles = $this->dbh->getAssoc($query); |
|---|
| | 179 | |
|---|
| | 180 | // remove roles that have no perms set |
|---|
| | 181 | foreach ($aAllRoles as $roleId => $name) { |
|---|
| | 182 | if ($roleId === SGL_ADMIN) { |
|---|
| | 183 | continue; |
|---|
| | 184 | } |
|---|
| | 185 | $query = |
|---|
| | 186 | 'SELECT COUNT(*) FROM ' . $this->conf['table']['role_permission'] . |
|---|
| | 187 | ' WHERE role_id =' . $roleId; |
|---|
| | 188 | $count = $this->dbh->getOne($query); |
|---|
| | 189 | if ($count < 1) { |
|---|
| | 190 | unset($aAllRoles[$roleId]); |
|---|
| | 191 | } |
|---|
| | 192 | } |
|---|
| | 193 | return $aAllRoles; |
|---|
| | 194 | } |
|---|
| | 195 | |
|---|
| | 196 | function _getUserById($id) |
|---|
| | 197 | { |
|---|
| | 198 | $oUser = DB_DataObject::factory($this->conf['table']['user']); |
|---|
| | 199 | if (!is_null($id)) { |
|---|
| | 200 | $oUser->get($id); |
|---|
| | 201 | } |
|---|
| | 202 | |
|---|
| | 203 | return $oUser; |
|---|
| | 204 | } |
|---|
| | 205 | |
|---|
| | 206 | function _isUniqueUsername($username) |
|---|
| | 207 | { |
|---|
| | 208 | $oUser = DB_DataObject::factory($this->conf['table']['user']); |
|---|
| | 209 | $oUser->whereAdd("username = '$username'"); |
|---|
| | 210 | $numRows = $oUser->find(); |
|---|
| | 211 | |
|---|
| | 212 | // return false if any rows found |
|---|
| | 213 | return (boolean) $numRows == 0; |
|---|
| | 214 | } |
|---|
| | 215 | |
|---|
| | 216 | function _isUniqueEmail($email) |
|---|
| | 217 | { |
|---|
| | 218 | $oUser = DB_DataObject::factory($this->conf['table']['user']); |
|---|
| | 219 | $oUser->whereAdd("email = '$email'"); |
|---|
| | 220 | $numRows = $oUser->find(); |
|---|
| | 221 | |
|---|
| | 222 | // return false if any rows found |
|---|
| | 223 | return (boolean) $numRows == 0; |
|---|
| | 224 | } |
|---|
| | 225 | |
|---|
| | 226 | function _getPermsByRoleId($roleId = 0) |
|---|
| | 227 | { |
|---|
| | 228 | // no logMessage allowed here |
|---|
| | 229 | // Tamme: WHY??? |
|---|
| | 230 | $query = " |
|---|
| | 231 | SELECT permission_id |
|---|
| | 232 | FROM {$this->conf['table']['role_permission']} |
|---|
| | 233 | WHERE role_id = $roleId"; |
|---|
| | 234 | $aRolePerms = $this->dbh->getCol($query); |
|---|
| | 235 | |
|---|
| | 236 | return $aRolePerms; |
|---|
| | 237 | } |
|---|
| | 238 | |
|---|
| | 239 | function _addPermsByUserId($aRolePerms, $userId) |
|---|
| | 240 | { |
|---|
| | 241 | SGL::logMessage(null, PEAR_LOG_DEBUG); |
|---|
| | 242 | |
|---|
| | 243 | if (!count($aRolePerms)) { |
|---|
| | 244 | return; |
|---|
| | 245 | } |
|---|
| | 246 | |
|---|
| | 247 | $query = " |
|---|
| | 248 | INSERT INTO {$this->conf['table']['user_permission']} |
|---|
| | 249 | (user_permission_id, usr_id, permission_id) |
|---|
| | 250 | VALUES (?, ?, ?)"; |
|---|
| | 251 | $sth = $this->dbh->prepare($query); |
|---|
| | 252 | if (PEAR::isError($sth)) { |
|---|
| | 253 | return $sth; |
|---|
| | 254 | } |
|---|
| | 255 | |
|---|
| | 256 | foreach ($aRolePerms as $permId) { |
|---|
| | 257 | $aParams = array( |
|---|
| | 258 | $this->dbh->nextId($this->conf['table']['user_permission']), |
|---|
| | 259 | $userId, |
|---|
| | 260 | $permId); |
|---|
| | 261 | |
|---|
| | 262 | $result = $this->dbh->execute($sth, $aParams); |
|---|
| | 263 | if (PEAR::isError($result)) { |
|---|
| | 264 | return $result; |
|---|
| | 265 | } |
|---|
| | 266 | } |
|---|
| | 267 | } |
|---|
| | 268 | |
|---|
| | 269 | function _getMasterPrefs($type = SGL_RET_NAME_VALUE) |
|---|
| | 270 | { |
|---|
| | 271 | switch ($type) { |
|---|
| | 272 | case SGL_RET_ID_VALUE: |
|---|
| | 273 | $term = 'preference_id'; |
|---|
| | 274 | break; |
|---|
| | 275 | |
|---|
| | 276 | case SGL_RET_NAME_VALUE: |
|---|
| | 277 | default: |
|---|
| | 278 | $term = 'name'; |
|---|
| | 279 | } |
|---|
| | 280 | |
|---|
| | 281 | $query = " |
|---|
| | 282 | SELECT $term, default_value |
|---|
| | 283 | FROM {$this->conf['table']['preference']}"; |
|---|
| | 284 | |
|---|
| | 285 | $aRes = $this->dbh->getAssoc($query); |
|---|
| | 286 | if (PEAR::isError($aRes)) { |
|---|
| | 287 | return $aRes; |
|---|
| | 288 | } |
|---|
| | 289 | |
|---|
| | 290 | // set default theme from config |
|---|
| | 291 | $key = ($type == SGL_RET_NAME_VALUE) |
|---|
| | 292 | ? 'theme' |
|---|
| | 293 | : 3; |
|---|
| | 294 | $c = &SGL_Config::singleton(); |
|---|
| | 295 | $defaultTheme = $c->get(array('site' => 'defaultTheme')); |
|---|
| | 296 | $aRes[$key] = $defaultTheme; |
|---|
| | 297 | |
|---|
| | 298 | return $aRes; |
|---|
| | 299 | } |
|---|
| | 300 | |
|---|
| | 301 | function _addPrefsByUserId($aPrefs, $userId) |
|---|
| | 302 | { |
|---|
| | 303 | if (!count($aPrefs)) { |
|---|
| | 304 | return true; |
|---|
| | 305 | } |
|---|
| | 306 | |
|---|
| | 307 | $query = " |
|---|
| | 308 | INSERT INTO {$this->conf['table']['user_preference']} |
|---|
| | 309 | (user_preference_id, usr_id, preference_id, value) |
|---|
| | 310 | VALUES (?, $userId, ?, ?)"; |
|---|
| | 311 | |
|---|
| | 312 | $sth = $this->dbh->prepare($query); |
|---|
| | 313 | if (PEAR::isError($sth)) { |
|---|
| | 314 | return $sth; |
|---|
| | 315 | } |
|---|
| | 316 | |
|---|
| | 317 | foreach ($aPrefs as $prefId => $prefValue) { |
|---|
| | 318 | $aParams = array( |
|---|
| | 319 | $this->dbh->nextId($this->conf['table']['user_preference']), |
|---|
| | 320 | $prefId, |
|---|
| | 321 | $prefValue); |
|---|
| | 322 | |
|---|
| | 323 | $result = $this->dbh->execute($sth, $aParams); |
|---|
| | 324 | if (PEAR::isError($result)) { |
|---|
| | 325 | return $result; |
|---|
| | 326 | } |
|---|
| | 327 | } |
|---|
| | 328 | |
|---|
| | 329 | return true; |
|---|
| | 330 | } |
|---|
| | 331 | |
|---|
| | 332 | /** |
|---|
| | 333 | * Deletes preferences for a given user. |
|---|
| | 334 | * |
|---|
| | 335 | * @access public |
|---|
| | 336 | * @param integer $userId The id of the user preferences are being deleted for |
|---|
| | 337 | * @return boolean True on success, PEAR error on failure |
|---|
| | 338 | */ |
|---|
| | 339 | function _deletePrefsByUserId($userId) |
|---|
| | 340 | { |
|---|
| | 341 | $query = " |
|---|
| | 342 | DELETE FROM {$this->conf['table']['user_preference']} |
|---|
| | 343 | WHERE usr_id = $userId"; |
|---|
| | 344 | return $this->dbh->query($query); |
|---|
| | 345 | } |
|---|
| | 346 | |
|---|
| | 347 | function _deleteUserByIdStatement() |
|---|
| | 348 | { |
|---|
| | 349 | $query = " |
|---|
| | 350 | DELETE FROM {$this->conf['table']['user']} |
|---|
| | 351 | WHERE usr_id = ?"; |
|---|
| | 352 | $sth = $this->dbh->prepare($query); |
|---|
| | 353 | |
|---|
| | 354 | return $sth; |
|---|
| | 355 | } |
|---|
| | 356 | |
|---|
| | 357 | function _deleteUserById($id, $sth = null) |
|---|
| | 358 | { |
|---|
| | 359 | $aDeleteOptions = array( |
|---|
| | 360 | 'userId' => $id |
|---|
| | 361 | ); |
|---|
| | 362 | $result = $this->_deleteUserPermissions($aDeleteOptions); |
|---|
| | 363 | if (PEAR::isError($result)) { |
|---|
| | 364 | return $result; |
|---|
| | 365 | } |
|---|
| | 366 | |
|---|
| | 367 | $result = $this->_deletePrefsByUserId($id); |
|---|
| | 368 | if (PEAR::isError($result)) { |
|---|
| | 369 | return $result; |
|---|
| | 370 | } |
|---|
| | 371 | |
|---|
| | 372 | $result = $this->dbh->execute($sth, $id); |
|---|
| | 373 | if (PEAR::isError($result)) { |
|---|
| | 374 | return $result; |
|---|
| | 375 | } |
|---|
| | 376 | } |
|---|
| | 377 | |
|---|
| | 378 | function _getPermsByUserId($userId) |
|---|
| | 379 | { |
|---|
| | 380 | SGL::logMessage(null, PEAR_LOG_DEBUG); |
|---|
| | 381 | |
|---|
| | 382 | $query = " |
|---|
| | 383 | SELECT permission_id |
|---|
| | 384 | FROM {$this->conf['table']['user_permission']} |
|---|
| | 385 | WHERE usr_id = $userId"; |
|---|
| | 386 | $aUserPerms = $this->dbh->getCol($query); |
|---|
| | 387 | |
|---|
| | 388 | return $aUserPerms; |
|---|
| | 389 | } |
|---|
| | 390 | |
|---|
| | 391 | /** |
|---|
| | 392 | * Returns an assoc array of all perms. |
|---|
| | 393 | * |
|---|
| | 394 | * @access public |
|---|
| | 395 | * @param int $moduleId only select perms for one module |
|---|
| | 396 | * @param int $type return type constant |
|---|
| | 397 | * @return array $aAllPerms array of perms returned |
|---|
| | 398 | */ |
|---|
| | 399 | function _getPermsByModuleId($moduleId, $type) |
|---|
| | 400 | { |
|---|
| | 401 | switch ($type) { |
|---|
| | 402 | case SGL_RET_ARRAY: |
|---|
| | 403 | $filter = (!empty($moduleId)) |
|---|
| | 404 | ? "AND p.module_id = $moduleId" |
|---|
| | 405 | : null; |
|---|
| | 406 | $query = " |
|---|
| | 407 | SELECT permission_id, p.name, m.name AS module_name, p.module_id |
|---|
| | 408 | FROM {$this->conf['table']['permission']} p, |
|---|
| | 409 | {$this->conf['table']['module']} m |
|---|
| | 410 | WHERE p.module_id = m.module_id |
|---|
| | 411 | $filter |
|---|
| | 412 | ORDER BY name"; |
|---|
| | 413 | $aAllPerms = $this->dbh->getAll($query, DB_FETCHMODE_ASSOC); |
|---|
| | 414 | break; |
|---|
| | 415 | |
|---|
| | 416 | case SGL_RET_ID_VALUE: |
|---|
| | 417 | default: |
|---|
| | 418 | $filter = (!empty($moduleId)) |
|---|
| | 419 | ? "WHERE module_id = $moduleId" |
|---|
| | 420 | : null; |
|---|
| | 421 | $query = " |
|---|
| | 422 | SELECT permission_id, name |
|---|
| | 423 | FROM {$this->conf['table']['permission']} p |
|---|
| | 424 | $filter |
|---|
| | 425 | ORDER BY name"; |
|---|
| | 426 | $aAllPerms = $this->dbh->getAssoc($query); |
|---|
| | 427 | } |
|---|
| | 428 | |
|---|
| | 429 | return $aAllPerms; |
|---|
| | 430 | } |
|---|
| | 431 | |
|---|
| | 432 | function _deleteUserPermissions($aOptions = null) |
|---|
| | 433 | { |
|---|
| | 434 | SGL::logMessage(null, PEAR_LOG_DEBUG); |
|---|
| | 435 | |
|---|
| | 436 | if (is_null($aOptions) || !is_array($aOptions) || !count($aOptions)) { |
|---|
| | 437 | return SGL::raiseError('no arguments supplied', SGL_ERROR_INVALIDARGS); |
|---|
| | 438 | } |
|---|
| | 439 | |
|---|
| | 440 | $wsh = &new WhereStack(); |
|---|
| | 441 | foreach ($aOptions as $key => $option) { |
|---|
| | 442 | switch ($key) { |
|---|
| | 443 | case 'userId': |
|---|
| | 444 | $wsh->add('usr_id = ' . (int) $aOptions['userId']); |
|---|
| | 445 | break; |
|---|
| | 446 | |
|---|
| | 447 | case 'permList': |
|---|
| | 448 | if (!is_array($option) || !count($option)) { |
|---|
| | 449 | return SGL::raiseError("option 'permList' requires array to operate", |
|---|
| | 450 | SGL_ERROR_INVALIDARGS); |
|---|
| | 451 | } |
|---|
| | 452 | $strPermList = implode(',', $option); |
|---|
| | 453 | $wsh->add("permission_id IN ($strPermList)"); |
|---|
| | 454 | break; |
|---|
| | 455 | |
|---|
| | 456 | default: |
|---|
| | 457 | return SGL::raiseError("invalid option '$key'", SGL_ERROR_INVALIDARGS); |
|---|
| | 458 | } |
|---|
| | 459 | } |
|---|
| | 460 | |
|---|
| | 461 | $query = " |
|---|
| | 462 | DELETE FROM {$this->conf['table']['user_permission']} |
|---|
| | 463 | {$wsh->getWhere()}"; |
|---|
| | 464 | $result = $this->dbh->query($query); |
|---|
| | 465 | if (PEAR::isError($result)) { |
|---|
| | 466 | return $result; |
|---|
| | 467 | } |
|---|
| | 468 | |
|---|
| | 469 | $affected = $this->dbh->affectedRows(); |
|---|
| | 470 | if (!PEAR::isError($affected, DB_ERROR_NOT_CAPABLE)) { |
|---|
| | 471 | SGL::logMessage("deleted $affected user permissions", PEAR_LOG_DEBUG); |
|---|
| | 472 | } |
|---|
| | 473 | } |
|---|
| | 474 | |
|---|
| | 475 | function _updateUserPerms($userId, $aNewPerms, $aOptions) |
|---|
| | 476 | { |
|---|
| | 477 | SGL::logMessage(null, PEAR_LOG_DEBUG); |
|---|
| | 478 | |
|---|
| | 479 | // first get old perms |
|---|
| | 480 | $aOldPerms = $this->_getPermsByUserId($userId); |
|---|
| | 481 | if (PEAR::isError($aOldPerms)) { |
|---|
| | 482 | return $aOldPerms; |
|---|
| | 483 | } |
|---|
| | 484 | |
|---|
| | 485 | // delete revoked perms |
|---|
| | 486 | $aPermsToRemove = array_diff($aOldPerms, $aNewPerms); |
|---|
| | 487 | if (count($aPermsToRemove)) { |
|---|
| | 488 | $aDeleteOptions = array( |
|---|
| | 489 | 'userId' => $userId, |
|---|
| | 490 | 'permList' => $aPermsToRemove |
|---|
| | 491 | ); |
|---|
| | 492 | $result = $this->_deleteUserPermissions($aDeleteOptions); |
|---|
| | 493 | if (PEAR::isError($result)) { |
|---|
| | 494 | return $result; |
|---|
| | 495 | } |
|---|
| | 496 | } |
|---|
| | 497 | |
|---|
| | 498 | // add missing perms |
|---|
| | 499 | $aPermsToAdd = array_diff($aNewPerms, $aOldPerms); |
|---|
| | 500 | if (count($aPermsToAdd)) { |
|---|
| | 501 | $result = $this->_addPermsByUserId($aPermsToAdd, $userId); |
|---|
| | 502 | if (PEAR::isError($result)) { |
|---|
| | 503 | return $result; |
|---|
| | 504 | } |
|---|
| | 505 | } |
|---|
| | 506 | } |
|---|
| | 507 | |
|---|
| | 508 | function _deleteUserLogin($aLoginId) |
|---|
| | 509 | { |
|---|
| | 510 | SGL::logMessage(null, PEAR_LOG_DEBUG); |
|---|
| | 511 | |
|---|
| | 512 | $strLoginId = implode(',', $aLoginId); |
|---|
| | 513 | |
|---|
| | 514 | $query = " |
|---|
| | 515 | DELETE FROM {$this->conf['table']['login']} |
|---|
| | 516 | WHERE login_id IN ({$strLoginId})"; |
|---|
| | 517 | $result = $this->dbh->query($query); |
|---|
| | 518 | if (PEAR::isError($result)) { |
|---|
| | 519 | return $result; |
|---|
| | 520 | } |
|---|
| | 521 | } |
|---|
| | 522 | } |
|---|
| | 523 | |
|---|
| | 524 | /** |
|---|
| | 525 | * WhereStack |
|---|
| | 526 | * |
|---|
| | 527 | * @package SGL |
|---|
| | 528 | * @author Marian Seitner <tamme@ups4ever.de> |
|---|
| | 529 | */ |
|---|
| | 530 | class WhereStack |
|---|
| | 531 | { |
|---|
| | 532 | var $_aStatements = array(); |
|---|
| | 533 | |
|---|
| | 534 | var $_aConnectors; |
|---|
| | 535 | |
|---|
| | 536 | function WhereStack() |
|---|
| | 537 | { |
|---|
| | 538 | $this->_aConnectors = array( |
|---|
| | 539 | 'AND', |
|---|
| | 540 | 'OR' |
|---|
| | 541 | ); |
|---|
| | 542 | } |
|---|
| | 543 | |
|---|
| | 544 | function add($statement, $connector = 'AND') |
|---|
| | 545 | { |
|---|
| | 546 | $connector = strtoupper($connector); |
|---|
| | 547 | if (!in_array($connector, $this->_aConnectors)) { |
|---|
| | 548 | SGL::logMessage("unknown connector $connector", PEAR_LOG_ERR); |
|---|
| | 549 | return; |
|---|
| | 550 | } |
|---|
| | 551 | |
|---|
| | 552 | $this->_aStatements[] = array( |
|---|
| | 553 | $statement, |
|---|
| | 554 | $connector |
|---|
| | 555 | ); |
|---|
| | 556 | } |
|---|
| | 557 | |
|---|
| | 558 | function getWhere() |
|---|
| | 559 | { |
|---|
| | 560 | $str = "WHERE ({$this->_aStatements[0][0]})"; |
|---|
| | 561 | array_shift($this->_aStatements); |
|---|
| | 562 | |
|---|
| | 563 | foreach ($this->_aStatements as $statement) { |
|---|
| | 564 | $str .= " {$statement[1]} ({$statement[0]})"; |
|---|
| | 565 | } |
|---|
| | 566 | |
|---|
| | 567 | return $str; |
|---|
| | 568 | } |
|---|
| | 569 | } |
|---|
| | 570 | |
|---|
| | 571 | define('RC_SUCC', '1'); |
|---|
| | 572 | define('RC_FAIL', '2'); |
|---|
| | 573 | define('RC_IGNO', '3'); |
|---|
| | 574 | |
|---|
| | 575 | /** |
|---|
| | 576 | * ResultCounter |
|---|
| | 577 | * |
|---|
| | 578 | * @package SGL |
|---|
| | 579 | * @author Marian Seitner <tamme@ups4ever.de> |
|---|
| | 580 | */ |
|---|
| | 581 | class ResultCounter |
|---|
| | 582 | { |
|---|
| | 583 | var $aResults = array(); |
|---|
| | 584 | |
|---|
| | 585 | function inc($element, $type = RC_SUCC) |
|---|
| | 586 | { |
|---|
| | 587 | $this->aResults[$type][] = $element; |
|---|
| | 588 | } |
|---|
| | 589 | |
|---|
| | 590 | function get($type = null) |
|---|
| | 591 | { |
|---|
| | 592 | if (is_null($type)) { |
|---|
| | 593 | return $this->aResults; |
|---|
| | 594 | } |
|---|
| | 595 | |
|---|
| | 596 | return (isset($this->aResults[$type])) |
|---|
| | 597 | ? $this->aResults[$type] |
|---|
| | 598 | : false; |
|---|
| | 599 | } |
|---|
| | 600 | |
|---|
| | 601 | function count($type = null) |
|---|
| | 602 | { |
|---|
| | 603 | // return overall element count |
|---|
| | 604 | if (is_null($type)) { |
|---|
| | 605 | $sum = 0; |
|---|
| | 606 | foreach ($this->aResults as $aPartialResult) { |
|---|
| | 607 | $sum += count($aPartialResult); |
|---|
| | 608 | } |
|---|
| | 609 | |
|---|
| | 610 | return $sum; |
|---|
| | 611 | } |
|---|
| | 612 | |
|---|
| | 613 | return (isset($this->aResults[$type])) |
|---|
| | 614 | ? count($this->aResults[$type]) |
|---|
| | 615 | : 0; |
|---|
| | 616 | } |
|---|
| | 617 | |
|---|
| | 618 | function merge($oResultCounter) |
|---|
| | 619 | { |
|---|
| | 620 | $aMergeResults = $oResultCounter->get(); |
|---|
| | 621 | foreach ($aMergeResults as $type => $result) { |
|---|
| | 622 | $this->aResults[$type][] = $result; |
|---|
| | 623 | } |
|---|
| | 624 | } |
|---|
| | 625 | } |
|---|
| | 626 | |
|---|
| | 627 | ?> |