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 if (!$atf->isError()) {
325 $tids = $atf->getAllArtifactTypeIds () ;
326 foreach ($tids as $tid) {
327 $result['tracker'][$tid] = $this->getVal ('tracker', $tid) ;
330 array_push ($sections,'tracker');
332 $sections_forum = array('forum_admin', 'new_forum');
333 foreach ($sections_forum as $section_forum) {
334 $result[$section_forum][$group_id] = $this->getVal ($section_forum, $group_id) ;
336 $sections = array_merge($sections, $sections_forum);
338 $ff = new ForumFactory ($project) ;
339 if (!$ff->isError()) {
340 $fids = $ff->getAllForumIdsWithNews () ;
341 foreach ($fids as $fid) {
342 $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);
353 $pgf = new ProjectGroupFactory ($project) ;
354 if (!$pgf->isError()) {
355 $pgids = $pgf->getAllProjectGroupIds () ;
356 foreach ($pgids as $pgid) {
357 $result['pm'][$pgid] = $this->getVal ('pm', $pgid) ;
360 array_push ($sections,'pm') ;
363 // Add settings not yet listed so far (probably plugins)
364 // Currently handled:
365 // - global settings (ignored here)
366 // - project-wide settings (core and plugins)
367 // - settings for multiple-instance tools coming from the core (trackers/pm/forums)
369 // - settings for multiple-instance tools from plugins
370 foreach (array_keys ($this->perms_array) as $section) {
371 if (!in_array ($section, $sections)) {
372 if (!in_array ($section, $this->global_settings)) {
373 $result[$section][$group_id] = $this->getVal ($section, $group_id) ;
382 * TODO: Enter description here ...
385 function getGlobalSettings () {
388 $sections = array ('forge_admin', 'forge_stats', 'approve_projects', 'approve_news') ;
389 foreach ($sections as $section) {
390 $result[$section][-1] = $this->getVal($section, -1) ;
392 // Add settings not yet listed so far (probably plugins)
393 foreach (array_keys ($this->perms_array) as $section) {
394 if (!in_array ($section, $sections)) {
395 if (in_array ($section, $this->global_settings)) {
396 $result[$section][-1] = $this->getVal ($section, -1) ;
405 * TODO: Enter description here ...
406 * @param unknown_type $section
407 * @param unknown_type $reference
408 * @return number|boolean
410 function getSetting($section, $reference) {
411 $value = $this->getSettingRaw($section, $reference);
412 if ($value == NULL) {
425 case 'approve_projects':
427 if ($this->hasGlobalPermission('forge_admin')) {
434 if ($this->hasGlobalPermission('forge_admin')) {
440 case 'project_admin':
441 if ($this->hasGlobalPermission('forge_admin')) {
448 case 'tracker_admin':
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('project_admin', $reference)) {
479 if ($this->hasPermission('forum_admin', forum_get_groupid($reference))) {
485 if ($this->hasPermission('forum_admin', $reference)) {
492 if ($this->hasPermission('tracker_admin', artifacttype_get_groupid($reference))) {
498 if ($this->hasPermission('tracker_admin', $reference)) {
505 if ($this->hasPermission('pm_admin', projectgroup_get_groupid($reference))) {
511 if ($this->hasPermission('pm_admin', $reference)) {
517 $hook_params = array ();
518 $hook_params['role'] = $this ;
519 $hook_params['section'] = $section ;
520 $hook_params['reference'] = $reference ;
521 $hook_params['value'] = $value ;
522 $hook_params['result'] = NULL ;
523 plugin_hook_by_reference ("role_get_setting", $hook_params);
524 return $hook_params['result'] ;
529 function getSettingRaw($section, $reference) {
530 if (isset ($this->perms_array[$section][$reference])) {
531 return $this->perms_array[$section][$reference] ;
536 * getVal - get a value out of the array of settings for this role.
538 * @param string The name of the role.
539 * @param integer The ref_id (ex: group_artifact_id, group_forum_id) for this item.
540 * @return integer The value of this item.
542 function getVal($section, $ref_id) {
543 global $role_default_array;
547 return $this->getSetting($section, $ref_id) ;
551 * &getRoleVals - get all the values and language text strings for this section.
553 * @return array Assoc array of values for this section.
555 function &getRoleVals($section) {
556 global $role_vals, $rbac_permission_names;
557 setup_rbac_strings () ;
560 // Optimization - save array so it is only built once per page view
562 if (!isset($role_vals[$section])) {
564 for ($i=0; $i<count($this->role_values[$section]); $i++) {
566 // Build an associative array of these key values + localized description
568 $role_vals[$section][$this->role_values[$section][$i]] =
569 util_ifsetor($rbac_permission_names["$section".$this->role_values[$section][$i]],
570 _('UNKNOWN (internal error, report bug to FusionForge)'));
573 return $role_vals[$section];
576 function hasPermission($section, $reference, $action = NULL) {
579 $value = $this->getSetting ($section, $reference) ;
586 case 'approve_projects':
588 case 'project_admin':
590 case 'tracker_admin':
593 return ($value >= 1) ;
599 return ($value >= 1) ;
602 return ($value >= 2) ;
610 return ($value >= 1) ;
613 return ($value >= 2) ;
621 return ($value >= 1) ;
624 return ($value >= 2) ;
627 return ($value >= 3) ;
630 return ($value >= 4) ;
638 return ($value >= 1) ;
641 return ($value >= 2) ;
644 return ($value >= 3) ;
653 return ($value >= 1) ;
656 return ($value >= 2) ;
658 case 'unmoderated_post':
659 return ($value >= 3) ;
662 return ($value >= 4) ;
671 return (($value & 1) != 0) ;
674 return (($value & 2) != 0) ;
677 return (($value & 4) != 0) ;
680 return (($value & 8) != 0) ;
683 * bit4 (value & 16) is reserved
684 * for tracker item vote from Evolvis
693 return (($value & 1) != 0) ;
696 return (($value & 2) != 0) ;
699 return (($value & 4) != 0) ;
704 $hook_params = array ();
705 $hook_params['section'] = $section ;
706 $hook_params['reference'] = $reference ;
707 $hook_params['action'] = $action ;
708 $hook_params['value'] = $value ;
709 $hook_params['result'] = false ;
710 plugin_hook_by_reference ("role_has_permission", $hook_params);
711 return $hook_params['result'] ;
717 * update - update a role in the database.
719 * @param string The name of the role.
720 * @param array A multi-dimensional array of data in this format: $data['section_name']['ref_id']=$val
721 * @param boolean Perform permission checking
722 * @return boolean True on success or false on failure.
724 function update($role_name,$data,$check_perms=true) {
727 if ($this->getHomeProject() == NULL) {
728 if (!forge_check_global_perm ('forge_admin')) {
729 $this->setPermissionDeniedError();
732 } elseif (!forge_check_perm ('project_admin', $this->getHomeProject()->getID())) {
733 $this->setPermissionDeniedError();
741 if ($role_name != $this->getName()) {
742 $this->setName($role_name) ;
745 foreach ($data as $sect => $refs) {
746 foreach ($refs as $refid => $value) {
747 $this->setSetting ($sect, $refid, $value) ;
751 $hook_params = array ();
752 $hook_params['role'] =& $this;
753 $hook_params['role_id'] = $this->getID();
754 $hook_params['data'] = $data;
755 plugin_hook ("role_update", $hook_params);
759 $this->fetchData($this->getID());
761 foreach ($this->getUsers() as $u) {
762 if (!$SYS->sysCheckCreateUser($u->getID())) {
763 $this->setError($SYS->getErrorMessage());
771 function getDisplayableName($group = NULL) {
772 if ($this->getHomeProject() == NULL) {
773 return sprintf (_('%s (global role)'),
775 } elseif ($group == NULL
776 || $this->getHomeProject()->getID() != $group->getID()) {
777 return sprintf (_('%s (in project %s)'),
779 $this->getHomeProject()->getPublicName()) ;
781 return $this->getName () ;
785 function removeObsoleteSettings () {
788 // Remove obsolete project-wide settings
789 $sections = array ('project_read', 'project_admin', 'frs', 'scm', 'docman', 'tracker_admin', 'new_tracker', 'forum_admin', 'new_forum', 'pm_admin', 'new_pm') ;
790 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)',
791 array ($this->getID(),
792 db_string_array_to_any_clause($sections))) ;
795 // Remove obsolete settings for multiple-instance tools
796 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))',
797 array ($this->getID(),
799 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))',
800 array ($this->getID(),
802 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))',
803 array ($this->getID(),
807 $this->fetchData($this->getID());
811 function normalizeDataForSection (&$new_sa, $section) {
812 if (array_key_exists ($section, $this->setting_array)) {
813 $new_sa[$section][0] = $this->setting_array[$section][0] ;
814 } elseif (array_key_exists ($this->data_array['role_name'], $this->defaults)
815 && array_key_exists ($section, $this->defaults[$this->data_array['role_name']])) {
816 $new_sa[$section][0] = $this->defaults[$this->data_array['role_name']][$section] ;
818 $new_sa[$section][0] = 0 ;
823 function normalizePermsForSection (&$new_pa, $section, $refid) {
824 if (array_key_exists ($section, $this->perms_array)
825 && array_key_exists ($refid, $this->perms_array[$section])) {
826 $new_pa[$section][$refid] = $this->perms_array[$section][$refid] ;
827 } elseif (array_key_exists ($this->data_array['role_name'], $this->defaults)
828 && array_key_exists ($section, $this->defaults[$this->data_array['role_name']])) {
829 $new_pa[$section][$refid] = $this->defaults[$this->data_array['role_name']][$section] ;
831 $new_pa[$section][$refid] = 0 ;
836 function normalizeData () { // From the PFO spec
837 $this->removeObsoleteSettings () ;
839 $this->fetchData ($this->getID()) ;
841 $projects = $this->getLinkedProjects() ;
845 // Add missing settings
846 // ...project-wide settings
847 $arr = array ('project_read', 'project_admin', 'frs', 'scm', 'docman', 'tracker_admin', 'new_tracker', 'forum_admin', 'new_forum', 'pm_admin', 'new_pm') ;
848 foreach ($projects as $p) {
849 foreach ($arr as $section) {
850 $this->normalizePermsForSection ($new_pa, $section, $p->getID()) ;
853 $this->normalizePermsForSection ($new_pa, 'forge_admin', -1) ;
854 $this->normalizePermsForSection ($new_pa, 'approve_projects', -1) ;
855 $this->normalizePermsForSection ($new_pa, 'approve_news', -1) ;
856 $this->normalizePermsForSection ($new_pa, 'forge_stats', -1) ;
858 $hook_params = array ();
859 $hook_params['role'] =& $this;
860 $hook_params['new_sa'] =& $new_sa ;
861 $hook_params['new_pa'] =& $new_pa ;
862 plugin_hook ("role_normalize", $hook_params);
864 // ...tracker-related settings
865 $new_sa['tracker'] = array () ;
866 $new_pa['tracker'] = array () ;
867 foreach ($projects as $p) {
868 $atf = new ArtifactTypeFactory ($p) ;
869 $trackerids = $atf->getAllArtifactTypeIds () ;
870 foreach ($trackerids as $tid) {
871 if (array_key_exists ('tracker', $this->perms_array)
872 && array_key_exists ($tid, $this->perms_array['tracker']) ) {
873 $new_pa['tracker'][$tid] = $this->perms_array['tracker'][$tid] ;
874 } elseif (array_key_exists ('new_tracker', $this->perms_array)
875 && array_key_exists ($p->getID(), $this->perms_array['new_tracker']) ) {
876 $new_pa['tracker'][$tid] = $new_pa['new_tracker'][$p->getID()] ;
881 // ...forum-related settings
882 $new_sa['forum'] = array () ;
883 $new_pa['forum'] = array () ;
884 foreach ($projects as $p) {
885 if (!$p->usesForum()) {
888 $ff = new ForumFactory ($p) ;
889 $fids = $ff->getAllForumIdsWithNews () ;
890 foreach ($fids as $fid) {
891 if (array_key_exists ('forum', $this->perms_array)
892 && array_key_exists ($fid, $this->perms_array['forum']) ) {
893 $new_pa['forum'][$fid] = $this->perms_array['forum'][$fid] ;
894 } elseif (array_key_exists ('new_forum', $this->perms_array)
895 && array_key_exists ($p->getID(), $this->perms_array['new_forum']) ) {
896 $new_pa['forum'][$fid] = $new_pa['new_forum'][$p->getID()] ;
901 // ...pm-related settings
902 $new_sa['pm'] = array () ;
903 $new_pa['pm'] = array () ;
904 foreach ($projects as $p) {
905 $pgf = new ProjectGroupFactory ($p) ;
906 $pgids = $pgf->getAllProjectGroupIds () ;
907 foreach ($pgids as $gid) {
908 if (array_key_exists ('pm', $this->perms_array)
909 && array_key_exists ($gid, $this->perms_array['pm']) ) {
910 $new_pa['pm'][$gid] = $this->perms_array['pm'][$gid] ;
911 } elseif (array_key_exists ('new_pm', $this->perms_array)
912 && array_key_exists ($p->getID(), $this->perms_array['new_pm']) ) {
913 $new_pa['pm'][$gid] = $new_pa['new_pm'][$p->getID()] ;
919 $this->update ($this->getName(), $new_pa, false) ;
927 * TODO: RBAC::RoleExplicit Enter description here ...
930 abstract class RoleExplicit extends BaseRole implements PFO_RoleExplicit {
931 public function addUsers($users) {
935 foreach ($users as $user) {
936 $ids[] = $user->getID();
939 $already_there = array();
940 $res = db_query_params('SELECT user_id FROM pfo_user_role WHERE user_id=ANY($1) AND role_id=$2',
941 array(db_int_array_to_any_clause($ids), $this->getID()));
945 while ($arr = db_fetch_array($res)) {
946 $already_there[] = $arr['user_id'] ;
949 foreach ($ids as $id) {
950 if (!in_array ($id, $already_there)) {
951 $res = db_query_params ('INSERT INTO pfo_user_role (user_id, role_id) VALUES ($1, $2)',
960 foreach ($this->getLinkedProjects() as $p) {
961 foreach ($ids as $uid) {
962 if (!$SYS->sysGroupCheckUser($p->getID(),$uid)) {
971 public function addUser ($user) {
972 if (!$this->addUsers (array ($user))) {
975 $hook_params['user'] = $user;
976 $hook_params['role'] = $this;
977 plugin_hook ("role_adduser", $hook_params);
982 public function removeUsers($users) {
986 foreach ($users as $user) {
987 $ids[] = $user->getID() ;
990 $already_there = array () ;
991 $res = db_query_params ('DELETE FROM pfo_user_role WHERE user_id=ANY($1) AND role_id=$2',
992 array (db_int_array_to_any_clause($ids), $this->getID())) ;
994 foreach ($this->getLinkedProjects() as $p) {
995 foreach ($ids as $uid) {
996 $SYS->sysGroupCheckUser($p->getID(),$uid) ;
1003 public function removeUser ($user) {
1004 if(!$this->removeUsers (array ($user))){
1007 $hook_params['user'] = $user;
1008 $hook_params['role'] = $this;
1009 plugin_hook ("role_removeuser", $hook_params);
1014 public function getUsers() {
1015 $result = array () ;
1016 $res = db_query_params ('SELECT user_id FROM pfo_user_role WHERE role_id=$1',
1017 array ($this->getID())) ;
1018 while ($arr = db_fetch_array($res)) {
1019 $result[] = user_get_object ($arr['user_id']) ;
1025 public function hasUser($user) {
1026 $res = db_query_params ('SELECT user_id FROM pfo_user_role WHERE user_id=$1 AND role_id=$2',
1027 array ($user->getID(), $this->getID())) ;
1028 if ($res && db_numrows($res)) {
1035 function getID() { // From the PFO spec
1036 return $this->data_array['role_id'];
1039 function getName() { // From the PFO spec
1040 return $this->data_array['role_name'];
1044 class RoleAnonymous extends BaseRole implements PFO_RoleAnonymous {
1045 // This role is implemented as a singleton
1046 private static $_instance ;
1048 public static function getInstance() {
1049 if (isset(self::$_instance)) {
1050 return self::$_instance ;
1054 self::$_instance = new $c ;
1056 $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',
1057 array ('PFO_RoleAnonymous')) ;
1058 if (!$res || !db_numrows($res)) {
1059 throw new Exception ("No PFO_RoleAnonymous role in the database") ;
1061 self::$_instance->_role_id = db_result ($res, 0, 'role_id') ;
1063 $hook_params = array ();
1064 $hook_params['role'] =& self::$_instance;
1065 plugin_hook ("role_get", $hook_params);
1067 self::$_instance->fetchData (self::$_instance->_role_id) ;
1069 return self::$_instance ;
1072 public function getID () {
1073 return $this->_role_id ;
1075 public function isPublic () {
1078 public function setPublic ($flag) {
1079 throw new Exception ("Can't setPublic() on RoleAnonymous") ;
1081 public function getHomeProject () {
1084 public function getName () {
1085 return _('Anonymous/not logged in') ;
1087 public function setName ($name) {
1088 throw new Exception ("Can't setName() on RoleAnonymous") ;
1092 class RoleLoggedIn extends BaseRole implements PFO_RoleLoggedIn {
1093 // This role is implemented as a singleton
1094 private static $_instance ;
1096 public static function getInstance() {
1097 if (isset(self::$_instance)) {
1098 return self::$_instance ;
1102 self::$_instance = new $c ;
1104 $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',
1105 array ('PFO_RoleLoggedIn')) ;
1106 if (!$res || !db_numrows($res)) {
1107 throw new Exception ("No PFO_RoleLoggedIn role in the database") ;
1109 self::$_instance->_role_id = db_result ($res, 0, 'role_id') ;
1111 $hook_params = array ();
1112 $hook_params['role'] =& self::$_instance;
1113 plugin_hook ("role_get", $hook_params);
1115 self::$_instance->fetchData (self::$_instance->_role_id) ;
1117 return self::$_instance ;
1120 public function getID () {
1121 return $this->_role_id ;
1123 public function isPublic () {
1126 public function setPublic ($flag) {
1127 throw new Exception ("Can't setPublic() on RoleLoggedIn") ;
1129 public function getHomeProject () {
1132 public function getName () {
1133 return _('Any user logged in') ;
1135 public function setName ($name) {
1136 throw new Exception ("Can't setName() on RoleLoggedIn") ;
1140 abstract class RoleUnion extends BaseRole implements PFO_RoleUnion {
1141 public function addRole ($role) {
1142 throw new Exception ("Not implemented") ;
1144 public function removeRole ($role) {
1145 throw new Exception ("Not implemented") ;
1150 * TODO: Enter description here ...
1153 class RoleComparator {
1154 var $criterion = 'composite' ;
1155 var $reference_project = NULL ;
1157 function Compare ($a, $b) {
1158 switch ($this->criterion) {
1160 return strcoll ($a->getName(), $b->getName()) ;
1163 $aid = $a->getID() ;
1164 $bid = $b->getID() ;
1168 return ($a < $b) ? -1 : 1;
1172 if ($this->reference_project == NULL) {
1173 return $this->CompareNoRef ($a, $b) ;
1175 $rpid = $this->reference_project->getID () ;
1176 $ap = $a->getHomeProject() ;
1177 $bp = $b->getHomeProject() ;
1178 $a_is_local = ($ap != NULL && $ap->getID() == $rpid) ; // Local
1179 $b_is_local = ($bp != NULL && $bp->getID() == $rpid) ;
1181 if ($a_is_local && !$b_is_local) {
1183 } elseif (!$a_is_local && $b_is_local) {
1186 return $this->CompareNoRef ($a, $b) ;
1191 * TODO: Enter description here ...
1196 function CompareNoRef ($a, $b) {
1197 $ap = $a->getHomeProject() ;
1198 $bp = $b->getHomeProject() ;
1199 if ($ap == NULL && $bp != NULL) {
1201 } elseif ($ap != NULL && $bp == NULL) {
1203 } elseif ($ap == NULL && $bp == NULL) {
1204 $tmp = strcoll ($a->getName(), $b->getName()) ;
1207 $projcmp = new ProjectComparator () ;
1208 $projcmp->criterion = 'name' ;
1209 $tmp = $projcmp->Compare ($ap, $bp) ;
1210 if ($tmp) { /* Different projects, sort accordingly */
1213 return strcoll ($a->getName(), $b->getName()) ;
1218 function sortRoleList (&$list, $relative_to = NULL, $criterion='composite') {
1219 $cmp = new RoleComparator () ;
1220 $cmp->criterion = $criterion ;
1221 $cmp->reference_project = $relative_to ;
1223 return usort ($list, array ($cmp, 'Compare')) ;
1228 // c-file-style: "bsd"