00001 <?php
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 require_once FASTFRAME_ROOT . 'lib/FastFrame/DataAccess.php';
00026
00027
00028
00029
00033 define('PERMS_READ', 'READ');
00034 define('PERMS_EDIT', 'EDIT');
00035 define('PERMS_DELETE', 'DELETE');
00036
00037
00038
00039
00050
00051 class FF_DataAccess_Permissions_pgsql extends FF_DataAccess {
00052
00053
00058 var $groupDataTable;
00059
00064 var $userGroupsTable;
00065
00070 var $objectsTable;
00071
00076 var $userId;
00077
00078
00079
00080
00087 function FF_DataAccess_Permissions_pgsql()
00088 {
00089 FF_DataAccess::FF_DataAccess();
00090 $this->table = $this->o_registry->getConfigParam('data/groups_table', 'perms_groups', 'permissions');
00091 $this->groupDataTable = $this->o_registry->getConfigParam('data/group_data_table', 'perms_group_data', 'permissions');
00092 $this->userGroupsTable = $this->o_registry->getConfigParam('data/user_groups_table', 'perms_user_groups', 'permissions');
00093 $this->objectsTable = $this->o_registry->getConfigParam('data/objects_table', 'perms_objects', 'permissions');
00094 $this->userId = FF_Auth::getCredential('userId');
00095 }
00096
00097
00098
00099
00108 function update($in_data)
00109 {
00110 $o_result = new FF_Result();
00111 $s_query = sprintf('UPDATE %s (name, description) VALUES (%s, %s) WHERE id = %s',
00112 $this->table,
00113 $this->o_data->quoteSmart($in_data['name']),
00114 $this->o_data->quoteSmart($in_data['description']),
00115 $in_data['id']);
00116
00117 if (DB::isError($result = $this->o_data->query($s_query))) {
00118 $o_result->addMessage($result->getMessage());
00119 $o_result->setSuccess(false);
00120 return $o_result;
00121 }
00122
00123
00124 $s_query = sprintf('DELETE FROM %s WHERE group_id = %s',
00125 $this->groupDataTable, $in_data['id']);
00126
00127 if (DB::isError($result = $this->o_data->query($s_query))) {
00128 $o_result->addMessage($result->getMessage());
00129 $o_result->setSuccess(false);
00130 return $o_result;
00131 }
00132
00133 $this->_addPermissionData($in_data['id'], $in_data['perms'], $o_result);
00134 return $o_result;
00135 }
00136
00137
00138
00139
00148 function add($in_data)
00149 {
00150 $o_result = new FF_Result();
00151 $s_query = sprintf('INSERT INTO %s (id, name, description, creator_id, created_date) VALUES (%s, %s, %s, %s, NOW())',
00152 $this->table,
00153 $in_data['id'],
00154 $this->o_data->quoteSmart($in_data['name']),
00155 $this->o_data->quoteSmart($in_data['description']),
00156 $this->o_data->quoteSmart($this->userId));
00157
00158 if (DB::isError($result = $this->o_data->query($s_query))) {
00159 $o_result->addMessage($result->getMessage());
00160 $o_result->setSuccess(false);
00161 return $o_result;
00162 }
00163
00164 $this->_addPermissionData($in_data['id'], $in_data['perms'], $o_result);
00165 return $o_result;
00166 }
00167
00168
00169
00170
00180 function remove($in_id)
00181 {
00182 $o_result =& parent::remove($in_id);
00183 if (!$o_result->isSuccess()) {
00184
00185 return $o_result;
00186 }
00187
00188 $s_query = sprintf('DELETE FROM %s WHERE group_id = %s',
00189 $this->groupDataTable, $in_id);
00190
00191 if (DB::isError($result = $this->o_data->query($s_query))) {
00192 $o_result->addMessage($result->getMessage());
00193 $o_result->setSuccess(false);
00194 }
00195
00196 $s_query = sprintf('DELETE FROM %s WHERE group_id = %s',
00197 $this->userGroupsTable, $in_id);
00198
00199 if (DB::isError($result = $this->o_data->query($s_query))) {
00200 $o_result->addMessage($result->getMessage());
00201 $o_result->setSuccess(false);
00202 }
00203
00204 return $o_result;
00205 }
00206
00207
00208
00209
00219 function getDataByPrimaryKey($in_id)
00220 {
00221 $a_data = parent::getDataByPrimaryKey($in_id);
00222 if (count($a_data) == 0) {
00223 return array();
00224 }
00225
00226 $s_query = sprintf('SELECT permission, application FROM %s WHERE group_id = %s ORDER BY application',
00227 $this->groupDataTable, $in_id);
00228
00229 if (DB::isError($result = $this->o_data->getAll($s_query))) {
00230 return array();
00231 }
00232
00233 $a_data['perms'] = $result;
00234 return $a_data;
00235 }
00236
00237
00238
00239
00251 function getPermCount($in_perm, $in_app, $in_userId)
00252 {
00253 if (is_array($in_perm)) {
00254 $s_where = '';
00255 foreach ($in_perm as $s_perm) {
00256 $s_where .= sprintf('t2.permission = %s OR ', $this->o_data->quoteSmart($s_perm));
00257 }
00258
00259 $s_where .= '0';
00260 }
00261 else {
00262 $s_where = sprintf('t2.permission = %s', $this->o_data->quoteSmart($in_perm));
00263 }
00264
00265 $s_query = sprintf('SELECT COUNT(*) FROM %s AS t1
00266 INNER JOIN %s AS t2 ON t1.group_id = t2.group_id
00267 WHERE t1.user_id = %s AND t2.application = %s AND (%s)',
00268 $this->userGroupsTable,
00269 $this->groupDataTable,
00270 $this->o_data->quoteSmart($in_userId),
00271 $this->o_data->quoteSmart($in_app),
00272 $s_where);
00273
00274 if (DB::isError($result = $this->o_data->getOne($s_query))) {
00275 return 0;
00276 }
00277
00278 return $result;
00279 }
00280
00281
00282
00283
00292 function updateUserGroups($in_data)
00293 {
00294 $o_result = new FF_Result();
00295 $this->removeUserGroups($in_data['user_id'], $o_result);
00296 if (!$o_result->isSuccess()) {
00297 return $o_result;
00298 }
00299
00300 foreach ($in_data['groups'] as $s_groupId) {
00301 $s_query = sprintf('INSERT INTO %s (user_id, group_id) VALUES (%s, %s)',
00302 $this->userGroupsTable,
00303 $in_data['user_id'],
00304 $this->o_data->quoteSmart($s_groupId));
00305
00306 if (DB::isError($result = $this->o_data->query($s_query))) {
00307 $o_result->addMessage($result->getMessage());
00308 $o_result->setSuccess(false);
00309 }
00310 }
00311
00312 return $o_result;
00313 }
00314
00315
00316
00317
00327 function removeUserGroups($in_userId, &$in_resultObj)
00328 {
00329 $s_query = sprintf('DELETE FROM %s WHERE user_id = %s',
00330 $this->userGroupsTable, $in_userId);
00331
00332 if (DB::isError($result = $this->o_data->query($s_query))) {
00333 $in_resultObj->addMessage($result->getMessage());
00334 $in_resultObj->setSuccess(false);
00335 }
00336
00337 return $in_resultObj;
00338 }
00339
00340
00341
00342
00357 function getObjectPerms($in_objectId, $in_module, $in_perm, $in_getGroups)
00358 {
00359 if (is_null($in_objectId)) {
00360 return array();
00361 }
00362
00363 $s_query = sprintf('SELECT owner_id FROM %s
00364 WHERE object_id = %s AND app =%s AND module = %s AND perm = %s AND owner_is_group = %s',
00365 $this->objectsTable,
00366 $in_objectId,
00367 $this->o_data->quoteSmart($this->o_registry->getCurrentApp()),
00368 $this->o_data->quoteSmart($in_module),
00369 $this->o_data->quoteSmart($in_perm),
00370 $this->o_data->quoteSmart($this->boolToScalar($in_getGroups)));
00371
00372 return $this->o_data->getCol($s_query, 0);
00373 }
00374
00375
00376
00377
00388 function saveObjectPerm($in_data, &$in_result)
00389 {
00390 $s_query = sprintf('INSERT INTO %s (object_id, app, module, perm, owner_id, owner_is_group) VALUES (%s, %s, %s, %s, %s, %s)',
00391 $this->objectsTable,
00392 $this->o_data->quoteSmart($in_data['object_id']),
00393 $this->o_data->quoteSmart($in_data['app']),
00394 $this->o_data->quoteSmart($in_data['module']),
00395 $this->o_data->quoteSmart($in_data['perm']),
00396 $this->o_data->quoteSmart($in_data['owner_id']),
00397 $this->o_data->quoteSmart($this->boolToScalar($in_data['owner_is_group'])));
00398
00399 if (DB::isError($result = $this->o_data->query($s_query))) {
00400 $in_result->addMessage($result->getMessage());
00401 $in_result->setSuccess(false);
00402 }
00403 }
00404
00405
00406
00407
00419 function removeObjectPerms($in_objectId, $in_app, $in_module, &$in_result)
00420 {
00421 $s_query = sprintf('DELETE FROM %s WHERE object_id = %s AND app = %s AND module = %s',
00422 $this->objectsTable,
00423 $this->o_data->quoteSmart($in_objectId),
00424 $this->o_data->quoteSmart($in_app),
00425 $this->o_data->quoteSmart($in_module));
00426
00427 if (DB::isError($result = $this->o_data->query($s_query))) {
00428 $in_result->addMessage($result->getMessage());
00429 $in_result->setSuccess(false);
00430 }
00431 }
00432
00433
00434
00435
00442 function getAllGroups()
00443 {
00444 $s_query = sprintf('SELECT id, name FROM %s', $this->table);
00445 return $this->o_data->getAssoc($s_query);
00446 }
00447
00448
00449
00450
00459 function getUserGroups($in_userId)
00460 {
00461 $s_query = sprintf('SELECT group_id FROM %s WHERE user_id = %s',
00462 $this->userGroupsTable, $in_userId);
00463
00464 return $this->o_data->getCol($s_query, 0);
00465 }
00466
00467
00468
00469
00479 function getUsersInGroup($in_groupId)
00480 {
00481 if (is_null($in_groupId)) {
00482 return array();
00483 }
00484
00485 $s_profileTable = $this->o_registry->getConfigParam('data/table', 'profile', 'profile');
00486 $s_query = sprintf('SELECT user_id, username FROM %s
00487 INNER JOIN %s ON user_id = id WHERE group_id = %s',
00488 $this->userGroupsTable, $s_profileTable, $in_groupId);
00489
00490 return $this->o_data->getAssoc($s_query);
00491 }
00492
00493
00494
00495
00506 function _addPermissionData($in_id, $in_data, &$in_resultObj)
00507 {
00508
00509 foreach ($in_data as $a_permData) {
00510 $s_query = sprintf('INSERT INTO %s (group_id, permission, application) VALUES (%s, %s, %s)',
00511 $this->groupDataTable,
00512 $in_id,
00513 $this->o_data->quoteSmart($a_permData['permission']),
00514 $this->o_data->quoteSmart($a_permData['application']));
00515
00516 if (DB::isError($result = $this->o_data->query($s_query))) {
00517 $in_resultObj->addMessage($result->getMessage());
00518 $in_resultObj->setSuccess(false);
00519 return;
00520 }
00521 }
00522 }
00523
00524
00525 }
00526 ?>