From 3360d357d4e67232bcf3dad89bbf1dc8dfc4496d Mon Sep 17 00:00:00 2001 From: Daniel Thee Roperto Date: Mon, 14 Nov 2016 15:02:30 +1100 Subject: [PATCH] Added warnings in case plugin is not properly configured. --- README.md | 1 + bootstrap.php | 2 +- classes/local/cli/cli_exception.php | 4 +-- classes/local/cli/clibase.php | 6 +++-- classes/local/outagelib.php | 38 ++++++++++++++++++++++++++-- lang/en/auth_outage.php | 3 ++- manage.php | 4 ++- settings.php | 5 +--- tests/phpunit/local/cli/cli_test.php | 2 +- views/manage.php | 2 ++ 10 files changed, 53 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 274c884..ace59ac 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,7 @@ enable the `Outage manager` plugin and place it on the top. after your `$CFG->dataroot` is set: ``` +// Insert this after $CFG->dataroot is defined. if (file_exists(__DIR__.'/auth/outage/bootstrap.php')) { require(__DIR__.'/auth/outage/bootstrap.php'); } diff --git a/bootstrap.php b/bootstrap.php index 566d843..fd319dc 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -54,4 +54,4 @@ if (file_exists($CFG->dataroot.'/climaintenance.php')) { } // 3) Set flag this file was loaded. -$CFG->auth_outage_check = 1; +$CFG->auth_outage_bootstrap_loaded = true; diff --git a/classes/local/cli/cli_exception.php b/classes/local/cli/cli_exception.php index 2b7a447..bb13d9f 100644 --- a/classes/local/cli/cli_exception.php +++ b/classes/local/cli/cli_exception.php @@ -74,9 +74,9 @@ class cli_exception extends Exception { const ERROR_OUTAGE_CHANGED = 7; /** - * The outage plugin is not enabled. + * The outage plugin is not properly configured. */ - const ERROR_PLUGIN_DISABLED = 8; + const ERROR_PLUGIN_CONFIGURATION = 8; /** * Moodle maintenance mode is enabled. diff --git a/classes/local/cli/clibase.php b/classes/local/cli/clibase.php index cc4b853..dc96578 100644 --- a/classes/local/cli/clibase.php +++ b/classes/local/cli/clibase.php @@ -25,6 +25,7 @@ namespace auth_outage\local\cli; +use auth_outage\local\outagelib; use coding_exception; use core\session\manager; @@ -58,8 +59,9 @@ abstract class clibase { global $CFG; require_once($CFG->libdir.'/clilib.php'); - if (!is_enabled_auth('outage')) { - throw new cli_exception(get_string('cliplugindisabled', 'auth_outage'), cli_exception::ERROR_PLUGIN_DISABLED); + $warning = outagelib::generate_plugin_configuration_warning(); + if ($warning) { + throw new cli_exception($warning, cli_exception::ERROR_PLUGIN_CONFIGURATION); } $this->become_admin_user(); diff --git a/classes/local/outagelib.php b/classes/local/outagelib.php index 8ddfd17..65b70c3 100644 --- a/classes/local/outagelib.php +++ b/classes/local/outagelib.php @@ -104,7 +104,7 @@ class outagelib { // There is a previewing or active outage. $CFG->additionalhtmltopofbody = renderer::get()->render_warningbar($active, $time, false, $preview). - $CFG->additionalhtmltopofbody; + $CFG->additionalhtmltopofbody; } catch (Exception $e) { debugging('Exception occured while injecting our code: '.$e->getMessage()); debugging($e->getTraceAsString(), DEBUG_DEVELOPER); @@ -144,7 +144,7 @@ class outagelib { 'default_warning_duration' => (string)(60 * 60), 'default_title' => get_string('defaulttitlevalue', 'auth_outage'), 'default_description' => get_string('defaultdescriptionvalue', 'auth_outage'), - 'remove_selectors' => '.usermenu', + 'remove_selectors' => ".usermenu\n.logininfo\n.homelink", ]; } @@ -290,4 +290,38 @@ EOT; file_put_contents($file, $code); } } + + /** + * Generates a warning message in case the plugin is not active and configured. + * + * @return string + * + * @internal stdClass $CFG + * @internal bootstrap_renderer $OUTPUT + */ + public static function generate_plugin_configuration_warning() { + global $CFG, $OUTPUT; + + $message = []; + + if (!isset($CFG->auth_outage_bootstrap_loaded) || !$CFG->auth_outage_bootstrap_loaded) { + $message[] = get_string('configurationwarning', 'auth_outage'); + } + + if (!is_enabled_auth('outage')) { + $message[] = get_string('configurationdisabled', 'auth_outage'); + } + + if (count($message) == 0) { + return ''; + } + + if (CLI_SCRIPT) { + $message = html_to_text(implode("; ", $message)); + } else { + $message = $OUTPUT->notification(implode("
", $message), 'notifyfailure'); + } + + return $message; + } } diff --git a/lang/en/auth_outage.php b/lang/en/auth_outage.php index d866276..bf6f256 100644 --- a/lang/en/auth_outage.php +++ b/lang/en/auth_outage.php @@ -43,7 +43,6 @@ $string['clifinishparamhelp'] = 'shows parameters help.'; $string['clifinishparamactive'] = 'finishes the currently active outage.'; $string['clifinishparamoutageid'] = 'the id of the outage to finish.'; $string['cliinmaintenancemode'] = 'Moodle maintenance mode is on. Use "php admin/cli/maintenance.php --disable" to disable it before finishing the outage.'; -$string['cliplugindisabled'] = 'The authentication plugin \'Outage\' is disabled. Please enable it in the site administration it and try again.'; $string['cliwaitforiterroridxoractive'] = 'You must use --outageid=# or --active parameter but not both.'; $string['cliwaitforithelp'] = 'Waits until an outage starts.'; $string['cliwaitforitoutagestarted'] = 'Outage started!'; @@ -60,6 +59,8 @@ $string['clierroroutageended'] = 'Outage has already ended.'; $string['clierroroutagenotfound'] = 'Outage not found.'; $string['clioutagecreated'] = 'Outage created, id: {$a->id}'; $string['clone'] = 'Clone'; +$string['configurationwarning'] = 'The outage plugin is not properly configured, please refer to README.md for more information.'; +$string['configurationdisabled'] = 'The authentication plugin \'Outage\' is disabled. Please enable it in the site administration it and try again.'; $string['datetimeformat'] = '%a %d %h %Y at %I:%M%P %Z'; $string['defaultlayoutcss'] = 'Layout CSS'; $string['defaultlayoutcssdescription'] = 'This CSS code can be used to override the Outage Warning Bar CSS.'; diff --git a/manage.php b/manage.php index 510661c..967d083 100644 --- a/manage.php +++ b/manage.php @@ -25,6 +25,7 @@ use auth_outage\dml\outagedb; use auth_outage\output\renderer; +use auth_outage\local\outagelib; require_once(__DIR__.'/../../config.php'); require_once($CFG->libdir.'/adminlib.php'); @@ -39,7 +40,8 @@ $now = time(); renderer::get()->output_view('manage.php', [ 'unended' => outagedb::get_all_unended($now), - 'ended' => outagedb::get_all_ended($now), + 'ended' => outagedb::get_all_ended($now), + 'warning' => outagelib::generate_plugin_configuration_warning(), ]); echo $OUTPUT->footer(); diff --git a/settings.php b/settings.php index 036876a..2261fbd 100644 --- a/settings.php +++ b/settings.php @@ -89,10 +89,7 @@ if ($hassiteconfig && is_enabled_auth('outage')) { // Create 'Allowed IPs' settings. $allowedips = outagelib::get_config()->allowedips; - $description = ''; - if (!isset($CFG->auth_outage_check) || !$CFG->auth_outage_check) { - $description .= $OUTPUT->notification(get_string('allowedipsnoconfig', 'auth_outage'), 'notifyfailure'); - } + $description = outagelib::generate_plugin_configuration_warning(); if (trim($allowedips) == '') { $message = 'allowedipsempty'; $type = 'notifymessage'; diff --git a/tests/phpunit/local/cli/cli_test.php b/tests/phpunit/local/cli/cli_test.php index c83fccf..d05fa7f 100644 --- a/tests/phpunit/local/cli/cli_test.php +++ b/tests/phpunit/local/cli/cli_test.php @@ -103,7 +103,7 @@ class cli_test extends auth_outage_cli_testcase { \core\session\manager::gc(); // Remove stale sessions. core_plugin_manager::reset_caches(); // Try to create an CLI object. - $this->set_expected_cli_exception(cli_exception::ERROR_PLUGIN_DISABLED); + $this->set_expected_cli_exception(cli_exception::ERROR_PLUGIN_CONFIGURATION); new create(); } } diff --git a/views/manage.php b/views/manage.php index 0a54f79..22925df 100644 --- a/views/manage.php +++ b/views/manage.php @@ -30,6 +30,8 @@ use auth_outage\output\renderer; defined('MOODLE_INTERNAL') || die(); $urlnew = new moodle_url('/auth/outage/edit.php'); + +echo $viewbag['warning']; ?>