Skip to content

Commit

Permalink
MDL-72720 auth: Implement enable_plugin() method
Browse files Browse the repository at this point in the history
  • Loading branch information
sarjona committed Oct 14, 2021
1 parent 6e693fb commit bbf3a2d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 27 deletions.
33 changes: 6 additions & 27 deletions admin/auth.php
Expand Up @@ -40,34 +40,15 @@

switch ($action) {
case 'disable':
// remove from enabled list
$key = array_search($auth, $authsenabled);
if ($key !== false) {
unset($authsenabled[$key]);
$value = implode(',', $authsenabled);
add_to_config_log('auth', $CFG->auth, $value, 'core');
set_config('auth', $value);
}

if ($auth == $CFG->registerauth) {
set_config('registerauth', '');
}
\core\session\manager::gc(); // Remove stale sessions.
core_plugin_manager::reset_caches();
// Remove from enabled list.
$class = \core_plugin_manager::resolve_plugininfo_class('auth');
$class::enable_plugin($auth, false);
break;

case 'enable':
// add to enabled list
if (!in_array($auth, $authsenabled)) {
$authsenabled[] = $auth;
$authsenabled = array_unique($authsenabled);
$value = implode(',', $authsenabled);
add_to_config_log('auth', $CFG->auth, $value, 'core');
set_config('auth', $value);
}

\core\session\manager::gc(); // Remove stale sessions.
core_plugin_manager::reset_caches();
// Add to enabled list.
$class = \core_plugin_manager::resolve_plugininfo_class('auth');
$class::enable_plugin($auth, true);
break;

case 'down':
Expand Down Expand Up @@ -109,5 +90,3 @@
}

redirect($returnurl);


34 changes: 34 additions & 0 deletions lib/classes/plugininfo/auth.php
Expand Up @@ -57,6 +57,40 @@ public static function get_enabled_plugins() {
return $enabled;
}

public static function enable_plugin(string $pluginname, int $enabled): bool {
global $CFG;

$haschanged = false;
$plugins = [];
if (!empty($CFG->auth)) {
$plugins = array_flip(explode(',', $CFG->auth));
}
// 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 ($pluginname == $CFG->registerauth) {
set_config('registerauth', '');
}
}

if ($haschanged) {
$new = implode(',', array_flip($plugins));
add_to_config_log('auth', $CFG->auth, $new, 'core');
set_config('auth', $new);
// Remove stale sessions.
\core\session\manager::gc();
// Reset caches.
\core_plugin_manager::reset_caches();
}

return $haschanged;
}

public function get_settings_section_name() {
return 'authsetting' . $this->name;
}
Expand Down

0 comments on commit bbf3a2d

Please sign in to comment.