Merge branch 'MDL-50472-37' of git://github.com/Chocolate-lightning/moodle into MOODL...
[moodle.git] / login / change_password_form.php
blob4a2597932c1e9315d544fc94809d042082282579
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');
32 class login_change_password_form extends moodleform {
34 function definition() {
35 global $USER, $CFG;
37 $mform = $this->_form;
38 $mform->setDisableShortforms(true);
40 $mform->addElement('header', 'changepassword', get_string('changepassword'), '');
42 // visible elements
43 $mform->addElement('static', 'username', get_string('username'), $USER->username);
45 $policies = array();
46 if (!empty($CFG->passwordpolicy)) {
47 $policies[] = print_password_policy();
49 if (!empty($CFG->passwordreuselimit) and $CFG->passwordreuselimit > 0) {
50 $policies[] = get_string('informminpasswordreuselimit', 'auth', $CFG->passwordreuselimit);
52 if ($policies) {
53 $mform->addElement('static', 'passwordpolicyinfo', '', implode('<br />', $policies));
55 $purpose = user_edit_map_field_purpose($USER->id, 'password');
56 $mform->addElement('password', 'password', get_string('oldpassword'), $purpose);
57 $mform->addRule('password', get_string('required'), 'required', null, 'client');
58 $mform->setType('password', PARAM_RAW);
60 $mform->addElement('password', 'newpassword1', get_string('newpassword'));
61 $mform->addRule('newpassword1', get_string('required'), 'required', null, 'client');
62 $mform->setType('newpassword1', PARAM_RAW);
64 $mform->addElement('password', 'newpassword2', get_string('newpassword').' ('.get_String('again').')');
65 $mform->addRule('newpassword2', get_string('required'), 'required', null, 'client');
66 $mform->setType('newpassword2', PARAM_RAW);
68 if (empty($CFG->passwordchangetokendeletion) and !empty(webservice::get_active_tokens($USER->id))) {
69 $mform->addElement('advcheckbox', 'signoutofotherservices', get_string('signoutofotherservices'));
70 $mform->addHelpButton('signoutofotherservices', 'signoutofotherservices');
71 $mform->setDefault('signoutofotherservices', 1);
74 // hidden optional params
75 $mform->addElement('hidden', 'id', 0);
76 $mform->setType('id', PARAM_INT);
78 // buttons
79 if (get_user_preferences('auth_forcepasswordchange')) {
80 $this->add_action_buttons(false);
81 } else {
82 $this->add_action_buttons(true);
86 /// perform extra password change validation
87 function validation($data, $files) {
88 global $USER;
89 $errors = parent::validation($data, $files);
90 $reason = null;
92 // ignore submitted username
93 if (!$user = authenticate_user_login($USER->username, $data['password'], true, $reason, false)) {
94 $errors['password'] = get_string('invalidlogin');
95 return $errors;
98 if ($data['newpassword1'] <> $data['newpassword2']) {
99 $errors['newpassword1'] = get_string('passwordsdiffer');
100 $errors['newpassword2'] = get_string('passwordsdiffer');
101 return $errors;
104 if ($data['password'] == $data['newpassword1']){
105 $errors['newpassword1'] = get_string('mustchangepassword');
106 $errors['newpassword2'] = get_string('mustchangepassword');
107 return $errors;
110 if (user_is_previously_used_password($USER->id, $data['newpassword1'])) {
111 $errors['newpassword1'] = get_string('errorpasswordreused', 'core_auth');
112 $errors['newpassword2'] = get_string('errorpasswordreused', 'core_auth');
115 $errmsg = '';//prevents eclipse warnings
116 if (!check_password_policy($data['newpassword1'], $errmsg)) {
117 $errors['newpassword1'] = $errmsg;
118 $errors['newpassword2'] = $errmsg;
119 return $errors;
122 return $errors;