| 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 | 
           /**
  | 
        
        
            | 
            | 
           18 | 
            * Page to reset factor for users.
  | 
        
        
            | 
            | 
           19 | 
            *
  | 
        
        
            | 
            | 
           20 | 
            * @package     tool_mfa
  | 
        
        
            | 
            | 
           21 | 
            * @author      Peter Burnett <peterburnett@catalyst-au.net>
  | 
        
        
            | 
            | 
           22 | 
            * @copyright   Catalyst IT
  | 
        
        
            | 
            | 
           23 | 
            * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           24 | 
            */
  | 
        
        
            | 
            | 
           25 | 
              | 
        
        
            | 
            | 
           26 | 
           require_once(__DIR__ . '/../../../config.php');
  | 
        
        
            | 
            | 
           27 | 
           require_once($CFG->libdir . '/adminlib.php');
  | 
        
        
            | 
            | 
           28 | 
           admin_externalpage_setup('tool_mfa_resetfactor');
  | 
        
        
            | 
            | 
           29 | 
              | 
        
        
            | 
            | 
           30 | 
           $bulk = !empty($SESSION->bulk_users);
  | 
        
        
            | 
            | 
           31 | 
           $returnurl = optional_param('returnurl', '', PARAM_LOCALURL);
  | 
        
        
            | 
            | 
           32 | 
              | 
        
        
            | 
            | 
           33 | 
           $factors = \tool_mfa\plugininfo\factor::get_factors();
  | 
        
        
            | 
            | 
           34 | 
           $form = new \tool_mfa\local\form\reset_factor(null, ['factors' => $factors, 'bulk' => $bulk]);
  | 
        
        
            | 
            | 
           35 | 
           if ($bulk) {
  | 
        
        
            | 
            | 
           36 | 
               $form->set_data(['returnurl' => $returnurl]);
  | 
        
        
            | 
            | 
           37 | 
               $return = new moodle_url($returnurl ?: '/admin/user/user_bulk.php');
  | 
        
        
            | 
            | 
           38 | 
           } else {
  | 
        
        
            | 
            | 
           39 | 
               $return = new moodle_url('/admin/category.php', ['category' => 'toolmfafolder']);
  | 
        
        
            | 
            | 
           40 | 
           }
  | 
        
        
            | 
            | 
           41 | 
              | 
        
        
            | 
            | 
           42 | 
           if ($form->is_cancelled()) {
  | 
        
        
            | 
            | 
           43 | 
               redirect($return);
  | 
        
        
            | 
            | 
           44 | 
           } else if ($fromform = $form->get_data()) {
  | 
        
        
            | 
            | 
           45 | 
               // Get factor from select index.
  | 
        
        
            | 
            | 
           46 | 
               if ($fromform->factor !== 'all') {
  | 
        
        
            | 
            | 
           47 | 
                   $factor = $factors[$fromform->factor];
  | 
        
        
            | 
            | 
           48 | 
               } else {
  | 
        
        
            | 
            | 
           49 | 
                   $factor = 'all';
  | 
        
        
            | 
            | 
           50 | 
               }
  | 
        
        
            | 
            | 
           51 | 
              | 
        
        
            | 
            | 
           52 | 
               // Setup var to put into notification strings.
  | 
        
        
            | 
            | 
           53 | 
               $stringvar = $factor === 'all' ? get_string('all') : $factor->get_display_name();
  | 
        
        
            | 
            | 
           54 | 
              | 
        
        
            | 
            | 
           55 | 
               // Setup user array for bulk action.
  | 
        
        
            | 
            | 
           56 | 
               $users = $bulk ? $SESSION->bulk_users : [$fromform->user];
  | 
        
        
            | 
            | 
           57 | 
              | 
        
        
            | 
            | 
           58 | 
               foreach ($users as $user) {
  | 
        
        
            | 
            | 
           59 | 
                   if (!$user instanceof stdClass) {
  | 
        
        
            | 
            | 
           60 | 
                       $user = \core_user::get_user($user);
  | 
        
        
            | 
            | 
           61 | 
                   }
  | 
        
        
            | 
            | 
           62 | 
              | 
        
        
            | 
            | 
           63 | 
                   // Add a user preference, to display a notification to the user that their factor was reset.
  | 
        
        
            | 
            | 
           64 | 
                   // This should only be done if the factor is active for the user, and has input.
  | 
        
        
            | 
            | 
           65 | 
                   $factors = $factor === 'all' ? \tool_mfa\plugininfo\factor::get_factors() : [$factor];
  | 
        
        
            | 
            | 
           66 | 
                   foreach ($factors as $factor) {
  | 
        
        
            | 
            | 
           67 | 
                       $factor->delete_factor_for_user($user);
  | 
        
        
            | 
            | 
           68 | 
                       if (count($factor->get_active_user_factors($user)) > 0 && $factor->has_setup()) {
  | 
        
        
            | 
            | 
           69 | 
                           $prefname = 'tool_mfa_reset_' . $factor->name;
  | 
        
        
            | 
            | 
           70 | 
                           set_user_preference($prefname, true, $user);
  | 
        
        
            | 
            | 
           71 | 
                       }
  | 
        
        
            | 
            | 
           72 | 
                   }
  | 
        
        
            | 
            | 
           73 | 
              | 
        
        
            | 
            | 
           74 | 
                   // If we are just doing 1 user.
  | 
        
        
            | 
            | 
           75 | 
                   if (!$bulk) {
  | 
        
        
            | 
            | 
           76 | 
                       $stringarr = ['factor' => $stringvar, 'username' => $user->username];
  | 
        
        
            | 
            | 
           77 | 
                       \core\notification::success(get_string('resetsuccess', 'tool_mfa', $stringarr));
  | 
        
        
            | 
            | 
           78 | 
              | 
        
        
            | 
            | 
           79 | 
                       // Reload page.
  | 
        
        
            | 
            | 
           80 | 
                       redirect($PAGE->url);
  | 
        
        
            | 
            | 
           81 | 
                   }
  | 
        
        
            | 
            | 
           82 | 
               }
  | 
        
        
            | 
            | 
           83 | 
              | 
        
        
            | 
            | 
           84 | 
               \core\notification::success(get_string('resetsuccessbulk', 'tool_mfa', $stringvar));
  | 
        
        
            | 
            | 
           85 | 
               unset($SESSION->bulk_users);
  | 
        
        
            | 
            | 
           86 | 
               // Redirect to bulk actions page.
  | 
        
        
            | 
            | 
           87 | 
               redirect($return);
  | 
        
        
            | 
            | 
           88 | 
           }
  | 
        
        
            | 
            | 
           89 | 
              | 
        
        
            | 
            | 
           90 | 
           echo $OUTPUT->header();
  | 
        
        
            | 
            | 
           91 | 
           echo $OUTPUT->heading(get_string('resetfactor', 'tool_mfa'));
  | 
        
        
            | 
            | 
           92 | 
           $form->display();
  | 
        
        
            | 
            | 
           93 | 
           echo $OUTPUT->footer();
  |