From a7cb6a8de6a1a8e1ff91fc27fa60a2ef91e2eec6 Mon Sep 17 00:00:00 2001 From: Kevin Percy Date: Thu, 22 Feb 2024 18:15:40 +0800 Subject: [PATCH] MDL-15594 url: Add setting to enable parameters for URL --- lib/db/upgrade.php | 10 +++++ mod/url/lang/en/url.php | 2 + mod/url/locallib.php | 9 ++-- mod/url/mod_form.php | 40 ++++++++--------- mod/url/settings.php | 3 ++ mod/url/tests/behat/url_variables.feature | 52 +++++++++++++++++++++++ mod/url/upgrade.txt | 6 ++- version.php | 2 +- 8 files changed, 99 insertions(+), 25 deletions(-) create mode 100644 mod/url/tests/behat/url_variables.feature diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index c04f124cbb1..051db26d7c8 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -1144,5 +1144,15 @@ function xmldb_main_upgrade($oldversion) { upgrade_main_savepoint(true, 2024032600.01); } + if ($oldversion < 2024040200.01) { + // Enable variables to be set for URL resource. + if (!get_config('url', 'allowvariables')) { + set_config('allowvariables', true, 'url'); + } + + // Main savepoint reached. + upgrade_main_savepoint(true, 2024040200.01); + } + return true; } diff --git a/mod/url/lang/en/url.php b/mod/url/lang/en/url.php index a8ca6fb0ea2..1eea0fea2ea 100644 --- a/mod/url/lang/en/url.php +++ b/mod/url/lang/en/url.php @@ -23,6 +23,8 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +$string['allowvariables'] = 'Allow URL variables'; +$string['allowvariables_desc'] = 'Allow variables to be added to URLs. Variables enable you to pass internal information, such as the user\'s name, as part of the URL. Be aware of potential privacy risks when using this feature.'; $string['clicktoopen'] = 'Click on {$a} to open the resource.'; $string['configdisplayoptions'] = 'Select all options that should be available, existing settings are not modified. Hold CTRL key to select multiple fields.'; $string['configframesize'] = 'When a web page or an uploaded file is displayed within a frame, this value is the height (in pixels) of the top frame (which contains the navigation).'; diff --git a/mod/url/locallib.php b/mod/url/locallib.php index 5aeac297301..ed3f63873be 100644 --- a/mod/url/locallib.php +++ b/mod/url/locallib.php @@ -114,11 +114,12 @@ function url_get_full_url($url, $cm, $course, $config=null) { $fullurl = str_replace('>', '%3E', $fullurl); } + if (!$config) { + $config = get_config('url'); + } + // add variable url parameters - if (!empty($parameters)) { - if (!$config) { - $config = get_config('url'); - } + if ($config->allowvariables && !empty($parameters)) { $paramvalues = url_get_variable_values($url, $cm, $course, $config); foreach ($parameters as $parse=>$parameter) { diff --git a/mod/url/mod_form.php b/mod/url/mod_form.php index 099b8978231..31b2da6f2d5 100644 --- a/mod/url/mod_form.php +++ b/mod/url/mod_form.php @@ -100,27 +100,29 @@ class mod_url_mod_form extends moodleform_mod { } //------------------------------------------------------- - $mform->addElement('header', 'parameterssection', get_string('parametersheader', 'url')); - $mform->addElement('static', 'parametersinfo', '', get_string('parametersheader_help', 'url')); + if ($config->allowvariables) { + $mform->addElement('header', 'parameterssection', get_string('parametersheader', 'url')); + $mform->addElement('static', 'parametersinfo', '', get_string('parametersheader_help', 'url')); - if (empty($this->current->parameters)) { - $parcount = 5; - } else { - $parcount = 5 + count((array) unserialize_array($this->current->parameters)); - $parcount = ($parcount > 100) ? 100 : $parcount; - } - $options = url_get_variable_options($config); + if (empty($this->current->parameters)) { + $parcount = 5; + } else { + $parcount = 5 + count((array)unserialize_array($this->current->parameters)); + $parcount = ($parcount > 100) ? 100 : $parcount; + } + $options = url_get_variable_options($config); - for ($i=0; $i < $parcount; $i++) { - $parameter = "parameter_$i"; - $variable = "variable_$i"; - $pargroup = "pargoup_$i"; - $group = array( - $mform->createElement('text', $parameter, '', array('size'=>'12')), - $mform->createElement('selectgroups', $variable, '', $options), - ); - $mform->addGroup($group, $pargroup, get_string('parameterinfo', 'url'), ' ', false); - $mform->setType($parameter, PARAM_RAW); + for ($i = 0; $i < $parcount; $i++) { + $parameter = "parameter_$i"; + $variable = "variable_$i"; + $pargroup = "pargoup_$i"; + $group = [ + $mform->createElement('text', $parameter, '', ['size' => '12']), + $mform->createElement('selectgroups', $variable, '', $options), + ]; + $mform->addGroup($group, $pargroup, get_string('parameterinfo', 'url'), ' ', false); + $mform->setType($parameter, PARAM_RAW); + } } //------------------------------------------------------- diff --git a/mod/url/settings.php b/mod/url/settings.php index a19f657beea..6e3f6c213e2 100644 --- a/mod/url/settings.php +++ b/mod/url/settings.php @@ -46,8 +46,11 @@ if ($ADMIN->fulltree) { get_string('framesize', 'url'), get_string('configframesize', 'url'), 130, PARAM_INT)); $settings->add(new admin_setting_configpasswordunmask('url/secretphrase', get_string('password'), get_string('configsecretphrase', 'url'), '')); + $settings->add(new admin_setting_configcheckbox('url/allowvariables', + get_string('allowvariables', 'url'), get_string('allowvariables_desc', 'url'), false)); $settings->add(new admin_setting_configcheckbox('url/rolesinparams', get_string('rolesinparams', 'url'), get_string('configrolesinparams', 'url'), false)); + $settings->hide_if('url/rolesinparams', 'url/allowvariables'); $settings->add(new admin_setting_configmultiselect('url/displayoptions', get_string('displayoptions', 'url'), get_string('configdisplayoptions', 'url'), $defaultdisplayoptions, $displayoptions)); diff --git a/mod/url/tests/behat/url_variables.feature b/mod/url/tests/behat/url_variables.feature new file mode 100644 index 00000000000..eb6d02b45a1 --- /dev/null +++ b/mod/url/tests/behat/url_variables.feature @@ -0,0 +1,52 @@ +@mod @mod_url @javascript +Feature: Manage URL variables + In order to maintain privacy for URLs + As a teacher + I need to be able to manage URL variables safely + + Background: + Given the following "courses" exist: + | fullname | shortname | category | + | Course 1 | C1 | 0 | + + Scenario: Disabling URL variables hides Role names as URL variables check box + Given the following config values are set as admin: + | allowvariables | 1 | url | + And I log in as "admin" + And I navigate to "Plugins > Activity modules > URL" in site administration + When I click on "Allow URL variables" "checkbox" + Then I should not see "Role names as URL variables" + And I click on "Allow URL variables" "checkbox" + And I should see "Role names as URL variables" + + Scenario: Disable the use of URL variables + Given the following config values are set as admin: + | allowvariables | 0 | url | + When I log in as "admin" + And I am on "Course 1" course homepage with editing mode on + And I add a "URL" to section "1" using the activity chooser + Then I should not see "URL variables" + + Scenario: Enable the use of URL variables without role names + Given the following config values are set as admin: + | allowvariables | 1 | url | + | rolesinparams | 0 | url | + When I log in as "admin" + And I am on "Course 1" course homepage with editing mode on + And I add a "URL" to section "1" using the activity chooser + Then I should see "URL variables" + And I expand all fieldsets + And I should see "Full site name" in the "id_variable_0" "select" + But I should not see "Roles" in the "id_variable_0" "select" + + Scenario: Enable the use of URL variables with role names + Given the following config values are set as admin: + | allowvariables | 1 | url | + | rolesinparams | 1 | url | + When I log in as "admin" + And I am on "Course 1" course homepage with editing mode on + And I add a "URL" to section "1" using the activity chooser + Then I should see "URL variables" + And I expand all fieldsets + And I should see "Full site name" in the "id_variable_0" "select" + And I should see "Your word for 'Student'" in the "id_variable_0" "select" diff --git a/mod/url/upgrade.txt b/mod/url/upgrade.txt index cbc598a736d..4078f25b512 100644 --- a/mod/url/upgrade.txt +++ b/mod/url/upgrade.txt @@ -1,4 +1,8 @@ -This files describes API changes in the quiz code. +This files describes API changes in the URL code. + +=== 4.4 === + +* A new admin setting "Allow URL variables" has been added, which allows/disallows the use of variables for URL resources. Variables enable you to pass internal information, such as the user's name, as part of the URL. === 4.0 === diff --git a/version.php b/version.php index 0d2a6f22720..91aba1a526d 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2024040200.00; // YYYYMMDD = weekly release date of this DEV branch. +$version = 2024040200.01; // YYYYMMDD = weekly release date of this DEV branch. // RR = release increments - 00 in DEV branches. // .XX = incremental changes. $release = '4.4dev+ (Build: 20240402)'; // Human-friendly version name