Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

   1  <?php
   2  
   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.
  14  //
  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/>.
  17  
  18  /**
  19   * Change password page.
  20   *
  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
  25   */
  26  
  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');
  31  require_once($CFG->dirroot.'/webservice/lib.php');
  32  require_once ('lib.php');
  33  
  34  $id     = optional_param('id', SITEID, PARAM_INT); // current course
  35  $return = optional_param('return', 0, PARAM_BOOL); // redirect after password change
  36  
  37  $systemcontext = context_system::instance();
  38  
  39  $PAGE->set_url('/login/change_password.php', array('id'=>$id));
  40  
  41  $PAGE->set_context($systemcontext);
  42  
  43  if ($return) {
  44      // this redirect prevents security warning because https can not POST to http pages
  45      if (empty($SESSION->wantsurl)
  46              or stripos(str_replace('https://', 'http://', $SESSION->wantsurl), str_replace('https://', 'http://', $CFG->wwwroot.'/login/change_password.php')) === 0) {
  47          $returnto = "$CFG->wwwroot/user/preferences.php?userid=$USER->id&course=$id";
  48      } else {
  49          $returnto = $SESSION->wantsurl;
  50      }
  51      unset($SESSION->wantsurl);
  52  
  53      redirect($returnto);
  54  }
  55  
  56  $strparticipants = get_string('participants');
  57  
  58  if (!$course = $DB->get_record('course', array('id'=>$id))) {
  59      print_error('invalidcourseid');
  60  }
  61  
  62  // require proper login; guest user can not change password
  63  if (!isloggedin() or isguestuser()) {
  64      if (empty($SESSION->wantsurl)) {
  65          $SESSION->wantsurl = $CFG->wwwroot.'/login/change_password.php';
  66      }
  67      redirect(get_login_url());
  68  }
  69  
  70  $PAGE->set_context(context_user::instance($USER->id));
  71  $PAGE->set_pagelayout('admin');
  72  $PAGE->set_course($course);
  73  
  74  // do not require change own password cap if change forced
  75  if (!get_user_preferences('auth_forcepasswordchange', false)) {
  76      require_capability('moodle/user:changeownpassword', $systemcontext);
  77  }
  78  
  79  // do not allow "Logged in as" users to change any passwords
  80  if (\core\session\manager::is_loggedinas()) {
  81      print_error('cannotcallscript');
  82  }
  83  
  84  if (is_mnet_remote_user($USER)) {
  85      $message = get_string('usercannotchangepassword', 'mnet');
  86      if ($idprovider = $DB->get_record('mnet_host', array('id'=>$USER->mnethostid))) {
  87          $message .= get_string('userchangepasswordlink', 'mnet', $idprovider);
  88      }
  89      print_error('userchangepasswordlink', 'mnet', '', $message);
  90  }
  91  
  92  // load the appropriate auth plugin
  93  $userauth = get_auth_plugin($USER->auth);
  94  
  95  if (!$userauth->can_change_password()) {
  96      print_error('nopasswordchange', 'auth');
  97  }
  98  
  99  if ($changeurl = $userauth->change_password_url()) {
 100      // this internal scrip not used
 101      redirect($changeurl);
 102  }
 103  
 104  $mform = new login_change_password_form();
 105  $mform->set_data(array('id'=>$course->id));
 106  
 107  $navlinks = array();
 108  $navlinks[] = array('name' => $strparticipants, 'link' => "$CFG->wwwroot/user/index.php?id=$course->id", 'type' => 'misc');
 109  
 110  if ($mform->is_cancelled()) {
 111      redirect($CFG->wwwroot.'/user/preferences.php?userid='.$USER->id.'&amp;course='.$course->id);
 112  } else if ($data = $mform->get_data()) {
 113  
 114      if (!$userauth->user_update_password($USER, $data->newpassword1)) {
 115          print_error('errorpasswordupdate', 'auth');
 116      }
 117  
 118      user_add_password_history($USER->id, $data->newpassword1);
 119  
 120      if (!empty($CFG->passwordchangelogout)) {
 121          \core\session\manager::kill_user_sessions($USER->id, session_id());
 122      }
 123  
 124      if (!empty($data->signoutofotherservices)) {
 125          webservice::delete_user_ws_tokens($USER->id);
 126      }
 127  
 128      // Reset login lockout - we want to prevent any accidental confusion here.
 129      login_unlock_account($USER);
 130  
 131      // register success changing password
 132      unset_user_preference('auth_forcepasswordchange', $USER);
 133      unset_user_preference('create_password', $USER);
 134  
 135      $strpasswordchanged = get_string('passwordchanged');
 136  
 137      // Plugins can perform post password change actions once data has been validated.
 138      core_login_post_change_password_requests($data);
 139  
 140      $fullname = fullname($USER, true);
 141  
 142      $PAGE->set_title($strpasswordchanged);
 143      $PAGE->set_heading(fullname($USER));
 144      echo $OUTPUT->header();
 145  
 146      notice($strpasswordchanged, new moodle_url($PAGE->url, array('return'=>1)));
 147  
 148      echo $OUTPUT->footer();
 149      exit;
 150  }
 151  
 152  $strchangepassword = get_string('changepassword');
 153  
 154  $fullname = fullname($USER, true);
 155  
 156  $PAGE->set_title($strchangepassword);
 157  $PAGE->set_heading($fullname);
 158  echo $OUTPUT->header();
 159  
 160  if (get_user_preferences('auth_forcepasswordchange')) {
 161      echo $OUTPUT->notification(get_string('forcepasswordchangenotice'));
 162  }
 163  $mform->display();
 164  echo $OUTPUT->footer();