-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathunsuspend.php
97 lines (79 loc) · 3.38 KB
/
unsuspend.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?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/>.
/**
* Unsuspend user page with a valid enrolkey.
*
* @package auth_enrolkey
* @author Nicholas Hoobin <[email protected]>
* @copyright 2021 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use auth_enrolkey\form\unsuspend_form;
use auth_enrolkey\utility;
require_once(__DIR__.'/../../config.php');
$context = context_system::instance();
$baseurl = new moodle_url('/auth/enrolkey/unsuspend.php');
if (!get_config('auth_enrolkey', 'unsuspendaccounts') || isloggedin()) {
redirect(new moodle_url('/'));
}
$PAGE->set_url($baseurl);
$PAGE->set_pagelayout('login');
$PAGE->set_title(get_string('title_unsuspend', 'auth_enrolkey'));
$PAGE->set_heading(get_string('heading_unsuspend', 'auth_enrolkey'));
$PAGE->set_context($context);
$output = $PAGE->get_renderer('auth_enrolkey');
$form = new unsuspend_form($baseurl);
if ($form->is_cancelled()) {
require_logout();
redirect(new moodle_url('/'));
} else if ($form->is_submitted() && $form->is_validated()) {
$valid = false;
$data = $form->get_data();
// At this stage we can say, due to mform validation the user exists, is suspended and the password checks out.
// Lets just double check these things anyway.
$user = utility::search_for_suspended_user((array) $data);
if ($user) {
$valid = validate_internal_user_password($user, $data->password);
}
if ($valid) {
// Setting the userid can imitate logging in. Be careful with this.
// This is used with the enrol_self() call.
$USER->id = $user->id;
try {
list($availableenrolids, $errors) = utility::unsuspend_and_enrol_user($data->signup_token, false);
// Only enrol a user to enrolkeys and courses which they are not already enrolled in.
if (!empty($availableenrolids)) {
complete_user_login($user);
utility::unsuspend_user($user);
// They are now unsuspended. We can actually called the real auth login function.
if (authenticate_user_login($user->username, $data->password)) {
utility::update_user($user, $availableenrolids);
\auth_enrolkey\persistent\enrolkey_redirect_mapping::redirect_during_signup($availableenrolids);
// Default redirect.
redirect(new moodle_url("/auth/enrolkey/view.php", ['ids' => implode(',', $availableenrolids)]));
}
}
} catch (Exception $e) {
require_logout();
}
}
// Well, we're not really logged in at all.
$USER->id = 0;
}
echo $output->header();
echo $output->heading($PAGE->heading);
$form->display();
echo $output->footer();