From 1dfa86b4b4076d58fa66be0044ae5ef5a910c9c4 Mon Sep 17 00:00:00 2001 From: Sara Arjona Date: Tue, 5 Oct 2021 16:30:22 +0200 Subject: [PATCH] MDL-72720 webservice: Implement enable_plugin() method --- admin/webservice/protocols.php | 41 +++++---------------------- lib/classes/plugininfo/webservice.php | 37 ++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 34 deletions(-) diff --git a/admin/webservice/protocols.php b/admin/webservice/protocols.php index 688fe144d78..4c5512b90f0 100644 --- a/admin/webservice/protocols.php +++ b/admin/webservice/protocols.php @@ -38,46 +38,19 @@ $action = optional_param('action', '', PARAM_ALPHANUMEXT); $webservice = optional_param('webservice', '', PARAM_SAFEDIR); $confirm = optional_param('confirm', 0, PARAM_BOOL); -// get currently installed and enabled auth plugins -$available_webservices = core_component::get_plugin_list('webservice'); -if (!empty($webservice) and empty($available_webservices[$webservice])) { +// Get currently installed and enabled auth plugins. +$availablewebservices = core_component::get_plugin_list('webservice'); +if (!empty($webservice) and empty($availablewebservices[$webservice])) { redirect($returnurl); } -$active_webservices = empty($CFG->webserviceprotocols) ? array() : explode(',', $CFG->webserviceprotocols); -foreach ($active_webservices as $key=>$active) { - if (empty($available_webservices[$active])) { - unset($active_webservices[$key]); - } -} - -//////////////////////////////////////////////////////////////////////////////// -// process actions - +// Process actions. if (!confirm_sesskey()) { redirect($returnurl); } -switch ($action) { - - case 'disable': - // remove from enabled list - $key = array_search($webservice, $active_webservices); - unset($active_webservices[$key]); - break; - - case 'enable': - // add to enabled list - if (!in_array($webservice, $active_webservices)) { - $active_webservices[] = $webservice; - $active_webservices = array_unique($active_webservices); - } - break; - - default: - break; -} - -set_config('webserviceprotocols', implode(',', $active_webservices)); +$enabled = ($action == 'enable'); +$class = \core_plugin_manager::resolve_plugininfo_class('webservice'); +$class::enable_plugin($webservice, $enabled); redirect($returnurl); diff --git a/lib/classes/plugininfo/webservice.php b/lib/classes/plugininfo/webservice.php index ece8dfaca05..4df6637fd9a 100644 --- a/lib/classes/plugininfo/webservice.php +++ b/lib/classes/plugininfo/webservice.php @@ -50,6 +50,43 @@ class webservice extends base { return $enabled; } + public static function enable_plugin(string $pluginname, int $enabled): bool { + global $CFG; + + $haschanged = false; + $plugins = []; + if (!empty($CFG->webserviceprotocols)) { + $plugins = array_flip(explode(',', $CFG->webserviceprotocols)); + } + + // Remove plugins that are no longer available. + $availablews = \core_component::get_plugin_list('webservice'); + foreach ($plugins as $key => $notused) { + if (empty($availablews[$key])) { + unset($plugins[$key]); + } + } + + // Only set visibility if it's different from the current value. + if ($enabled && !array_key_exists($pluginname, $plugins)) { + $plugins[$pluginname] = $pluginname; + $haschanged = true; + } else if (!$enabled && array_key_exists($pluginname, $plugins)) { + unset($plugins[$pluginname]); + $haschanged = true; + } + + if ($haschanged) { + $new = implode(',', array_flip($plugins)); + add_to_config_log('webserviceprotocols', $CFG->webserviceprotocols, $new, 'core'); + set_config('webserviceprotocols', $new); + // Reset caches. + \core_plugin_manager::reset_caches(); + } + + return $haschanged; + } + public function get_settings_section_name() { return 'webservicesetting' . $this->name; } -- 2.43.0