MDL-51584 gradebook: use direct $CFG access for freeze.
[moodle.git] / login / change_password.php
blob5efce2c81163ec5c95b84c9a8809fc43f76394cd
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * Change password page.
21 * @package core
22 * @subpackage auth
23 * @copyright 1999 onwards Martin Dougiamas http://dougiamas.com
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 require('../config.php');
28 require_once($CFG->dirroot.'/user/lib.php');
29 require_once('change_password_form.php');
30 require_once($CFG->libdir.'/authlib.php');
32 $id = optional_param('id', SITEID, PARAM_INT); // current course
33 $return = optional_param('return', 0, PARAM_BOOL); // redirect after password change
35 $systemcontext = context_system::instance();
37 //HTTPS is required in this page when $CFG->loginhttps enabled
38 $PAGE->https_required();
40 $PAGE->set_url('/login/change_password.php', array('id'=>$id));
42 $PAGE->set_context($systemcontext);
44 if ($return) {
45 // this redirect prevents security warning because https can not POST to http pages
46 if (empty($SESSION->wantsurl)
47 or stripos(str_replace('https://', 'http://', $SESSION->wantsurl), str_replace('https://', 'http://', $CFG->wwwroot.'/login/change_password.php')) === 0) {
48 $returnto = "$CFG->wwwroot/user/preferences.php?userid=$USER->id&course=$id";
49 } else {
50 $returnto = $SESSION->wantsurl;
52 unset($SESSION->wantsurl);
54 redirect($returnto);
57 $strparticipants = get_string('participants');
59 if (!$course = $DB->get_record('course', array('id'=>$id))) {
60 print_error('invalidcourseid');
63 // require proper login; guest user can not change password
64 if (!isloggedin() or isguestuser()) {
65 if (empty($SESSION->wantsurl)) {
66 $SESSION->wantsurl = $CFG->httpswwwroot.'/login/change_password.php';
68 redirect(get_login_url());
71 $PAGE->set_context(context_user::instance($USER->id));
72 $PAGE->set_pagelayout('admin');
73 $PAGE->set_course($course);
75 // do not require change own password cap if change forced
76 if (!get_user_preferences('auth_forcepasswordchange', false)) {
77 require_capability('moodle/user:changeownpassword', $systemcontext);
80 // do not allow "Logged in as" users to change any passwords
81 if (\core\session\manager::is_loggedinas()) {
82 print_error('cannotcallscript');
85 if (is_mnet_remote_user($USER)) {
86 $message = get_string('usercannotchangepassword', 'mnet');
87 if ($idprovider = $DB->get_record('mnet_host', array('id'=>$USER->mnethostid))) {
88 $message .= get_string('userchangepasswordlink', 'mnet', $idprovider);
90 print_error('userchangepasswordlink', 'mnet', '', $message);
93 // load the appropriate auth plugin
94 $userauth = get_auth_plugin($USER->auth);
96 if (!$userauth->can_change_password()) {
97 print_error('nopasswordchange', 'auth');
100 if ($changeurl = $userauth->change_password_url()) {
101 // this internal scrip not used
102 redirect($changeurl);
105 $mform = new login_change_password_form();
106 $mform->set_data(array('id'=>$course->id));
108 $navlinks = array();
109 $navlinks[] = array('name' => $strparticipants, 'link' => "$CFG->wwwroot/user/index.php?id=$course->id", 'type' => 'misc');
111 if ($mform->is_cancelled()) {
112 redirect($CFG->wwwroot.'/user/preferences.php?userid='.$USER->id.'&amp;course='.$course->id);
113 } else if ($data = $mform->get_data()) {
115 if (!$userauth->user_update_password($USER, $data->newpassword1)) {
116 print_error('errorpasswordupdate', 'auth');
119 user_add_password_history($USER->id, $data->newpassword1);
121 if (!empty($CFG->passwordchangelogout)) {
122 \core\session\manager::kill_user_sessions($USER->id, session_id());
125 // Reset login lockout - we want to prevent any accidental confusion here.
126 login_unlock_account($USER);
128 // register success changing password
129 unset_user_preference('auth_forcepasswordchange', $USER);
130 unset_user_preference('create_password', $USER);
132 $strpasswordchanged = get_string('passwordchanged');
134 $fullname = fullname($USER, true);
136 $PAGE->set_title($strpasswordchanged);
137 $PAGE->set_heading(fullname($USER));
138 echo $OUTPUT->header();
140 notice($strpasswordchanged, new moodle_url($PAGE->url, array('return'=>1)));
142 echo $OUTPUT->footer();
143 exit;
146 // make sure we really are on the https page when https login required
147 $PAGE->verify_https_required();
149 $strchangepassword = get_string('changepassword');
151 $fullname = fullname($USER, true);
153 $PAGE->set_title($strchangepassword);
154 $PAGE->set_heading($fullname);
155 echo $OUTPUT->header();
157 if (get_user_preferences('auth_forcepasswordchange')) {
158 echo $OUTPUT->notification(get_string('forcepasswordchangenotice'));
160 $mform->display();
161 echo $OUTPUT->footer();