From c64a33c345376032c27f432e4f49260beb995145 Mon Sep 17 00:00:00 2001 From: Jason den Dulk Date: Thu, 28 Apr 2022 09:47:58 +1000 Subject: [PATCH 1/2] Issue #288: Split allowed IPs settings into two. Merge two settings when accessing config. Bump version. --- classes/local/outagelib.php | 7 ++++++- lang/en/auth_outage.php | 4 ++++ settings.php | 10 ++++++++++ tests/phpunit/local/outagelib_test.php | 15 +++++++++++++-- version.php | 4 ++-- 5 files changed, 35 insertions(+), 5 deletions(-) diff --git a/classes/local/outagelib.php b/classes/local/outagelib.php index 5a09ace..6d76759 100644 --- a/classes/local/outagelib.php +++ b/classes/local/outagelib.php @@ -168,7 +168,12 @@ class outagelib { } } - return (object)array_merge(self::get_config_defaults(), $config); + $config = array_merge(self::get_config_defaults(), $config); + // Combine allowed IPs config values together. + if (isset($config['allowedips_forced'])) { + $config['allowedips'] = trim($config['allowedips'] . "\n" . $config['allowedips_forced']); + } + return (object)$config; } /** diff --git a/lang/en/auth_outage.php b/lang/en/auth_outage.php index 8842f8a..3192e85 100644 --- a/lang/en/auth_outage.php +++ b/lang/en/auth_outage.php @@ -158,6 +158,10 @@ $string['warningduration'] = 'Warning duration'; $string['warningduration_help'] = 'How long before the start of the outage should the warning be displayed.'; $string['warningreenablemaintenancemode'] = 'Please note that saving this outage will re-enable maintenance mode.
Untick "Auto start maintenance mode" if you want to prevent this.'; +$string['builtinallowediplist'] = 'Builtin Allowed IP List'; +$string['builtinallowediplist_desc'] = 'Allowed IP list defined via configuration'; +$string['ips_combine'] = 'The IPs listed above will be combined with the IPs listed below.'; + /* * Privacy provider (GDPR) */ diff --git a/settings.php b/settings.php index c86670e..cfb919e 100644 --- a/settings.php +++ b/settings.php @@ -110,6 +110,8 @@ if ($hassiteconfig && is_enabled_auth('outage')) { $description .= $OUTPUT->notification(get_string($message, 'auth_outage', ['ip' => getremoteaddr()]), $type); $description .= '

'.get_string('ipblockersyntax', 'admin').'

'; + $description .= '

'.get_string('ips_combine', 'auth_outage').'

'; + $iplist = new admin_setting_configiplist( 'auth_outage/allowedips', get_string('allowediplist', 'admin'), @@ -119,6 +121,14 @@ if ($hassiteconfig && is_enabled_auth('outage')) { $iplist->set_updatedcallback('auth_outage_outagelib_prepare_next_outage'); $settings->add($iplist); + $iplist = new admin_setting_configiplist( + 'auth_outage/allowedips_forced', + get_string('builtinallowediplist', 'auth_outage'), + get_string('builtinallowediplist_desc', 'auth_outage'), + '' + ); + $settings->add($iplist); + // Create 'Static Page - Elements to Remove' settings. $toremove = new admin_setting_configtextarea( 'auth_outage/remove_selectors', diff --git a/tests/phpunit/local/outagelib_test.php b/tests/phpunit/local/outagelib_test.php index 2a7b687..942fa1b 100644 --- a/tests/phpunit/local/outagelib_test.php +++ b/tests/phpunit/local/outagelib_test.php @@ -216,6 +216,10 @@ class auth_outage_outagelib_test extends auth_outage_base_testcase { foreach ($keys as $k) { self::assertSame($config->$k, $k.'_value', 'auth_outage'); } + + set_config('allowedips_forced', 'allowedips_forced_value', 'auth_outage'); + $config = outagelib::get_config(); + self::assertSame($config->allowedips, "allowedips_value\nallowedips_forced_value", 'auth_outage'); } /** @@ -336,8 +340,10 @@ EOT; /** * Test create maintenance php code without age + * + * @dataProvider test_createmaintenancephpcode_withoutage_provider */ - public function test_createmaintenancephpcode_withoutage() { + public function test_createmaintenancephpcode_withoutage($configkey) { global $CFG; $this->resetAfterTest(true); @@ -380,7 +386,7 @@ EOT; 'stoptime' => 456, ]); $file = $CFG->dataroot.'/climaintenance.php'; - set_config('allowedips', '127.0.0.1', 'auth_outage'); + set_config($configkey, '127.0.0.1', 'auth_outage'); outagelib::update_climaintenance_code($outage); self::assertFileExists($file); @@ -388,6 +394,10 @@ EOT; self::assertSame($found, $expected); } + public function test_createmaintenancephpcode_withoutage_provider(): array { + return [['allowedips'], ['allowedips_forced']]; + } + /** * Test create maintenance php code without IPs */ @@ -401,6 +411,7 @@ EOT; ]); $file = $CFG->dataroot.'/climaintenance.php'; set_config('allowedips', '', 'auth_outage'); + set_config('allowedips_forced', '', 'auth_outage'); touch($file); outagelib::update_climaintenance_code($outage); diff --git a/version.php b/version.php index 4925c76..304b90b 100644 --- a/version.php +++ b/version.php @@ -28,8 +28,8 @@ defined('MOODLE_INTERNAL') || die(); $plugin->component = "auth_outage"; -$plugin->version = 2022011400; // The current plugin version (Date: YYYYMMDDXX). -$plugin->release = 2022011400; // Human-readable release information. +$plugin->version = 2022042800; // The current plugin version (Date: YYYYMMDDXX). +$plugin->release = 2022042800; // Human-readable release information. $plugin->requires = 2017111309; // 2017111309 = T13, but this really requires 3.9 and higher. $plugin->maturity = MATURITY_STABLE; // Suitable for PRODUCTION environments! $plugin->supported = [39, 311]; // A range of branch numbers of supported moodle versions. From 1f08a44a1a9073b23705f8818548098499db49f3 Mon Sep 17 00:00:00 2001 From: Jason den Dulk Date: Thu, 28 Apr 2022 13:16:40 +1000 Subject: [PATCH 2/2] Change builtinallowediplist_desc. Move the new lang strings to be in alphabetical order. --- lang/en/auth_outage.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lang/en/auth_outage.php b/lang/en/auth_outage.php index 3192e85..0324a19 100644 --- a/lang/en/auth_outage.php +++ b/lang/en/auth_outage.php @@ -26,6 +26,8 @@ $string['auth_outagedescription'] = 'Auxiliary plugin that warns users about a future outage and prevents them from logging in once the outage starts.'; $string['autostart'] = 'Auto start maintenance mode.'; $string['autostart_help'] = 'If selected, when the outage starts it will automatically turn on Moodle maintenance mode.'; +$string['builtinallowediplist'] = 'Builtin Allowed IP List'; +$string['builtinallowediplist_desc'] = 'A second allowed IP list which makes it easier to have some IPs forced in config.php and others editable in the UI'; $string['clicreatehelp'] = 'Creates a new outage.'; $string['clicreateexamples'] = "Create an outage starting in 10 seconds\n\n> php create.php -s=10"; $string['clicreateparamautostart'] = 'must be Y or N, sets if the outage automatically triggers maintenance mode.'; @@ -100,6 +102,7 @@ $string['infostart'] = 'start'; $string['infostartofwarning'] = 'start of warning'; $string['infostaticpage'] = 'static page'; $string['infopagestaticgenerated'] = 'This warning was generated on {$a->time}.'; +$string['ips_combine'] = 'The IPs listed above will be combined with the IPs listed below.'; $string['allowedipsempty'] = 'When the allowed IPs list is empty we will not block anyone. You can add your own IP address ({$a->ip}) and block all other IPs.'; $string['allowedipshasmyip'] = 'Your IP ({$a->ip}) is in the list and you will not be blocked out during an Outage.'; $string['allowedipshasntmyip'] = 'Your IP ({$a->ip}) is not in the list and you will be blocked out during an outage.'; @@ -158,10 +161,6 @@ $string['warningduration'] = 'Warning duration'; $string['warningduration_help'] = 'How long before the start of the outage should the warning be displayed.'; $string['warningreenablemaintenancemode'] = 'Please note that saving this outage will re-enable maintenance mode.
Untick "Auto start maintenance mode" if you want to prevent this.'; -$string['builtinallowediplist'] = 'Builtin Allowed IP List'; -$string['builtinallowediplist_desc'] = 'Allowed IP list defined via configuration'; -$string['ips_combine'] = 'The IPs listed above will be combined with the IPs listed below.'; - /* * Privacy provider (GDPR) */