Merge branch 'MDL-47736_master' of git://github.com/markn86/moodle
[moodle.git] / login / set_password_form.php
blob62e8b5c0e3e8d98f4e4cf20c0c713301bbbe660e
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Set password form definition.
20 * @package core
21 * @subpackage auth
22 * @copyright 2006 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 require_once($CFG->libdir.'/formslib.php');
30 /**
31 * Set forgotten password form definition.
33 * @package core
34 * @subpackage auth
35 * @copyright 2006 Petr Skoda {@link http://skodak.org}
36 * @copyright 2013 Peter Bulmer
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class login_set_password_form extends moodleform {
41 /**
42 * Define the set password form.
44 public function definition() {
45 global $USER, $CFG;
46 // Prepare a string showing whether the site wants login password autocompletion to be available to user.
47 if (empty($CFG->loginpasswordautocomplete)) {
48 $autocomplete = 'autocomplete="on"';
49 } else {
50 $autocomplete = '';
53 $mform = $this->_form;
54 $mform->setDisableShortforms(true);
55 $mform->addElement('header', 'setpassword', get_string('setpassword'), '');
57 // Include the username in the form so browsers will recognise that a password is being set.
58 $mform->addElement('text', 'username', '', 'style="display: none;" ' . $autocomplete);
59 $mform->setType('username', PARAM_RAW);
60 // Token gives authority to change password.
61 $mform->addElement('hidden', 'token', '');
62 $mform->setType('token', PARAM_ALPHANUM);
64 // Visible elements.
65 $mform->addElement('static', 'username2', get_string('username'));
67 if (!empty($CFG->passwordpolicy)) {
68 $mform->addElement('static', 'passwordpolicyinfo', '', print_password_policy());
70 $mform->addElement('password', 'password', get_string('newpassword'), $autocomplete);
71 $mform->addRule('password', get_string('required'), 'required', null, 'client');
72 $mform->setType('password', PARAM_RAW);
74 $strpasswordagain = get_string('newpassword') . ' (' . get_string('again') . ')';
75 $mform->addElement('password', 'password2', $strpasswordagain, $autocomplete);
76 $mform->addRule('password2', get_string('required'), 'required', null, 'client');
77 $mform->setType('password2', PARAM_RAW);
79 $this->add_action_buttons(true);
82 /**
83 * Perform extra password change validation.
84 * @param array $data submitted form fields.
85 * @param array $files submitted with the form.
86 * @return array errors occuring during validation.
88 public function validation($data, $files) {
89 global $USER;
90 $errors = parent::validation($data, $files);
92 // Ignore submitted username.
93 if ($data['password'] !== $data['password2']) {
94 $errors['password'] = get_string('passwordsdiffer');
95 $errors['password2'] = get_string('passwordsdiffer');
96 return $errors;
99 $errmsg = ''; // Prevents eclipse warnings.
100 if (!check_password_policy($data['password'], $errmsg)) {
101 $errors['password'] = $errmsg;
102 $errors['password2'] = $errmsg;
103 return $errors;
106 return $errors;