MDL-72720 webservice: Implement enable_plugin() method

This commit is contained in:
Sara Arjona
2021-10-05 16:30:22 +02:00
parent 96a16c14a0
commit 1dfa86b4b4
2 changed files with 44 additions and 34 deletions

View File

@@ -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);

View File

@@ -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;
}