3 * FusionForge role-based access control
5 * Copyright 2004, GForge, LLC
6 * Copyright 2009-2010, Roland Mas
7 * Copyright 2012, Franck Villaume - TrivialDev
8 * http://fusionforge.org
10 * This file is part of FusionForge. FusionForge is free software;
11 * you can redistribute it and/or modify it under the terms of the
12 * GNU General Public License as published by the Free Software
13 * Foundation; either version 2 of the Licence, or (at your option)
16 * FusionForge is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 require "PFO-RBAC.interface.php";
28 define ('USE_PFO_RBAC', true);
30 // Code shared between classes
33 * TODO: RBAC::BaseRole Enter description here ...
36 abstract class BaseRole extends Error {
38 * TODO: Enter description here ...
43 * TODO: Enter description here ...
48 * TODO: Enter description here ...
54 // var $setting_array;
56 public function BaseRole() {
57 // TODO: document these tables
58 // $gfcommon.'include/rbac_texts.php' may provide some hints...
59 $this->role_values = array(
60 'forge_admin' => array(0, 1),
61 'approve_projects' => array(0, 1),
62 'approve_news' => array(0, 1),
63 'forge_stats' => array(0, 1, 2),
65 'project_read' => array(0, 1),
66 'project_admin' => array(0, 1),
68 'tracker_admin' => array(0, 1),
69 'pm_admin' => array(0, 1),
70 'forum_admin' => array(0, 1),
72 'tracker' => array(0, 1, 3, 5, 7),
73 'pm' => array(0, 1, 3, 5, 7),
74 'forum' => array(0, 1, 2, 3, 4),
76 'new_tracker' => array(0, 1, 3, 5, 7),
77 'new_pm' => array(0, 1, 3, 5, 7),
78 'new_forum' => array(0, 1, 2, 3, 4),
80 'scm' => array (0, 1, 2),
81 'docman' => array (0, 1, 2, 3, 4),
82 'frs' => array (0, 1, 2, 3),
84 // 'webcal' => array(0, 1, 2),
88 $this->global_settings = array(
89 'forge_admin', // “God mode”: all actions allowed
90 'approve_projects', // Ability to approve pending projects
91 'approve_news', // Ability to approve news bits to the forge front page
95 // TODO: document these (Project-related permissions ?)
96 $this->defaults = array(
97 'Admin' => array( 'project_admin'=> 1,
104 'tracker_admin' => 1,
110 'Senior Developer' => array( 'project_read' => 1,
116 'tracker_admin' => 1,
122 'Junior Developer' => array( 'project_read' => 1,
131 'Doc Writer' => array( 'project_read' => 1,
139 'Support Tech' => array( 'project_read' => 1,
143 'tracker_admin' => 1,
152 public function getUsers() {
155 public function hasUser($user) {
156 throw new Exception ("Not implemented") ;
158 function hasGlobalPermission($section, $action = NULL) {
159 return $this->hasPermission ($section, -1, $action) ;
161 public function getSettings() {
162 throw new Exception ("Not implemented") ;
164 public function setSettings($data) {
165 throw new Exception ("Not implemented") ;
167 public function delete () {
168 throw new Exception ("Not implemented") ;
172 * getLinkedProjects - List of projects referencing that role
174 * Includes the home project (for roles that have one)
176 * @return array Array of Group objects
178 public function getLinkedProjects() {
181 $hp = $this->getHomeProject();
183 $ids[] = $hp->getID();
186 $res = db_query_params('SELECT group_id FROM role_project_refs WHERE role_id=$1',
187 array($this->getID()));
189 while ($arr = db_fetch_array ($res)) {
190 $ids[] = $arr['group_id'];
194 return group_get_objects(array_unique($ids));
197 function linkProject ($project) { // From the PFO spec
199 $hp = $this->getHomeProject();
200 if ($hp != NULL && $hp->getID() == $project->getID()) {
201 $this->setError(_("Can't link to home project"));
205 $res = db_query_params('SELECT group_id FROM role_project_refs WHERE role_id=$1 AND group_id=$2',
206 array($this->getID(),
209 if (db_numrows($res)) {
212 $res = db_query_params('INSERT INTO role_project_refs (role_id, group_id) VALUES ($1, $2)',
213 array($this->getID(),
215 if (!$res || db_affected_rows($res) < 1) {
216 $this->setError('linkProject('.$project->getID().') '.db_error());
220 $this->normalizeData();
222 foreach ($this->getUsers() as $u) {
223 if (!$SYS->sysCheckCreateUser($u->getID())) {
224 $this->setError($SYS->getErrorMessage());
232 function unlinkProject($project) { // From the PFO spec
234 $hp = $this->getHomeProject();
235 if ($hp != NULL && $hp->getID() == $project->getID()) {
236 $this->setError (_("Can't unlink from home project"));
240 $res = db_query_params('DELETE FROM role_project_refs WHERE role_id=$1 AND group_id=$2',
241 array($this->getID(),
244 $this->setError('unlinkProject('.$project->getID().') '.db_error());
248 $this->removeObsoleteSettings ();
250 foreach ($this->getUsers() as $u) {
251 if (!$SYS->sysCheckCreateUser($u->getID())) {
252 $this->setError($SYS->getErrorMessage());
261 * fetchData - May need to refresh database fields.
263 * If an update occurred and you need to access the updated info.
265 * @return boolean success;
267 function fetchData($role_id) {
268 unset($this->data_array);
269 unset($this->setting_array);
270 unset($this->perms_array);
272 $res = db_query_params('SELECT * FROM pfo_role WHERE role_id=$1',
274 if (!$res || db_numrows($res) < 1) {
275 $this->setError('BaseRole::fetchData()::'.db_error());
278 $this->data_array = db_fetch_array($res);
279 if ($this->data_array['is_public'] == 't') {
280 $this->data_array['is_public'] = true;
282 $this->data_array['is_public'] = false;
284 $res = db_query_params('SELECT section_name, ref_id, perm_val FROM pfo_role_setting WHERE role_id=$1',
287 $this->setError('BaseRole::fetchData()::'.db_error());
290 // TODO: document perms_array
291 $this->perms_array=array();
292 while ($arr = db_fetch_array($res)) {
293 $this->perms_array[$arr['section_name']][$arr['ref_id']] = $arr['perm_val'];
299 function setSetting ($section, $reference, $value) {
300 $cur = $this->getSettingRaw($section, $reference);
301 if (($value == $cur) && ($cur != NULL)) {
305 $role_id = $this->getID () ;
307 $res = db_query_params ('DELETE FROM pfo_role_setting WHERE role_id=$1 AND section_name=$2 AND ref_id=$3',
312 $res = db_query_params ('INSERT INTO pfo_role_setting (role_id, section_name, ref_id, perm_val) VALUES ($1, $2, $3, $4)',
317 $this->perms_array[$section][$reference] = $value;
320 function getSettingsForProject ($project) {
322 $group_id = $project->getID() ;
324 $sections = array ('project_read', 'project_admin', 'frs', 'scm', 'docman', 'tracker_admin', 'new_tracker') ;
325 foreach ($sections as $section) {
326 $result[$section][$group_id] = $this->getVal ($section, $group_id) ;
329 $atf = new ArtifactTypeFactory ($project) ;
330 $tids = $atf->getAllArtifactTypeIds () ;
331 foreach ($tids as $tid) {
332 $result['tracker'][$tid] = $this->getVal ('tracker', $tid) ;
334 array_push ($sections,'tracker');
336 $sections_forum = array('forum_admin', 'new_forum');
337 foreach ($sections_forum as $section_forum) {
338 $result[$section_forum][$group_id] = $this->getVal ($section_forum, $group_id) ;
340 $sections = array_merge($sections, $sections_forum);
342 $ff = new ForumFactory ($project) ;
343 $fids = $ff->getAllForumIdsWithNews () ;
344 foreach ($fids as $fid) {
345 $result['forum'][$fid] = $this->getVal ('forum', $fid) ;
347 array_push ($sections,'forum');
349 $sections_pm = array('pm_admin', 'new_pm');
350 foreach ($sections_pm as $section_pm) {
351 $result[$section_pm][$group_id] = $this->getVal ($section_pm, $group_id) ;
353 $sections = array_merge($sections, $sections_pm);
354 $pgf = new ProjectGroupFactory ($project) ;
355 $pgids = $pgf->getAllProjectGroupIds () ;
356 foreach ($pgids as $pgid) {
357 $result['pm'][$pgid] = $this->getVal ('pm', $pgid) ;
359 array_push ($sections,'pm') ;
362 // Add settings not yet listed so far (probably plugins)
363 // Currently handled:
364 // - global settings (ignored here)
365 // - project-wide settings (core and plugins)
366 // - settings for multiple-instance tools coming from the core (trackers/pm/forums)
368 // - settings for multiple-instance tools from plugins
369 foreach (array_keys ($this->perms_array) as $section) {
370 if (!in_array ($section, $sections)) {
371 if (!in_array ($section, $this->global_settings)) {
372 $result[$section][$group_id] = $this->getVal ($section, $group_id) ;
381 * TODO: Enter description here ...
384 function getGlobalSettings () {
387 $sections = array ('forge_admin', 'forge_stats', 'approve_projects', 'approve_news') ;
388 foreach ($sections as $section) {
389 $result[$section][-1] = $this->getVal($section, -1) ;
391 // Add settings not yet listed so far (probably plugins)
392 foreach (array_keys ($this->perms_array) as $section) {
393 if (!in_array ($section, $sections)) {
394 if (in_array ($section, $this->global_settings)) {
395 $result[$section][-1] = $this->getVal ($section, -1) ;
404 * TODO: Enter description here ...
405 * @param unknown_type $section
406 * @param unknown_type $reference
407 * @return number|boolean
409 function getSetting($section, $reference) {
410 $value = $this->getSettingRaw($section, $reference);
411 if ($value == NULL) {
424 case 'approve_projects':
426 if ($this->hasGlobalPermission('forge_admin')) {
433 if ($this->hasGlobalPermission('forge_admin')) {
439 case 'project_admin':
440 if ($this->hasGlobalPermission('forge_admin')) {
447 case 'tracker_admin':
450 if ($this->hasPermission('project_admin', $reference)) {
457 if ($this->hasPermission('project_admin', $reference)) {
464 if ($this->hasPermission('project_admin', $reference)) {
471 if ($this->hasPermission('project_admin', $reference)) {
478 if ($this->hasPermission('forum_admin', forum_get_groupid($reference))) {
484 if ($this->hasPermission('forum_admin', $reference)) {
491 if ($this->hasPermission('tracker_admin', artifacttype_get_groupid($reference))) {
497 if ($this->hasPermission('tracker_admin', $reference)) {
504 if ($this->hasPermission('pm_admin', projectgroup_get_groupid($reference))) {
510 if ($this->hasPermission('pm_admin', $reference)) {
516 $hook_params = array ();
517 $hook_params['role'] = $this ;
518 $hook_params['section'] = $section ;
519 $hook_params['reference'] = $reference ;
520 $hook_params['value'] = $value ;
521 $hook_params['result'] = NULL ;
522 plugin_hook_by_reference ("role_get_setting", $hook_params);
523 return $hook_params['result'] ;
528 function getSettingRaw($section, $reference) {
529 if (isset ($this->perms_array[$section][$reference])) {
530 return $this->perms_array[$section][$reference] ;
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) ;
685 return (($value & 1) != 0) ;
688 return (($value & 2) != 0) ;
691 return (($value & 4) != 0) ;
696 $hook_params = array ();
697 $hook_params['section'] = $section ;
698 $hook_params['reference'] = $reference ;
699 $hook_params['action'] = $action ;
700 $hook_params['value'] = $value ;
701 $hook_params['result'] = false ;
702 plugin_hook_by_reference ("role_has_permission", $hook_params);
703 return $hook_params['result'] ;
709 * update - update a role in the database.
711 * @param string The name of the role.
712 * @param array A multi-dimensional array of data in this format: $data['section_name']['ref_id']=$val
713 * @param boolean Perform permission checking
714 * @return boolean True on success or false on failure.
716 function update($role_name,$data,$check_perms=true) {
719 if ($this->getHomeProject() == NULL) {
720 if (!forge_check_global_perm ('forge_admin')) {
721 $this->setPermissionDeniedError();
724 } elseif (!forge_check_perm ('project_admin', $this->getHomeProject()->getID())) {
725 $this->setPermissionDeniedError();
733 if ($role_name != $this->getName()) {
734 $this->setName($role_name) ;
737 foreach ($data as $sect => $refs) {
738 foreach ($refs as $refid => $value) {
739 $this->setSetting ($sect, $refid, $value) ;
743 $hook_params = array ();
744 $hook_params['role'] =& $this;
745 $hook_params['role_id'] = $this->getID();
746 $hook_params['data'] = $data;
747 plugin_hook ("role_update", $hook_params);
751 $this->fetchData($this->getID());
753 foreach ($this->getUsers() as $u) {
754 if (!$SYS->sysCheckCreateUser($u->getID())) {
755 $this->setError($SYS->getErrorMessage());
763 function getDisplayableName($group = NULL) {
764 if ($this->getHomeProject() == NULL) {
765 return sprintf (_('%s (global role)'),
767 } elseif ($group == NULL
768 || $this->getHomeProject()->getID() != $group->getID()) {
769 return sprintf (_('%s (in project %s)'),
771 $this->getHomeProject()->getPublicName()) ;
773 return $this->getName () ;
777 function removeObsoleteSettings () {
780 // Remove obsolete project-wide settings
781 $sections = array ('project_read', 'project_admin', 'frs', 'scm', 'docman', 'tracker_admin', 'new_tracker', 'forum_admin', 'new_forum', 'pm_admin', 'new_pm', 'webcal') ;
782 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)',
783 array ($this->getID(),
784 db_string_array_to_any_clause($sections))) ;
787 // Remove obsolete settings for multiple-instance tools
788 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))',
789 array ($this->getID(),
791 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))',
792 array ($this->getID(),
794 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))',
795 array ($this->getID(),
799 $this->fetchData($this->getID());
803 function normalizeDataForSection (&$new_sa, $section) {
804 if (array_key_exists ($section, $this->setting_array)) {
805 $new_sa[$section][0] = $this->setting_array[$section][0] ;
806 } elseif (array_key_exists ($this->data_array['role_name'], $this->defaults)
807 && array_key_exists ($section, $this->defaults[$this->data_array['role_name']])) {
808 $new_sa[$section][0] = $this->defaults[$this->data_array['role_name']][$section] ;
810 $new_sa[$section][0] = 0 ;
815 function normalizePermsForSection (&$new_pa, $section, $refid) {
816 if (array_key_exists ($section, $this->perms_array)
817 && array_key_exists ($refid, $this->perms_array[$section])) {
818 $new_pa[$section][$refid] = $this->perms_array[$section][$refid] ;
819 } elseif (array_key_exists ($this->data_array['role_name'], $this->defaults)
820 && array_key_exists ($section, $this->defaults[$this->data_array['role_name']])) {
821 $new_pa[$section][$refid] = $this->defaults[$this->data_array['role_name']][$section] ;
823 $new_pa[$section][$refid] = 0 ;
828 function normalizeData () { // From the PFO spec
829 $this->removeObsoleteSettings () ;
831 $this->fetchData ($this->getID()) ;
833 $projects = $this->getLinkedProjects() ;
837 // Add missing settings
838 // ...project-wide settings
839 $arr = array ('project_read', 'project_admin', 'frs', 'scm', 'docman', 'tracker_admin', 'new_tracker', 'forum_admin', 'new_forum', 'pm_admin', 'new_pm', 'webcal') ;
840 foreach ($projects as $p) {
841 foreach ($arr as $section) {
842 $this->normalizePermsForSection ($new_pa, $section, $p->getID()) ;
845 $this->normalizePermsForSection ($new_pa, 'forge_admin', -1) ;
846 $this->normalizePermsForSection ($new_pa, 'approve_projects', -1) ;
847 $this->normalizePermsForSection ($new_pa, 'approve_news', -1) ;
848 $this->normalizePermsForSection ($new_pa, 'forge_stats', -1) ;
850 $hook_params = array ();
851 $hook_params['role'] =& $this;
852 $hook_params['new_sa'] =& $new_sa ;
853 $hook_params['new_pa'] =& $new_pa ;
854 plugin_hook ("role_normalize", $hook_params);
856 // ...tracker-related settings
857 $new_sa['tracker'] = array () ;
858 $new_pa['tracker'] = array () ;
859 foreach ($projects as $p) {
860 $atf = new ArtifactTypeFactory ($p) ;
861 $trackerids = $atf->getAllArtifactTypeIds () ;
862 foreach ($trackerids as $tid) {
863 if (array_key_exists ('tracker', $this->perms_array)
864 && array_key_exists ($tid, $this->perms_array['tracker']) ) {
865 $new_pa['tracker'][$tid] = $this->perms_array['tracker'][$tid] ;
866 } elseif (array_key_exists ('new_tracker', $this->perms_array)
867 && array_key_exists ($p->getID(), $this->perms_array['new_tracker']) ) {
868 $new_pa['tracker'][$tid] = $new_pa['new_tracker'][$p->getID()] ;
873 // ...forum-related settings
874 $new_sa['forum'] = array () ;
875 $new_pa['forum'] = array () ;
876 foreach ($projects as $p) {
877 if (!$p->usesForum()) {
880 $ff = new ForumFactory ($p) ;
881 $fids = $ff->getAllForumIdsWithNews () ;
882 foreach ($fids as $fid) {
883 if (array_key_exists ('forum', $this->perms_array)
884 && array_key_exists ($fid, $this->perms_array['forum']) ) {
885 $new_pa['forum'][$fid] = $this->perms_array['forum'][$fid] ;
886 } elseif (array_key_exists ('new_forum', $this->perms_array)
887 && array_key_exists ($p->getID(), $this->perms_array['new_forum']) ) {
888 $new_pa['forum'][$fid] = $new_pa['new_forum'][$p->getID()] ;
893 // ...pm-related settings
894 $new_sa['pm'] = array () ;
895 $new_pa['pm'] = array () ;
896 foreach ($projects as $p) {
897 $pgf = new ProjectGroupFactory ($p) ;
898 $pgids = $pgf->getAllProjectGroupIds () ;
899 foreach ($pgids as $gid) {
900 if (array_key_exists ('pm', $this->perms_array)
901 && array_key_exists ($gid, $this->perms_array['pm']) ) {
902 $new_pa['pm'][$gid] = $this->perms_array['pm'][$gid] ;
903 } elseif (array_key_exists ('new_pm', $this->perms_array)
904 && array_key_exists ($p->getID(), $this->perms_array['new_pm']) ) {
905 $new_pa['pm'][$gid] = $new_pa['new_pm'][$p->getID()] ;
911 $this->update ($this->getName(), $new_pa, false) ;
919 * TODO: RBAC::RoleExplicit Enter description here ...
922 abstract class RoleExplicit extends BaseRole implements PFO_RoleExplicit {
923 public function addUsers($users) {
927 foreach ($users as $user) {
928 $ids[] = $user->getID();
931 $already_there = array();
932 $res = db_query_params('SELECT user_id FROM pfo_user_role WHERE user_id=ANY($1) AND role_id=$2',
933 array(db_int_array_to_any_clause($ids), $this->getID()));
937 while ($arr = db_fetch_array($res)) {
938 $already_there[] = $arr['user_id'] ;
941 foreach ($ids as $id) {
942 if (!in_array ($id, $already_there)) {
943 $res = db_query_params ('INSERT INTO pfo_user_role (user_id, role_id) VALUES ($1, $2)',
952 foreach ($this->getLinkedProjects() as $p) {
953 foreach ($ids as $uid) {
954 if (!$SYS->sysGroupCheckUser($p->getID(),$uid)) {
963 public function addUser ($user) {
964 return $this->addUsers (array ($user)) ;
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 return $this->removeUsers (array ($user)) ;
992 public function getUsers() {
994 $res = db_query_params ('SELECT user_id FROM pfo_user_role WHERE role_id=$1',
995 array ($this->getID())) ;
996 while ($arr = db_fetch_array($res)) {
997 $result[] = user_get_object ($arr['user_id']) ;
1003 public function hasUser($user) {
1004 $res = db_query_params ('SELECT user_id FROM pfo_user_role WHERE user_id=$1 AND role_id=$2',
1005 array ($user->getID(), $this->getID())) ;
1006 if ($res && db_numrows($res)) {
1013 function getID() { // From the PFO spec
1014 return $this->data_array['role_id'];
1017 function getName() { // From the PFO spec
1018 return $this->data_array['role_name'];
1022 class RoleAnonymous extends BaseRole implements PFO_RoleAnonymous {
1023 // This role is implemented as a singleton
1024 private static $_instance ;
1026 public static function getInstance() {
1027 if (isset(self::$_instance)) {
1028 return self::$_instance ;
1032 self::$_instance = new $c ;
1034 $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',
1035 array ('PFO_RoleAnonymous')) ;
1036 if (!$res || !db_numrows($res)) {
1037 throw new Exception ("No PFO_RoleAnonymous role in the database") ;
1039 self::$_instance->_role_id = db_result ($res, 0, 'role_id') ;
1041 $hook_params = array ();
1042 $hook_params['role'] =& self::$_instance;
1043 plugin_hook ("role_get", $hook_params);
1045 self::$_instance->fetchData (self::$_instance->_role_id) ;
1047 return self::$_instance ;
1050 public function getID () {
1051 return $this->_role_id ;
1053 public function isPublic () {
1056 public function setPublic ($flag) {
1057 throw new Exception ("Can't setPublic() on RoleAnonymous") ;
1059 public function getHomeProject () {
1062 public function getName () {
1063 return _('Anonymous/not logged in') ;
1065 public function setName ($name) {
1066 throw new Exception ("Can't setName() on RoleAnonymous") ;
1070 class RoleLoggedIn extends BaseRole implements PFO_RoleLoggedIn {
1071 // This role is implemented as a singleton
1072 private static $_instance ;
1074 public static function getInstance() {
1075 if (isset(self::$_instance)) {
1076 return self::$_instance ;
1080 self::$_instance = new $c ;
1082 $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',
1083 array ('PFO_RoleLoggedIn')) ;
1084 if (!$res || !db_numrows($res)) {
1085 throw new Exception ("No PFO_RoleLoggedIn role in the database") ;
1087 self::$_instance->_role_id = db_result ($res, 0, 'role_id') ;
1089 $hook_params = array ();
1090 $hook_params['role'] =& self::$_instance;
1091 plugin_hook ("role_get", $hook_params);
1093 self::$_instance->fetchData (self::$_instance->_role_id) ;
1095 return self::$_instance ;
1098 public function getID () {
1099 return $this->_role_id ;
1101 public function isPublic () {
1104 public function setPublic ($flag) {
1105 throw new Exception ("Can't setPublic() on RoleLoggedIn") ;
1107 public function getHomeProject () {
1110 public function getName () {
1111 return _('Any user logged in') ;
1113 public function setName ($name) {
1114 throw new Exception ("Can't setName() on RoleLoggedIn") ;
1118 abstract class RoleUnion extends BaseRole implements PFO_RoleUnion {
1119 public function addRole ($role) {
1120 throw new Exception ("Not implemented") ;
1122 public function removeRole ($role) {
1123 throw new Exception ("Not implemented") ;
1128 * TODO: Enter description here ...
1131 class RoleComparator {
1132 var $criterion = 'composite' ;
1133 var $reference_project = NULL ;
1135 function Compare ($a, $b) {
1136 switch ($this->criterion) {
1138 return strcoll ($a->getName(), $b->getName()) ;
1141 $aid = $a->getID() ;
1142 $bid = $b->getID() ;
1146 return ($a < $b) ? -1 : 1;
1150 if ($this->reference_project == NULL) {
1151 return $this->CompareNoRef ($a, $b) ;
1153 $rpid = $this->reference_project->getID () ;
1154 $ap = $a->getHomeProject() ;
1155 $bp = $b->getHomeProject() ;
1156 $a_is_local = ($ap != NULL && $ap->getID() == $rpid) ; // Local
1157 $b_is_local = ($bp != NULL && $bp->getID() == $rpid) ;
1159 if ($a_is_local && !$b_is_local) {
1161 } elseif (!$a_is_local && $b_is_local) {
1164 return $this->CompareNoRef ($a, $b) ;
1169 * TODO: Enter description here ...
1174 function CompareNoRef ($a, $b) {
1175 $ap = $a->getHomeProject() ;
1176 $bp = $b->getHomeProject() ;
1177 if ($ap == NULL && $bp != NULL) {
1179 } elseif ($ap != NULL && $bp == NULL) {
1181 } elseif ($ap == NULL && $bp == NULL) {
1182 $tmp = strcoll ($a->getName(), $b->getName()) ;
1185 $projcmp = new ProjectComparator () ;
1186 $projcmp->criterion = 'name' ;
1187 $tmp = $projcmp->Compare ($ap, $bp) ;
1188 if ($tmp) { /* Different projects, sort accordingly */
1191 return strcoll ($a->getName(), $b->getName()) ;
1196 function sortRoleList (&$list, $relative_to = NULL, $criterion='composite') {
1197 $cmp = new RoleComparator () ;
1198 $cmp->criterion = $criterion ;
1199 $cmp->reference_project = $relative_to ;
1201 return usort ($list, array ($cmp, 'Compare')) ;
1206 // c-file-style: "bsd"