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_nosetup;
  18  
  19  use stdClass;
  20  use tool_mfa\local\factor\object_factor_base;
  21  
  22  /**
  23   * No setup factor class.
  24   *
  25   * @package     factor_nosetup
  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       * No Setup 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       * No Setup 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       * No Setup 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          // Check if user has any other input or setup factors active.
  78          $factors = \tool_mfa\plugininfo\factor::get_active_user_factor_types();
  79          foreach ($factors as $factor) {
  80              if ($factor->has_input() || $factor->has_setup()) {
  81                  return \tool_mfa\plugininfo\factor::STATE_NEUTRAL;
  82              }
  83          }
  84  
  85          return \tool_mfa\plugininfo\factor::STATE_PASS;
  86      }
  87  
  88      /**
  89       * No setup implementation.
  90       * Copy of get_state, but can take other user..
  91       *
  92       * @param stdClass $user
  93       * @return void
  94       */
  95      public function possible_states(stdClass $user): array {
  96          // We return Neutral here because to support optional rollouts
  97          // it needs to report neutral or the menu to setup will not display.
  98          return [\tool_mfa\plugininfo\factor::STATE_NEUTRAL];
  99      }
 100  
 101      /**
 102       * No Setup Factor implementation.
 103       * The state can never be set. Always return true.
 104       *
 105       * @param string $state the state constant to set
 106       * @return bool
 107       */
 108      public function set_state(string $state): bool {
 109          return true;
 110      }
 111  
 112      /**
 113       * No Setup Factor implementation.
 114       * nosetup should not be a valid combination with another factor.
 115       *
 116       * @param array $combination array of factors that make up the combination
 117       * @return bool
 118       */
 119      public function check_combination(array $combination): bool {
 120          // If this combination has more than 1 factor that has setup or input, not valid.
 121          foreach ($combination as $factor) {
 122              if ($factor->has_setup() || $factor->has_input()) {
 123                  return false;
 124              }
 125          }
 126          return true;
 127      }
 128  }