From ebce32b5dd423b994bd15ed37408d45e0151896c Mon Sep 17 00:00:00 2001 From: skodak Date: Thu, 1 Feb 2007 10:24:26 +0000 Subject: [PATCH] (MDL-8367) Add configuration option to select role for guest - guestroleid --- admin/settings/users.php | 14 +++++++++----- lang/en_utf8/admin.php | 2 ++ lib/accesslib.php | 21 ++++++++++++++++++--- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/admin/settings/users.php b/admin/settings/users.php index ae4185cb22..5d3fcfbbba 100644 --- a/admin/settings/users.php +++ b/admin/settings/users.php @@ -29,7 +29,7 @@ $ADMIN->add('roles', new admin_externalpage('assignroles', get_string('assignrol // "userpolicies" settingpage $temp = new admin_settingpage('userpolicies', get_string('userpolicies', 'admin')); -$context = get_context_instance(CONTEXT_SYSTEM, SITEID); +$context = get_context_instance(CONTEXT_SYSTEM); if (!$guestrole = get_guest_role()) { $guestrole->id = 0; } @@ -43,7 +43,9 @@ if ($userroles = get_roles_with_capability('moodle/legacy:user', CAP_ALLOW)) { } else { $userrole->id = 0; } -$assignableroles = get_assignable_roles($context); +// we must not use assignable roles here: +// 1/ unsetting roles as assignable for admin might bork the settings! +// 2/ default user role should not be assignable anyway $allroles = array(); if ($roles = get_all_roles()) { foreach ($roles as $role) { @@ -52,16 +54,18 @@ if ($roles = get_all_roles()) { } $temp->add(new admin_setting_configselect('notloggedinroleid', get_string('notloggedinroleid', 'admin'), - get_string('confignotloggedinroleid', 'admin'), $guestrole->id, $assignableroles )); + get_string('confignotloggedinroleid', 'admin'), $guestrole->id, $allroles )); +$temp->add(new admin_setting_configselect('guestroleid', get_string('guestroleid', 'admin'), + get_string('configguestroleid', 'admin'), $guestrole->id, $allroles)); $temp->add(new admin_setting_configselect('defaultuserroleid', get_string('defaultuserroleid', 'admin'), get_string('configdefaultuserroleid', 'admin'), $userrole->id, $allroles)); $temp->add(new admin_setting_configselect('defaultcourseroleid', get_string('defaultcourseroleid', 'admin'), - get_string('configdefaultcourseroleid', 'admin'), $studentrole->id, $assignableroles)); + get_string('configdefaultcourseroleid', 'admin'), $studentrole->id, $allroles)); $temp->add(new admin_setting_configcheckbox('autologinguests', get_string('autologinguests', 'admin'), get_string('configautologinguests', 'admin'), 0)); $temp->add(new admin_setting_configmultiselect('nonmetacoursesyncroleids', get_string('nonmetacoursesyncroleids', 'admin'), - get_string('confignonmetacoursesyncroleids', 'admin'), array(), $assignableroles)); + get_string('confignonmetacoursesyncroleids', 'admin'), array(), $allroles)); //$temp->add(new admin_setting_configcheckbox('allusersaresitestudents', get_string('allusersaresitestudents', 'admin'), get_string('configallusersaresitestudents','admin'), 1)); $temp->add(new admin_setting_configmultiselect('hiddenuserfields', get_string('hiddenuserfields', 'admin'), diff --git a/lang/en_utf8/admin.php b/lang/en_utf8/admin.php index e44e0b8601..ee184f640d 100644 --- a/lang/en_utf8/admin.php +++ b/lang/en_utf8/admin.php @@ -103,6 +103,7 @@ $string['configfrontpageloggedin'] = 'The items selected above will be displayed $string['configfullnamedisplay'] = 'This defines how names are shown when they are displayed in full. For most mono-lingual sites the most efficient setting is the default \"Given names + Surname\", but you may choose to hide surnames altogether, or to leave it up to the current language pack to decide (some languages have different conventions).'; $string['configgdversion'] = 'Indicate the version of GD that is installed. The version shown by default is the one that has been auto-detected. Don\'t change this unless you really know what you\'re doing.'; $string['configgradebookroles'] = 'This setting allows you to control who appears on the gradebook. Users need to have at least one of these roles in a course to be shown in the gradebook for that course.'; +$string['configguestroleid'] = 'This role is automatically assigned to the guest user. It is also temporarily assigned to not enrolled users when they enter course that allows guests without password. Please verify that the role has moodle/legacy:guest and moodle/course:view capability.'; $string['confighiddenuserfields'] = 'Select which user infomation fields you wish to hide from other users other than course teachers/admins. This will increase student privacy. Hold CTRL key to select multiple fields.'; $string['confightmleditor'] = 'Choose whether or not to allow use of the embedded HTML text editor. Even if you choose allow, this editor will only appear when the user is using a compatible web browser. Users can also choose not to use it.'; $string['configidnumber'] = 'This option specifies whether (a) Users are not be asked for an ID number at all, (b) Users are asked for an ID number but can leave it blank or (c) Users are asked for an ID Number and cannot leave it blank. If given the User\'s ID number is displayed in their Profile.'; @@ -290,6 +291,7 @@ $string['globalsquoteswarning'] = '

Security Warning: to oper $string['gotofirst'] = 'Go to first missing string'; $string['gradebook'] = 'Gradebook'; $string['gradebookroles'] = 'Graded roles'; +$string['guestroleid'] = 'Role for guest'; $string['helpadminseesall'] = 'Do admins see all calendar events or just those that apply to themselves?'; $string['helpcalendarsettings'] = 'Configure various calendar and date/time-related aspects of Moodle'; $string['helpforcetimezone'] = 'You can allow users to individually select their timezone, or force a timezone for everyone.'; diff --git a/lib/accesslib.php b/lib/accesslib.php index 308c350dae..ca712b75ca 100755 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -187,10 +187,25 @@ function load_defaultuser_role($return=false) { * @return object role */ function get_guest_role() { - if ($roles = get_roles_with_capability('moodle/legacy:guest', CAP_ALLOW)) { - return array_shift($roles); // Pick the first one + global $CFG; + + if (empty($CFG->guestroleid)) { + if ($roles = get_roles_with_capability('moodle/legacy:guest', CAP_ALLOW)) { + $guestrole = array_shift($roles); // Pick the first one + set_config('guestroleid', $guestrole->id); + return $guestrole; + } else { + debugging('Can not find any guest role!'); + return false; + } } else { - return false; + if ($guestrole = get_record('role','id', $CFG->guestroleid)) { + return $guestrole; + } else { + //somebody is messing with guest roles, remove incorrect setting and try to find a new one + set_config('guestroleid', ''); + return get_guest_role(); + } } } -- 2.39.5