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.
   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   * Moodle MFA plugin lib
  19   *
  20   * @package     tool_mfa
  21   * @author      Mikhail Golenkov <golenkovm@gmail.com>
  22   * @copyright   Catalyst IT
  23   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  use core\context;
  27  
  28  /**
  29   * Main hook.
  30   *
  31   * e.g. Add permissions logic across a site or course
  32   *
  33   * @param mixed $courseorid
  34   * @param mixed $autologinguest
  35   * @param mixed $cm
  36   * @param mixed $setwantsurltome
  37   * @param mixed $preventredirect
  38   * @return void
  39   * @throws \moodle_exception
  40   */
  41  function tool_mfa_after_require_login($courseorid = null, $autologinguest = null, $cm = null,
  42      $setwantsurltome = null, $preventredirect = null): void {
  43  
  44      global $SESSION;
  45      // Tests for hooks being fired to test patches.
  46      if (PHPUNIT_TEST) {
  47          $SESSION->mfa_login_hook_test = true;
  48      }
  49  
  50      if (empty($SESSION->tool_mfa_authenticated)) {
  51          \tool_mfa\manager::require_auth($courseorid, $autologinguest, $cm, $setwantsurltome, $preventredirect);
  52      }
  53  }
  54  
  55  /**
  56   * Extends navigation bar and injects MFA Preferences menu to user preferences.
  57   *
  58   * @param navigation_node $navigation
  59   * @param stdClass $user
  60   * @param context_user $usercontext
  61   * @param stdClass $course
  62   * @param context_course $coursecontext
  63   *
  64   * @return mix void or null
  65   * @throws \moodle_exception
  66   */
  67  function tool_mfa_extend_navigation_user_settings(navigation_node $navigation, stdClass $user, $usercontext, stdClass $course, $coursecontext) {
  68      global $PAGE;
  69  
  70      // Only inject if user is on the preferences page.
  71      $onpreferencepage = $PAGE->url->compare(new moodle_url('/user/preferences.php'), URL_MATCH_BASE);
  72      if (!$onpreferencepage) {
  73          return null;
  74      }
  75  
  76      if (\tool_mfa\manager::is_ready() && \tool_mfa\manager::possible_factor_setup()) {
  77          $url = new moodle_url('/admin/tool/mfa/user_preferences.php');
  78          $node = navigation_node::create(get_string('preferences:header', 'tool_mfa'), $url,
  79              navigation_node::TYPE_SETTING);
  80          $usernode = $navigation->find('useraccount', navigation_node::TYPE_CONTAINER);
  81          $usernode->add_node($node);
  82      }
  83  }
  84  
  85  /**
  86   * Triggered as soon as practical on every moodle bootstrap after config has
  87   * been loaded. The $USER object is available at this point too.
  88   *
  89   * @return void
  90   */
  91  function tool_mfa_after_config(): void {
  92      global $CFG, $SESSION;
  93  
  94      // Tests for hooks being fired to test patches.
  95      // Store in $CFG, $SESSION not present at this point.
  96      if (PHPUNIT_TEST) {
  97          $CFG->mfa_config_hook_test = true;
  98      }
  99  
 100      // Check for not logged in.
 101      if (isloggedin() && !isguestuser()) {
 102          // If not authenticated, force login required.
 103          if (empty($SESSION->tool_mfa_authenticated)) {
 104              \tool_mfa\manager::require_auth();
 105          }
 106      }
 107  }
 108  
 109  /**
 110   * Any plugin typically an admin tool can add new bulk user actions
 111   *
 112   * @return array
 113   */
 114  function tool_mfa_bulk_user_actions(): array {
 115      return [
 116          'tool_mfa_reset_factors' => new action_link(
 117              new moodle_url('/admin/tool/mfa/reset_factor.php'),
 118              get_string('resetfactor', 'tool_mfa')
 119          ),
 120      ];
 121  }
 122  
 123  /**
 124   * Serves any files for the guidance page.
 125   *
 126   * @param stdClass $course
 127   * @param stdClass $cm
 128   * @param context $context
 129   * @param string $filearea
 130   * @param array $args
 131   * @param bool $forcedownload
 132   * @param array $options
 133   * @return bool
 134   */
 135  function tool_mfa_pluginfile(stdClass $course, stdClass $cm, context $context, string $filearea,
 136      array $args, bool $forcedownload, array $options = []): bool {
 137      // Hardcode to only send guidance files from the top level.
 138      $fs = get_file_storage();
 139      $file = $fs->get_file(
 140          $context->id,
 141          'tool_mfa',
 142          'guidance',
 143          0,
 144          '/',
 145          $args[1]
 146      );
 147      if (!$file) {
 148          send_file_not_found();
 149          return false;
 150      }
 151      send_file($file, $file->get_filename());
 152  
 153      return true;
 154  }