Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
 
1441 ariadna 45
// If pass is set, do checks and pass for this session.
46
// Require login to force $SESSION and user, and pass for that session.
1 efrain 47
if (!empty($instance) && $pass != 0 && $secret != 0) {
48
    require_login();
49
    if ($factor->get_state() === \tool_mfa\plugininfo\factor::STATE_LOCKED) {
50
        // Redirect through to auth, this will bounce them to the next factor.
51
        redirect(new moodle_url('/admin/tool/mfa/auth.php'));
52
    }
53
    // Check the code with the same measures on the page entry.
54
    if ($instance->secret != $secret) {
55
        \tool_mfa\manager::sleep_timer();
56
        $factor->increment_lock_counter();
57
        throw new moodle_exception('error:parameters', 'factor_email');
58
    }
59
    $factor = \tool_mfa\plugininfo\factor::get_factor('email');
60
    $factor->set_state(\tool_mfa\plugininfo\factor::STATE_PASS);
61
    // If wantsurl is already set in session, go to it.
62
    if (!empty($SESSION->wantsurl)) {
63
        redirect($SESSION->wantsurl);
64
    } else {
65
        redirect(new moodle_url('/'));
66
    }
67
}
68
 
69
$form = new \factor_email\form\email($url);
70
 
71
if ($form->is_cancelled()) {
72
    redirect(new moodle_url('/'));
1441 ariadna 73
}
74
 
75
// If submitted without the pass param, is a cancel request - do checks and revoke email factor.
76
if ($fromform = $form->get_data()) {
77
    // Only allow revoke attempts from requests with a valid instance and secret.
78
    if (empty($instance) || empty($secret) || $instance->secret != $secret) {
1 efrain 79
        $message = get_string('error:badcode', 'factor_email');
80
    } else {
81
        $user = $DB->get_record('user', ['id' => $instance->userid]);
82
 
83
        // Stop attacker from using email factor at all, by revoking all email until admin fixes.
84
        $DB->set_field('tool_mfa', 'revoked', 1, ['userid' => $user->id, 'factor' => 'email']);
85
 
86
        // Remotely logout all sessions for user.
1441 ariadna 87
        \core\session\manager::destroy_user_sessions($instance->userid);
1 efrain 88
 
89
        // Log event.
90
        $ip = $instance->createdfromip;
91
        $useragent = $instance->label;
92
        $event = \factor_email\event\unauth_email::unauth_email_event($user, $ip, $useragent);
93
        $event->trigger();
94
 
95
        // Suspend user account.
96
        if (get_config('factor_email', 'suspend')) {
97
            $DB->set_field('user', 'suspended', 1, ['id' => $user->id]);
98
        }
99
 
100
        $message = get_string('email:revokesuccess', 'factor_email', fullname($user));
101
    }
102
}
103
 
104
echo $OUTPUT->header();
105
echo $OUTPUT->heading(get_string('unauthemail', 'factor_email'));
106
if (!empty($message)) {
107
    echo $message;
108
} else {
109
    $form->display();
110
}
111
echo $OUTPUT->footer();