Documentation is available at RSDLiveUserApplication.php
1 <?php
2 // RSDEngine: The Rapid and Secure Development Engine
3 // Copyright (C) 2003 Lukas Feiler
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 /**This file is part of RSD - Rapid and Secure Development.
20 *
21 * @author Lukas Feiler
22 * @copyright Lukas Feiler 2003
23 * @package RSDEngine
24 * @version 0.1.9
25 * @filesource
26 */
27
28 /**RSDLiveUserApplication extends RSDApplication.
29 */
30 require_once('RSDEngine/RSDApplication.php');
31
32 /**RSDLiveUserApplication is the base class for any LiveUser application class.
33 *
34 * It holds handles to all objects used in this application.
35 * It can be used as the only entry point to the whole application!
36 * @author Lukas Feiler
37 * @copyright Lukas Feiler 2003
38 * @package RSDEngine
39 */
40 class RSDLiveUserApplication extends RSDApplication {
41
42 /**An instance of PEAR::LiveUser.
43 * @var LiveUser
44 */
45 var $liveUser = null;
46
47 /**An instance of one of the LiveUser perm admin classes.
48 * @var mixed
49 */
50 var $_liveUserPermAdmin = null;
51
52 /**An instance of one of the LiveUser auth admin classes.
53 * @var mixed
54 */
55 var $_liveUserAuthAdmin = null;
56
57 /**An multidimensional array of configuration options.
58 * @var array
59 */
60 var $_liveUserOptions = null;
61
62 /**Sets the property _liveUserOptions.
63 * @see _liveUserOptions
64 * @param Array $options An multidimensional array of configuration options.
65 */
66 function setLiveUserOptions($options)
67 {
68 $this->_liveUserOptions = $options;
69 }
70
71 /**Returns the property _liveUserOptions.
72 * @see _liveUserOptions
73 * @see setLiveUserOptions
74 * @return mixed An multidimensional array of configuration options
75 * or null if the property has not been set using setLiveUserOptions.
76 */
77 function getLiveUserOptions()
78 {
79 return $this->_liveUserOptions;
80 }
81
82 /**Calls a method of the LiveUser perm admin class with the specified arguments.
83 *
84 * This method loads the LiveUser perm admin class on demand to save resouces.
85 * If $this->_liveUserPermAdmin is not of the type Object it will be assigned
86 * the return value of setUpLiveUserPermAdmin.
87 * @see _liveUserPermAdmin
88 * @see setUpLiveUserPermAdmin
89 *
90 * @param String $methodName The name of the method to call.
91 * @param Array $args An array of arguments to pass to $methodName.
92 * @return mixed If the LiveUser perm admin class does not have such a method an instance of RSError is returned.
93 * Otherwise the return value of the method call is returned.
94 */
95 function &callLiveUserPermAdminMethod($methodName, $args = array())
96 {
97 if (!is_object($this->_liveUserPermAdmin)) {
98 $this->_liveUserPermAdmin =& $this->setUpLiveUserPermAdmin();
99 }
100 if (!method_exists($this->_liveUserPermAdmin, $methodName)) {
101 return RSErrorManager::raiseRSError('$className', '_liveUserPermAdmin', $methodName, "The object \$this->_liveUserPermAdmin has no such method: '$methodName'.", RSERROR_NO_SUCH_METHOD);
102 }
103 return call_user_func_array(array(&$this->_liveUserPermAdmin, $methodName), $args);
104 }
105
106 /**Calls a method of the LiveUser auth admin class with the specified arguments.
107 *
108 * This method loads the LiveUser auth admin class on demand to save resouces.
109 * If $this->_liveUserAuthAdmin is not of the type Object it will be assigned
110 * the return value of setUpLiveUserAuthAdmin.
111 * @see _liveUserAuthAdmin
112 * @see setUpLiveUserAuthAdmin
113 *
114 * @param String $methodName The name of the method to call.
115 * @param Array $args An array of arguments to pass to $methodName.
116 * @return mixed If the LiveUser auth admin class does not have such a method an instance of RSError is returned.
117 * Otherwise the return value of the method call is returned.
118 */
119 function &callLiveUserAuthAdminMethod($methodName, $args = array())
120 {
121 if (!is_object($this->_liveUserAuthAdmin)) {
122 $this->_liveUserAuthAdmin =& $this->setUpLiveUserAuthAdmin();
123 }
124 if (!method_exists($this->_liveUserAuthAdmin, $methodName)) {
125 return RSErrorManager::raiseRSError('$className', '_liveUserAuthAdmin', $methodName, "The object \$this->_liveUserAuthAdmin has no such method: '$methodName'.", RSERROR_NO_SUCH_METHOD);
126 }
127 return call_user_func_array(array(&$this->_liveUserAuthAdmin, $methodName), $args);
128 }
129
130 /**Adds a LiveUser Right including users and groups that have this right.
131 *
132 * This method combines the funtionality of the methods 'addRight',
133 * 'grantUserRight' and 'grantGroupRight' of the class LiveUser_Perm_Container_DB_Complex_Admin
134 * (by calling these methos over callLiveUserPermAdminMethod).
135 * Can be used in a defaultInert statement as follows:
136 * $this->app->addLiveUserRight($this->db->getOne("SELECT f_folderarea FROM t_folder WHERE f_folderid='" . $this->db->quote($inserts['f_folder_id']), 'EFX_DOCUMENT_LIVEUSER_SELECT_RIGHT', 'EFX_DOCUMENT_LIVEUSER_SELECT_RIGHT', array($this->app->liveUser->getUserId()), array($this->app->liveUser->getGroupIds))
137 * @see callLiveUserPermAdminMethod
138 *
139 *
140 * @param integer $areaId id of area
141 * @param string $defineName name of right constant
142 * @param string $rightName name of right
143 * @param array $users An array of user IDs that shall have this right.
144 * @param array $groups An array of group IDs that shall have this right.
145 * @param string $rightComment (optional) description of right
146 * @param boolean $hasImplied (optional) allow implied rights for this right?
147 * @param boolean $hasLevel (optional) apply levels to this right?
148 * @param boolean $hasScope (optional) apply scopes to this right?
149 * @return mixed integer (rightId) or DB Error object
150 *
151 */
152 function addLiveUserRight($areaId, $defineName, $rightName, $users = array(), $groups = array(), $rightComment = null, $hasImplied = false, $hasLevel = false, $hasScope = false)
153 {
154
155 $rightId = $this->callLiveUserPermAdminMethod(
156 'addRight',
157 array(
158 $areaId,
159 $defineName,
160 $rightName,
161 $rightComment,
162 $hasImplied,
163 $hasLevel,
164 $hasScope
165 )
166 );
167 if (PEAR::isError($rightId)) {
168 return $rightId;
169 }
170
171 if (is_array($users)) {
172 while (list($key, $userId) = each($users)) {
173 $error = $this->callLiveUserPermAdminMethod(
174 'grantUserRight',
175 array(
176 $userId,
177 $rightId,
178 1
179 )
180 );
181 if (PEAR::isError($error)) {
182 //echo $error->getMessage() . ":" . $error->toString() . "<br>";
183 //transaction rollback
184 //return;
185 }
186 }
187 } else {
188 //echo "WRONG USAGE";
189 }
190
191 if (is_array($groups)) {
192 while (list($key, $groupId) = each($groups)) {
193 $error = $this->callLiveUserPermAdminMethod(
194 'grantGroupRight',
195 array(
196 $groupId,
197 $rightId,
198 1
199 )
200 );
201 if (PEAR::isError($error)) {
202 //transaction rollback
203 //return;
204 }
205 }
206 }
207 return $rightId;
208 }
209
210 /**Adds a LiveUser group and users to that group.
211 *
212 * @param string $defineName name of group constant
213 * @param string $groupName name of group
214 * @param array $users (optional) array of user IDs that should be added to this group.
215 * @param boolean $groupComent (optional) description of group
216 * @param boolean $active (optional) activate group?
217 * @param integer $ownerUser (optional) owner_perm_user_id of group
218 * @param integer $ownerGroup (optional) owner_group_id of group
219 */
220 function addLiveUserGroup($groupName, $users = array(), $groupComment = null, $active = true, $ownerUser = 0, $ownerGroup = 0)
221 {
222 $groupId = $this->callLiveUserPermAdminMethod(
223 'addGroup',
224 array(
225 $groupName,
226 $groupName,
227 $groupComment,
228 $active,
229 $ownerUser,
230 $ownerGroup
231 )
232 );
233
234 if (PEAR::isError($groupId)) {
235 return $groupId;
236 }
237 if (is_array($users)) {
238 while (list($key, $userId) = each($users)) {
239 $success = $this->callLiveUserPermAdminMethod(
240 'addUserToGroup',
241 array(
242 $userId,
243 $groupId
244 )
245 );
246 /*
247 if (PEAR::isError($success)) {
248 echo $success->getMessage() . ":" . $success->toString() . "<br>";
249 }
250 */
251 }
252 }
253 return $groupId;
254 }
255
256 /**Adds a LiveUser Area and Area Admins.
257 *
258 * @param string $applicationId id of application
259 * @param string $defineName name of area constant
260 * @param string $areaName name of area
261 * @param array $areaAdmins An array of user IDs that shall be admins of this area.
262 * @param string $areaComment (optional) description of area
263 *
264 * @access public
265 * @return mixed integer (area_id) or DB Error object
266 */
267 function addLiveUserArea($applicationId, $defineName, $areaName, $areaAdmins = array(), $areaComment = null)
268 {
269 $areaId = $this->callLiveUserPermAdminMethod(
270 'addArea',
271 array(
272 $applicationId,
273 $defineName,
274 $areaName,
275 $areaComment
276 )
277 );
278
279 if (PEAR::isError($areaId)) {
280 return $areaId;
281 }
282
283 // create area admins here!
284 // It seems that the handling of area admins is not yet implemented in LiveUser_Perm_Container_DB_Complex_Admin.
285 if (is_array($areaAdmins)) {
286 while (list($key, $userId) = each($areaAdmins)) {
287 $this->addLiveUserAreaAdmin($userId, $areaId);
288 }
289 }
290
291 return $areaId;
292 }
293
294 /**Adds an LiveUser area admin.
295 * @param int $userId The ID of the user that shall be the area of this admin.
296 * @param int $areaId The ID of the area the user shall be the admin of.
297 * @return mixed True or an instance of DB_Error.
298 */
299 function addLiveUserAreaAdmin($userId, $areaId)
300 {
301 $success = $this->callLiveUserPermAdminMethod(
302 'setUserAreaAdmin',
303 array(
304 $userId,
305 $areaId
306 )
307 );
308 return $success;
309 }
310
311 /**Removes an LiveUser area admin.
312 * @param int $userId The ID of the user that shall be removed of the list of admins for a certain area.
313 * @param int $areaId The ID of the area the user shall removed from.
314 * @return mixed True or an instance of DB_Error.
315 */
316 function removeLiveUserAreaAdmin($userId, $areaId, $preventAdminSucide = true)
317 {
318 if ($preventAdminSucide && $this->countLiveUserAreaAdminsByAreaId($areaId) < 2) {
319 return RSErrorManager::raiseRSError('RSDLiveUserApplication', '', 'removeLiveUserAreaAdmin', "At least one user must be area admin!", RSERROR_INVALID);
320 }
321 $success = $this->callLiveUserPermAdminMethod(
322 'removeUserAreaAdmin',
323 array(
324 $userId,
325 $areaId
326 )
327 );
328 return $success;
329 }
330
331 function grantLiveUserUserRight($userId, $rightId, $levelId = 1)
332 {
333 $success = $this->callLiveUserPermAdminMethod(
334 'grantUserRight',
335 array(
336 $userId,
337 $rightId,
338 $levelId
339 )
340 );
341 return $success;
342 }
343
344 function grantLiveUserGroupRight($groupId, $rightId, $levelId = 1)
345 {
346 $success = $this->callLiveUserPermAdminMethod(
347 'grantGroupRight',
348 array(
349 $groupId,
350 $rightId,
351 $levelId
352 )
353 );
354 return $success;
355 }
356
357 function revokeLiveUserUserRight($userId, $rightId, $preventAdminSucide = true)
358 {
359 if ($preventAdminSucide && $this->countLiveUserUsersByRightId($rightId) < 2) {
360 return RSErrorManager::raiseRSError('RSDLiveUserApplication', '', 'revokeLiveUserUserRight', "At least one user must have this right!", RSERROR_INVALID);
361 }
362 $success = $this->callLiveUserPermAdminMethod(
363 'revokeUserRight',
364 array(
365 $userId,
366 $rightId,
367 )
368 );
369 return $success;
370 }
371
372 function revokeLiveUserGroupRight($groupId, $rightId, $preventAdminSucide = true)
373 {
374 if ($preventAdminSucide && $this->countLiveUserGroupsByRightId($rightId) < 2) {
375 return RSErrorManager::raiseRSError('RSDLiveUserApplication', '', 'revokeLiveUserGroupRight', "At least one group must have this right!", RSERROR_INVALID);
376 }
377 $success = $this->callLiveUserPermAdminMethod(
378 'revokeGroupRight',
379 array(
380 $groupId,
381 $rightId
382 )
383 );
384 return $success;
385 }
386
387 /**Returns all users that personally have a certain right.
388 */
389 function getLiveUserUsersByRightId($rightId, $whereActive = true, $config = false, $fields = false)
390 {
391 if ($config === false) {
392 $config = $this->getLiveUserOptions();
393 }
394 $prefix = $config['permContainer']['prefix'];
395 $authTable = $config['authContainers'][0]['authTable'];
396 $authTableUserId = $config['authContainers'][0]['authTableCols']['userId'];
397 $authTableIsActive = $config['authContainers'][0]['authTableCols']['isActive'];
398
399 $rightId = (int) $rightId;
400
401 if (!$fields) {
402 $fields = "${authTable}.*";
403 }
404
405 $query = "
406 SELECT
407 $fields
408 FROM
409 ${prefix}userrights,
410 ${prefix}perm_users,
411 ${authTable}
412 WHERE
413 ${prefix}userrights.perm_user_id = ${prefix}perm_users.perm_user_id
414 AND
415 ${prefix}perm_users.auth_user_id = ${authTable}.${authTableUserId}
416 AND
417 ${prefix}userrights.right_id = $rightId
418 ";
419
420 if ($whereActive) {
421 $query .= " AND ${authTable}.$authTableIsActive = 'Y'";
422 }
423 return $this->db->getAll($query);
424 }
425
426 /**Returns the number of users that have a certain right.
427 */
428 function countLiveUserUsersByRightId($rightId, $whereActive = true, $config = false)
429 {
430 $countRecord = $this->getLiveUserUsersByRightId($rightId, $whereActive, $config, 'COUNT(*) AS c');
431 if (PEAR::isError($countRecord)) {
432 return $countRecord;
433 }
434 return $countRecord[0]['c'];
435 }
436
437 /**Returns all users that personally do not have a certain right.
438 */
439 function getLiveUserUsersByNegativeRightId($rightId, $whereActive = true, $config = false)
440 {
441 if ($config === false) {
442 $config = $this->getLiveUserOptions();
443 }
444 $prefix = $config['permContainer']['prefix'];
445 $authTable = $config['authContainers'][0]['authTable'];
446 $authTableUserId = $config['authContainers'][0]['authTableCols']['userId'];
447 $authTableIsActive = $config['authContainers'][0]['authTableCols']['isActive'];
448
449 $rightId = (int) $rightId;
450
451 $query = "
452 SELECT
453 *
454 FROM
455 ${authTable}
456 ";
457
458 if ($whereActive) {
459 $query .= "WHERE ${authTable}.$authTableIsActive = 'Y'";
460 }
461
462 $allUserRecords = $this->db->getAll($query);
463 if (PEAR::isError($allUserRecords)) {
464 return $allUserRecords;
465 }
466 $allUsers = array();
467 reset($allUserRecords);
468 while (list($key, $user) = each($allUserRecords)) {
469 $allUsers[$user[$authTableUserId]] =& $allUserRecords[$key];
470 }
471
472 $havingRightUserRecords = $this->getLiveUserUsersByRightId($rightId, $whereActive, $config);
473 if (PEAR::isError($havingRightUserRecords)) {
474 return $havingRightUserRecords;
475 }
476
477 reset($havingRightUserRecords);
478 while (list($key, $user) = each($havingRightUserRecords)) {
479 if (isset($allUsers[$user[$authTableUserId]])) {
480 unset($allUsers[$user[$authTableUserId]]);
481 }
482 }
483
484 return $allUsers;
485 }
486
487
488 /**Returns all groups that have a certain right.
489 */
490 function getLiveUserGroupsByRightId($rightId, $whereActive = true, $config = false, $fields = false)
491 {
492 if ($config === false) {
493 $config = $this->getLiveUserOptions();
494 }
495 $prefix = $config['permContainer']['prefix'];
496 $authTable = $config['authContainers'][0]['authTable'];
497 $authTableUserId = $config['authContainers'][0]['authTableCols']['userId'];
498 $authTableIsActive = $config['authContainers'][0]['authTableCols']['isActive'];
499
500 $rightId = (int) $rightId;
501
502 if (!$fields) {
503 $fields = "${prefix}groups.*";
504 }
505 $query = "
506 SELECT
507 $fields
508 FROM
509 ${prefix}grouprights,
510 ${prefix}groups
511 WHERE
512 ${prefix}grouprights.group_id = ${prefix}groups.group_id
513 AND
514 ${prefix}grouprights.right_id = $rightId
515 ";
516
517 if ($whereActive) {
518 $query .= " AND ${prefix}groups.is_active = 'Y'";
519 }
520 return $this->db->getAll($query);
521 }
522
523 /**Returns the number of users that have a certain right.
524 */
525 function countLiveUserGroupsByRightId($rightId, $whereActive = true, $config = false)
526 {
527 $countRecord = $this->getLiveUserGroupsByRightId($rightId, $whereActive, $config, 'COUNT(*) AS c');
528 if (PEAR::isError($countRecord)) {
529 return $countRecord;
530 }
531 return $countRecord[0]['c'];
532 }
533
534 /**Returns all groups that do not have a certain right.
535 */
536 function getLiveUserGroupsByNegativeRightId($rightId, $whereActive = true, $config = false)
537 {
538 if ($config === false) {
539 $config = $this->getLiveUserOptions();
540 }
541 $prefix = $config['permContainer']['prefix'];
542 $authTable = $config['authContainers'][0]['authTable'];
543 $authTableUserId = $config['authContainers'][0]['authTableCols']['userId'];
544 $authTableIsActive = $config['authContainers'][0]['authTableCols']['isActive'];
545
546 $rightId = (int) $rightId;
547
548 $query = "
549 SELECT
550 ${prefix}groups.*
551 FROM
552 ${prefix}groups
553 ";
554
555 if ($whereActive) {
556 $query .= " WHERE ${prefix}groups.is_active = 'Y'";
557 }
558
559 $allGroupRecords = $this->db->getAll($query);
560 if (PEAR::isError($allGroupRecords)) {
561 return $allGroupRecords;
562 }
563
564 $allGroups = array();
565 reset($allGroupRecords);
566 while (list($key, $group) = each($allGroupRecords)) {
567 $allGroups[$group['group_id']] =& $allGroupRecords[$key];
568 }
569
570 $havingRightGroupRecords = $this->getLiveUserGroupsByRightId($rightId, $whereActive, $config);
571 if (PEAR::isError($havingRightGroupRecords)) {
572 return $havingRightGroupRecords;
573 }
574
575 reset($havingRightGroupRecords);
576 while (list($key, $group) = each($havingRightGroupRecords)) {
577 if (isset($allGroups[$group['group_id']])) {
578 unset($allGroups[$group['group_id']]);
579 }
580 }
581
582 return $allGroups;
583 }
584
585 /**Returns all users that are admin of a certain area.
586 */
587 function getLiveUserAreaAdminsByAreaId($areaId, $whereActive = true, $config = false, $fields = false)
588 {
589 if ($config === false) {
590 $config = $this->getLiveUserOptions();
591 }
592 $prefix = $config['permContainer']['prefix'];
593 $authTable = $config['authContainers'][0]['authTable'];
594 $authTableUserId = $config['authContainers'][0]['authTableCols']['userId'];
595 $authTableIsActive = $config['authContainers'][0]['authTableCols']['isActive'];
596
597 $areaId = (int) $areaId;
598
599 if (!$fields) {
600 $fields = "${authTable}.*";
601 }
602 $query = "
603 SELECT
604 $fields
605 FROM
606 ${authTable},
607 ${prefix}perm_users,
608 ${prefix}area_admin_areas
609 WHERE
610 ${authTable}.${authTableUserId} = ${prefix}perm_users.auth_user_id
611 AND
612 ${prefix}perm_users.perm_user_id = ${prefix}area_admin_areas.perm_user_id
613 AND
614 ${prefix}area_admin_areas.area_id = $areaId
615 ";
616
617 if ($whereActive) {
618 $query .= " AND ${authTable}.$authTableIsActive = 'Y'";
619 }
620
621 return $this->db->getAll($query);
622 }
623
624 /**Returns the number of users that are admin of a certain area.
625 */
626 function countLiveUserAreaAdminsByAreaId($areaId, $whereActive = true, $config = false, $fields = false)
627 {
628 $countRecord = $this->getLiveUserAreaAdminsByAreaId($areaId, $whereActive, $config, 'COUNT(*) AS c');
629 if (PEAR::isError($countRecord)) {
630 return $countRecord;
631 }
632 return $countRecord[0]['c'];
633 }
634
635 /**Returns all users that are not admin of a certain area.
636 */
637 function getLiveUserAreaAdminsByNegativeAreaId($areaId, $whereActive = true, $config = false)
638 {
639 if ($config === false) {
640 $config = $this->getLiveUserOptions();
641 }
642 $prefix = $config['permContainer']['prefix'];
643 $authTable = $config['authContainers'][0]['authTable'];
644 $authTableUserId = $config['authContainers'][0]['authTableCols']['userId'];
645 $authTableIsActive = $config['authContainers'][0]['authTableCols']['isActive'];
646
647 $areaId = (int) $areaId;
648
649 $query = "
650 SELECT
651 *
652 FROM
653 ${authTable}
654 ";
655
656 if ($whereActive) {
657 $query .= "WHERE ${authTable}.$authTableIsActive = 'Y'";
658 }
659
660 $allUserRecords = $this->db->getAll($query);
661 if (PEAR::isError($allUserRecords)) {
662 return $allUserRecords;
663 }
664 $allUsers = array();
665 reset($allUserRecords);
666 while (list($key, $user) = each($allUserRecords)) {
667 $allUsers[$user[$authTableUserId]] =& $allUserRecords[$key];
668 }
669
670 $areaAdminUserRecords = $this->getLiveUserAreaAdminsByAreaId($areaId, $whereActive, $config);
671 if (PEAR::isError($areaAdminUserRecords)) {
672 return $areaAdminUserRecords;
673 }
674 reset($areaAdminUserRecords);
675 while (list($key, $user) = each($areaAdminUserRecords)) {
676 if (isset($allUsers[$user[$authTableUserId]])) {
677 unset($allUsers[$user[$authTableUserId]]);
678 }
679 }
680
681 return $allUsers;
682 }
683
684 /**Whether the current user is admin of the given area.
685 * @param int $areaId The ID of the area.
686 */
687 function isLiveUserAreaAdmin($areaId, $config = false)
688 {
689 if ($config === false) {
690 $config = $this->getLiveUserOptions();
691 }
692 $prefix = $config['permContainer']['prefix'];
693 $authTable = $config['authContainers'][0]['authTable'];
694 $authTableUserId = $config['authContainers'][0]['authTableCols']['userId'];
695 $authTableIsActive = $config['authContainers'][0]['authTableCols']['isActive'];
696
697 $areaId = (int) $areaId;
698 $userId = $this->liveUser->getProperty('permUserId');
699
700 $query = "
701 SELECT
702 COUNT(*)
703 FROM
704 ${prefix}area_admin_areas
705 WHERE
706 area_id = $areaId
707 AND
708 perm_user_id = $userId
709 ";
710 $count = $this->db->getOne($query);
711 if (PEAR::isError($count)) {
712 return false;
713 }
714 return $count > 0;
715 }
716
717 function getLiveUserAreaById($areaId, $config = false)
718 {
719 if ($config === false) {
720 $config = $this->getLiveUserOptions();
721 }
722 $prefix = $config['permContainer']['prefix'];
723 $authTable = $config['authContainers'][0]['authTable'];
724 $authTableUserId = $config['authContainers'][0]['authTableCols']['userId'];
725 $authTableIsActive = $config['authContainers'][0]['authTableCols']['isActive'];
726
727 $areaId = (int) $areaId;
728 $userId = $this->liveUser->getProperty('permUserId');
729
730 $query ="
731 SELECT
732 area_id,
733 application_id,
734 area_define_name AS define_name,
735 area_define_name AS name
736 FROM
737 ${prefix}areas
738 WHERE
739 ${prefix}areas.area_id=7
740 ";
741 return $this->db->getRow($query);
742
743 //getAreas performs a join over liveuser_translations wich does
744 //not get filled with data by addArea
745 //$this->callLiveUserPermAdminMethod('setCurrentLanguage', array('de'));
746 //return $this->callLiveUserPermAdminMethod('getAreas', array($options));
747 }
748
749 function addLiveUserUserToGroup($userId, $groupId)
750 {
751 $success = $this->callLiveUserPermAdminMethod(
752 'addUserToGroup',
753 array(
754 $userId,
755 $groupId
756 )
757 );
758 return $success;
759 }
760
761 function removeLiveUserUserFromGroup($userId, $groupId, $preventAdminSucide = true)
762 {
763 if ($preventAdminSucide && $this->countLiveUserUsersByGroupId($groupId) < 2) {
764 return RSErrorManager::raiseRSError('RSDLiveUserApplication', '', 'removeLiveUserUserFromGroup', "At least one user must be member of a group!", RSERROR_INVALID);
765 }
766 $success = $this->callLiveUserPermAdminMethod(
767 'removeUserFromGroup',
768 array(
769 $userId,
770 $groupId
771 )
772 );
773 return $success;
774 }
775
776 function getLiveUserGroupById($groupId, $config = false)
777 {
778 if ($config === false) {
779 $config = $this->getLiveUserOptions();
780 }
781 $prefix = $config['permContainer']['prefix'];
782 $authTable = $config['authContainers'][0]['authTable'];
783 $authTableUserId = $config['authContainers'][0]['authTableCols']['userId'];
784 $authTableIsActive = $config['authContainers'][0]['authTableCols']['isActive'];
785
786 $groupId = (int) $groupId;
787
788 $query ="
789 SELECT
790 group_id,
791 owner_perm_user_id,
792 owner_group_id,
793 '' AS name,
794 '' AS comment,
795 is_active
796 FROM
797 ${prefix}groups
798 WHERE
799 ${prefix}groups.group_id=7
800 ";
801 return $this->db->getRow($query);
802
803 //getGroups performs a join over liveuser_translations wich does
804 //not get filled with data by addGroup
805 }
806
807 function countLiveUserUsersByGroupId($groupId, $whereActive = true, $config = false)
808 {
809 $countRecord = $this->getLiveUserUsersByGroupid($groupId, $whereActive, $config, 'COUNT(*) AS c');
810 if (PEAR::isError($countRecord)) {
811 return $countRecord;
812 }
813 return $countRecord[0]['c'];
814 }
815
816 /**Returns all users that are member of a certain group.
817 */
818 function getLiveUserUsersByGroupid($groupId, $whereActive = true, $config = false, $fields = false)
819 {
820 if ($config === false) {
821 $config = $this->getLiveUserOptions();
822 }
823 $prefix = $config['permContainer']['prefix'];
824 $authTable = $config['authContainers'][0]['authTable'];
825 $authTableUserId = $config['authContainers'][0]['authTableCols']['userId'];
826 $authTableIsActive = $config['authContainers'][0]['authTableCols']['isActive'];
827
828 $groupId = (int) $groupId;
829 if (!$fields) {
830 $fields ="${authTable}.*";
831 }
832 return $this->db->getAll("
833 SELECT
834 $fields
835 FROM
836 ${authTable},
837 ${prefix}perm_users,
838 ${prefix}groupusers
839 WHERE
840 ${authTable}.${authTableUserId} = ${prefix}perm_users.auth_user_id
841 AND
842 ${prefix}perm_users.perm_user_id = ${prefix}groupusers.perm_user_id
843 AND
844 ${prefix}groupusers.group_id = $groupId
845 ");
846
847 }
848
849 /**Returns all users that are not admin of a certain area.
850 */
851 function getLiveUserUsersByNegativeGroupId($groupId, $whereActive = true, $config = false)
852 {
853 if ($config === false) {
854 $config = $this->getLiveUserOptions();
855 }
856 $prefix = $config['permContainer']['prefix'];
857 $authTable = $config['authContainers'][0]['authTable'];
858 $authTableUserId = $config['authContainers'][0]['authTableCols']['userId'];
859 $authTableIsActive = $config['authContainers'][0]['authTableCols']['isActive'];
860
861 $groupId = (int) $groupId;
862
863 $query = "
864 SELECT
865 *
866 FROM
867 ${authTable}
868 ";
869
870 if ($whereActive) {
871 $query .= "WHERE ${authTable}.$authTableIsActive = 'Y'";
872 }
873
874 $allUserRecords = $this->db->getAll($query);
875 if (PEAR::isError($allUserRecords)) {
876 return $allUserRecords;
877 }
878 $allUsers = array();
879 reset($allUserRecords);
880 while (list($key, $user) = each($allUserRecords)) {
881 $allUsers[$user[$authTableUserId]] =& $allUserRecords[$key];
882 }
883
884 $groupUserRecords = $this->getLiveUserUsersByGroupId($groupId, $whereActive, $config);
885 if (PEAR::isError($groupUserRecords)) {
886 return $groupUserRecords;
887 }
888
889 reset($groupUserRecords);
890 while (list($key, $user) = each($groupUserRecords)) {
891 if (isset($allUsers[$user[$authTableUserId]])) {
892 unset($allUsers[$user[$authTableUserId]]);
893 }
894 }
895
896 return $allUsers;
897 }
898
899
900 function isAdmin()
901 {
902 return $this->liveUser->checkRight(-1);
903 }
904
905 function getUserId()
906 {
907 return $this->liveUser->getProperty('permUserId');
908 }
909
910 function getAuthUserId()
911 {
912 return $this->liveUser->getProperty('authUserId');
913 }
914
915 function userIsInGroup($userId, $groupId, $config = false)
916 {
917 if ($config === false) {
918 $config = $this->getLiveUserOptions();
919 }
920 $prefix = $config['permContainer']['prefix'];
921 $authTable = $config['authContainers'][0]['authTable'];
922 $authTableUserId = $config['authContainers'][0]['authTableCols']['userId'];
923 $authTableIsActive = $config['authContainers'][0]['authTableCols']['isActive'];
924
925 $groupId = (int) $groupId;
926 $userId = (int) $userId;
927 $count = $this->db->getOne("
928 SELECT
929 COUNT(*)
930 FROM
931 ${prefix}groupusers
932 WHERE
933 ${prefix}groupusers.perm_user_id = $userId
934 AND
935 ${prefix}groupusers.group_id = $groupId
936 ");
937
938 if (PEAR::isError($count)) {
939 return $count;
940 }
941 return $count > 0;
942 }
943
944 function isInGroup($groupId)
945 {
946 return $this->userIsInGroup($this->getUserId(), $groupId);
947 }
948
949 /**Adds a new user in the auth and the perm container.
950 * @return True or an PEAR_Error
951 */
952 function addLiveUserUser($username, $password, $activated, $userTypeId = LIVEUSER_USER_TYPE_ID)
953 {
954 $authId = $this->callLiveUserAuthAdminMethod(
955 'addUser',
956 array($username, $password, $activated)
957 );
958
959 if (PEAR::isError($authId)) {
960 return $authId;
961 }
962
963 if (is_numeric($authId)) {
964 $permId = $authId;
965 } else {
966 $permId = null;
967 }
968
969 $permId = $this->callLiveUserPermAdminMethod(
970 'addUser',
971 array($authId, $userTypeId, $permId)
972 );
973
974 if (PEAR::isError($permId)) {
975 //perform a pseudo rollback
976 $this->callLiveUserAuthAdminMethod(
977 'removeUser',
978 array($authId)
979 );
980
981 return $permId;
982 }
983
984 return $permId;
985 }
986
987 /**Removes a user from the auth and the perm container.
988 * @return True or an PEAR_Error
989 */
990 function removeLiveUserUser($permId)
991 {
992 $authId = $this->callLiveUserPermAdminMethod('_getAuthUserId', array($permId));
993 if (PEAR::isError($authId)) {
994 return $authId;
995 }
996 if ($authId == '') {
997 return RSErrorManager::raiseRSError('RSDLiveUserApplication', '', 'removeLiveUserUser', "There is no such user in the permission container.", RSERROR_NO_SUCH_METHOD);
998 }
999 $success = $this->callLiveUserAuthAdminMethod(
1000 'removeUser',
1001 array($authId)
1002 );
1003
1004 if (PEAR::isError($success)) {
1005 return $success;
1006 }
1007
1008 $success = $this->callLiveUserPermAdminMethod(
1009 'removeUser',
1010 array($authId)
1011 );
1012
1013 //we cannot care if we could not remove the user from the perm container
1014 return true;
1015 }
1016 }
Documentation generated on Mon, 8 Dec 2003 13:12:57 +0100 by phpDocumentor 1.2.3