PageRenderTime 31ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/login/change_password_form.php

https://bitbucket.org/moodle/moodle
PHP | 134 lines | 81 code | 24 blank | 29 comment | 13 complexity | b44022d6c788fcb124185c1e86cd2c91 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  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.
  13. //
  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/>.
  16. /**
  17. * Change password form definition.
  18. *
  19. * @package core
  20. * @subpackage auth
  21. * @copyright 2006 Petr Skoda {@link http://skodak.org}
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. defined('MOODLE_INTERNAL') || die();
  25. require_once($CFG->libdir.'/formslib.php');
  26. require_once($CFG->dirroot.'/user/lib.php');
  27. require_once('lib.php');
  28. class login_change_password_form extends moodleform {
  29. function definition() {
  30. global $USER, $CFG;
  31. $mform = $this->_form;
  32. $mform->setDisableShortforms(true);
  33. $mform->addElement('header', 'changepassword', get_string('changepassword'), '');
  34. // visible elements
  35. $mform->addElement('static', 'username', get_string('username'), $USER->username);
  36. $policies = array();
  37. if (!empty($CFG->passwordpolicy)) {
  38. $policies[] = print_password_policy();
  39. }
  40. if (!empty($CFG->passwordreuselimit) and $CFG->passwordreuselimit > 0) {
  41. $policies[] = get_string('informminpasswordreuselimit', 'auth', $CFG->passwordreuselimit);
  42. }
  43. if ($policies) {
  44. $mform->addElement('static', 'passwordpolicyinfo', '', implode('<br />', $policies));
  45. }
  46. $purpose = user_edit_map_field_purpose($USER->id, 'password');
  47. $mform->addElement('password', 'password', get_string('oldpassword'), $purpose);
  48. $mform->addRule('password', get_string('required'), 'required', null, 'client');
  49. $mform->setType('password', PARAM_RAW);
  50. $mform->addElement('password', 'newpassword1', get_string('newpassword'),
  51. ['autocomplete' => 'new-password']);
  52. $mform->addRule('newpassword1', get_string('required'), 'required', null, 'client');
  53. $mform->setType('newpassword1', PARAM_RAW);
  54. $mform->addElement('password', 'newpassword2',
  55. get_string('newpassword').' ('.get_String('again').')',
  56. ['autocomplete' => 'new-password']);
  57. $mform->addRule('newpassword2', get_string('required'), 'required', null, 'client');
  58. $mform->setType('newpassword2', PARAM_RAW);
  59. if (empty($CFG->passwordchangetokendeletion) and !empty(webservice::get_active_tokens($USER->id))) {
  60. $mform->addElement('advcheckbox', 'signoutofotherservices', get_string('signoutofotherservices'));
  61. $mform->addHelpButton('signoutofotherservices', 'signoutofotherservices');
  62. $mform->setDefault('signoutofotherservices', 1);
  63. }
  64. // hidden optional params
  65. $mform->addElement('hidden', 'id', 0);
  66. $mform->setType('id', PARAM_INT);
  67. // Hook for plugins to extend form definition.
  68. core_login_extend_change_password_form($mform, $USER);
  69. // buttons
  70. if (get_user_preferences('auth_forcepasswordchange')) {
  71. $this->add_action_buttons(false);
  72. } else {
  73. $this->add_action_buttons(true);
  74. }
  75. }
  76. /// perform extra password change validation
  77. function validation($data, $files) {
  78. global $USER;
  79. $errors = parent::validation($data, $files);
  80. $reason = null;
  81. // Extend validation for any form extensions from plugins.
  82. $errors = array_merge($errors, core_login_validate_extend_change_password_form($data, $USER));
  83. // ignore submitted username
  84. if (!$user = authenticate_user_login($USER->username, $data['password'], true, $reason, false)) {
  85. $errors['password'] = get_string('invalidlogin');
  86. return $errors;
  87. }
  88. if ($data['newpassword1'] <> $data['newpassword2']) {
  89. $errors['newpassword1'] = get_string('passwordsdiffer');
  90. $errors['newpassword2'] = get_string('passwordsdiffer');
  91. return $errors;
  92. }
  93. if ($data['password'] == $data['newpassword1']){
  94. $errors['newpassword1'] = get_string('mustchangepassword');
  95. $errors['newpassword2'] = get_string('mustchangepassword');
  96. return $errors;
  97. }
  98. if (user_is_previously_used_password($USER->id, $data['newpassword1'])) {
  99. $errors['newpassword1'] = get_string('errorpasswordreused', 'core_auth');
  100. $errors['newpassword2'] = get_string('errorpasswordreused', 'core_auth');
  101. }
  102. $errmsg = '';//prevents eclipse warnings
  103. if (!check_password_policy($data['newpassword1'], $errmsg, $USER)) {
  104. $errors['newpassword1'] = $errmsg;
  105. $errors['newpassword2'] = $errmsg;
  106. return $errors;
  107. }
  108. return $errors;
  109. }
  110. }