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   * Configure user factor page
  18   *
  19   * @package     tool_mfa
  20   * @author      Mikhail Golenkov <golenkovm@gmail.com>
  21   * @copyright   Catalyst IT
  22   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require_once(__DIR__ . '/../../../config.php');
  26  
  27  use tool_mfa\local\form\setup_factor_form;
  28  use tool_mfa\local\form\revoke_factor_form;
  29  
  30  require_login(null, false);
  31  if (isguestuser()) {
  32      throw new require_login_exception('Guests are not allowed here.');
  33  }
  34  
  35  $action = optional_param('action', '', PARAM_ALPHANUMEXT);
  36  $factor = optional_param('factor', '', PARAM_ALPHANUMEXT);
  37  $factorid = optional_param('factorid', '', PARAM_INT);
  38  
  39  $params = ['action' => $action, 'factor' => $factor, 'factorid' => $factorid];
  40  $currenturl = new moodle_url('/admin/tool/mfa/action.php', $params);
  41  
  42  $returnurl = new moodle_url('/admin/tool/mfa/user_preferences.php');
  43  
  44  if (empty($factor) || empty($action)) {
  45      throw new moodle_exception('error:directaccess', 'tool_mfa', $returnurl);
  46  }
  47  
  48  if (!\tool_mfa\plugininfo\factor::factor_exists($factor)) {
  49      throw new moodle_exception('error:factornotfound', 'tool_mfa', $returnurl, $factor);
  50  }
  51  
  52  if (!in_array($action, \tool_mfa\plugininfo\factor::get_factor_actions())) {
  53      throw new moodle_exception('error:actionnotfound', 'tool_mfa', $returnurl, $action);
  54  }
  55  
  56  if (!empty($factorid) && !\tool_mfa\manager::is_factorid_valid($factorid, $USER)) {
  57      throw new moodle_exception('error:incorrectfactorid', 'tool_mfa', $returnurl, $factorid);
  58  }
  59  
  60  $factorobject = \tool_mfa\plugininfo\factor::get_factor($factor);
  61  
  62  $context = context_user::instance($USER->id);
  63  $PAGE->set_context($context);
  64  $PAGE->set_url('/admin/tool/mfa/action.php');
  65  $PAGE->set_pagelayout('standard');
  66  $PAGE->set_title(get_string($action.'factor', 'tool_mfa'));
  67  $PAGE->set_cacheable(false);
  68  
  69  if ($node = $PAGE->settingsnav->find('usercurrentsettings', null)) {
  70      $PAGE->navbar->add($node->get_content(), $node->action());
  71  }
  72  $PAGE->navbar->add(get_string('preferences:header', 'tool_mfa'), new \moodle_url('/admin/tool/mfa/user_preferences.php'));
  73  
  74  switch ($action) {
  75      case 'setup':
  76          if (!$factorobject || !$factorobject->has_setup()) {
  77              redirect($returnurl);
  78          }
  79  
  80          $PAGE->navbar->add(get_string('setupfactor', 'factor_'.$factor));
  81          $OUTPUT = $PAGE->get_renderer('tool_mfa');
  82          $form = new setup_factor_form($currenturl, ['factorname' => $factor]);
  83  
  84          if ($form->is_submitted()) {
  85              $form->is_validated();
  86  
  87              if ($form->is_cancelled()) {
  88                  redirect($returnurl);
  89              }
  90  
  91              if ($data = $form->get_data()) {
  92                  $record = $factorobject->setup_user_factor($data);
  93                  if (!empty($record)) {
  94                      $factorobject->set_state(\tool_mfa\plugininfo\factor::STATE_PASS);
  95                      $finalurl = new moodle_url($returnurl, ['action' => 'setup', 'factorid' => $record->id]);
  96                      redirect($finalurl);
  97                  }
  98  
  99                  throw new moodle_exception('error:setupfactor', 'tool_mfa', $returnurl);
 100              }
 101          }
 102  
 103          echo $OUTPUT->header();
 104          $form->display();
 105  
 106          break;
 107  
 108      case 'revoke':
 109          // Ensure sesskey is valid.
 110          require_sesskey();
 111  
 112          if (!$factorobject || !$factorobject->has_revoke()) {
 113              throw new moodle_exception('error:revoke', 'tool_mfa', $returnurl);
 114          }
 115  
 116          $PAGE->navbar->add(get_string('action:revoke', 'factor_'.$factor));
 117          $OUTPUT = $PAGE->get_renderer('tool_mfa');
 118  
 119          $revokeparams = [
 120              'factorname' => $factorobject->get_display_name(),
 121              'devicename' => $factorobject->get_label($factorid),
 122          ];
 123          $form = new revoke_factor_form($currenturl, $revokeparams);
 124  
 125          if ($form->is_submitted()) {
 126              $form->is_validated();
 127  
 128              if ($form->is_cancelled()) {
 129                  redirect($returnurl);
 130              }
 131  
 132              if ($form->get_data()) {
 133                  if ($factorobject->revoke_user_factor($factorid)) {
 134                      $finalurl = new moodle_url($returnurl, ['action' => 'revoked', 'factorid' => $factorid]);
 135                      redirect($finalurl);
 136                  }
 137  
 138                  throw new moodle_exception('error:revoke', 'tool_mfa', $returnurl);
 139              }
 140          }
 141  
 142          echo $OUTPUT->header();
 143          $form->display();
 144  
 145          break;
 146  
 147      default:
 148          break;
 149  }
 150  
 151  echo $OUTPUT->footer();