weekly release 4.5dev
[moodle.git] / lib / classes / plugininfo / auth.php
blob1c7a4e71be6e61cdeb9172440560f986c3c78daa
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Defines classes used for plugin info.
20 * @package core
21 * @copyright 2013 Petr Skoda {@link http://skodak.org}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 namespace core\plugininfo;
26 use admin_settingpage;
27 use moodle_url;
28 use part_of_admin_tree;
30 /**
31 * Class for authentication plugins
33 class auth extends base {
35 public static function plugintype_supports_disabling(): bool {
36 return true;
39 public function is_uninstall_allowed() {
40 global $DB;
42 if (in_array($this->name, array('manual', 'nologin', 'webservice', 'mnet'))) {
43 return false;
46 return !$DB->record_exists('user', array('auth'=>$this->name));
49 /**
50 * Finds all enabled plugins, the result may include missing plugins.
51 * @return array|null of enabled plugins $pluginname=>$pluginname, null means unknown
53 public static function get_enabled_plugins() {
54 global $CFG;
56 // These two are always enabled and can't be disabled.
57 $enabled = array('nologin'=>'nologin', 'manual'=>'manual');
58 foreach (explode(',', $CFG->auth) as $auth) {
59 $enabled[$auth] = $auth;
62 return $enabled;
65 public static function enable_plugin(string $pluginname, int $enabled): bool {
66 global $CFG;
68 $haschanged = false;
69 $plugins = [];
70 if (!empty($CFG->auth)) {
71 $plugins = array_flip(explode(',', $CFG->auth));
73 // Only set visibility if it's different from the current value.
74 if ($enabled && !array_key_exists($pluginname, $plugins)) {
75 $plugins[$pluginname] = $pluginname;
76 $haschanged = true;
77 } else if (!$enabled && array_key_exists($pluginname, $plugins)) {
78 unset($plugins[$pluginname]);
79 $haschanged = true;
81 if ($pluginname == $CFG->registerauth) {
82 set_config('registerauth', '');
86 if ($haschanged) {
87 $new = implode(',', array_flip($plugins));
88 add_to_config_log('auth', $CFG->auth, $new, 'core');
89 set_config('auth', $new);
90 // Remove stale sessions.
91 \core\session\manager::gc();
92 // Reset caches.
93 \core_plugin_manager::reset_caches();
96 return $haschanged;
99 public function get_settings_section_name() {
100 return 'authsetting' . $this->name;
103 public function load_settings(part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
104 global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
105 /** @var \admin_root $ADMIN */
106 $ADMIN = $adminroot; // May be used in settings.php.
107 $plugininfo = $this; // Also can be used inside settings.php.
108 $auth = $this; // Also to be used inside settings.php.
110 if (!$this->is_installed_and_upgraded()) {
111 return;
114 if (!$hassiteconfig) {
115 return;
118 $section = $this->get_settings_section_name();
120 $settings = null;
121 if (file_exists($this->full_path('settings.php'))) {
122 // TODO: finish implementation of common settings - locking, etc.
123 $settings = new admin_settingpage($section, $this->displayname,
124 'moodle/site:config', $this->is_enabled() === false);
125 include($this->full_path('settings.php')); // This may also set $settings to null.
128 if ($settings) {
129 $ADMIN->add($parentnodename, $settings);
134 * Return URL used for management of plugins of this type.
135 * @return moodle_url
137 public static function get_manage_url() {
138 return new moodle_url('/admin/settings.php', array('section'=>'manageauths'));
142 * Pre-uninstall hook.
144 * This is intended for disabling of plugin, some DB table purging, etc.
146 * NOTE: to be called from uninstall_plugin() only.
147 * @private
149 public function uninstall_cleanup() {
150 global $CFG;
152 if (!empty($CFG->auth)) {
153 $auths = explode(',', $CFG->auth);
154 $auths = array_unique($auths);
155 } else {
156 $auths = array();
158 if (($key = array_search($this->name, $auths)) !== false) {
159 unset($auths[$key]);
160 $value = implode(',', $auths);
161 add_to_config_log('auth', $CFG->auth, $value, 'core');
162 set_config('auth', $value);
165 if (!empty($CFG->registerauth) and $CFG->registerauth === $this->name) {
166 unset_config('registerauth');
169 parent::uninstall_cleanup();