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 revoke and disable an email code.
|
|
|
19 |
*
|
|
|
20 |
* @package factor_email
|
|
|
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 |
// Ignore coding standards for login check, this page does not require login.
|
|
|
27 |
// phpcs:disable moodle.Files.RequireLogin.Missing
|
|
|
28 |
require_once(__DIR__ . '/../../../../../config.php');
|
|
|
29 |
|
|
|
30 |
$instanceid = required_param('instance', PARAM_INT);
|
|
|
31 |
$pass = optional_param('pass', '0', PARAM_INT);
|
|
|
32 |
$secret = optional_param('secret', 0, PARAM_INT);
|
|
|
33 |
|
|
|
34 |
$context = context_system::instance();
|
|
|
35 |
$PAGE->set_context($context);
|
|
|
36 |
$url = new moodle_url('/admin/tool/mfa/factor/email/email.php',
|
|
|
37 |
['instance' => $instanceid, 'pass' => $pass, 'secret' => $secret]);
|
|
|
38 |
$PAGE->set_url($url);
|
|
|
39 |
$PAGE->set_pagelayout('secure');
|
|
|
40 |
$PAGE->set_title(get_string('unauthemail', 'factor_email'));
|
|
|
41 |
$PAGE->set_cacheable(false);
|
|
|
42 |
$instance = $DB->get_record('tool_mfa', ['id' => $instanceid]);
|
|
|
43 |
$factor = \tool_mfa\plugininfo\factor::get_factor('email');
|
|
|
44 |
|
|
|
45 |
// If pass is set, require login to force $SESSION and user, and pass for that session.
|
|
|
46 |
if (!empty($instance) && $pass != 0 && $secret != 0) {
|
|
|
47 |
require_login();
|
|
|
48 |
if ($factor->get_state() === \tool_mfa\plugininfo\factor::STATE_LOCKED) {
|
|
|
49 |
// Redirect through to auth, this will bounce them to the next factor.
|
|
|
50 |
redirect(new moodle_url('/admin/tool/mfa/auth.php'));
|
|
|
51 |
}
|
|
|
52 |
// Check the code with the same measures on the page entry.
|
|
|
53 |
if ($instance->secret != $secret) {
|
|
|
54 |
\tool_mfa\manager::sleep_timer();
|
|
|
55 |
$factor->increment_lock_counter();
|
|
|
56 |
throw new moodle_exception('error:parameters', 'factor_email');
|
|
|
57 |
}
|
|
|
58 |
$factor = \tool_mfa\plugininfo\factor::get_factor('email');
|
|
|
59 |
$factor->set_state(\tool_mfa\plugininfo\factor::STATE_PASS);
|
|
|
60 |
// If wantsurl is already set in session, go to it.
|
|
|
61 |
if (!empty($SESSION->wantsurl)) {
|
|
|
62 |
redirect($SESSION->wantsurl);
|
|
|
63 |
} else {
|
|
|
64 |
redirect(new moodle_url('/'));
|
|
|
65 |
}
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
$form = new \factor_email\form\email($url);
|
|
|
69 |
|
|
|
70 |
if ($form->is_cancelled()) {
|
|
|
71 |
redirect(new moodle_url('/'));
|
|
|
72 |
} else if ($fromform = $form->get_data()) {
|
|
|
73 |
if (empty($instance)) {
|
|
|
74 |
$message = get_string('error:badcode', 'factor_email');
|
|
|
75 |
} else {
|
|
|
76 |
$user = $DB->get_record('user', ['id' => $instance->userid]);
|
|
|
77 |
|
|
|
78 |
// Stop attacker from using email factor at all, by revoking all email until admin fixes.
|
|
|
79 |
$DB->set_field('tool_mfa', 'revoked', 1, ['userid' => $user->id, 'factor' => 'email']);
|
|
|
80 |
|
|
|
81 |
// Remotely logout all sessions for user.
|
|
|
82 |
$manager = \core\session\manager::kill_user_sessions($instance->userid);
|
|
|
83 |
|
|
|
84 |
// Log event.
|
|
|
85 |
$ip = $instance->createdfromip;
|
|
|
86 |
$useragent = $instance->label;
|
|
|
87 |
$event = \factor_email\event\unauth_email::unauth_email_event($user, $ip, $useragent);
|
|
|
88 |
$event->trigger();
|
|
|
89 |
|
|
|
90 |
// Suspend user account.
|
|
|
91 |
if (get_config('factor_email', 'suspend')) {
|
|
|
92 |
$DB->set_field('user', 'suspended', 1, ['id' => $user->id]);
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
$message = get_string('email:revokesuccess', 'factor_email', fullname($user));
|
|
|
96 |
}
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
echo $OUTPUT->header();
|
|
|
100 |
echo $OUTPUT->heading(get_string('unauthemail', 'factor_email'));
|
|
|
101 |
if (!empty($message)) {
|
|
|
102 |
echo $message;
|
|
|
103 |
} else {
|
|
|
104 |
$form->display();
|
|
|
105 |
}
|
|
|
106 |
echo $OUTPUT->footer();
|