Fixes for Bug MDL-8617 "Implement groupings & course modules..."
[moodle-pu.git] / login / change_password.php
blobe7f78e6a58669ec8dab7f5d9e5e4627b78c7e2f5
1 <?PHP // $Id$
3 require_once('../config.php');
4 require_once('change_password_form.php');
6 $id = optional_param('id', SITEID, PARAM_INT);
8 //HTTPS is potentially required in this page
9 httpsrequired();
11 $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID);
13 if (!$course = get_record('course', 'id', $id)) {
14 error('No such course!');
17 if (is_mnet_remote_user($USER)) {
18 $message = get_string('usercannotchangepassword', 'mnet');
19 if ($idprovider = get_record('mnet_host', 'id', $USER->mnethostid)) {
20 $message .= get_string('userchangepasswordlink', 'mnet', $idprovider);
22 error($message);
25 // require proper login; guest can not change password
26 // TODO: add change password capability so that we can prevent participants to change password
27 if (empty($USER->id) or isguestuser() or has_capability('moodle/legacy:guest', $sitecontext, $USER->id, false)) {
28 if (empty($SESSION->wantsurl)) {
29 $SESSION->wantsurl = $CFG->httpswwwroot.'/login/change_password.php';
31 redirect($CFG->httpswwwroot.'/login/index.php');
34 // do not allow "Logged in as" users to change any passwords
35 if (!empty($USER->realuser)) {
36 error('Can not use this script when "Logged in as"!');
39 // load the appropriate auth plugin
40 $userauth = get_auth_plugin($USER->auth);
42 if (!$userauth->can_change_password()) {
43 error(get_string('nopasswordchange', 'auth'));
46 if (method_exists($userauth, 'change_password_url') and $userauth->change_password_url()) {
47 // this internal scrip not used
48 redirect($userauth->change_password_url());
51 $mform = new login_change_password_form();
52 $mform->set_data(array('id'=>$course->id, 'username'=>$USER->username));
54 if ($mform->is_cancelled()) {
55 redirect($CFG->wwwroot.'/user/view.php?id='.$USER->id.'&amp;course='.$course->id);
56 } else if ($data = $mform->get_data()) {
58 if (!has_capability('moodle/user:update', $sitecontext)) {
59 //ignore submitted username - the same is done in form validation
60 $data->username = $USER->username;
63 if ($data->username == $USER->username) {
64 $user =& $USER;
65 } else {
66 $user = get_complete_user_data('username', $data->username);
69 // register success changing password
70 unset_user_preference('auth_forcepasswordchange', $user->id);
72 $strpasswordchanged = get_string('passwordchanged');
74 add_to_log($course->id, 'user', 'change password', "view.php?id=$user->id&amp;course=$course->id", "$user->id");
76 $fullname = fullname($USER, true);
78 if ($course->id != SITEID) {
79 $navstr = "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a> -> ";
80 } else {
81 $navstr = '';
83 $navstr .= "<a href=\"$CFG->wwwroot/user/index.php?id=$course->id\">".get_string("participants")."</a> -> <a href=\"$CFG->wwwroot/user/view.php?id=$USER->id&amp;course=$course->id\">$fullname</a> -> $strpasswordchanged";
85 print_header($strpasswordchanged, $strpasswordchanged, $navstr);
87 if (empty($SESSION->wantsurl) or $SESSION->wantsurl == $CFG->httpswwwroot.'/login/change_password.php') {
88 $returnto = "$CFG->wwwroot/user/view.php?id=$USER->id&amp;course=$id";
89 } else {
90 $returnto = $SESSION->wantsurl;
93 notice($strpasswordchanged, $returnto);
95 print_footer();
96 exit;
100 $strchangepassword = get_string('changepassword');
102 $fullname = fullname($USER, true);
104 if ($course->id != SITEID) {
105 $navstr = "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a> -> ";
106 } else {
107 $navstr = '';
109 $navstr .= "<a href=\"$CFG->wwwroot/user/index.php?id=$course->id\">".get_string('participants')."</a> -> <a href=\"$CFG->wwwroot/user/view.php?id=$USER->id&amp;course=$course->id\">$fullname</a> -> $strchangepassword";
112 print_header($strchangepassword, $strchangepassword, $navstr);
113 if (!empty($USER->preference['auth_forcepasswordchange'])) {
114 notify(get_string('forcepasswordchangenotice'));
116 $mform->display();
117 print_footer();