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  namespace tool_mfa\local\form;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  
  21  require_once($CFG->libdir . "/formslib.php");
  22  
  23  /**
  24   * MFA login form
  25   *
  26   * @package     tool_mfa
  27   * @author      Mikhail Golenkov <golenkovm@gmail.com>
  28   * @copyright   Catalyst IT
  29   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class login_form extends \moodleform {
  32  
  33      /** @var \tool_mfa\local\form\global_form_manager */
  34      public $globalmanager;
  35  
  36      /**
  37       * Create an instance of this class.
  38       *
  39       * @param mixed $action the action attribute for the form. If empty defaults to auto detect the
  40       *              current url. If a moodle_url object then outputs params as hidden variables.
  41       * @param mixed $customdata if your form defintion method needs access to data such as $course
  42       *              $cm, etc. to construct the form definition then pass it in this array. You can
  43       *              use globals for somethings.
  44       * @param string $method if you set this to anything other than 'post' then _GET and _POST will
  45       *               be merged and used as incoming data to the form.
  46       * @param string $target target frame for form submission. You will rarely use this. Don't use
  47       *               it if you don't need to as the target attribute is deprecated in xhtml strict.
  48       * @param mixed $attributes you can pass a string of html attributes here or an array.
  49       *               Special attribute 'data-random-ids' will randomise generated elements ids. This
  50       *               is necessary when there are several forms on the same page.
  51       *               Special attribute 'data-double-submit-protection' set to 'off' will turn off
  52       *               double-submit protection JavaScript - this may be necessary if your form sends
  53       *               downloadable files in response to a submit button, and can't call
  54       *               \core_form\util::form_download_complete();
  55       * @param bool $editable
  56       * @param array $ajaxformdata Forms submitted via ajax, must pass their data here, instead of relying on _GET and _POST.
  57       */
  58      public function __construct($action = null, $customdata = null, $method = 'post', $target = '',
  59              $attributes = null, $editable = true, $ajaxformdata = null) {
  60          $this->globalmanager = new \tool_mfa\local\form\global_form_manager();
  61          parent::__construct($action, $customdata, $method, $target, $attributes, $editable, $ajaxformdata);
  62      }
  63  
  64      /**
  65       * {@inheritDoc}
  66       * @see moodleform::definition()
  67       */
  68      public function definition(): void {
  69          $mform = $this->_form;
  70          $factor = $this->_customdata['factor'];
  71          $mform = $factor->login_form_definition($mform);
  72          // Add a hidden field with the factor name so it is always available.
  73          $factorname = $mform->addElement('hidden', 'factor', $factor->name);
  74          $factorname->setType(PARAM_ALPHAEXT);
  75          $this->globalmanager->definition($mform);
  76      }
  77  
  78      /**
  79       * Invokes factor login_form_definition_after_data() method after form data has been set.
  80       *
  81       * @return void
  82       */
  83      public function definition_after_data(): void {
  84          $mform = $this->_form;
  85          $factor = $this->_customdata['factor'];
  86  
  87          $factor->login_form_definition_after_data($mform);
  88          $this->globalmanager->definition_after_data($mform);
  89  
  90          $buttonarray = [];
  91          $buttonarray[] = &$mform->createElement('submit', 'submitbutton', get_string('loginsubmit', 'factor_' . $factor->name));
  92          $mform->addGroup($buttonarray, 'buttonar', '', [' '], false);
  93          $mform->closeHeaderBefore('buttonar');
  94      }
  95  
  96      /**
  97       * Validates the login form with given factor validation method.
  98       *
  99       * @param array $data
 100       * @param array $files
 101       * @return array
 102       */
 103      public function validation($data, $files) {
 104          $errors = parent::validation($data, $files);
 105  
 106          $factor = $this->_customdata['factor'];
 107          $errors += $factor->login_form_validation($data);
 108          $errors += $this->globalmanager->validation($data, $files);
 109  
 110          // Execute sleep time bruteforce mitigation.
 111          \tool_mfa\manager::sleep_timer();
 112  
 113          return $errors;
 114      }
 115  
 116      /**
 117       * Returns error corresponding to validated element.
 118       *
 119       * @param string $elementname Name of form element to check.
 120       * @return string|null Error message corresponding to the validated element.
 121       */
 122      public function get_element_error(string $elementname): ?string {
 123          return $this->_form->getElementError($elementname);
 124      }
 125  
 126      /**
 127       * Set an error message for a form element.
 128       *
 129       * @param string $elementname Name of form element to set error for.
 130       * @param string $error Error message, if empty then removes the current error message.
 131       * @return void
 132       */
 133      public function set_element_error(string $elementname, string $error): void {
 134          $this->_form->setElementError($elementname, $error);
 135      }
 136  
 137      /**
 138       * Freeze a form element.
 139       *
 140       * @param string $elementname Name of form element to freeze.
 141       * @return void
 142       */
 143      public function freeze(string $elementname): void {
 144          $this->_form->freeze($elementname);
 145      }
 146  
 147      /**
 148       * Returns true if the form element exists.
 149       *
 150       * @param string $elementname Name of form element to check.
 151       * @return bool
 152       */
 153      public function element_exists(string $elementname): bool {
 154          return $this->_form->elementExists($elementname);
 155      }
 156  }