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 factor_admin;
  18  
  19  use stdClass;
  20  use tool_mfa\local\factor\object_factor_base;
  21  
  22  /**
  23   * Admin factor class.
  24   *
  25   * @package     factor_admin
  26   * @author      Peter Burnett <peterburnett@catalyst-au.net>
  27   * @copyright   Catalyst IT
  28   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  29   */
  30  class factor extends object_factor_base {
  31  
  32      /**
  33       * Admin Factor implementation.
  34       * Factor is a singleton, can only be one instance.
  35       *
  36       * @param stdClass $user the user to check against.
  37       * @return array
  38       */
  39      public function get_all_user_factors(stdClass $user): array {
  40          global $DB;
  41          $records = $DB->get_records('tool_mfa', ['userid' => $user->id, 'factor' => $this->name]);
  42  
  43          if (!empty($records)) {
  44              return $records;
  45          }
  46  
  47          // Null records returned, build new record.
  48          $record = [
  49              'userid' => $user->id,
  50              'factor' => $this->name,
  51              'timecreated' => time(),
  52              'createdfromip' => $user->lastip,
  53              'timemodified' => time(),
  54              'revoked' => 0,
  55          ];
  56          $record['id'] = $DB->insert_record('tool_mfa', $record, true);
  57          return [(object) $record];
  58      }
  59  
  60      /**
  61       * Admin Factor implementation.
  62       * Factor does not have input.
  63       *
  64       * {@inheritDoc}
  65       */
  66      public function has_input(): bool {
  67          return false;
  68      }
  69  
  70      /**
  71       * Admin Factor implementation.
  72       * State check is performed here, as there is no form to do it in.
  73       *
  74       * {@inheritDoc}
  75       */
  76      public function get_state(): string {
  77          if (is_siteadmin()) {
  78              return \tool_mfa\plugininfo\factor::STATE_NEUTRAL;
  79          }
  80          return \tool_mfa\plugininfo\factor::STATE_PASS;
  81      }
  82  
  83      /**
  84       * Admin Factor implementation.
  85       * The state can never be set. Always return true.
  86       *
  87       * @param string $state the state constant to set
  88       * @return bool
  89       */
  90      public function set_state($state): bool {
  91          return true;
  92      }
  93  }