This repository has been archived by the owner on Jan 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
change_password.php
164 lines (126 loc) · 5.25 KB
/
change_password.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?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/>.
/**
* Change password page.
*
* @package core
* @subpackage auth
* @copyright 1999 onwards Martin Dougiamas http://dougiamas.com
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require('../config.php');
require_once($CFG->dirroot.'/user/lib.php');
require_once('change_password_form.php');
require_once($CFG->libdir.'/authlib.php');
require_once($CFG->dirroot.'/webservice/lib.php');
require_once('lib.php');
$id = optional_param('id', SITEID, PARAM_INT); // current course
$return = optional_param('return', 0, PARAM_BOOL); // redirect after password change
$systemcontext = context_system::instance();
$PAGE->set_url('/login/change_password.php', array('id'=>$id));
$PAGE->set_context($systemcontext);
if ($return) {
// this redirect prevents security warning because https can not POST to http pages
if (empty($SESSION->wantsurl)
or stripos(str_replace('https://', 'http://', $SESSION->wantsurl), str_replace('https://', 'http://', $CFG->wwwroot.'/login/change_password.php')) === 0) {
$returnto = "$CFG->wwwroot/user/preferences.php?userid=$USER->id&course=$id";
} else {
$returnto = $SESSION->wantsurl;
}
unset($SESSION->wantsurl);
redirect($returnto);
}
$strparticipants = get_string('participants');
if (!$course = $DB->get_record('course', array('id'=>$id))) {
print_error('invalidcourseid');
}
// require proper login; guest user can not change password
if (!isloggedin() or isguestuser()) {
if (empty($SESSION->wantsurl)) {
$SESSION->wantsurl = $CFG->wwwroot.'/login/change_password.php';
}
redirect(get_login_url());
}
$PAGE->set_context(context_user::instance($USER->id));
$PAGE->set_pagelayout('admin');
$PAGE->set_course($course);
// do not require change own password cap if change forced
if (!get_user_preferences('auth_forcepasswordchange', false)) {
require_capability('moodle/user:changeownpassword', $systemcontext);
}
// do not allow "Logged in as" users to change any passwords
if (\core\session\manager::is_loggedinas()) {
print_error('cannotcallscript');
}
if (is_mnet_remote_user($USER)) {
$message = get_string('usercannotchangepassword', 'mnet');
if ($idprovider = $DB->get_record('mnet_host', array('id'=>$USER->mnethostid))) {
$message .= get_string('userchangepasswordlink', 'mnet', $idprovider);
}
print_error('userchangepasswordlink', 'mnet', '', $message);
}
// load the appropriate auth plugin
$userauth = get_auth_plugin($USER->auth);
if (!$userauth->can_change_password()) {
print_error('nopasswordchange', 'auth');
}
if ($changeurl = $userauth->change_password_url()) {
// this internal scrip not used
redirect($changeurl);
}
$mform = new login_change_password_form();
$mform->set_data(array('id'=>$course->id));
$navlinks = array();
$navlinks[] = array('name' => $strparticipants, 'link' => "$CFG->wwwroot/user/index.php?id=$course->id", 'type' => 'misc');
if ($mform->is_cancelled()) {
redirect($CFG->wwwroot.'/user/preferences.php?userid='.$USER->id.'&course='.$course->id);
} else if ($data = $mform->get_data()) {
if (!$userauth->user_update_password($USER, $data->newpassword1)) {
print_error('errorpasswordupdate', 'auth');
}
user_add_password_history($USER->id, $data->newpassword1);
if (!empty($CFG->passwordchangelogout)) {
\core\session\manager::kill_user_sessions($USER->id, session_id());
}
if (!empty($data->signoutofotherservices)) {
webservice::delete_user_ws_tokens($USER->id);
}
// Reset login lockout - we want to prevent any accidental confusion here.
login_unlock_account($USER);
// register success changing password
unset_user_preference('auth_forcepasswordchange', $USER);
unset_user_preference('create_password', $USER);
$strpasswordchanged = get_string('passwordchanged');
// Plugins can perform post password change actions once data has been validated.
core_login_post_change_password_requests($data);
$fullname = fullname($USER, true);
$PAGE->set_title($strpasswordchanged);
$PAGE->set_heading(fullname($USER));
echo $OUTPUT->header();
notice($strpasswordchanged, new moodle_url($PAGE->url, array('return'=>1)));
echo $OUTPUT->footer();
exit;
}
$strchangepassword = get_string('changepassword');
$fullname = fullname($USER, true);
$PAGE->set_title($strchangepassword);
$PAGE->set_heading($fullname);
echo $OUTPUT->header();
if (get_user_preferences('auth_forcepasswordchange')) {
echo $OUTPUT->notification(get_string('forcepasswordchangenotice'));
}
$mform->display();
echo $OUTPUT->footer();