-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathlib.php
84 lines (71 loc) · 2.83 KB
/
lib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Enrolkey core hooks
*
* @package auth_enrolkey
* @copyright 2023 Matthew Hilton <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
use auth_enrolkey\utility;
require_once($CFG->dirroot . '/login/lib.php');
/**
* Post forgot password request hook
*
* This is to send password reset emails to suspended users based on if a config is enabled.
*
* @param object $data password forget request data
*/
function auth_enrolkey_post_forgot_password_requests($data) {
// This config allows suspended users to unsuspend their accounts if they provide a valid enrolkey.
// However, if they forget their password, they cannot login to do this.
// So we hook in here and if the config is enabled and they ARE suspended, we send them a password email anyway.
if (empty(get_config('auth_enrolkey', 'unsuspendaccounts'))) {
return;
}
$user = utility::search_for_suspended_user((array) $data);
if (empty($user) || $user->auth != "enrolkey") {
return;
}
// Make the user appear unsuspended so the email is successfully sent.
$user->suspended = 0;
$resetrecord = core_login_generate_password_reset($user);
send_password_change_confirmation_email($user, $resetrecord);
}
/**
* Post password set hook
*
* This is used to logout users who reset their password while being suspended.
* Otherwise they are logged in, but still suspended.
* We log them out so they can use the unsuspend.php page in conjunction with their enrolkey.
* @param object $data
*/
function auth_enrolkey_post_set_password_requests($data) {
global $USER;
if ($USER->auth != 'enrolkey' || empty($USER->suspended)) {
return;
}
// Log them out immediately.
// Required since resetting password logs you in,
// but the user is still suspended, so they get stuck in a halfway state.
require_logout();
// Redirect them to the unsuspend page afterwards.
// So they can enter their new details and enrolkey and unsuspend themselves.
if (!PHPUNIT_TEST) {
redirect(new moodle_url('/auth/enrolkey/unsuspend.php'));
}
}