3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
19 * This file is part of the login section Moodle
21 * @copyright 1999 Martin Dougiamas http://dougiamas.com
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require_once('../config.php');
27 require_once('change_password_form.php');
29 $id = optional_param('id', SITEID
, PARAM_INT
); // current course
31 $url = new moodle_url('/login/change_password.php');
33 $url->param('id', $id);
37 $strparticipants = get_string('participants');
39 //HTTPS is potentially required in this page
42 $systemcontext = get_context_instance(CONTEXT_SYSTEM
);
44 if (!$course = $DB->get_record('course', array('id'=>$id))) {
45 print_error('invalidcourseid');
48 // require proper login; guest user can not change password
49 if (!isloggedin() or isguestuser()) {
50 if (empty($SESSION->wantsurl
)) {
51 $SESSION->wantsurl
= $CFG->httpswwwroot
.'/login/change_password.php';
53 redirect(get_login_url());
56 // do not require change own password cap if change forced
57 if (!get_user_preferences('auth_forcepasswordchange', false)) {
59 require_capability('moodle/user:changeownpassword', $systemcontext);
62 // do not allow "Logged in as" users to change any passwords
63 if (session_is_loggedinas()) {
64 print_error('cannotcallscript');
67 if (is_mnet_remote_user($USER)) {
68 $message = get_string('usercannotchangepassword', 'mnet');
69 if ($idprovider = $DB->get_record('mnet_host', array('id'=>$USER->mnethostid
))) {
70 $message .= get_string('userchangepasswordlink', 'mnet', $idprovider);
72 print_error('userchangepasswordlink', 'mnet', '', $message);
75 // load the appropriate auth plugin
76 $userauth = get_auth_plugin($USER->auth
);
78 if (!$userauth->can_change_password()) {
79 print_error('nopasswordchange', 'auth');
82 if ($changeurl = $userauth->change_password_url()) {
83 // this internal scrip not used
87 $mform = new login_change_password_form();
88 $mform->set_data(array('id'=>$course->id
));
91 $navlinks[] = array('name' => $strparticipants, 'link' => "$CFG->wwwroot/user/index.php?id=$course->id", 'type' => 'misc');
93 if ($mform->is_cancelled()) {
94 redirect($CFG->wwwroot
.'/user/view.php?id='.$USER->id
.'&course='.$course->id
);
95 } else if ($data = $mform->get_data()) {
97 if (!$userauth->user_update_password($USER, $data->newpassword1
)) {
98 print_error('errorpasswordupdate', 'auth');
101 // register success changing password
102 unset_user_preference('auth_forcepasswordchange', $USER->id
);
104 $strpasswordchanged = get_string('passwordchanged');
106 add_to_log($course->id
, 'user', 'change password', "view.php?id=$USER->id&course=$course->id", "$USER->id");
108 $fullname = fullname($USER, true);
110 $PAGE->navbar
->add($fullname, new moodle_url('/user/view.php', array('id'=>$USER->id
, 'course'=>$course->id
)));
111 $PAGE->navbar
->add($strpasswordchanged);
112 $PAGE->set_title($strpasswordchanged);
113 $PAGE->set_heading($strpasswordchanged);
114 echo $OUTPUT->header();
116 if (empty($SESSION->wantsurl
) or $SESSION->wantsurl
== $CFG->httpswwwroot
.'/login/change_password.php') {
117 $returnto = "$CFG->wwwroot/user/view.php?id=$USER->id&course=$id";
119 $returnto = $SESSION->wantsurl
;
122 notice($strpasswordchanged, $returnto);
124 echo $OUTPUT->footer();
129 $strchangepassword = get_string('changepassword');
131 $fullname = fullname($USER, true);
133 $PAGE->navbar
->add($fullname, new moodle_url('/user/view.php', array('id'=>$USER->id
, 'course'=>$course->id
)));
134 $PAGE->navbar
->add($strchangepassword);
135 $PAGE->set_title($strchangepassword);
136 $PAGE->set_heading($strchangepassword);
137 echo $OUTPUT->header();
139 if (get_user_preferences('auth_forcepasswordchange')) {
140 echo $OUTPUT->notification(get_string('forcepasswordchangenotice'));
143 echo $OUTPUT->footer();