3 * FusionForge role-based access control
5 * Copyright 2004, GForge, LLC
6 * Copyright 2009-2010, Roland Mas
7 * Copyright 2012-2013, Franck Villaume - TrivialDev
8 * Copyright 2013, French Ministry of National Education
9 * http://fusionforge.org
11 * This file is part of FusionForge. FusionForge is free software;
12 * you can redistribute it and/or modify it under the terms of the
13 * GNU General Public License as published by the Free Software
14 * Foundation; either version 2 of the Licence, or (at your option)
17 * FusionForge is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 require 'PFO-RBAC.interface.php';
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),
86 $this->global_settings = array(
87 'forge_admin', // “God mode”: all actions allowed
88 'approve_projects', // Ability to approve pending projects
89 'approve_news', // Ability to approve news bits to the forge front page
93 // TODO: document these (Project-related permissions ?)
94 $this->defaults = array(
95 'Admin' => array( 'project_admin'=> 1,
102 'tracker_admin' => 1,
107 'Senior Developer' => array( 'project_read' => 1,
113 'tracker_admin' => 1,
118 'Junior Developer' => array( 'project_read' => 1,
126 'Doc Writer' => array( 'project_read' => 1,
133 'Support Tech' => array( 'project_read' => 1,
137 'tracker_admin' => 1,
145 public function getUsers() {
148 public function hasUser($user) {
149 throw new Exception ("Not implemented") ;
151 function hasGlobalPermission($section, $action = NULL) {
152 return $this->hasPermission ($section, -1, $action) ;
154 public function getSettings() {
155 throw new Exception ("Not implemented") ;
157 public function setSettings($data) {
158 throw new Exception ("Not implemented") ;
160 public function delete () {
161 throw new Exception ("Not implemented") ;
165 * getLinkedProjects - List of projects referencing that role
167 * Includes the home project (for roles that have one)
169 * @return array Array of Group objects
171 public function getLinkedProjects() {
174 $hp = $this->getHomeProject();
176 $ids[] = $hp->getID();
179 $res = db_query_params('SELECT group_id FROM role_project_refs WHERE role_id=$1',
180 array($this->getID()));
182 while ($arr = db_fetch_array ($res)) {
183 $ids[] = $arr['group_id'];
187 return group_get_objects(array_unique($ids));
190 function linkProject ($project) { // From the PFO spec
192 $hp = $this->getHomeProject();
193 if ($hp != NULL && $hp->getID() == $project->getID()) {
194 $this->setError(_("Can't link to home project"));
198 $res = db_query_params('SELECT group_id FROM role_project_refs WHERE role_id=$1 AND group_id=$2',
199 array($this->getID(),
202 if (db_numrows($res)) {
205 $res = db_query_params('INSERT INTO role_project_refs (role_id, group_id) VALUES ($1, $2)',
206 array($this->getID(),
208 if (!$res || db_affected_rows($res) < 1) {
209 $this->setError('linkProject('.$project->getID().') '.db_error());
213 $this->normalizeData();
215 foreach ($this->getUsers() as $u) {
216 if (!$SYS->sysCheckCreateUser($u->getID())) {
217 $this->setError($SYS->getErrorMessage());
225 function unlinkProject($project) { // From the PFO spec
227 $hp = $this->getHomeProject();
228 if ($hp != NULL && $hp->getID() == $project->getID()) {
229 $this->setError (_("Can't unlink from home project"));
233 $res = db_query_params('DELETE FROM role_project_refs WHERE role_id=$1 AND group_id=$2',
234 array($this->getID(),
237 $this->setError('unlinkProject('.$project->getID().') '.db_error());
241 $this->removeObsoleteSettings ();
243 foreach ($this->getUsers() as $u) {
244 if (!$SYS->sysCheckCreateUser($u->getID())) {
245 $this->setError($SYS->getErrorMessage());
254 * fetchData - May need to refresh database fields.
256 * If an update occurred and you need to access the updated info.
258 * @return boolean success;
260 function fetchData($role_id) {
261 unset($this->data_array);
262 unset($this->setting_array);
263 unset($this->perms_array);
265 $res = db_query_params('SELECT * FROM pfo_role WHERE role_id=$1',
267 if (!$res || db_numrows($res) < 1) {
268 $this->setError('BaseRole::fetchData()::'.db_error());
271 $this->data_array = db_fetch_array($res);
272 if ($this->data_array['is_public'] == 't') {
273 $this->data_array['is_public'] = true;
275 $this->data_array['is_public'] = false;
277 $res = db_query_params('SELECT section_name, ref_id, perm_val FROM pfo_role_setting WHERE role_id=$1',
280 $this->setError('BaseRole::fetchData()::'.db_error());
283 // TODO: document perms_array
284 $this->perms_array=array();
285 while ($arr = db_fetch_array($res)) {
286 $this->perms_array[$arr['section_name']][$arr['ref_id']] = $arr['perm_val'];
292 function setSetting ($section, $reference, $value) {
293 $cur = $this->getSettingRaw($section, $reference);
294 if (($value == $cur) && ($cur != NULL)) {
298 $role_id = $this->getID () ;
300 $res = db_query_params ('DELETE FROM pfo_role_setting WHERE role_id=$1 AND section_name=$2 AND ref_id=$3',
305 $res = db_query_params ('INSERT INTO pfo_role_setting (role_id, section_name, ref_id, perm_val) VALUES ($1, $2, $3, $4)',
310 $this->perms_array[$section][$reference] = $value;
313 function getSettingsForProject ($project) {
315 $group_id = $project->getID() ;
317 $sections = array ('project_read', 'project_admin', 'frs', 'scm', 'docman', 'tracker_admin', 'new_tracker') ;
318 foreach ($sections as $section) {
319 $result[$section][$group_id] = $this->getVal ($section, $group_id) ;
322 $atf = new ArtifactTypeFactory ($project) ;
323 if (!$atf->isError()) {
324 $tids = $atf->getAllArtifactTypeIds () ;
325 foreach ($tids as $tid) {
326 $result['tracker'][$tid] = $this->getVal ('tracker', $tid) ;
329 array_push ($sections,'tracker');
331 $sections_forum = array('forum_admin', 'new_forum');
332 foreach ($sections_forum as $section_forum) {
333 $result[$section_forum][$group_id] = $this->getVal ($section_forum, $group_id) ;
335 $sections = array_merge($sections, $sections_forum);
337 $ff = new ForumFactory ($project) ;
338 if (!$ff->isError()) {
339 $fids = $ff->getAllForumIdsWithNews () ;
340 foreach ($fids as $fid) {
341 $result['forum'][$fid] = $this->getVal ('forum', $fid) ;
344 array_push ($sections,'forum');
346 $sections_pm = array('pm_admin', 'new_pm');
347 foreach ($sections_pm as $section_pm) {
348 $result[$section_pm][$group_id] = $this->getVal ($section_pm, $group_id) ;
350 $sections = array_merge($sections, $sections_pm);
352 $pgf = new ProjectGroupFactory ($project) ;
353 if (!$pgf->isError()) {
354 $pgids = $pgf->getAllProjectGroupIds () ;
355 foreach ($pgids as $pgid) {
356 $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 if ($this->hasPermission('project_admin', $reference)) {
452 case 'tracker_admin':
455 if ($this->hasPermission('project_admin', $reference)) {
457 } elseif (!$this->hasPermission('project_read', $reference)) {
464 if ($this->hasPermission('project_admin', $reference)) {
466 } elseif (!$this->hasPermission('project_read', $reference)) {
473 if ($this->hasPermission('project_admin', $reference)) {
475 } elseif (!$this->hasPermission('project_read', $reference)) {
482 if ($this->hasPermission('project_admin', $reference)) {
484 } elseif (!$this->hasPermission('project_read', $reference)) {
491 if ($this->hasPermission('forum_admin', forum_get_groupid($reference))) {
493 } elseif (!$this->hasPermission('project_read', forum_get_groupid($reference))) {
499 if ($this->hasPermission('forum_admin', $reference)) {
501 } elseif (!$this->hasPermission('project_read', $reference)) {
508 if ($this->hasPermission('tracker_admin', artifacttype_get_groupid($reference))) {
510 } elseif (!$this->hasPermission('project_read', artifacttype_get_groupid($reference))) {
516 if ($this->hasPermission('tracker_admin', $reference)) {
518 } elseif (!$this->hasPermission('project_read', $reference)) {
525 if ($this->hasPermission('pm_admin', projectgroup_get_groupid($reference))) {
527 } elseif (!$this->hasPermission('project_read', projectgroup_get_groupid($reference))) {
533 if ($this->hasPermission('pm_admin', $reference)) {
535 } elseif (!$this->hasPermission('project_read', $reference)) {
541 $hook_params = array ();
542 $hook_params['role'] = $this ;
543 $hook_params['section'] = $section ;
544 $hook_params['reference'] = $reference ;
545 $hook_params['value'] = $value ;
546 $hook_params['result'] = NULL ;
547 plugin_hook_by_reference ("role_get_setting", $hook_params);
548 return $hook_params['result'] ;
553 function getSettingRaw($section, $reference) {
554 if (isset ($this->perms_array[$section][$reference])) {
555 return $this->perms_array[$section][$reference] ;
560 * getVal - get a value out of the array of settings for this role.
562 * @param string The name of the role.
563 * @param integer The ref_id (ex: group_artifact_id, group_forum_id) for this item.
564 * @return integer The value of this item.
566 function getVal($section, $ref_id) {
567 global $role_default_array;
571 return $this->getSetting($section, $ref_id) ;
575 * &getRoleVals - get all the values and language text strings for this section.
577 * @return array Assoc array of values for this section.
579 function &getRoleVals($section) {
580 global $role_vals, $rbac_permission_names;
581 setup_rbac_strings () ;
584 // Optimization - save array so it is only built once per page view
586 if (!isset($role_vals[$section])) {
588 for ($i=0; $i<count($this->role_values[$section]); $i++) {
590 // Build an associative array of these key values + localized description
592 $role_vals[$section][$this->role_values[$section][$i]] =
593 util_ifsetor($rbac_permission_names["$section".$this->role_values[$section][$i]],
594 _('UNKNOWN (internal error, report bug to FusionForge)'));
597 return $role_vals[$section];
600 function hasPermission($section, $reference, $action = NULL) {
603 $value = $this->getSetting ($section, $reference) ;
610 case 'approve_projects':
612 case 'project_admin':
614 case 'tracker_admin':
617 return ($value >= 1) ;
623 return ($value >= 1) ;
626 return ($value >= 2) ;
634 return ($value >= 1) ;
637 return ($value >= 2) ;
645 return ($value >= 1) ;
648 return ($value >= 2) ;
651 return ($value >= 3) ;
654 return ($value >= 4) ;
662 return ($value >= 1) ;
665 return ($value >= 2) ;
668 return ($value >= 3) ;
677 return ($value >= 1) ;
680 return ($value >= 2) ;
682 case 'unmoderated_post':
683 return ($value >= 3) ;
686 return ($value >= 4) ;
695 return (($value & 1) != 0) ;
698 return (($value & 2) != 0) ;
701 return (($value & 4) != 0) ;
704 return (($value & 8) != 0) ;
707 * bit4 (value & 16) is reserved
708 * for tracker item vote from Evolvis
717 return (($value & 1) != 0) ;
720 return (($value & 2) != 0) ;
723 return (($value & 4) != 0) ;
728 $hook_params = array ();
729 $hook_params['section'] = $section ;
730 $hook_params['reference'] = $reference ;
731 $hook_params['action'] = $action ;
732 $hook_params['value'] = $value ;
733 $hook_params['result'] = false ;
734 plugin_hook_by_reference ("role_has_permission", $hook_params);
735 return $hook_params['result'] ;
741 * update - update a role in the database.
743 * @param string The name of the role.
744 * @param array A multi-dimensional array of data in this format: $data['section_name']['ref_id']=$val
745 * @param boolean Perform permission checking
746 * @return boolean True on success or false on failure.
748 function update($role_name,$data,$check_perms=true) {
751 if ($this->getHomeProject() == NULL) {
752 if (!forge_check_global_perm ('forge_admin')) {
753 $this->setPermissionDeniedError();
756 } elseif (!forge_check_perm ('project_admin', $this->getHomeProject()->getID())) {
757 $this->setPermissionDeniedError();
765 if ($role_name != $this->getName()) {
766 $this->setName($role_name) ;
769 foreach ($data as $sect => $refs) {
770 foreach ($refs as $refid => $value) {
771 $this->setSetting ($sect, $refid, $value) ;
775 $hook_params = array ();
776 $hook_params['role'] =& $this;
777 $hook_params['role_id'] = $this->getID();
778 $hook_params['data'] = $data;
779 plugin_hook ("role_update", $hook_params);
783 $this->fetchData($this->getID());
785 foreach ($this->getUsers() as $u) {
786 if (!$SYS->sysCheckCreateUser($u->getID())) {
787 $this->setError($SYS->getErrorMessage());
795 function getDisplayableName($group = NULL) {
796 if ($this->getHomeProject() == NULL) {
797 return sprintf (_('%s (global role)'),
799 } elseif ($group == NULL
800 || $this->getHomeProject()->getID() != $group->getID()) {
801 return sprintf (_('%s (in project %s)'),
803 $this->getHomeProject()->getPublicName()) ;
805 return $this->getName () ;
809 function removeObsoleteSettings () {
812 // Remove obsolete project-wide settings
813 $sections = array ('project_read', 'project_admin', 'frs', 'scm', 'docman', 'tracker_admin', 'new_tracker', 'forum_admin', 'new_forum', 'pm_admin', 'new_pm') ;
814 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)',
815 array ($this->getID(),
816 db_string_array_to_any_clause($sections))) ;
819 // Remove obsolete settings for multiple-instance tools
820 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))',
821 array ($this->getID(),
823 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))',
824 array ($this->getID(),
826 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))',
827 array ($this->getID(),
831 $this->fetchData($this->getID());
835 function normalizeDataForSection (&$new_sa, $section) {
836 if (array_key_exists ($section, $this->setting_array)) {
837 $new_sa[$section][0] = $this->setting_array[$section][0] ;
838 } elseif (array_key_exists ($this->data_array['role_name'], $this->defaults)
839 && array_key_exists ($section, $this->defaults[$this->data_array['role_name']])) {
840 $new_sa[$section][0] = $this->defaults[$this->data_array['role_name']][$section] ;
842 $new_sa[$section][0] = 0 ;
847 function normalizePermsForSection (&$new_pa, $section, $refid) {
848 if (array_key_exists ($section, $this->perms_array)
849 && array_key_exists ($refid, $this->perms_array[$section])) {
850 $new_pa[$section][$refid] = $this->perms_array[$section][$refid] ;
851 } elseif (array_key_exists ($this->data_array['role_name'], $this->defaults)
852 && array_key_exists ($section, $this->defaults[$this->data_array['role_name']])) {
853 $new_pa[$section][$refid] = $this->defaults[$this->data_array['role_name']][$section] ;
855 $new_pa[$section][$refid] = 0 ;
860 function normalizeData () { // From the PFO spec
861 $this->removeObsoleteSettings () ;
863 $this->fetchData ($this->getID()) ;
865 $projects = $this->getLinkedProjects() ;
869 // Add missing settings
870 // ...project-wide settings
871 $arr = array ('project_read', 'project_admin', 'frs', 'scm', 'docman', 'tracker_admin', 'new_tracker', 'forum_admin', 'new_forum', 'pm_admin', 'new_pm') ;
872 foreach ($projects as $p) {
873 foreach ($arr as $section) {
874 $this->normalizePermsForSection ($new_pa, $section, $p->getID()) ;
877 $this->normalizePermsForSection ($new_pa, 'forge_admin', -1) ;
878 $this->normalizePermsForSection ($new_pa, 'approve_projects', -1) ;
879 $this->normalizePermsForSection ($new_pa, 'approve_news', -1) ;
880 $this->normalizePermsForSection ($new_pa, 'forge_stats', -1) ;
882 $hook_params = array ();
883 $hook_params['role'] =& $this;
884 $hook_params['new_sa'] =& $new_sa ;
885 $hook_params['new_pa'] =& $new_pa ;
886 plugin_hook ("role_normalize", $hook_params);
888 // ...tracker-related settings
889 $new_sa['tracker'] = array () ;
890 $new_pa['tracker'] = array () ;
891 foreach ($projects as $p) {
892 if (!$p->usesTracker()) {
895 $atf = new ArtifactTypeFactory ($p) ;
896 if (!$atf->isError()) {
897 $trackerids = $atf->getAllArtifactTypeIds () ;
898 foreach ($trackerids as $tid) {
899 if (array_key_exists ('tracker', $this->perms_array)
900 && array_key_exists ($tid, $this->perms_array['tracker']) ) {
901 $new_pa['tracker'][$tid] = $this->perms_array['tracker'][$tid] ;
902 } elseif (array_key_exists ('new_tracker', $this->perms_array)
903 && array_key_exists ($p->getID(), $this->perms_array['new_tracker']) ) {
904 $new_pa['tracker'][$tid] = $new_pa['new_tracker'][$p->getID()] ;
910 // ...forum-related settings
911 $new_sa['forum'] = array () ;
912 $new_pa['forum'] = array () ;
913 foreach ($projects as $p) {
914 if (!$p->usesForum()) {
917 $ff = new ForumFactory ($p) ;
918 if (!$ff->isError()) {
919 $fids = $ff->getAllForumIdsWithNews () ;
920 foreach ($fids as $fid) {
921 if (array_key_exists ('forum', $this->perms_array)
922 && array_key_exists ($fid, $this->perms_array['forum']) ) {
923 $new_pa['forum'][$fid] = $this->perms_array['forum'][$fid] ;
924 } elseif (array_key_exists ('new_forum', $this->perms_array)
925 && array_key_exists ($p->getID(), $this->perms_array['new_forum']) ) {
926 $new_pa['forum'][$fid] = $new_pa['new_forum'][$p->getID()] ;
932 // ...pm-related settings
933 $new_sa['pm'] = array () ;
934 $new_pa['pm'] = array () ;
935 foreach ($projects as $p) {
939 $pgf = new ProjectGroupFactory ($p) ;
940 if (!$pgf->isError()) {
941 $pgids = $pgf->getAllProjectGroupIds () ;
942 foreach ($pgids as $gid) {
943 if (array_key_exists ('pm', $this->perms_array)
944 && array_key_exists ($gid, $this->perms_array['pm']) ) {
945 $new_pa['pm'][$gid] = $this->perms_array['pm'][$gid] ;
946 } elseif (array_key_exists ('new_pm', $this->perms_array)
947 && array_key_exists ($p->getID(), $this->perms_array['new_pm']) ) {
948 $new_pa['pm'][$gid] = $new_pa['new_pm'][$p->getID()] ;
955 $this->update ($this->getName(), $new_pa, false) ;
963 * TODO: RBAC::RoleExplicit Enter description here ...
966 abstract class RoleExplicit extends BaseRole implements PFO_RoleExplicit {
967 public function addUsers($users) {
971 foreach ($users as $user) {
972 $ids[] = $user->getID();
975 $already_there = array();
976 $res = db_query_params('SELECT user_id FROM pfo_user_role WHERE user_id=ANY($1) AND role_id=$2',
977 array(db_int_array_to_any_clause($ids), $this->getID()));
981 while ($arr = db_fetch_array($res)) {
982 $already_there[] = $arr['user_id'] ;
985 foreach ($ids as $id) {
986 if (!in_array ($id, $already_there)) {
987 $res = db_query_params ('INSERT INTO pfo_user_role (user_id, role_id) VALUES ($1, $2)',
996 foreach ($this->getLinkedProjects() as $p) {
997 foreach ($ids as $uid) {
998 if (!$SYS->sysGroupCheckUser($p->getID(),$uid)) {
1007 public function addUser ($user) {
1008 if (!$this->addUsers (array ($user))) {
1011 $hook_params['user'] = $user;
1012 $hook_params['role'] = $this;
1013 plugin_hook ("role_adduser", $hook_params);
1018 public function removeUsers($users) {
1022 foreach ($users as $user) {
1023 $ids[] = $user->getID() ;
1026 $already_there = array () ;
1027 $res = db_query_params ('DELETE FROM pfo_user_role WHERE user_id=ANY($1) AND role_id=$2',
1028 array (db_int_array_to_any_clause($ids), $this->getID())) ;
1030 foreach ($this->getLinkedProjects() as $p) {
1031 foreach ($ids as $uid) {
1032 $SYS->sysGroupCheckUser($p->getID(),$uid) ;
1039 public function removeUser ($user) {
1040 if(!$this->removeUsers (array ($user))){
1043 $hook_params['user'] = $user;
1044 $hook_params['role'] = $this;
1045 plugin_hook ("role_removeuser", $hook_params);
1050 public function getUsers() {
1051 $result = array () ;
1052 $res = db_query_params ('SELECT user_id FROM pfo_user_role WHERE role_id=$1',
1053 array ($this->getID())) ;
1054 while ($arr = db_fetch_array($res)) {
1055 $result[] = user_get_object ($arr['user_id']) ;
1061 public function hasUser($user) {
1062 $res = db_query_params ('SELECT user_id FROM pfo_user_role WHERE user_id=$1 AND role_id=$2',
1063 array ($user->getID(), $this->getID())) ;
1064 if ($res && db_numrows($res)) {
1071 function getID() { // From the PFO spec
1072 return $this->data_array['role_id'];
1075 function getName() { // From the PFO spec
1076 return $this->data_array['role_name'];
1080 class RoleAnonymous extends BaseRole implements PFO_RoleAnonymous {
1081 // This role is implemented as a singleton
1082 private static $_instance ;
1084 public static function getInstance() {
1085 if (isset(self::$_instance)) {
1086 return self::$_instance ;
1090 self::$_instance = new $c ;
1092 $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',
1093 array ('PFO_RoleAnonymous')) ;
1094 if (!$res || !db_numrows($res)) {
1095 throw new Exception ("No PFO_RoleAnonymous role in the database") ;
1097 self::$_instance->_role_id = db_result ($res, 0, 'role_id') ;
1099 $hook_params = array ();
1100 $hook_params['role'] =& self::$_instance;
1101 plugin_hook ("role_get", $hook_params);
1103 self::$_instance->fetchData (self::$_instance->_role_id) ;
1105 return self::$_instance ;
1108 public function getID () {
1109 return $this->_role_id ;
1111 public function isPublic () {
1114 public function setPublic ($flag) {
1115 throw new Exception ("Can't setPublic() on RoleAnonymous") ;
1117 public function getHomeProject () {
1120 public function getName () {
1121 return _('Anonymous/not logged in') ;
1123 public function setName ($name) {
1124 throw new Exception ("Can't setName() on RoleAnonymous") ;
1128 class RoleLoggedIn extends BaseRole implements PFO_RoleLoggedIn {
1129 // This role is implemented as a singleton
1130 private static $_instance ;
1132 public static function getInstance() {
1133 if (isset(self::$_instance)) {
1134 return self::$_instance ;
1138 self::$_instance = new $c ;
1140 $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',
1141 array ('PFO_RoleLoggedIn')) ;
1142 if (!$res || !db_numrows($res)) {
1143 throw new Exception ("No PFO_RoleLoggedIn role in the database") ;
1145 self::$_instance->_role_id = db_result ($res, 0, 'role_id') ;
1147 $hook_params = array ();
1148 $hook_params['role'] =& self::$_instance;
1149 plugin_hook ("role_get", $hook_params);
1151 self::$_instance->fetchData (self::$_instance->_role_id) ;
1153 return self::$_instance ;
1156 public function getID () {
1157 return $this->_role_id ;
1159 public function isPublic () {
1162 public function setPublic ($flag) {
1163 throw new Exception ("Can't setPublic() on RoleLoggedIn") ;
1165 public function getHomeProject () {
1168 public function getName () {
1169 return _('Any user logged in') ;
1171 public function setName ($name) {
1172 throw new Exception ("Can't setName() on RoleLoggedIn") ;
1176 abstract class RoleUnion extends BaseRole implements PFO_RoleUnion {
1177 public function addRole ($role) {
1178 throw new Exception ("Not implemented") ;
1180 public function removeRole ($role) {
1181 throw new Exception ("Not implemented") ;
1186 * TODO: Enter description here ...
1189 class RoleComparator {
1190 var $criterion = 'composite' ;
1191 var $reference_project = NULL ;
1193 function Compare ($a, $b) {
1194 switch ($this->criterion) {
1196 return strcoll ($a->getName(), $b->getName()) ;
1199 $aid = $a->getID() ;
1200 $bid = $b->getID() ;
1204 return ($a < $b) ? -1 : 1;
1208 if ($this->reference_project == NULL) {
1209 return $this->CompareNoRef ($a, $b) ;
1211 $rpid = $this->reference_project->getID () ;
1212 $ap = $a->getHomeProject() ;
1213 $bp = $b->getHomeProject() ;
1214 $a_is_local = ($ap != NULL && $ap->getID() == $rpid) ; // Local
1215 $b_is_local = ($bp != NULL && $bp->getID() == $rpid) ;
1217 if ($a_is_local && !$b_is_local) {
1219 } elseif (!$a_is_local && $b_is_local) {
1222 return $this->CompareNoRef ($a, $b) ;
1227 * TODO: Enter description here ...
1232 function CompareNoRef ($a, $b) {
1233 $ap = $a->getHomeProject() ;
1234 $bp = $b->getHomeProject() ;
1235 if ($ap == NULL && $bp != NULL) {
1237 } elseif ($ap != NULL && $bp == NULL) {
1239 } elseif ($ap == NULL && $bp == NULL) {
1240 $tmp = strcoll ($a->getName(), $b->getName()) ;
1243 $projcmp = new ProjectComparator () ;
1244 $projcmp->criterion = 'name' ;
1245 $tmp = $projcmp->Compare ($ap, $bp) ;
1246 if ($tmp) { /* Different projects, sort accordingly */
1249 return strcoll ($a->getName(), $b->getName()) ;
1254 function sortRoleList (&$list, $relative_to = NULL, $criterion='composite') {
1255 $cmp = new RoleComparator () ;
1256 $cmp->criterion = $criterion ;
1257 $cmp->reference_project = $relative_to ;
1259 return usort ($list, array ($cmp, 'Compare')) ;
1264 // c-file-style: "bsd"