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   * Page to reset factor for users.
  19   *
  20   * @package     tool_mfa
  21   * @author      Peter Burnett <peterburnett@catalyst-au.net>
  22   * @copyright   Catalyst IT
  23   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  require_once(__DIR__ . '/../../../config.php');
  27  require_once($CFG->libdir . '/adminlib.php');
  28  admin_externalpage_setup('tool_mfa_resetfactor');
  29  
  30  $bulk = !empty($SESSION->bulk_users);
  31  
  32  $factors = \tool_mfa\plugininfo\factor::get_factors();
  33  $form = new \tool_mfa\local\form\reset_factor(null, ['factors' => $factors, 'bulk' => $bulk]);
  34  
  35  if ($form->is_cancelled()) {
  36      if ($bulk) {
  37          $url = new moodle_url('/admin/user/user_bulk.php');
  38      } else {
  39          $url = new moodle_url('/admin/category.php', ['category' => 'toolmfafolder']);
  40      }
  41      redirect($url);
  42  } else if ($fromform = $form->get_data()) {
  43      // Get factor from select index.
  44      if ($fromform->factor !== 'all') {
  45          $factor = $factors[$fromform->factor];
  46      } else {
  47          $factor = 'all';
  48      }
  49  
  50      // Setup var to put into notification strings.
  51      $stringvar = $factor === 'all' ? get_string('all') : $factor->get_display_name();
  52  
  53      // Setup user array for bulk action.
  54      $users = $bulk ? $SESSION->bulk_users : [$fromform->user];
  55  
  56      foreach ($users as $user) {
  57          if (!$user instanceof stdClass) {
  58              $user = \core_user::get_user($user);
  59          }
  60  
  61          // Add a user preference, to display a notification to the user that their factor was reset.
  62          // This should only be done if the factor is active for the user, and has input.
  63          $factors = $factor === 'all' ? \tool_mfa\plugininfo\factor::get_factors() : [$factor];
  64          foreach ($factors as $factor) {
  65              $factor->delete_factor_for_user($user);
  66              if (count($factor->get_active_user_factors($user)) > 0 && $factor->has_setup()) {
  67                  $prefname = 'tool_mfa_reset_' . $factor->name;
  68                  set_user_preference($prefname, true, $user);
  69              }
  70          }
  71  
  72          // If we are just doing 1 user.
  73          if (!$bulk) {
  74              $stringarr = ['factor' => $stringvar, 'username' => $user->username];
  75              \core\notification::success(get_string('resetsuccess', 'tool_mfa', $stringarr));
  76  
  77              // Reload page.
  78              redirect($PAGE->url);
  79          }
  80      }
  81  
  82      \core\notification::success(get_string('resetsuccessbulk', 'tool_mfa', $stringvar));
  83      unset($SESSION->bulk_users);
  84      // Redirect to bulk actions page.
  85      redirect(new moodle_url('/admin/user/user_bulk.php'));
  86  }
  87  
  88  echo $OUTPUT->header();
  89  echo $OUTPUT->heading(get_string('resetfactor', 'tool_mfa'));
  90  $form->display();
  91  echo $OUTPUT->footer();