Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 401 and 402] [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 moodle_url, part_of_admin_tree, admin_settingpage, admin_externalpage;
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  /**
  31   * Class for authentication plugins
  32   */
  33  class auth extends base {
  34      public function is_uninstall_allowed() {
  35          global $DB;
  36  
  37          if (in_array($this->name, array('manual', 'nologin', 'webservice', 'mnet'))) {
  38              return false;
  39          }
  40  
  41          return !$DB->record_exists('user', array('auth'=>$this->name));
  42      }
  43  
  44      /**
  45       * Finds all enabled plugins, the result may include missing plugins.
  46       * @return array|null of enabled plugins $pluginname=>$pluginname, null means unknown
  47       */
  48      public static function get_enabled_plugins() {
  49          global $CFG;
  50  
  51          // These two are always enabled and can't be disabled.
  52          $enabled = array('nologin'=>'nologin', 'manual'=>'manual');
  53          foreach (explode(',', $CFG->auth) as $auth) {
  54              $enabled[$auth] = $auth;
  55          }
  56  
  57          return $enabled;
  58      }
  59  
  60      public static function enable_plugin(string $pluginname, int $enabled): bool {
  61          global $CFG;
  62  
  63          $haschanged = false;
  64          $plugins = [];
  65          if (!empty($CFG->auth)) {
  66              $plugins = array_flip(explode(',', $CFG->auth));
  67          }
  68          // Only set visibility if it's different from the current value.
  69          if ($enabled && !array_key_exists($pluginname, $plugins)) {
  70              $plugins[$pluginname] = $pluginname;
  71              $haschanged = true;
  72          } else if (!$enabled && array_key_exists($pluginname, $plugins)) {
  73              unset($plugins[$pluginname]);
  74              $haschanged = true;
  75  
  76              if ($pluginname == $CFG->registerauth) {
  77                  set_config('registerauth', '');
  78              }
  79          }
  80  
  81          if ($haschanged) {
  82              $new = implode(',', array_flip($plugins));
  83              add_to_config_log('auth', $CFG->auth, $new, 'core');
  84              set_config('auth', $new);
  85              // Remove stale sessions.
  86              \core\session\manager::gc();
  87              // Reset caches.
  88              \core_plugin_manager::reset_caches();
  89          }
  90  
  91          return $haschanged;
  92      }
  93  
  94      public function get_settings_section_name() {
  95          return 'authsetting' . $this->name;
  96      }
  97  
  98      public function load_settings(part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
  99          global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
 100          $ADMIN = $adminroot; // May be used in settings.php.
 101          $plugininfo = $this; // Also can be used inside settings.php.
 102          $auth = $this;       // Also to be used inside settings.php.
 103  
 104          if (!$this->is_installed_and_upgraded()) {
 105              return;
 106          }
 107  
 108          if (!$hassiteconfig) {
 109              return;
 110          }
 111  
 112          $section = $this->get_settings_section_name();
 113  
 114          $settings = null;
 115          if (file_exists($this->full_path('settings.php'))) {
 116              // TODO: finish implementation of common settings - locking, etc.
 117              $settings = new admin_settingpage($section, $this->displayname,
 118                  'moodle/site:config', $this->is_enabled() === false);
 119              include($this->full_path('settings.php')); // This may also set $settings to null.
 120          } else if (file_exists($this->full_path('config.html'))) {
 121              $settingsurl = new moodle_url('/admin/auth_config.php', array('auth' => $this->name));
 122              $settings = new admin_externalpage($section, $this->displayname,
 123                  $settingsurl, 'moodle/site:config', $this->is_enabled() === false);
 124          }
 125  
 126          if ($settings) {
 127              $ADMIN->add($parentnodename, $settings);
 128          }
 129      }
 130  
 131      /**
 132       * Return URL used for management of plugins of this type.
 133       * @return moodle_url
 134       */
 135      public static function get_manage_url() {
 136          return new moodle_url('/admin/settings.php', array('section'=>'manageauths'));
 137      }
 138  
 139      /**
 140       * Pre-uninstall hook.
 141       *
 142       * This is intended for disabling of plugin, some DB table purging, etc.
 143       *
 144       * NOTE: to be called from uninstall_plugin() only.
 145       * @private
 146       */
 147      public function uninstall_cleanup() {
 148          global $CFG;
 149  
 150          if (!empty($CFG->auth)) {
 151              $auths = explode(',', $CFG->auth);
 152              $auths = array_unique($auths);
 153          } else {
 154              $auths = array();
 155          }
 156          if (($key = array_search($this->name, $auths)) !== false) {
 157              unset($auths[$key]);
 158              $value = implode(',', $auths);
 159              add_to_config_log('auth', $CFG->auth, $value, 'core');
 160              set_config('auth', $value);
 161          }
 162  
 163          if (!empty($CFG->registerauth) and $CFG->registerauth === $this->name) {
 164              unset_config('registerauth');
 165          }
 166  
 167          parent::uninstall_cleanup();
 168      }
 169  }