Merge branch 'MDL-81449-main' of https://github.com/lucaboesch/moodle
[moodle.git] / login / change_password_form.php
blobce2b4c3b9ff2946606de0ac09cebb6376c8966c9
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 form definition.
21 * @package core
22 * @subpackage auth
23 * @copyright 2006 Petr Skoda {@link http://skodak.org}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
29 require_once($CFG->libdir.'/formslib.php');
30 require_once($CFG->dirroot.'/user/lib.php');
31 require_once('lib.php');
33 class login_change_password_form extends moodleform {
35 function definition() {
36 global $USER, $CFG;
38 $mform = $this->_form;
39 $mform->setDisableShortforms(true);
41 $mform->addElement('header', 'changepassword', get_string('changepassword'), '');
43 // visible elements
44 $mform->addElement('static', 'username', get_string('username'), $USER->username);
46 $policies = array();
47 if (!empty($CFG->passwordpolicy)) {
48 $policies[] = print_password_policy();
50 if (!empty($CFG->passwordreuselimit) and $CFG->passwordreuselimit > 0) {
51 $policies[] = get_string('informminpasswordreuselimit', 'auth', $CFG->passwordreuselimit);
53 if ($policies) {
54 $mform->addElement('static', 'passwordpolicyinfo', '', implode('<br />', $policies));
56 $purpose = user_edit_map_field_purpose($USER->id, 'password');
57 $mform->addElement('password', 'password', get_string('oldpassword'), $purpose);
58 $mform->addRule('password', get_string('required'), 'required', null, 'client');
59 $mform->setType('password', PARAM_RAW);
61 $mform->addElement('password', 'newpassword1', get_string('newpassword'),
62 ['autocomplete' => 'new-password', 'maxlength' => MAX_PASSWORD_CHARACTERS]);
63 $mform->addRule('newpassword1', get_string('required'), 'required', null, 'client');
64 $mform->addRule('password', get_string('maximumchars', '', MAX_PASSWORD_CHARACTERS),
65 'maxlength', MAX_PASSWORD_CHARACTERS, 'client');
66 $mform->setType('newpassword1', PARAM_RAW);
68 $mform->addElement('password', 'newpassword2', get_string('newpassword').' ('.get_String('again').')',
69 ['autocomplete' => 'new-password', 'maxlength' => MAX_PASSWORD_CHARACTERS]);
70 $mform->addRule('newpassword2', get_string('required'), 'required', null, 'client');
71 $mform->setType('newpassword2', PARAM_RAW);
73 if (empty($CFG->passwordchangetokendeletion) and !empty(webservice::get_active_tokens($USER->id))) {
74 $mform->addElement('advcheckbox', 'signoutofotherservices', get_string('signoutofotherservices'));
75 $mform->addHelpButton('signoutofotherservices', 'signoutofotherservices');
76 $mform->setDefault('signoutofotherservices', 1);
79 // hidden optional params
80 $mform->addElement('hidden', 'id', 0);
81 $mform->setType('id', PARAM_INT);
83 // Hook for plugins to extend form definition.
84 core_login_extend_change_password_form($mform, $USER);
86 // buttons
87 if (get_user_preferences('auth_forcepasswordchange')) {
88 $this->add_action_buttons(false);
89 } else {
90 $this->add_action_buttons(true);
94 /// perform extra password change validation
95 function validation($data, $files) {
96 global $USER;
97 $errors = parent::validation($data, $files);
98 $reason = null;
100 // Extend validation for any form extensions from plugins.
101 $errors = array_merge($errors, core_login_validate_extend_change_password_form($data, $USER));
103 // ignore submitted username
104 if (!$user = authenticate_user_login($USER->username, $data['password'], true, $reason, false)) {
105 $errors['password'] = get_string('invalidlogin');
106 return $errors;
109 if ($data['newpassword1'] <> $data['newpassword2']) {
110 $errors['newpassword1'] = get_string('passwordsdiffer');
111 $errors['newpassword2'] = get_string('passwordsdiffer');
112 return $errors;
115 if ($data['password'] == $data['newpassword1']){
116 $errors['newpassword1'] = get_string('mustchangepassword');
117 $errors['newpassword2'] = get_string('mustchangepassword');
118 return $errors;
121 if (user_is_previously_used_password($USER->id, $data['newpassword1'])) {
122 $errors['newpassword1'] = get_string('errorpasswordreused', 'core_auth');
123 $errors['newpassword2'] = get_string('errorpasswordreused', 'core_auth');
126 $errmsg = '';//prevents eclipse warnings
127 if (!check_password_policy($data['newpassword1'], $errmsg, $USER)) {
128 $errors['newpassword1'] = $errmsg;
129 $errors['newpassword2'] = $errmsg;
130 return $errors;
133 return $errors;