Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403]

   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.
  13  //
  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/>.
  16  
  17  /**
  18   * Defines classes used for plugin info.
  19   *
  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
  23   */
  24  namespace core\plugininfo;
  25  
  26  use admin_settingpage;
  27  use moodle_url;
  28  use part_of_admin_tree;
  29  
  30  /**
  31   * Class for authentication plugins
  32   */
  33  class auth extends base {
  34  
  35      public static function plugintype_supports_disabling(): bool {
  36          return true;
  37      }
  38  
  39      public function is_uninstall_allowed() {
  40          global $DB;
  41  
  42          if (in_array($this->name, array('manual', 'nologin', 'webservice', 'mnet'))) {
  43              return false;
  44          }
  45  
  46          return !$DB->record_exists('user', array('auth'=>$this->name));
  47      }
  48  
  49      /**
  50       * Finds all enabled plugins, the result may include missing plugins.
  51       * @return array|null of enabled plugins $pluginname=>$pluginname, null means unknown
  52       */
  53      public static function get_enabled_plugins() {
  54          global $CFG;
  55  
  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;
  60          }
  61  
  62          return $enabled;
  63      }
  64  
  65      public static function enable_plugin(string $pluginname, int $enabled): bool {
  66          global $CFG;
  67  
  68          $haschanged = false;
  69          $plugins = [];
  70          if (!empty($CFG->auth)) {
  71              $plugins = array_flip(explode(',', $CFG->auth));
  72          }
  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;
  80  
  81              if ($pluginname == $CFG->registerauth) {
  82                  set_config('registerauth', '');
  83              }
  84          }
  85  
  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();
  94          }
  95  
  96          return $haschanged;
  97      }
  98  
  99      public function get_settings_section_name() {
 100          return 'authsetting' . $this->name;
 101      }
 102  
 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.
 109  
 110          if (!$this->is_installed_and_upgraded()) {
 111              return;
 112          }
 113  
 114          if (!$hassiteconfig) {
 115              return;
 116          }
 117  
 118          $section = $this->get_settings_section_name();
 119  
 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.
 126          }
 127  
 128          if ($settings) {
 129              $ADMIN->add($parentnodename, $settings);
 130          }
 131      }
 132  
 133      /**
 134       * Return URL used for management of plugins of this type.
 135       * @return moodle_url
 136       */
 137      public static function get_manage_url() {
 138          return new moodle_url('/admin/settings.php', array('section'=>'manageauths'));
 139      }
 140  
 141      /**
 142       * Pre-uninstall hook.
 143       *
 144       * This is intended for disabling of plugin, some DB table purging, etc.
 145       *
 146       * NOTE: to be called from uninstall_plugin() only.
 147       * @private
 148       */
 149      public function uninstall_cleanup() {
 150          global $CFG;
 151  
 152          if (!empty($CFG->auth)) {
 153              $auths = explode(',', $CFG->auth);
 154              $auths = array_unique($auths);
 155          } else {
 156              $auths = array();
 157          }
 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);
 163          }
 164  
 165          if (!empty($CFG->registerauth) and $CFG->registerauth === $this->name) {
 166              unset_config('registerauth');
 167          }
 168  
 169          parent::uninstall_cleanup();
 170      }
 171  }