3 * FusionForge role-based access control
5 * Copyright 2004, GForge, LLC
6 * Copyright 2009-2010, Roland Mas
7 * http://fusionforge.org
9 * This file is part of FusionForge. FusionForge is free software;
10 * you can redistribute it and/or modify it under the terms of the
11 * GNU General Public License as published by the Free Software
12 * Foundation; either version 2 of the Licence, or (at your option)
15 * FusionForge is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 require "PFO-RBAC.interface.php";
27 define ('USE_PFO_RBAC', true);
29 // Code shared between classes
32 * TODO: RBAC::BaseRole Enter description here ...
35 abstract class BaseRole extends Error {
37 * TODO: Enter description here ...
42 * TODO: Enter description here ...
47 * TODO: Enter description here ...
53 // var $setting_array;
55 public function BaseRole() {
56 // TODO: document these tables
57 // $gfcommon.'include/rbac_texts.php' may provide some hints...
58 $this->role_values = array(
59 'forge_admin' => array(0, 1),
60 'approve_projects' => array(0, 1),
61 'approve_news' => array(0, 1),
62 'forge_stats' => array(0, 1, 2),
64 'project_read' => array(0, 1),
65 'project_admin' => array(0, 1),
67 'tracker_admin' => array(0, 1),
68 'pm_admin' => array(0, 1),
69 'forum_admin' => array(0, 1),
71 'tracker' => array(0, 1, 9, 11, 13, 15),
72 'pm' => array(0, 1, 3, 5, 7),
73 'forum' => array(0, 1, 2, 3, 4),
75 'new_tracker' => array(0, 1, 9, 11, 13, 15),
76 'new_pm' => array(0, 1, 3, 5, 7),
77 'new_forum' => array(0, 1, 2, 3, 4),
79 'scm' => array (0, 1, 2),
80 'docman' => array (0, 1, 2, 3, 4),
81 'frs' => array (0, 1, 2, 3),
83 // 'webcal' => array(0, 1, 2),
87 $this->global_settings = array(
88 'forge_admin', // “God mode”: all actions allowed
89 'approve_projects', // Ability to approve pending projects
90 'approve_news', // Ability to approve news bits to the forge front page
94 // TODO: document these (Project-related permissions ?)
95 $this->defaults = array(
96 'Admin' => array( 'project_admin'=> 1,
103 'tracker_admin' => 1,
109 'Senior Developer' => array( 'project_read' => 1,
115 'tracker_admin' => 1,
121 'Junior Developer' => array( 'project_read' => 1,
130 'Doc Writer' => array( 'project_read' => 1,
138 'Support Tech' => array( 'project_read' => 1,
142 'tracker_admin' => 1,
151 public function getUsers() {
154 public function hasUser($user) {
155 throw new Exception ("Not implemented") ;
157 function hasGlobalPermission($section, $action = NULL) {
158 return $this->hasPermission ($section, -1, $action) ;
160 public function getSettings() {
161 throw new Exception ("Not implemented") ;
163 public function setSettings($data) {
164 throw new Exception ("Not implemented") ;
166 public function delete () {
167 throw new Exception ("Not implemented") ;
171 * getLinkedProjects - List of projects referencing that role
173 * Includes the home project (for roles that have one)
175 * @return array Array of Group objects
177 public function getLinkedProjects() {
180 $hp = $this->getHomeProject();
182 $ids[] = $hp->getID();
185 $res = db_query_params('SELECT group_id FROM role_project_refs WHERE role_id=$1',
186 array($this->getID()));
188 while ($arr = db_fetch_array ($res)) {
189 $ids[] = $arr['group_id'];
193 return group_get_objects(array_unique($ids));
196 function linkProject ($project) { // From the PFO spec
198 $hp = $this->getHomeProject();
199 if ($hp != NULL && $hp->getID() == $project->getID()) {
200 $this->setError(_("Can't link to home project"));
204 $res = db_query_params('SELECT group_id FROM role_project_refs WHERE role_id=$1 AND group_id=$2',
205 array($this->getID(),
208 if (db_numrows($res)) {
211 $res = db_query_params('INSERT INTO role_project_refs (role_id, group_id) VALUES ($1, $2)',
212 array($this->getID(),
214 if (!$res || db_affected_rows($res) < 1) {
215 $this->setError('linkProject('.$project->getID().') '.db_error());
219 $this->normalizeData();
221 foreach ($this->getUsers() as $u) {
222 if (!$SYS->sysCheckCreateUser($u->getID())) {
223 $this->setError($SYS->getErrorMessage());
231 function unlinkProject($project) { // From the PFO spec
233 $hp = $this->getHomeProject();
234 if ($hp != NULL && $hp->getID() == $project->getID()) {
235 $this->setError (_("Can't unlink from home project"));
239 $res = db_query_params('DELETE FROM role_project_refs WHERE role_id=$1 AND group_id=$2',
240 array($this->getID(),
243 $this->setError('unlinkProject('.$project->getID().') '.db_error());
247 $this->removeObsoleteSettings ();
249 foreach ($this->getUsers() as $u) {
250 if (!$SYS->sysCheckCreateUser($u->getID())) {
251 $this->setError($SYS->getErrorMessage());
260 * fetchData - May need to refresh database fields.
262 * If an update occurred and you need to access the updated info.
264 * @return boolean success;
266 function fetchData($role_id) {
267 unset($this->data_array);
268 unset($this->setting_array);
269 unset($this->perms_array);
271 $res = db_query_params('SELECT * FROM pfo_role WHERE role_id=$1',
273 if (!$res || db_numrows($res) < 1) {
274 $this->setError('BaseRole::fetchData()::'.db_error());
277 $this->data_array = db_fetch_array($res);
278 if ($this->data_array['is_public'] == 't') {
279 $this->data_array['is_public'] = true;
281 $this->data_array['is_public'] = false;
283 $res = db_query_params('SELECT section_name, ref_id, perm_val FROM pfo_role_setting WHERE role_id=$1',
286 $this->setError('BaseRole::fetchData()::'.db_error());
289 // TODO: document perms_array
290 $this->perms_array=array();
291 while ($arr = db_fetch_array($res)) {
292 $this->perms_array[$arr['section_name']][$arr['ref_id']] = $arr['perm_val'];
298 function setSetting ($section, $reference, $value) {
299 $cur = $this->getSettingRaw($section, $reference);
300 if (($value == $cur) && ($cur != NULL)) {
304 $role_id = $this->getID () ;
306 $res = db_query_params ('DELETE FROM pfo_role_setting WHERE role_id=$1 AND section_name=$2 AND ref_id=$3',
311 $res = db_query_params ('INSERT INTO pfo_role_setting (role_id, section_name, ref_id, perm_val) VALUES ($1, $2, $3, $4)',
318 function getSettingsForProject ($project) {
320 $group_id = $project->getID() ;
322 $sections = array ('project_read', 'project_admin', 'frs', 'scm', 'docman', 'tracker_admin', 'new_tracker', 'forum_admin', 'new_forum', 'pm_admin', 'new_pm') ;
323 foreach ($sections as $section) {
324 $result[$section][$group_id] = $this->getVal ($section, $group_id) ;
327 $atf = new ArtifactTypeFactory ($project) ;
328 $tids = $atf->getAllArtifactTypeIds () ;
329 foreach ($tids as $tid) {
330 $result['tracker'][$tid] = $this->getVal ('tracker', $tid) ;
332 $sections[] = 'tracker' ;
334 $ff = new ForumFactory ($project) ;
335 $fids = $ff->getAllForumIds () ;
336 foreach ($fids as $fid) {
337 $result['forum'][$fid] = $this->getVal ('forum', $fid) ;
339 $sections[] = 'forum' ;
341 $pgf = new ProjectGroupFactory ($project) ;
342 $pgids = $pgf->getAllProjectGroupIds () ;
343 foreach ($pgids as $pgid) {
344 $result['pm'][$pgid] = $this->getVal ('pm', $pgid) ;
349 // Add settings not yet listed so far (probably plugins)
350 // Currently handled:
351 // - global settings (ignored here)
352 // - project-wide settings (core and plugins)
353 // - settings for multiple-instance tools coming from the core (trackers/pm/forums)
355 // - settings for multiple-instance tools from plugins
356 foreach (array_keys ($this->perms_array) as $section) {
357 if (!in_array ($section, $sections)) {
358 if (!in_array ($section, $this->global_settings)) {
359 $result[$section][$group_id] = $this->getVal ($section, $group_id) ;
368 * TODO: Enter description here ...
371 function getGlobalSettings () {
374 $sections = array ('forge_admin', 'forge_stats', 'approve_projects', 'approve_news') ;
375 foreach ($sections as $section) {
376 $result[$section][-1] = $this->getVal($section, -1) ;
378 // Add settings not yet listed so far (probably plugins)
379 foreach (array_keys ($this->perms_array) as $section) {
380 if (!in_array ($section, $sections)) {
381 if (in_array ($section, $this->global_settings)) {
382 $result[$section][-1] = $this->getVal ($section, -1) ;
391 * TODO: Enter description here ...
392 * @param unknown_type $section
393 * @param unknown_type $reference
394 * @return number|boolean
396 function getSetting($section, $reference) {
397 $r = $this->getSettingRaw($section, $reference);
404 function getSettingRaw($section, $reference) {
405 if (isset ($this->perms_array[$section][$reference])) {
406 $value = $this->perms_array[$section][$reference] ;
419 case 'approve_projects':
421 if ($this->hasGlobalPermission('forge_admin')) {
428 if ($this->hasGlobalPermission('forge_admin')) {
434 case 'project_admin':
435 if ($this->hasGlobalPermission('forge_admin')) {
442 case 'tracker_admin':
445 if ($this->hasPermission('project_admin', $reference)) {
452 if ($this->hasPermission('project_admin', $reference)) {
459 if ($this->hasPermission('project_admin', $reference)) {
466 if ($this->hasPermission('project_admin', $reference)) {
473 if ($this->hasPermission('forum_admin', forum_get_groupid($reference))) {
479 if ($this->hasPermission('forum_admin', $reference)) {
486 if ($this->hasPermission('tracker_admin', artifacttype_get_groupid($reference))) {
492 if ($this->hasPermission('tracker_admin', $reference)) {
499 if ($this->hasPermission('pm_admin', projectgroup_get_groupid($reference))) {
505 if ($this->hasPermission('pm_admin', $reference)) {
511 $hook_params = array ();
512 $hook_params['role'] = $this ;
513 $hook_params['section'] = $section ;
514 $hook_params['reference'] = $reference ;
515 $hook_params['value'] = $value ;
516 $hook_params['result'] = NULL ;
517 plugin_hook_by_reference ("role_get_setting", $hook_params);
518 return $hook_params['result'] ;
524 * getVal - get a value out of the array of settings for this role.
526 * @param string The name of the role.
527 * @param integer The ref_id (ex: group_artifact_id, group_forum_id) for this item.
528 * @return integer The value of this item.
530 function getVal($section, $ref_id) {
531 global $role_default_array;
535 return $this->getSetting($section, $ref_id) ;
539 * &getRoleVals - get all the values and language text strings for this section.
541 * @return array Assoc array of values for this section.
543 function &getRoleVals($section) {
544 global $role_vals, $rbac_permission_names;
545 setup_rbac_strings () ;
548 // Optimization - save array so it is only built once per page view
550 if (!isset($role_vals[$section])) {
552 for ($i=0; $i<count($this->role_values[$section]); $i++) {
554 // Build an associative array of these key values + localized description
556 $role_vals[$section][$this->role_values[$section][$i]] =
557 util_ifsetor($rbac_permission_names["$section".$this->role_values[$section][$i]],
558 _('UNKNOWN (internal error, report bug to FusionForge)'));
561 return $role_vals[$section];
564 function hasPermission($section, $reference, $action = NULL) {
567 $value = $this->getSetting ($section, $reference) ;
574 case 'approve_projects':
576 case 'project_admin':
578 case 'tracker_admin':
581 return ($value >= 1) ;
587 return ($value >= 1) ;
590 return ($value >= 2) ;
598 return ($value >= 1) ;
601 return ($value >= 2) ;
609 return ($value >= 1) ;
612 return ($value >= 2) ;
615 return ($value >= 3) ;
618 return ($value >= 4) ;
626 return ($value >= 1) ;
629 return ($value >= 2) ;
632 return ($value >= 3) ;
641 return ($value >= 1) ;
644 return ($value >= 2) ;
646 case 'unmoderated_post':
647 return ($value >= 3) ;
650 return ($value >= 4) ;
659 return (($value & 1) != 0) ;
662 return (($value & 2) != 0) ;
665 return (($value & 4) != 0) ;
668 return (($value & 8) != 0) ;
671 * bit4 (value & 16) is reserved
672 * for tracker item vote from Evolvis
681 return (($value & 1) != 0) ;
684 return (($value & 2) != 0) ;
687 return (($value & 4) != 0) ;
692 $hook_params = array ();
693 $hook_params['section'] = $section ;
694 $hook_params['reference'] = $reference ;
695 $hook_params['action'] = $action ;
696 $hook_params['value'] = $value ;
697 $hook_params['result'] = false ;
698 plugin_hook_by_reference ("role_has_permission", $hook_params);
699 return $hook_params['result'] ;
705 * update - update a role in the database.
707 * @param string The name of the role.
708 * @param array A multi-dimensional array of data in this format: $data['section_name']['ref_id']=$val
709 * @param boolean Perform permission checking
710 * @return boolean True on success or false on failure.
712 function update($role_name,$data,$check_perms=true) {
715 if ($this->getHomeProject() == NULL) {
716 if (!forge_check_global_perm ('forge_admin')) {
717 $this->setPermissionDeniedError();
720 } elseif (!forge_check_perm ('project_admin', $this->getHomeProject()->getID())) {
721 $this->setPermissionDeniedError();
729 if ($role_name != $this->getName()) {
730 $this->setName($role_name) ;
733 foreach ($data as $sect => $refs) {
734 foreach ($refs as $refid => $value) {
735 $this->setSetting ($sect, $refid, $value) ;
739 $hook_params = array ();
740 $hook_params['role'] =& $this;
741 $hook_params['role_id'] = $this->getID();
742 $hook_params['data'] = $data;
743 plugin_hook ("role_update", $hook_params);
747 $this->fetchData($this->getID());
749 foreach ($this->getUsers() as $u) {
750 if (!$SYS->sysCheckCreateUser($u->getID())) {
751 $this->setError($SYS->getErrorMessage());
759 function getDisplayableName($group = NULL) {
760 if ($this->getHomeProject() == NULL) {
761 return sprintf (_('%s (global role)'),
763 } elseif ($group == NULL
764 || $this->getHomeProject()->getID() != $group->getID()) {
765 return sprintf (_('%s (in project %s)'),
767 $this->getHomeProject()->getPublicName()) ;
769 return $this->getName () ;
773 function removeObsoleteSettings () {
776 // Remove obsolete project-wide settings
777 $sections = array ('project_read', 'project_admin', 'frs', 'scm', 'docman', 'tracker_admin', 'new_tracker', 'forum_admin', 'new_forum', 'pm_admin', 'new_pm', 'webcal') ;
778 db_query_params ('DELETE FROM pfo_role_setting where role_id=$1 AND section_name=ANY($2) and ref_id NOT IN (SELECT home_group_id FROM pfo_role WHERE role_id=$1 AND home_group_id IS NOT NULL UNION SELECT group_id from role_project_refs WHERE role_id=$1)',
779 array ($this->getID(),
780 db_string_array_to_any_clause($sections))) ;
783 // Remove obsolete settings for multiple-instance tools
784 db_query_params ('DELETE FROM pfo_role_setting where role_id=$1 AND section_name=$2 and ref_id NOT IN (SELECT group_artifact_id FROM artifact_group_list WHERE group_id IN (SELECT home_group_id FROM pfo_role WHERE role_id=$1 AND home_group_id IS NOT NULL UNION SELECT group_id from role_project_refs WHERE role_id=$1))',
785 array ($this->getID(),
787 db_query_params ('DELETE FROM pfo_role_setting where role_id=$1 AND section_name=$2 and ref_id NOT IN (SELECT group_project_id FROM project_group_list WHERE group_id IN (SELECT home_group_id FROM pfo_role WHERE role_id=$1 AND home_group_id IS NOT NULL UNION SELECT group_id from role_project_refs WHERE role_id=$1))',
788 array ($this->getID(),
790 db_query_params ('DELETE FROM pfo_role_setting where role_id=$1 AND section_name=$2 and ref_id NOT IN (SELECT group_forum_id FROM forum_group_list WHERE group_id IN (SELECT home_group_id FROM pfo_role WHERE role_id=$1 AND home_group_id IS NOT NULL UNION SELECT group_id from role_project_refs WHERE role_id=$1))',
791 array ($this->getID(),
795 $this->fetchData($this->getID());
799 function normalizeDataForSection (&$new_sa, $section) {
800 if (array_key_exists ($section, $this->setting_array)) {
801 $new_sa[$section][0] = $this->setting_array[$section][0] ;
802 } elseif (array_key_exists ($this->data_array['role_name'], $this->defaults)
803 && array_key_exists ($section, $this->defaults[$this->data_array['role_name']])) {
804 $new_sa[$section][0] = $this->defaults[$this->data_array['role_name']][$section] ;
806 $new_sa[$section][0] = 0 ;
811 function normalizePermsForSection (&$new_pa, $section, $refid) {
812 if (array_key_exists ($section, $this->perms_array)
813 && array_key_exists ($refid, $this->perms_array[$section])) {
814 $new_pa[$section][$refid] = $this->perms_array[$section][$refid] ;
815 } elseif (array_key_exists ($this->data_array['role_name'], $this->defaults)
816 && array_key_exists ($section, $this->defaults[$this->data_array['role_name']])) {
817 $new_pa[$section][$refid] = $this->defaults[$this->data_array['role_name']][$section] ;
819 $new_pa[$section][$refid] = 0 ;
824 function normalizeData () { // From the PFO spec
825 $this->removeObsoleteSettings () ;
827 $this->fetchData ($this->getID()) ;
829 $projects = $this->getLinkedProjects() ;
833 // Add missing settings
834 // ...project-wide settings
835 $arr = array ('project_read', 'project_admin', 'frs', 'scm', 'docman', 'tracker_admin', 'new_tracker', 'forum_admin', 'new_forum', 'pm_admin', 'new_pm', 'webcal') ;
836 foreach ($projects as $p) {
837 foreach ($arr as $section) {
838 $this->normalizePermsForSection ($new_pa, $section, $p->getID()) ;
841 $this->normalizePermsForSection ($new_pa, 'forge_admin', -1) ;
842 $this->normalizePermsForSection ($new_pa, 'approve_projects', -1) ;
843 $this->normalizePermsForSection ($new_pa, 'approve_news', -1) ;
844 $this->normalizePermsForSection ($new_pa, 'forge_stats', -1) ;
846 $hook_params = array ();
847 $hook_params['role'] =& $this;
848 $hook_params['new_sa'] =& $new_sa ;
849 $hook_params['new_pa'] =& $new_pa ;
850 plugin_hook ("role_normalize", $hook_params);
852 // ...tracker-related settings
853 $new_sa['tracker'] = array () ;
854 $new_pa['tracker'] = array () ;
855 foreach ($projects as $p) {
856 $atf = new ArtifactTypeFactory ($p) ;
857 $trackerids = $atf->getAllArtifactTypeIds () ;
858 foreach ($trackerids as $tid) {
859 if (array_key_exists ('tracker', $this->perms_array)
860 && array_key_exists ($tid, $this->perms_array['tracker']) ) {
861 $new_pa['tracker'][$tid] = $this->perms_array['tracker'][$tid] ;
862 } elseif (array_key_exists ('new_tracker', $this->perms_array)
863 && array_key_exists ($p->getID(), $this->perms_array['new_tracker']) ) {
864 $new_pa['tracker'][$tid] = $new_pa['new_tracker'][$p->getID()] ;
869 // ...forum-related settings
870 $new_sa['forum'] = array () ;
871 $new_pa['forum'] = array () ;
872 foreach ($projects as $p) {
873 $ff = new ForumFactory ($p) ;
874 $fids = $ff->getAllForumIds () ;
875 foreach ($fids as $fid) {
876 if (array_key_exists ('forum', $this->perms_array)
877 && array_key_exists ($fid, $this->perms_array['forum']) ) {
878 $new_pa['forum'][$fid] = $this->perms_array['forum'][$fid] ;
879 } elseif (array_key_exists ('new_forum', $this->perms_array)
880 && array_key_exists ($p->getID(), $this->perms_array['new_forum']) ) {
881 $new_pa['forum'][$fid] = $new_pa['new_forum'][$p->getID()] ;
886 // ...pm-related settings
887 $new_sa['pm'] = array () ;
888 $new_pa['pm'] = array () ;
889 foreach ($projects as $p) {
890 $pgf = new ProjectGroupFactory ($p) ;
891 $pgids = $pgf->getAllProjectGroupIds () ;
892 foreach ($pgids as $gid) {
893 if (array_key_exists ('pm', $this->perms_array)
894 && array_key_exists ($gid, $this->perms_array['pm']) ) {
895 $new_pa['pm'][$gid] = $this->perms_array['pm'][$gid] ;
896 } elseif (array_key_exists ('new_pm', $this->perms_array)
897 && array_key_exists ($p->getID(), $this->perms_array['new_pm']) ) {
898 $new_pa['pm'][$gid] = $new_pa['new_pm'][$p->getID()] ;
904 $this->update ($this->getName(), $new_pa, false) ;
912 * TODO: RBAC::RoleExplicit Enter description here ...
915 abstract class RoleExplicit extends BaseRole implements PFO_RoleExplicit {
916 public function addUsers($users) {
920 foreach ($users as $user) {
921 $ids[] = $user->getID();
924 $already_there = array();
925 $res = db_query_params('SELECT user_id FROM pfo_user_role WHERE user_id=ANY($1) AND role_id=$2',
926 array(db_int_array_to_any_clause($ids), $this->getID()));
930 while ($arr = db_fetch_array($res)) {
931 $already_there[] = $arr['user_id'] ;
934 foreach ($ids as $id) {
935 if (!in_array ($id, $already_there)) {
936 $res = db_query_params ('INSERT INTO pfo_user_role (user_id, role_id) VALUES ($1, $2)',
945 foreach ($this->getLinkedProjects() as $p) {
946 foreach ($ids as $uid) {
947 if (!$SYS->sysGroupCheckUser($p->getID(),$uid)) {
956 public function addUser ($user) {
957 if (!$this->addUsers (array ($user))) {
960 $hook_params['user'] = $user;
961 $hook_params['role'] = $this;
962 plugin_hook ("role_adduser", $hook_params);
967 public function removeUsers($users) {
971 foreach ($users as $user) {
972 $ids[] = $user->getID() ;
975 $already_there = array () ;
976 $res = db_query_params ('DELETE FROM pfo_user_role WHERE user_id=ANY($1) AND role_id=$2',
977 array (db_int_array_to_any_clause($ids), $this->getID())) ;
979 foreach ($this->getLinkedProjects() as $p) {
980 foreach ($ids as $uid) {
981 $SYS->sysGroupCheckUser($p->getID(),$uid) ;
988 public function removeUser ($user) {
989 if(!$this->removeUsers (array ($user))){
992 $hook_params['user'] = $user;
993 $hook_params['role'] = $this;
994 plugin_hook ("role_removeuser", $hook_params);
999 public function getUsers() {
1000 $result = array () ;
1001 $res = db_query_params ('SELECT user_id FROM pfo_user_role WHERE role_id=$1',
1002 array ($this->getID())) ;
1003 while ($arr = db_fetch_array($res)) {
1004 $result[] = user_get_object ($arr['user_id']) ;
1010 public function hasUser($user) {
1011 $res = db_query_params ('SELECT user_id FROM pfo_user_role WHERE user_id=$1 AND role_id=$2',
1012 array ($user->getID(), $this->getID())) ;
1013 if ($res && db_numrows($res)) {
1020 function getID() { // From the PFO spec
1021 return $this->data_array['role_id'];
1024 function getName() { // From the PFO spec
1025 return $this->data_array['role_name'];
1029 class RoleAnonymous extends BaseRole implements PFO_RoleAnonymous {
1030 // This role is implemented as a singleton
1031 private static $_instance ;
1033 public static function getInstance() {
1034 if (isset(self::$_instance)) {
1035 return self::$_instance ;
1039 self::$_instance = new $c ;
1041 $res = db_query_params ('SELECT r.role_id FROM pfo_role r, pfo_role_class c WHERE r.role_class = c.class_id AND c.class_name = $1',
1042 array ('PFO_RoleAnonymous')) ;
1043 if (!$res || !db_numrows($res)) {
1044 throw new Exception ("No PFO_RoleAnonymous role in the database") ;
1046 self::$_instance->_role_id = db_result ($res, 0, 'role_id') ;
1048 $hook_params = array ();
1049 $hook_params['role'] =& self::$_instance;
1050 plugin_hook ("role_get", $hook_params);
1052 self::$_instance->fetchData (self::$_instance->_role_id) ;
1054 return self::$_instance ;
1057 public function getID () {
1058 return $this->_role_id ;
1060 public function isPublic () {
1063 public function setPublic ($flag) {
1064 throw new Exception ("Can't setPublic() on RoleAnonymous") ;
1066 public function getHomeProject () {
1069 public function getName () {
1070 return _('Anonymous/not logged in') ;
1072 public function setName ($name) {
1073 throw new Exception ("Can't setName() on RoleAnonymous") ;
1077 class RoleLoggedIn extends BaseRole implements PFO_RoleLoggedIn {
1078 // This role is implemented as a singleton
1079 private static $_instance ;
1081 public static function getInstance() {
1082 if (isset(self::$_instance)) {
1083 return self::$_instance ;
1087 self::$_instance = new $c ;
1089 $res = db_query_params ('SELECT r.role_id FROM pfo_role r, pfo_role_class c WHERE r.role_class = c.class_id AND c.class_name = $1',
1090 array ('PFO_RoleLoggedIn')) ;
1091 if (!$res || !db_numrows($res)) {
1092 throw new Exception ("No PFO_RoleLoggedIn role in the database") ;
1094 self::$_instance->_role_id = db_result ($res, 0, 'role_id') ;
1096 $hook_params = array ();
1097 $hook_params['role'] =& self::$_instance;
1098 plugin_hook ("role_get", $hook_params);
1100 self::$_instance->fetchData (self::$_instance->_role_id) ;
1102 return self::$_instance ;
1105 public function getID () {
1106 return $this->_role_id ;
1108 public function isPublic () {
1111 public function setPublic ($flag) {
1112 throw new Exception ("Can't setPublic() on RoleLoggedIn") ;
1114 public function getHomeProject () {
1117 public function getName () {
1118 return _('Any user logged in') ;
1120 public function setName ($name) {
1121 throw new Exception ("Can't setName() on RoleLoggedIn") ;
1125 abstract class RoleUnion extends BaseRole implements PFO_RoleUnion {
1126 public function addRole ($role) {
1127 throw new Exception ("Not implemented") ;
1129 public function removeRole ($role) {
1130 throw new Exception ("Not implemented") ;
1135 * TODO: Enter description here ...
1138 class RoleComparator {
1139 var $criterion = 'composite' ;
1140 var $reference_project = NULL ;
1142 function Compare ($a, $b) {
1143 switch ($this->criterion) {
1145 return strcoll ($a->getName(), $b->getName()) ;
1148 $aid = $a->getID() ;
1149 $bid = $b->getID() ;
1153 return ($a < $b) ? -1 : 1;
1157 if ($this->reference_project == NULL) {
1158 return $this->CompareNoRef ($a, $b) ;
1160 $rpid = $this->reference_project->getID () ;
1161 $ap = $a->getHomeProject() ;
1162 $bp = $b->getHomeProject() ;
1163 $a_is_local = ($ap != NULL && $ap->getID() == $rpid) ; // Local
1164 $b_is_local = ($bp != NULL && $bp->getID() == $rpid) ;
1166 if ($a_is_local && !$b_is_local) {
1168 } elseif (!$a_is_local && $b_is_local) {
1171 return $this->CompareNoRef ($a, $b) ;
1176 * TODO: Enter description here ...
1181 function CompareNoRef ($a, $b) {
1182 $ap = $a->getHomeProject() ;
1183 $bp = $b->getHomeProject() ;
1184 if ($ap == NULL && $bp != NULL) {
1186 } elseif ($ap != NULL && $bp == NULL) {
1188 } elseif ($ap == NULL && $bp == NULL) {
1189 $tmp = strcoll ($a->getName(), $b->getName()) ;
1192 $projcmp = new ProjectComparator () ;
1193 $projcmp->criterion = 'name' ;
1194 $tmp = $projcmp->Compare ($ap, $bp) ;
1195 if ($tmp) { /* Different projects, sort accordingly */
1198 return strcoll ($a->getName(), $b->getName()) ;
1203 function sortRoleList (&$list, $relative_to = NULL, $criterion='composite') {
1204 $cmp = new RoleComparator () ;
1205 $cmp->criterion = $criterion ;
1206 $cmp->reference_project = $relative_to ;
1208 return usort ($list, array ($cmp, 'Compare')) ;
1213 // c-file-style: "bsd"