Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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 function get_settings_section_name() {
  61          return 'authsetting' . $this->name;
  62      }
  63  
  64      public function load_settings(part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
  65          global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
  66          $ADMIN = $adminroot; // May be used in settings.php.
  67          $plugininfo = $this; // Also can be used inside settings.php.
  68          $auth = $this;       // Also to be used inside settings.php.
  69  
  70          if (!$this->is_installed_and_upgraded()) {
  71              return;
  72          }
  73  
  74          if (!$hassiteconfig) {
  75              return;
  76          }
  77  
  78          $section = $this->get_settings_section_name();
  79  
  80          $settings = null;
  81          if (file_exists($this->full_path('settings.php'))) {
  82              // TODO: finish implementation of common settings - locking, etc.
  83              $settings = new admin_settingpage($section, $this->displayname,
  84                  'moodle/site:config', $this->is_enabled() === false);
  85              include($this->full_path('settings.php')); // This may also set $settings to null.
  86          } else if (file_exists($this->full_path('config.html'))) {
  87              $settingsurl = new moodle_url('/admin/auth_config.php', array('auth' => $this->name));
  88              $settings = new admin_externalpage($section, $this->displayname,
  89                  $settingsurl, 'moodle/site:config', $this->is_enabled() === false);
  90          }
  91  
  92          if ($settings) {
  93              $ADMIN->add($parentnodename, $settings);
  94          }
  95      }
  96  
  97      /**
  98       * Return URL used for management of plugins of this type.
  99       * @return moodle_url
 100       */
 101      public static function get_manage_url() {
 102          return new moodle_url('/admin/settings.php', array('section'=>'manageauths'));
 103      }
 104  
 105      /**
 106       * Pre-uninstall hook.
 107       *
 108       * This is intended for disabling of plugin, some DB table purging, etc.
 109       *
 110       * NOTE: to be called from uninstall_plugin() only.
 111       * @private
 112       */
 113      public function uninstall_cleanup() {
 114          global $CFG;
 115  
 116          if (!empty($CFG->auth)) {
 117              $auths = explode(',', $CFG->auth);
 118              $auths = array_unique($auths);
 119          } else {
 120              $auths = array();
 121          }
 122          if (($key = array_search($this->name, $auths)) !== false) {
 123              unset($auths[$key]);
 124              set_config('auth', implode(',', $auths));
 125          }
 126  
 127          if (!empty($CFG->registerauth) and $CFG->registerauth === $this->name) {
 128              unset_config('registerauth');
 129          }
 130  
 131          parent::uninstall_cleanup();
 132      }
 133  }