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_iprange;
  18  
  19  use stdClass;
  20  use tool_mfa\local\factor\object_factor_base;
  21  
  22  /**
  23   * IP Range factor class.
  24   *
  25   * @package     factor_iprange
  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       * IP Range Factor implementation.
  34       * This factor is a singleton, return single 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       * IP Range Factor implementation.
  62       * Factor has no input
  63       *
  64       * {@inheritDoc}
  65       */
  66      public function has_input(): bool {
  67          return false;
  68      }
  69  
  70      /**
  71       * IP Range Factor implementation.
  72       * Checks a users current IP against allowed and disallowed ranges.
  73       *
  74       * {@inheritDoc}
  75       */
  76      public function get_state(): string {
  77          $safeips = get_config('factor_iprange', 'safeips');
  78  
  79          // TODO: Check for failures here.
  80  
  81          if (!empty($safeips)) {
  82              if (remoteip_in_list($safeips)) {
  83                  return \tool_mfa\plugininfo\factor::STATE_PASS;
  84              }
  85          }
  86  
  87          return \tool_mfa\plugininfo\factor::STATE_NEUTRAL;
  88      }
  89  
  90      /**
  91       * IP Range Factor implementation.
  92       * Cannot set state, return true.
  93       *
  94       * @param string $state the state constant to set
  95       * @return bool
  96       */
  97      public function set_state(string $state): bool {
  98          return true;
  99      }
 100  
 101      /**
 102       * IP Range Factor implementation.
 103       * User can influence state prior to login.
 104       * Possible states are either neutral or pass.
 105       *
 106       * @param stdClass $user
 107       */
 108      public function possible_states(stdClass $user): array {
 109          return [
 110              \tool_mfa\plugininfo\factor::STATE_PASS,
 111              \tool_mfa\plugininfo\factor::STATE_NEUTRAL,
 112          ];
 113      }
 114  }