| 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 |  * Reporting page for each factor vs auth type
 | 
        
           |  |  | 18 |  *
 | 
        
           |  |  | 19 |  * @package   tool_mfa
 | 
        
           |  |  | 20 |  * @author    Peter Burnett <peterburnett@catalyst-au.net>
 | 
        
           |  |  | 21 |  * @copyright 2019 Catalyst IT
 | 
        
           |  |  | 22 |  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 23 |  */
 | 
        
           |  |  | 24 |   | 
        
           |  |  | 25 | require_once(__DIR__ . '/../../../config.php');
 | 
        
           |  |  | 26 | require_once($CFG->libdir.'/adminlib.php');
 | 
        
           |  |  | 27 |   | 
        
           |  |  | 28 | admin_externalpage_setup('factorreport');
 | 
        
           |  |  | 29 |   | 
        
           |  |  | 30 | $reset = optional_param('reset', null, PARAM_TEXT);
 | 
        
           |  |  | 31 | $userid = optional_param('id', null, PARAM_INT);
 | 
        
           |  |  | 32 | $view = optional_param('view', null, PARAM_TEXT);
 | 
        
           |  |  | 33 |   | 
        
           |  |  | 34 | $PAGE->set_title(get_string('factorreport', 'tool_mfa'));
 | 
        
           |  |  | 35 | $PAGE->set_heading(get_string('factorreport', 'tool_mfa'));
 | 
        
           |  |  | 36 | $renderer = $PAGE->get_renderer('tool_mfa');
 | 
        
           |  |  | 37 |   | 
        
           |  |  | 38 | // Handle page actions.
 | 
        
           |  |  | 39 | if (!empty($reset) && confirm_sesskey()) {
 | 
        
           |  |  | 40 |     // Check factor is valid.
 | 
        
           |  |  | 41 |     $factor = \tool_mfa\plugininfo\factor::get_factor($reset);
 | 
        
           |  |  | 42 |     if (!$factor instanceof \tool_mfa\local\factor\object_factor_base) {
 | 
        
           |  |  | 43 |         throw new moodle_exception('error:factornotfound', 'tool_mfa');
 | 
        
           |  |  | 44 |     }
 | 
        
           |  |  | 45 |   | 
        
           |  |  | 46 |     // One user.
 | 
        
           |  |  | 47 |     if (!empty($userid)) {
 | 
        
           |  |  | 48 |         // Just reset the factor and reload.
 | 
        
           |  |  | 49 |         $DB->delete_records('tool_mfa', ['factor' => $factor->name, 'userid' => $userid]);
 | 
        
           |  |  | 50 |         $stringarr = ['factor' => $factor->name, 'username' => $userid];
 | 
        
           |  |  | 51 |         redirect(new moodle_url($PAGE->url, ['view' => $factor->name]), get_string('resetsuccess', 'tool_mfa', $stringarr));
 | 
        
           |  |  | 52 |     }
 | 
        
           |  |  | 53 |   | 
        
           |  |  | 54 |     // Bulk action for locked users.
 | 
        
           |  |  | 55 |     $locklevel = (int) get_config('tool_mfa', 'lockout');
 | 
        
           |  |  | 56 |     $sql = "SELECT DISTINCT(userid)
 | 
        
           |  |  | 57 |               FROM {tool_mfa}
 | 
        
           |  |  | 58 |              WHERE factor = ?
 | 
        
           |  |  | 59 |                AND lockcounter >= ?
 | 
        
           |  |  | 60 |                AND revoked = 0";
 | 
        
           |  |  | 61 |     $lockedusers = $DB->get_records_sql($sql, [$factor->name, $locklevel]);
 | 
        
           |  |  | 62 |     $lockedusers = array_map(function ($el) {
 | 
        
           |  |  | 63 |         return $el->userid;
 | 
        
           |  |  | 64 |     }, (array) $lockedusers);
 | 
        
           |  |  | 65 |     $SESSION->bulk_users = $lockedusers;
 | 
        
           |  |  | 66 |     redirect(new moodle_url('/admin/user/user_bulk.php'));
 | 
        
           |  |  | 67 | }
 | 
        
           |  |  | 68 |   | 
        
           |  |  | 69 | // Configure the lookback period for the report.
 | 
        
           |  |  | 70 | $days = optional_param('days', 0, PARAM_INT);
 | 
        
           |  |  | 71 | if ($days === 0) {
 | 
        
           |  |  | 72 |     $lookback = 0;
 | 
        
           |  |  | 73 | } else {
 | 
        
           |  |  | 74 |     $lookback = time() - (DAYSECS * $days);
 | 
        
           |  |  | 75 | }
 | 
        
           |  |  | 76 |   | 
        
           |  |  | 77 | // Construct a select to use for viewing time periods.
 | 
        
           |  |  | 78 | $selectarr = [
 | 
        
           |  |  | 79 |   | 
        
           |  |  | 80 |     1 => get_string('numday', '', 1),
 | 
        
           |  |  | 81 |     7 => get_string('numweek', '', 1),
 | 
        
           |  |  | 82 |     31 => get_string('nummonth', '', 1),
 | 
        
           |  |  | 83 |     90 => get_string('nummonths', '', 3),
 | 
        
           |  |  | 84 |     180 => get_string('nummonths', '', 6),
 | 
        
           |  |  | 85 |     365 => get_string('numyear', '', 1),
 | 
        
           |  |  | 86 | ];
 | 
        
           |  |  | 87 | $select = new single_select($PAGE->url, 'days', $selectarr);
 | 
        
           |  |  | 88 |   | 
        
           |  |  | 89 | echo $renderer->header();
 | 
        
           |  |  | 90 |   | 
        
           |  |  | 91 | if (!empty($view)) {
 | 
        
           |  |  | 92 |     // View locked users for a particular factor.
 | 
        
           |  |  | 93 |     $factor = \tool_mfa\plugininfo\factor::get_factor($view);
 | 
        
           |  |  | 94 |     if (!$factor instanceof \tool_mfa\local\factor\object_factor_base) {
 | 
        
           |  |  | 95 |         throw new moodle_exception('error:factornotfound', 'tool_mfa');
 | 
        
           |  |  | 96 |     }
 | 
        
           |  |  | 97 |   | 
        
           |  |  | 98 |     $backbutton = new single_button(new moodle_url($PAGE->url), get_string('back'));
 | 
        
           |  |  | 99 |     echo $renderer->heading(get_string('lockedusersforfactor', 'tool_mfa', $factor->get_display_name()));
 | 
        
           |  |  | 100 |     echo \html_writer::tag('p', $renderer->factor_locked_users_table($factor));
 | 
        
           |  |  | 101 |     echo $renderer->render($backbutton);
 | 
        
           |  |  | 102 | } else {
 | 
        
           |  |  | 103 |     echo $renderer->heading(get_string('factorreport', 'tool_mfa'));
 | 
        
           |  |  | 104 |   | 
        
           |  |  | 105 |     // Regular page content.
 | 
        
           |  |  | 106 |     echo html_writer::tag('p', get_string('selectperiod', 'tool_mfa'));
 | 
        
           |  |  | 107 |     echo $renderer->render($select);
 | 
        
           |  |  | 108 |   | 
        
           |  |  | 109 |     // Render the factors in use table.
 | 
        
           |  |  | 110 |     echo html_writer::tag('p', $renderer->factors_in_use_table($lookback));
 | 
        
           |  |  | 111 |   | 
        
           |  |  | 112 |     echo $renderer->heading(get_string('lockedusersforallfactors', 'tool_mfa'));
 | 
        
           |  |  | 113 |   | 
        
           |  |  | 114 |     // Now output a locked factors table.
 | 
        
           |  |  | 115 |     echo html_writer::tag('p', $renderer->factors_locked_table());
 | 
        
           |  |  | 116 | }
 | 
        
           |  |  | 117 |   | 
        
           |  |  | 118 | echo $OUTPUT->footer();
 |