| 1 | 
           efrain | 
           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_capability;
  | 
        
        
            | 
            | 
           18 | 
              | 
        
        
            | 
            | 
           19 | 
           use stdClass;
  | 
        
        
            | 
            | 
           20 | 
           use tool_mfa\local\factor\object_factor_base;
  | 
        
        
            | 
            | 
           21 | 
              | 
        
        
            | 
            | 
           22 | 
           /**
  | 
        
        
            | 
            | 
           23 | 
            * User capability factor class.
  | 
        
        
            | 
            | 
           24 | 
            *
  | 
        
        
            | 
            | 
           25 | 
            * @package     factor_capability
  | 
        
        
            | 
            | 
           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 | 
                * User capability 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 | 
                * User capability implementation.
  | 
        
        
            | 
            | 
           62 | 
                * Factor has no input
  | 
        
        
            | 
            | 
           63 | 
                *
  | 
        
        
            | 
            | 
           64 | 
                * {@inheritDoc}
  | 
        
        
            | 
            | 
           65 | 
                */
  | 
        
        
            | 
            | 
           66 | 
               public function has_input(): bool {
  | 
        
        
            | 
            | 
           67 | 
                   return false;
  | 
        
        
            | 
            | 
           68 | 
               }
  | 
        
        
            | 
            | 
           69 | 
              | 
        
        
            | 
            | 
           70 | 
               /**
  | 
        
        
            | 
            | 
           71 | 
                * User capability implementation.
  | 
        
        
            | 
            | 
           72 | 
                * Checks whether user has the negative capability.
  | 
        
        
            | 
            | 
           73 | 
                *
  | 
        
        
            | 
            | 
           74 | 
                * {@inheritDoc}
  | 
        
        
            | 
            | 
           75 | 
                */
  | 
        
        
            | 
            | 
           76 | 
               public function get_state(): string {
  | 
        
        
            | 
            | 
           77 | 
                   global $USER;
  | 
        
        
            | 
            | 
           78 | 
                   $adminpass = (bool) get_config('factor_capability', 'adminpasses');
  | 
        
        
            | 
            | 
           79 | 
              | 
        
        
            | 
            | 
           80 | 
                   // Do anything check is controlled from factor config.
  | 
        
        
            | 
            | 
           81 | 
                   if (!has_capability('factor/capability:cannotpassfactor', \context_system::instance(), $USER, $adminpass)) {
  | 
        
        
            | 
            | 
           82 | 
                       return \tool_mfa\plugininfo\factor::STATE_PASS;
  | 
        
        
            | 
            | 
           83 | 
                   } else {
  | 
        
        
            | 
            | 
           84 | 
                       return \tool_mfa\plugininfo\factor::STATE_NEUTRAL;
  | 
        
        
            | 
            | 
           85 | 
                   }
  | 
        
        
            | 
            | 
           86 | 
               }
  | 
        
        
            | 
            | 
           87 | 
              | 
        
        
            | 
            | 
           88 | 
               /**
  | 
        
        
            | 
            | 
           89 | 
                * User Capability implementation.
  | 
        
        
            | 
            | 
           90 | 
                * Cannot set state, return true.
  | 
        
        
            | 
            | 
           91 | 
                *
  | 
        
        
            | 
            | 
           92 | 
                * @param string $state the state constant to set
  | 
        
        
            | 
            | 
           93 | 
                * @return bool
  | 
        
        
            | 
            | 
           94 | 
                */
  | 
        
        
            | 
            | 
           95 | 
               public function set_state(string $state): bool {
  | 
        
        
            | 
            | 
           96 | 
                   return true;
  | 
        
        
            | 
            | 
           97 | 
               }
  | 
        
        
            | 
            | 
           98 | 
              | 
        
        
            | 
            | 
           99 | 
               /**
  | 
        
        
            | 
            | 
           100 | 
                * User capability implementation.
  | 
        
        
            | 
            | 
           101 | 
                * Possible states are either neutral or pass.
  | 
        
        
            | 
            | 
           102 | 
                *
  | 
        
        
            | 
            | 
           103 | 
                * @param stdClass $user
  | 
        
        
            | 
            | 
           104 | 
                * @return array
  | 
        
        
            | 
            | 
           105 | 
                */
  | 
        
        
            | 
            | 
           106 | 
               public function possible_states(stdClass $user): array {
  | 
        
        
            | 
            | 
           107 | 
                   return [
  | 
        
        
            | 
            | 
           108 | 
                       \tool_mfa\plugininfo\factor::STATE_PASS,
  | 
        
        
            | 
            | 
           109 | 
                       \tool_mfa\plugininfo\factor::STATE_NEUTRAL,
  | 
        
        
            | 
            | 
           110 | 
                   ];
  | 
        
        
            | 
            | 
           111 | 
               }
  | 
        
        
            | 
            | 
           112 | 
           }
  |