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, 9, 11, 13, 15),
73 'pm' => array(0, 1, 3, 5, 7),
74 'forum' => array(0, 1, 2, 3, 4),
76 'new_tracker' => array(0, 1, 9, 11, 13, 15),
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),
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,
108 'Senior Developer' => array( 'project_read' => 1,
114 'tracker_admin' => 1,
119 'Junior Developer' => array( 'project_read' => 1,
127 'Doc Writer' => array( 'project_read' => 1,
134 'Support Tech' => array( 'project_read' => 1,
138 'tracker_admin' => 1,
146 public function getUsers() {
149 public function hasUser($user) {
150 throw new Exception ("Not implemented") ;
152 function hasGlobalPermission($section, $action = NULL) {
153 return $this->hasPermission ($section, -1, $action) ;
155 public function getSettings() {
156 throw new Exception ("Not implemented") ;
158 public function setSettings($data) {
159 throw new Exception ("Not implemented") ;
161 public function delete () {
162 throw new Exception ("Not implemented") ;
166 * getLinkedProjects - List of projects referencing that role
168 * Includes the home project (for roles that have one)
170 * @return array Array of Group objects
172 public function getLinkedProjects() {
175 $hp = $this->getHomeProject();
177 $ids[] = $hp->getID();
180 $res = db_query_params('SELECT group_id FROM role_project_refs WHERE role_id=$1',
181 array($this->getID()));
183 while ($arr = db_fetch_array ($res)) {
184 $ids[] = $arr['group_id'];
188 return group_get_objects(array_unique($ids));
191 function linkProject ($project) { // From the PFO spec
193 $hp = $this->getHomeProject();
194 if ($hp != NULL && $hp->getID() == $project->getID()) {
195 $this->setError(_("Can't link to home project"));
199 $res = db_query_params('SELECT group_id FROM role_project_refs WHERE role_id=$1 AND group_id=$2',
200 array($this->getID(),
203 if (db_numrows($res)) {
206 $res = db_query_params('INSERT INTO role_project_refs (role_id, group_id) VALUES ($1, $2)',
207 array($this->getID(),
209 if (!$res || db_affected_rows($res) < 1) {
210 $this->setError('linkProject('.$project->getID().') '.db_error());
214 $this->normalizeData();
216 foreach ($this->getUsers() as $u) {
217 if (!$SYS->sysCheckCreateUser($u->getID())) {
218 $this->setError($SYS->getErrorMessage());
226 function unlinkProject($project) { // From the PFO spec
228 $hp = $this->getHomeProject();
229 if ($hp != NULL && $hp->getID() == $project->getID()) {
230 $this->setError (_("Can't unlink from home project"));
234 $res = db_query_params('DELETE FROM role_project_refs WHERE role_id=$1 AND group_id=$2',
235 array($this->getID(),
238 $this->setError('unlinkProject('.$project->getID().') '.db_error());
242 $this->removeObsoleteSettings ();
244 foreach ($this->getUsers() as $u) {
245 if (!$SYS->sysCheckCreateUser($u->getID())) {
246 $this->setError($SYS->getErrorMessage());
255 * fetchData - May need to refresh database fields.
257 * If an update occurred and you need to access the updated info.
259 * @return boolean success;
261 function fetchData($role_id) {
262 unset($this->data_array);
263 unset($this->setting_array);
264 unset($this->perms_array);
266 $res = db_query_params('SELECT * FROM pfo_role WHERE role_id=$1',
268 if (!$res || db_numrows($res) < 1) {
269 $this->setError('BaseRole::fetchData()::'.db_error());
272 $this->data_array = db_fetch_array($res);
273 if ($this->data_array['is_public'] == 't') {
274 $this->data_array['is_public'] = true;
276 $this->data_array['is_public'] = false;
278 $res = db_query_params('SELECT section_name, ref_id, perm_val FROM pfo_role_setting WHERE role_id=$1',
281 $this->setError('BaseRole::fetchData()::'.db_error());
284 // TODO: document perms_array
285 $this->perms_array=array();
286 while ($arr = db_fetch_array($res)) {
287 $this->perms_array[$arr['section_name']][$arr['ref_id']] = $arr['perm_val'];
293 function setSetting ($section, $reference, $value) {
294 $cur = $this->getSettingRaw($section, $reference);
295 if (($value == $cur) && ($cur != NULL)) {
299 $role_id = $this->getID () ;
301 $res = db_query_params ('DELETE FROM pfo_role_setting WHERE role_id=$1 AND section_name=$2 AND ref_id=$3',
306 $res = db_query_params ('INSERT INTO pfo_role_setting (role_id, section_name, ref_id, perm_val) VALUES ($1, $2, $3, $4)',
311 $this->perms_array[$section][$reference] = $value;
314 function getSettingsForProject ($project) {
316 $group_id = $project->getID() ;
318 $sections = array ('project_read', 'project_admin', 'frs', 'scm', 'docman', 'tracker_admin', 'new_tracker') ;
319 foreach ($sections as $section) {
320 $result[$section][$group_id] = $this->getVal ($section, $group_id) ;
323 $atf = new ArtifactTypeFactory ($project) ;
324 $tids = $atf->getAllArtifactTypeIds () ;
325 foreach ($tids as $tid) {
326 $result['tracker'][$tid] = $this->getVal ('tracker', $tid) ;
328 array_push ($sections,'tracker');
330 $sections_forum = array('forum_admin', 'new_forum');
331 foreach ($sections_forum as $section_forum) {
332 $result[$section_forum][$group_id] = $this->getVal ($section_forum, $group_id) ;
334 $sections = array_merge($sections, $sections_forum);
336 $ff = new ForumFactory ($project) ;
337 $fids = $ff->getAllForumIds () ;
338 foreach ($fids as $fid) {
339 $result['forum'][$fid] = $this->getVal ('forum', $fid) ;
341 array_push ($sections,'forum');
343 $sections_pm = array('pm_admin', 'new_pm');
344 foreach ($sections_pm as $section_pm) {
345 $result[$section_pm][$group_id] = $this->getVal ($section_pm, $group_id) ;
347 $sections = array_merge($sections, $sections_pm);
348 $pgf = new ProjectGroupFactory ($project) ;
349 $pgids = $pgf->getAllProjectGroupIds () ;
350 foreach ($pgids as $pgid) {
351 $result['pm'][$pgid] = $this->getVal ('pm', $pgid) ;
353 array_push ($sections,'pm') ;
356 // Add settings not yet listed so far (probably plugins)
357 // Currently handled:
358 // - global settings (ignored here)
359 // - project-wide settings (core and plugins)
360 // - settings for multiple-instance tools coming from the core (trackers/pm/forums)
362 // - settings for multiple-instance tools from plugins
363 foreach (array_keys ($this->perms_array) as $section) {
364 if (!in_array ($section, $sections)) {
365 if (!in_array ($section, $this->global_settings)) {
366 $result[$section][$group_id] = $this->getVal ($section, $group_id) ;
375 * TODO: Enter description here ...
378 function getGlobalSettings () {
381 $sections = array ('forge_admin', 'forge_stats', 'approve_projects', 'approve_news') ;
382 foreach ($sections as $section) {
383 $result[$section][-1] = $this->getVal($section, -1) ;
385 // Add settings not yet listed so far (probably plugins)
386 foreach (array_keys ($this->perms_array) as $section) {
387 if (!in_array ($section, $sections)) {
388 if (in_array ($section, $this->global_settings)) {
389 $result[$section][-1] = $this->getVal ($section, -1) ;
398 * TODO: Enter description here ...
399 * @param unknown_type $section
400 * @param unknown_type $reference
401 * @return number|boolean
403 function getSetting($section, $reference) {
404 $value = $this->getSettingRaw($section, $reference);
405 if ($value == NULL) {
418 case 'approve_projects':
420 if ($this->hasGlobalPermission('forge_admin')) {
427 if ($this->hasGlobalPermission('forge_admin')) {
433 case 'project_admin':
434 if ($this->hasGlobalPermission('forge_admin')) {
441 case 'tracker_admin':
444 if ($this->hasPermission('project_admin', $reference)) {
451 if ($this->hasPermission('project_admin', $reference)) {
458 if ($this->hasPermission('project_admin', $reference)) {
465 if ($this->hasPermission('project_admin', $reference)) {
472 if ($this->hasPermission('forum_admin', forum_get_groupid($reference))) {
478 if ($this->hasPermission('forum_admin', $reference)) {
485 if ($this->hasPermission('tracker_admin', artifacttype_get_groupid($reference))) {
491 if ($this->hasPermission('tracker_admin', $reference)) {
498 if ($this->hasPermission('pm_admin', projectgroup_get_groupid($reference))) {
504 if ($this->hasPermission('pm_admin', $reference)) {
510 $hook_params = array ();
511 $hook_params['role'] = $this ;
512 $hook_params['section'] = $section ;
513 $hook_params['reference'] = $reference ;
514 $hook_params['value'] = $value ;
515 $hook_params['result'] = NULL ;
516 plugin_hook_by_reference ("role_get_setting", $hook_params);
517 return $hook_params['result'] ;
522 function getSettingRaw($section, $reference) {
523 if (isset ($this->perms_array[$section][$reference])) {
524 return $this->perms_array[$section][$reference] ;
529 * getVal - get a value out of the array of settings for this role.
531 * @param string The name of the role.
532 * @param integer The ref_id (ex: group_artifact_id, group_forum_id) for this item.
533 * @return integer The value of this item.
535 function getVal($section, $ref_id) {
536 global $role_default_array;
540 return $this->getSetting($section, $ref_id) ;
544 * &getRoleVals - get all the values and language text strings for this section.
546 * @return array Assoc array of values for this section.
548 function &getRoleVals($section) {
549 global $role_vals, $rbac_permission_names;
550 setup_rbac_strings () ;
553 // Optimization - save array so it is only built once per page view
555 if (!isset($role_vals[$section])) {
557 for ($i=0; $i<count($this->role_values[$section]); $i++) {
559 // Build an associative array of these key values + localized description
561 $role_vals[$section][$this->role_values[$section][$i]] =
562 util_ifsetor($rbac_permission_names["$section".$this->role_values[$section][$i]],
563 _('UNKNOWN (internal error, report bug to FusionForge)'));
566 return $role_vals[$section];
569 function hasPermission($section, $reference, $action = NULL) {
572 $value = $this->getSetting ($section, $reference) ;
579 case 'approve_projects':
581 case 'project_admin':
583 case 'tracker_admin':
586 return ($value >= 1) ;
592 return ($value >= 1) ;
595 return ($value >= 2) ;
603 return ($value >= 1) ;
606 return ($value >= 2) ;
614 return ($value >= 1) ;
617 return ($value >= 2) ;
620 return ($value >= 3) ;
623 return ($value >= 4) ;
631 return ($value >= 1) ;
634 return ($value >= 2) ;
637 return ($value >= 3) ;
646 return ($value >= 1) ;
649 return ($value >= 2) ;
651 case 'unmoderated_post':
652 return ($value >= 3) ;
655 return ($value >= 4) ;
664 return (($value & 1) != 0) ;
667 return (($value & 2) != 0) ;
670 return (($value & 4) != 0) ;
673 return (($value & 8) != 0) ;
676 * bit4 (value & 16) is reserved
677 * for tracker item vote from Evolvis
686 return (($value & 1) != 0) ;
689 return (($value & 2) != 0) ;
692 return (($value & 4) != 0) ;
697 $hook_params = array ();
698 $hook_params['section'] = $section ;
699 $hook_params['reference'] = $reference ;
700 $hook_params['action'] = $action ;
701 $hook_params['value'] = $value ;
702 $hook_params['result'] = false ;
703 plugin_hook_by_reference ("role_has_permission", $hook_params);
704 return $hook_params['result'] ;
710 * update - update a role in the database.
712 * @param string The name of the role.
713 * @param array A multi-dimensional array of data in this format: $data['section_name']['ref_id']=$val
714 * @param boolean Perform permission checking
715 * @return boolean True on success or false on failure.
717 function update($role_name,$data,$check_perms=true) {
720 if ($this->getHomeProject() == NULL) {
721 if (!forge_check_global_perm ('forge_admin')) {
722 $this->setPermissionDeniedError();
725 } elseif (!forge_check_perm ('project_admin', $this->getHomeProject()->getID())) {
726 $this->setPermissionDeniedError();
734 if ($role_name != $this->getName()) {
735 $this->setName($role_name) ;
738 foreach ($data as $sect => $refs) {
739 foreach ($refs as $refid => $value) {
740 $this->setSetting ($sect, $refid, $value) ;
744 $hook_params = array ();
745 $hook_params['role'] =& $this;
746 $hook_params['role_id'] = $this->getID();
747 $hook_params['data'] = $data;
748 plugin_hook ("role_update", $hook_params);
752 $this->fetchData($this->getID());
754 foreach ($this->getUsers() as $u) {
755 if (!$SYS->sysCheckCreateUser($u->getID())) {
756 $this->setError($SYS->getErrorMessage());
764 function getDisplayableName($group = NULL) {
765 if ($this->getHomeProject() == NULL) {
766 return sprintf (_('%s (global role)'),
768 } elseif ($group == NULL
769 || $this->getHomeProject()->getID() != $group->getID()) {
770 return sprintf (_('%s (in project %s)'),
772 $this->getHomeProject()->getPublicName()) ;
774 return $this->getName () ;
778 function removeObsoleteSettings () {
781 // Remove obsolete project-wide settings
782 $sections = array ('project_read', 'project_admin', 'frs', 'scm', 'docman', 'tracker_admin', 'new_tracker', 'forum_admin', 'new_forum', 'pm_admin', 'new_pm') ;
783 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)',
784 array ($this->getID(),
785 db_string_array_to_any_clause($sections))) ;
788 // Remove obsolete settings for multiple-instance tools
789 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))',
790 array ($this->getID(),
792 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))',
793 array ($this->getID(),
795 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))',
796 array ($this->getID(),
800 $this->fetchData($this->getID());
804 function normalizeDataForSection (&$new_sa, $section) {
805 if (array_key_exists ($section, $this->setting_array)) {
806 $new_sa[$section][0] = $this->setting_array[$section][0] ;
807 } elseif (array_key_exists ($this->data_array['role_name'], $this->defaults)
808 && array_key_exists ($section, $this->defaults[$this->data_array['role_name']])) {
809 $new_sa[$section][0] = $this->defaults[$this->data_array['role_name']][$section] ;
811 $new_sa[$section][0] = 0 ;
816 function normalizePermsForSection (&$new_pa, $section, $refid) {
817 if (array_key_exists ($section, $this->perms_array)
818 && array_key_exists ($refid, $this->perms_array[$section])) {
819 $new_pa[$section][$refid] = $this->perms_array[$section][$refid] ;
820 } elseif (array_key_exists ($this->data_array['role_name'], $this->defaults)
821 && array_key_exists ($section, $this->defaults[$this->data_array['role_name']])) {
822 $new_pa[$section][$refid] = $this->defaults[$this->data_array['role_name']][$section] ;
824 $new_pa[$section][$refid] = 0 ;
829 function normalizeData () { // From the PFO spec
830 $this->removeObsoleteSettings () ;
832 $this->fetchData ($this->getID()) ;
834 $projects = $this->getLinkedProjects() ;
838 // Add missing settings
839 // ...project-wide settings
840 $arr = array ('project_read', 'project_admin', 'frs', 'scm', 'docman', 'tracker_admin', 'new_tracker', 'forum_admin', 'new_forum', 'pm_admin', 'new_pm') ;
841 foreach ($projects as $p) {
842 foreach ($arr as $section) {
843 $this->normalizePermsForSection ($new_pa, $section, $p->getID()) ;
846 $this->normalizePermsForSection ($new_pa, 'forge_admin', -1) ;
847 $this->normalizePermsForSection ($new_pa, 'approve_projects', -1) ;
848 $this->normalizePermsForSection ($new_pa, 'approve_news', -1) ;
849 $this->normalizePermsForSection ($new_pa, 'forge_stats', -1) ;
851 $hook_params = array ();
852 $hook_params['role'] =& $this;
853 $hook_params['new_sa'] =& $new_sa ;
854 $hook_params['new_pa'] =& $new_pa ;
855 plugin_hook ("role_normalize", $hook_params);
857 // ...tracker-related settings
858 $new_sa['tracker'] = array () ;
859 $new_pa['tracker'] = array () ;
860 foreach ($projects as $p) {
861 $atf = new ArtifactTypeFactory ($p) ;
862 $trackerids = $atf->getAllArtifactTypeIds () ;
863 foreach ($trackerids as $tid) {
864 if (array_key_exists ('tracker', $this->perms_array)
865 && array_key_exists ($tid, $this->perms_array['tracker']) ) {
866 $new_pa['tracker'][$tid] = $this->perms_array['tracker'][$tid] ;
867 } elseif (array_key_exists ('new_tracker', $this->perms_array)
868 && array_key_exists ($p->getID(), $this->perms_array['new_tracker']) ) {
869 $new_pa['tracker'][$tid] = $new_pa['new_tracker'][$p->getID()] ;
874 // ...forum-related settings
875 $new_sa['forum'] = array () ;
876 $new_pa['forum'] = array () ;
877 foreach ($projects as $p) {
878 $ff = new ForumFactory ($p) ;
879 $fids = $ff->getAllForumIds () ;
880 foreach ($fids as $fid) {
881 if (array_key_exists ('forum', $this->perms_array)
882 && array_key_exists ($fid, $this->perms_array['forum']) ) {
883 $new_pa['forum'][$fid] = $this->perms_array['forum'][$fid] ;
884 } elseif (array_key_exists ('new_forum', $this->perms_array)
885 && array_key_exists ($p->getID(), $this->perms_array['new_forum']) ) {
886 $new_pa['forum'][$fid] = $new_pa['new_forum'][$p->getID()] ;
891 // ...pm-related settings
892 $new_sa['pm'] = array () ;
893 $new_pa['pm'] = array () ;
894 foreach ($projects as $p) {
895 $pgf = new ProjectGroupFactory ($p) ;
896 $pgids = $pgf->getAllProjectGroupIds () ;
897 foreach ($pgids as $gid) {
898 if (array_key_exists ('pm', $this->perms_array)
899 && array_key_exists ($gid, $this->perms_array['pm']) ) {
900 $new_pa['pm'][$gid] = $this->perms_array['pm'][$gid] ;
901 } elseif (array_key_exists ('new_pm', $this->perms_array)
902 && array_key_exists ($p->getID(), $this->perms_array['new_pm']) ) {
903 $new_pa['pm'][$gid] = $new_pa['new_pm'][$p->getID()] ;
909 $this->update ($this->getName(), $new_pa, false) ;
917 * TODO: RBAC::RoleExplicit Enter description here ...
920 abstract class RoleExplicit extends BaseRole implements PFO_RoleExplicit {
921 public function addUsers($users) {
925 foreach ($users as $user) {
926 $ids[] = $user->getID();
929 $already_there = array();
930 $res = db_query_params('SELECT user_id FROM pfo_user_role WHERE user_id=ANY($1) AND role_id=$2',
931 array(db_int_array_to_any_clause($ids), $this->getID()));
935 while ($arr = db_fetch_array($res)) {
936 $already_there[] = $arr['user_id'] ;
939 foreach ($ids as $id) {
940 if (!in_array ($id, $already_there)) {
941 $res = db_query_params ('INSERT INTO pfo_user_role (user_id, role_id) VALUES ($1, $2)',
950 foreach ($this->getLinkedProjects() as $p) {
951 foreach ($ids as $uid) {
952 if (!$SYS->sysGroupCheckUser($p->getID(),$uid)) {
961 public function addUser ($user) {
962 if (!$this->addUsers (array ($user))) {
965 $hook_params['user'] = $user;
966 $hook_params['role'] = $this;
967 plugin_hook ("role_adduser", $hook_params);
972 public function removeUsers($users) {
976 foreach ($users as $user) {
977 $ids[] = $user->getID() ;
980 $already_there = array () ;
981 $res = db_query_params ('DELETE FROM pfo_user_role WHERE user_id=ANY($1) AND role_id=$2',
982 array (db_int_array_to_any_clause($ids), $this->getID())) ;
984 foreach ($this->getLinkedProjects() as $p) {
985 foreach ($ids as $uid) {
986 $SYS->sysGroupCheckUser($p->getID(),$uid) ;
993 public function removeUser ($user) {
994 if(!$this->removeUsers (array ($user))){
997 $hook_params['user'] = $user;
998 $hook_params['role'] = $this;
999 plugin_hook ("role_removeuser", $hook_params);
1004 public function getUsers() {
1005 $result = array () ;
1006 $res = db_query_params ('SELECT user_id FROM pfo_user_role WHERE role_id=$1',
1007 array ($this->getID())) ;
1008 while ($arr = db_fetch_array($res)) {
1009 $result[] = user_get_object ($arr['user_id']) ;
1015 public function hasUser($user) {
1016 $res = db_query_params ('SELECT user_id FROM pfo_user_role WHERE user_id=$1 AND role_id=$2',
1017 array ($user->getID(), $this->getID())) ;
1018 if ($res && db_numrows($res)) {
1025 function getID() { // From the PFO spec
1026 return $this->data_array['role_id'];
1029 function getName() { // From the PFO spec
1030 return $this->data_array['role_name'];
1034 class RoleAnonymous extends BaseRole implements PFO_RoleAnonymous {
1035 // This role is implemented as a singleton
1036 private static $_instance ;
1038 public static function getInstance() {
1039 if (isset(self::$_instance)) {
1040 return self::$_instance ;
1044 self::$_instance = new $c ;
1046 $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',
1047 array ('PFO_RoleAnonymous')) ;
1048 if (!$res || !db_numrows($res)) {
1049 throw new Exception ("No PFO_RoleAnonymous role in the database") ;
1051 self::$_instance->_role_id = db_result ($res, 0, 'role_id') ;
1053 $hook_params = array ();
1054 $hook_params['role'] =& self::$_instance;
1055 plugin_hook ("role_get", $hook_params);
1057 self::$_instance->fetchData (self::$_instance->_role_id) ;
1059 return self::$_instance ;
1062 public function getID () {
1063 return $this->_role_id ;
1065 public function isPublic () {
1068 public function setPublic ($flag) {
1069 throw new Exception ("Can't setPublic() on RoleAnonymous") ;
1071 public function getHomeProject () {
1074 public function getName () {
1075 return _('Anonymous/not logged in') ;
1077 public function setName ($name) {
1078 throw new Exception ("Can't setName() on RoleAnonymous") ;
1082 class RoleLoggedIn extends BaseRole implements PFO_RoleLoggedIn {
1083 // This role is implemented as a singleton
1084 private static $_instance ;
1086 public static function getInstance() {
1087 if (isset(self::$_instance)) {
1088 return self::$_instance ;
1092 self::$_instance = new $c ;
1094 $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',
1095 array ('PFO_RoleLoggedIn')) ;
1096 if (!$res || !db_numrows($res)) {
1097 throw new Exception ("No PFO_RoleLoggedIn role in the database") ;
1099 self::$_instance->_role_id = db_result ($res, 0, 'role_id') ;
1101 $hook_params = array ();
1102 $hook_params['role'] =& self::$_instance;
1103 plugin_hook ("role_get", $hook_params);
1105 self::$_instance->fetchData (self::$_instance->_role_id) ;
1107 return self::$_instance ;
1110 public function getID () {
1111 return $this->_role_id ;
1113 public function isPublic () {
1116 public function setPublic ($flag) {
1117 throw new Exception ("Can't setPublic() on RoleLoggedIn") ;
1119 public function getHomeProject () {
1122 public function getName () {
1123 return _('Any user logged in') ;
1125 public function setName ($name) {
1126 throw new Exception ("Can't setName() on RoleLoggedIn") ;
1130 abstract class RoleUnion extends BaseRole implements PFO_RoleUnion {
1131 public function addRole ($role) {
1132 throw new Exception ("Not implemented") ;
1134 public function removeRole ($role) {
1135 throw new Exception ("Not implemented") ;
1140 * TODO: Enter description here ...
1143 class RoleComparator {
1144 var $criterion = 'composite' ;
1145 var $reference_project = NULL ;
1147 function Compare ($a, $b) {
1148 switch ($this->criterion) {
1150 return strcoll ($a->getName(), $b->getName()) ;
1153 $aid = $a->getID() ;
1154 $bid = $b->getID() ;
1158 return ($a < $b) ? -1 : 1;
1162 if ($this->reference_project == NULL) {
1163 return $this->CompareNoRef ($a, $b) ;
1165 $rpid = $this->reference_project->getID () ;
1166 $ap = $a->getHomeProject() ;
1167 $bp = $b->getHomeProject() ;
1168 $a_is_local = ($ap != NULL && $ap->getID() == $rpid) ; // Local
1169 $b_is_local = ($bp != NULL && $bp->getID() == $rpid) ;
1171 if ($a_is_local && !$b_is_local) {
1173 } elseif (!$a_is_local && $b_is_local) {
1176 return $this->CompareNoRef ($a, $b) ;
1181 * TODO: Enter description here ...
1186 function CompareNoRef ($a, $b) {
1187 $ap = $a->getHomeProject() ;
1188 $bp = $b->getHomeProject() ;
1189 if ($ap == NULL && $bp != NULL) {
1191 } elseif ($ap != NULL && $bp == NULL) {
1193 } elseif ($ap == NULL && $bp == NULL) {
1194 $tmp = strcoll ($a->getName(), $b->getName()) ;
1197 $projcmp = new ProjectComparator () ;
1198 $projcmp->criterion = 'name' ;
1199 $tmp = $projcmp->Compare ($ap, $bp) ;
1200 if ($tmp) { /* Different projects, sort accordingly */
1203 return strcoll ($a->getName(), $b->getName()) ;
1208 function sortRoleList (&$list, $relative_to = NULL, $criterion='composite') {
1209 $cmp = new RoleComparator () ;
1210 $cmp->criterion = $criterion ;
1211 $cmp->reference_project = $relative_to ;
1213 return usort ($list, array ($cmp, 'Compare')) ;
1218 // c-file-style: "bsd"