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') ;
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 array_push ($sections,'tracker');
334 $sections_forum = array('forum_admin', 'new_forum');
335 foreach ($sections_forum as $section_forum) {
336 $result[$section_forum][$group_id] = $this->getVal ($section_forum, $group_id) ;
338 $sections = array_merge($sections, $sections_forum);
340 $ff = new ForumFactory ($project) ;
341 $fids = $ff->getAllForumIds () ;
342 foreach ($fids as $fid) {
343 $result['forum'][$fid] = $this->getVal ('forum', $fid) ;
345 array_push ($sections,'forum');
347 $sections_pm = array('pm_admin', 'new_pm');
348 foreach ($sections_pm as $section_pm) {
349 $result[$section_pm][$group_id] = $this->getVal ($section_pm, $group_id) ;
351 $sections = array_merge($sections, $sections_pm);
352 $pgf = new ProjectGroupFactory ($project) ;
353 $pgids = $pgf->getAllProjectGroupIds () ;
354 foreach ($pgids as $pgid) {
355 $result['pm'][$pgid] = $this->getVal ('pm', $pgid) ;
357 array_push ($sections,'pm') ;
360 // Add settings not yet listed so far (probably plugins)
361 // Currently handled:
362 // - global settings (ignored here)
363 // - project-wide settings (core and plugins)
364 // - settings for multiple-instance tools coming from the core (trackers/pm/forums)
366 // - settings for multiple-instance tools from plugins
367 foreach (array_keys ($this->perms_array) as $section) {
368 if (!in_array ($section, $sections)) {
369 if (!in_array ($section, $this->global_settings)) {
370 $result[$section][$group_id] = $this->getVal ($section, $group_id) ;
379 * TODO: Enter description here ...
382 function getGlobalSettings () {
385 $sections = array ('forge_admin', 'forge_stats', 'approve_projects', 'approve_news') ;
386 foreach ($sections as $section) {
387 $result[$section][-1] = $this->getVal($section, -1) ;
389 // Add settings not yet listed so far (probably plugins)
390 foreach (array_keys ($this->perms_array) as $section) {
391 if (!in_array ($section, $sections)) {
392 if (in_array ($section, $this->global_settings)) {
393 $result[$section][-1] = $this->getVal ($section, -1) ;
402 * TODO: Enter description here ...
403 * @param unknown_type $section
404 * @param unknown_type $reference
405 * @return number|boolean
407 function getSetting($section, $reference) {
408 $r = $this->getSettingRaw($section, $reference);
415 function getSettingRaw($section, $reference) {
416 if (isset ($this->perms_array[$section][$reference])) {
417 $value = $this->perms_array[$section][$reference] ;
430 case 'approve_projects':
432 if ($this->hasGlobalPermission('forge_admin')) {
439 if ($this->hasGlobalPermission('forge_admin')) {
445 case 'project_admin':
446 if ($this->hasGlobalPermission('forge_admin')) {
453 case 'tracker_admin':
456 if ($this->hasPermission('project_admin', $reference)) {
463 if ($this->hasPermission('project_admin', $reference)) {
470 if ($this->hasPermission('project_admin', $reference)) {
477 if ($this->hasPermission('project_admin', $reference)) {
484 if ($this->hasPermission('forum_admin', forum_get_groupid($reference))) {
490 if ($this->hasPermission('forum_admin', $reference)) {
497 if ($this->hasPermission('tracker_admin', artifacttype_get_groupid($reference))) {
503 if ($this->hasPermission('tracker_admin', $reference)) {
510 if ($this->hasPermission('pm_admin', projectgroup_get_groupid($reference))) {
516 if ($this->hasPermission('pm_admin', $reference)) {
522 $hook_params = array ();
523 $hook_params['role'] = $this ;
524 $hook_params['section'] = $section ;
525 $hook_params['reference'] = $reference ;
526 $hook_params['value'] = $value ;
527 $hook_params['result'] = NULL ;
528 plugin_hook_by_reference ("role_get_setting", $hook_params);
529 return $hook_params['result'] ;
535 * getVal - get a value out of the array of settings for this role.
537 * @param string The name of the role.
538 * @param integer The ref_id (ex: group_artifact_id, group_forum_id) for this item.
539 * @return integer The value of this item.
541 function getVal($section, $ref_id) {
542 global $role_default_array;
546 return $this->getSetting($section, $ref_id) ;
550 * &getRoleVals - get all the values and language text strings for this section.
552 * @return array Assoc array of values for this section.
554 function &getRoleVals($section) {
555 global $role_vals, $rbac_permission_names;
556 setup_rbac_strings () ;
559 // Optimization - save array so it is only built once per page view
561 if (!isset($role_vals[$section])) {
563 for ($i=0; $i<count($this->role_values[$section]); $i++) {
565 // Build an associative array of these key values + localized description
567 $role_vals[$section][$this->role_values[$section][$i]] =
568 util_ifsetor($rbac_permission_names["$section".$this->role_values[$section][$i]],
569 _('UNKNOWN (internal error, report bug to FusionForge)'));
572 return $role_vals[$section];
575 function hasPermission($section, $reference, $action = NULL) {
578 $value = $this->getSetting ($section, $reference) ;
585 case 'approve_projects':
587 case 'project_admin':
589 case 'tracker_admin':
592 return ($value >= 1) ;
598 return ($value >= 1) ;
601 return ($value >= 2) ;
609 return ($value >= 1) ;
612 return ($value >= 2) ;
620 return ($value >= 1) ;
623 return ($value >= 2) ;
626 return ($value >= 3) ;
629 return ($value >= 4) ;
637 return ($value >= 1) ;
640 return ($value >= 2) ;
643 return ($value >= 3) ;
652 return ($value >= 1) ;
655 return ($value >= 2) ;
657 case 'unmoderated_post':
658 return ($value >= 3) ;
661 return ($value >= 4) ;
670 return (($value & 1) != 0) ;
673 return (($value & 2) != 0) ;
676 return (($value & 4) != 0) ;
679 return (($value & 8) != 0) ;
682 * bit4 (value & 16) is reserved
683 * for tracker item vote from Evolvis
692 return (($value & 1) != 0) ;
695 return (($value & 2) != 0) ;
698 return (($value & 4) != 0) ;
703 $hook_params = array ();
704 $hook_params['section'] = $section ;
705 $hook_params['reference'] = $reference ;
706 $hook_params['action'] = $action ;
707 $hook_params['value'] = $value ;
708 $hook_params['result'] = false ;
709 plugin_hook_by_reference ("role_has_permission", $hook_params);
710 return $hook_params['result'] ;
716 * update - update a role in the database.
718 * @param string The name of the role.
719 * @param array A multi-dimensional array of data in this format: $data['section_name']['ref_id']=$val
720 * @param boolean Perform permission checking
721 * @return boolean True on success or false on failure.
723 function update($role_name,$data,$check_perms=true) {
726 if ($this->getHomeProject() == NULL) {
727 if (!forge_check_global_perm ('forge_admin')) {
728 $this->setPermissionDeniedError();
731 } elseif (!forge_check_perm ('project_admin', $this->getHomeProject()->getID())) {
732 $this->setPermissionDeniedError();
740 if ($role_name != $this->getName()) {
741 $this->setName($role_name) ;
744 foreach ($data as $sect => $refs) {
745 foreach ($refs as $refid => $value) {
746 $this->setSetting ($sect, $refid, $value) ;
750 $hook_params = array ();
751 $hook_params['role'] =& $this;
752 $hook_params['role_id'] = $this->getID();
753 $hook_params['data'] = $data;
754 plugin_hook ("role_update", $hook_params);
758 $this->fetchData($this->getID());
760 foreach ($this->getUsers() as $u) {
761 if (!$SYS->sysCheckCreateUser($u->getID())) {
762 $this->setError($SYS->getErrorMessage());
770 function getDisplayableName($group = NULL) {
771 if ($this->getHomeProject() == NULL) {
772 return sprintf (_('%s (global role)'),
774 } elseif ($group == NULL
775 || $this->getHomeProject()->getID() != $group->getID()) {
776 return sprintf (_('%s (in project %s)'),
778 $this->getHomeProject()->getPublicName()) ;
780 return $this->getName () ;
784 function removeObsoleteSettings () {
787 // Remove obsolete project-wide settings
788 $sections = array ('project_read', 'project_admin', 'frs', 'scm', 'docman', 'tracker_admin', 'new_tracker', 'forum_admin', 'new_forum', 'pm_admin', 'new_pm', 'webcal') ;
789 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)',
790 array ($this->getID(),
791 db_string_array_to_any_clause($sections))) ;
794 // Remove obsolete settings for multiple-instance tools
795 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))',
796 array ($this->getID(),
798 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))',
799 array ($this->getID(),
801 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))',
802 array ($this->getID(),
806 $this->fetchData($this->getID());
810 function normalizeDataForSection (&$new_sa, $section) {
811 if (array_key_exists ($section, $this->setting_array)) {
812 $new_sa[$section][0] = $this->setting_array[$section][0] ;
813 } elseif (array_key_exists ($this->data_array['role_name'], $this->defaults)
814 && array_key_exists ($section, $this->defaults[$this->data_array['role_name']])) {
815 $new_sa[$section][0] = $this->defaults[$this->data_array['role_name']][$section] ;
817 $new_sa[$section][0] = 0 ;
822 function normalizePermsForSection (&$new_pa, $section, $refid) {
823 if (array_key_exists ($section, $this->perms_array)
824 && array_key_exists ($refid, $this->perms_array[$section])) {
825 $new_pa[$section][$refid] = $this->perms_array[$section][$refid] ;
826 } elseif (array_key_exists ($this->data_array['role_name'], $this->defaults)
827 && array_key_exists ($section, $this->defaults[$this->data_array['role_name']])) {
828 $new_pa[$section][$refid] = $this->defaults[$this->data_array['role_name']][$section] ;
830 $new_pa[$section][$refid] = 0 ;
835 function normalizeData () { // From the PFO spec
836 $this->removeObsoleteSettings () ;
838 $this->fetchData ($this->getID()) ;
840 $projects = $this->getLinkedProjects() ;
844 // Add missing settings
845 // ...project-wide settings
846 $arr = array ('project_read', 'project_admin', 'frs', 'scm', 'docman', 'tracker_admin', 'new_tracker', 'forum_admin', 'new_forum', 'pm_admin', 'new_pm', 'webcal') ;
847 foreach ($projects as $p) {
848 foreach ($arr as $section) {
849 $this->normalizePermsForSection ($new_pa, $section, $p->getID()) ;
852 $this->normalizePermsForSection ($new_pa, 'forge_admin', -1) ;
853 $this->normalizePermsForSection ($new_pa, 'approve_projects', -1) ;
854 $this->normalizePermsForSection ($new_pa, 'approve_news', -1) ;
855 $this->normalizePermsForSection ($new_pa, 'forge_stats', -1) ;
857 $hook_params = array ();
858 $hook_params['role'] =& $this;
859 $hook_params['new_sa'] =& $new_sa ;
860 $hook_params['new_pa'] =& $new_pa ;
861 plugin_hook ("role_normalize", $hook_params);
863 // ...tracker-related settings
864 $new_sa['tracker'] = array () ;
865 $new_pa['tracker'] = array () ;
866 foreach ($projects as $p) {
867 $atf = new ArtifactTypeFactory ($p) ;
868 $trackerids = $atf->getAllArtifactTypeIds () ;
869 foreach ($trackerids as $tid) {
870 if (array_key_exists ('tracker', $this->perms_array)
871 && array_key_exists ($tid, $this->perms_array['tracker']) ) {
872 $new_pa['tracker'][$tid] = $this->perms_array['tracker'][$tid] ;
873 } elseif (array_key_exists ('new_tracker', $this->perms_array)
874 && array_key_exists ($p->getID(), $this->perms_array['new_tracker']) ) {
875 $new_pa['tracker'][$tid] = $new_pa['new_tracker'][$p->getID()] ;
880 // ...forum-related settings
881 $new_sa['forum'] = array () ;
882 $new_pa['forum'] = array () ;
883 foreach ($projects as $p) {
884 $ff = new ForumFactory ($p) ;
885 $fids = $ff->getAllForumIds () ;
886 foreach ($fids as $fid) {
887 if (array_key_exists ('forum', $this->perms_array)
888 && array_key_exists ($fid, $this->perms_array['forum']) ) {
889 $new_pa['forum'][$fid] = $this->perms_array['forum'][$fid] ;
890 } elseif (array_key_exists ('new_forum', $this->perms_array)
891 && array_key_exists ($p->getID(), $this->perms_array['new_forum']) ) {
892 $new_pa['forum'][$fid] = $new_pa['new_forum'][$p->getID()] ;
897 // ...pm-related settings
898 $new_sa['pm'] = array () ;
899 $new_pa['pm'] = array () ;
900 foreach ($projects as $p) {
901 $pgf = new ProjectGroupFactory ($p) ;
902 $pgids = $pgf->getAllProjectGroupIds () ;
903 foreach ($pgids as $gid) {
904 if (array_key_exists ('pm', $this->perms_array)
905 && array_key_exists ($gid, $this->perms_array['pm']) ) {
906 $new_pa['pm'][$gid] = $this->perms_array['pm'][$gid] ;
907 } elseif (array_key_exists ('new_pm', $this->perms_array)
908 && array_key_exists ($p->getID(), $this->perms_array['new_pm']) ) {
909 $new_pa['pm'][$gid] = $new_pa['new_pm'][$p->getID()] ;
915 $this->update ($this->getName(), $new_pa, false) ;
923 * TODO: RBAC::RoleExplicit Enter description here ...
926 abstract class RoleExplicit extends BaseRole implements PFO_RoleExplicit {
927 public function addUsers($users) {
931 foreach ($users as $user) {
932 $ids[] = $user->getID();
935 $already_there = array();
936 $res = db_query_params('SELECT user_id FROM pfo_user_role WHERE user_id=ANY($1) AND role_id=$2',
937 array(db_int_array_to_any_clause($ids), $this->getID()));
941 while ($arr = db_fetch_array($res)) {
942 $already_there[] = $arr['user_id'] ;
945 foreach ($ids as $id) {
946 if (!in_array ($id, $already_there)) {
947 $res = db_query_params ('INSERT INTO pfo_user_role (user_id, role_id) VALUES ($1, $2)',
956 foreach ($this->getLinkedProjects() as $p) {
957 foreach ($ids as $uid) {
958 if (!$SYS->sysGroupCheckUser($p->getID(),$uid)) {
967 public function addUser ($user) {
968 if (!$this->addUsers (array ($user))) {
971 $hook_params['user'] = $user;
972 $hook_params['role'] = $this;
973 plugin_hook ("role_adduser", $hook_params);
978 public function removeUsers($users) {
982 foreach ($users as $user) {
983 $ids[] = $user->getID() ;
986 $already_there = array () ;
987 $res = db_query_params ('DELETE FROM pfo_user_role WHERE user_id=ANY($1) AND role_id=$2',
988 array (db_int_array_to_any_clause($ids), $this->getID())) ;
990 foreach ($this->getLinkedProjects() as $p) {
991 foreach ($ids as $uid) {
992 $SYS->sysGroupCheckUser($p->getID(),$uid) ;
999 public function removeUser ($user) {
1000 if(!$this->removeUsers (array ($user))){
1003 $hook_params['user'] = $user;
1004 $hook_params['role'] = $this;
1005 plugin_hook ("role_removeuser", $hook_params);
1010 public function getUsers() {
1011 $result = array () ;
1012 $res = db_query_params ('SELECT user_id FROM pfo_user_role WHERE role_id=$1',
1013 array ($this->getID())) ;
1014 while ($arr = db_fetch_array($res)) {
1015 $result[] = user_get_object ($arr['user_id']) ;
1021 public function hasUser($user) {
1022 $res = db_query_params ('SELECT user_id FROM pfo_user_role WHERE user_id=$1 AND role_id=$2',
1023 array ($user->getID(), $this->getID())) ;
1024 if ($res && db_numrows($res)) {
1031 function getID() { // From the PFO spec
1032 return $this->data_array['role_id'];
1035 function getName() { // From the PFO spec
1036 return $this->data_array['role_name'];
1040 class RoleAnonymous extends BaseRole implements PFO_RoleAnonymous {
1041 // This role is implemented as a singleton
1042 private static $_instance ;
1044 public static function getInstance() {
1045 if (isset(self::$_instance)) {
1046 return self::$_instance ;
1050 self::$_instance = new $c ;
1052 $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',
1053 array ('PFO_RoleAnonymous')) ;
1054 if (!$res || !db_numrows($res)) {
1055 throw new Exception ("No PFO_RoleAnonymous role in the database") ;
1057 self::$_instance->_role_id = db_result ($res, 0, 'role_id') ;
1059 $hook_params = array ();
1060 $hook_params['role'] =& self::$_instance;
1061 plugin_hook ("role_get", $hook_params);
1063 self::$_instance->fetchData (self::$_instance->_role_id) ;
1065 return self::$_instance ;
1068 public function getID () {
1069 return $this->_role_id ;
1071 public function isPublic () {
1074 public function setPublic ($flag) {
1075 throw new Exception ("Can't setPublic() on RoleAnonymous") ;
1077 public function getHomeProject () {
1080 public function getName () {
1081 return _('Anonymous/not logged in') ;
1083 public function setName ($name) {
1084 throw new Exception ("Can't setName() on RoleAnonymous") ;
1088 class RoleLoggedIn extends BaseRole implements PFO_RoleLoggedIn {
1089 // This role is implemented as a singleton
1090 private static $_instance ;
1092 public static function getInstance() {
1093 if (isset(self::$_instance)) {
1094 return self::$_instance ;
1098 self::$_instance = new $c ;
1100 $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',
1101 array ('PFO_RoleLoggedIn')) ;
1102 if (!$res || !db_numrows($res)) {
1103 throw new Exception ("No PFO_RoleLoggedIn role in the database") ;
1105 self::$_instance->_role_id = db_result ($res, 0, 'role_id') ;
1107 $hook_params = array ();
1108 $hook_params['role'] =& self::$_instance;
1109 plugin_hook ("role_get", $hook_params);
1111 self::$_instance->fetchData (self::$_instance->_role_id) ;
1113 return self::$_instance ;
1116 public function getID () {
1117 return $this->_role_id ;
1119 public function isPublic () {
1122 public function setPublic ($flag) {
1123 throw new Exception ("Can't setPublic() on RoleLoggedIn") ;
1125 public function getHomeProject () {
1128 public function getName () {
1129 return _('Any user logged in') ;
1131 public function setName ($name) {
1132 throw new Exception ("Can't setName() on RoleLoggedIn") ;
1136 abstract class RoleUnion extends BaseRole implements PFO_RoleUnion {
1137 public function addRole ($role) {
1138 throw new Exception ("Not implemented") ;
1140 public function removeRole ($role) {
1141 throw new Exception ("Not implemented") ;
1146 * TODO: Enter description here ...
1149 class RoleComparator {
1150 var $criterion = 'composite' ;
1151 var $reference_project = NULL ;
1153 function Compare ($a, $b) {
1154 switch ($this->criterion) {
1156 return strcoll ($a->getName(), $b->getName()) ;
1159 $aid = $a->getID() ;
1160 $bid = $b->getID() ;
1164 return ($a < $b) ? -1 : 1;
1168 if ($this->reference_project == NULL) {
1169 return $this->CompareNoRef ($a, $b) ;
1171 $rpid = $this->reference_project->getID () ;
1172 $ap = $a->getHomeProject() ;
1173 $bp = $b->getHomeProject() ;
1174 $a_is_local = ($ap != NULL && $ap->getID() == $rpid) ; // Local
1175 $b_is_local = ($bp != NULL && $bp->getID() == $rpid) ;
1177 if ($a_is_local && !$b_is_local) {
1179 } elseif (!$a_is_local && $b_is_local) {
1182 return $this->CompareNoRef ($a, $b) ;
1187 * TODO: Enter description here ...
1192 function CompareNoRef ($a, $b) {
1193 $ap = $a->getHomeProject() ;
1194 $bp = $b->getHomeProject() ;
1195 if ($ap == NULL && $bp != NULL) {
1197 } elseif ($ap != NULL && $bp == NULL) {
1199 } elseif ($ap == NULL && $bp == NULL) {
1200 $tmp = strcoll ($a->getName(), $b->getName()) ;
1203 $projcmp = new ProjectComparator () ;
1204 $projcmp->criterion = 'name' ;
1205 $tmp = $projcmp->Compare ($ap, $bp) ;
1206 if ($tmp) { /* Different projects, sort accordingly */
1209 return strcoll ($a->getName(), $b->getName()) ;
1214 function sortRoleList (&$list, $relative_to = NULL, $criterion='composite') {
1215 $cmp = new RoleComparator () ;
1216 $cmp->criterion = $criterion ;
1217 $cmp->reference_project = $relative_to ;
1219 return usort ($list, array ($cmp, 'Compare')) ;
1224 // c-file-style: "bsd"