Debian Codegraph @

templates/changePassword.php

raw
1 <?php
2
3 namespace LAM\INIT;
4
5 use htmlButton;
6 use htmlOutputText;
7 use htmlResponsiveInputField;
8 use htmlResponsiveRow;
9 use htmlStatusMessage;
10
11 /*
12
13 This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
14 Copyright (C) 2020 - 2026 Roland Gruber
15
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
18 the Free Software Foundation; either version 2 of the License, or
19 (at your option) any later version.
20
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
25
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29
30 */
31
32 /**
33 * Password change dialog for expired passwords.
34 *
35 * @author Roland Gruber
36 * @package main
37 */
38
39 /** security functions */
40 include_once(__DIR__ . "/../lib/security.inc");
41 /** access to configuration settings */
42 include_once(__DIR__ . "/../lib/config.inc");
43 /** LDAP access */
44 include_once(__DIR__ . "/../lib/ldap.inc");
45 /** status messages */
46 include_once(__DIR__ . "/../lib/status.inc");
47
48 // start session
49 startSecureSession();
50 enforceUserIsLoggedIn();
51
52 if (!checkIfWriteAccessIsAllowed()) {
53 die();
54 }
55
56 setlanguage();
57
58 if (!empty($_POST)) {
59 validateSecurityToken();
60 }
61
62 $message = null;
63
64 // check if user already pressed button
65 if (isset($_POST['changePassword'])) {
66 // check new password
67 $password1 = $_POST['password1'];
68 $password2 = $_POST['password2'];
69 if ($password1 == '') {
70 $message = new htmlStatusMessage('ERROR', _('No password was entered!'));
71 printContent($message);
72 exit();
73 }
74 // check if passwords match
75 if ($password1 != $password2) {
76 $message = new htmlStatusMessage('ERROR', _('Passwords are different!'));
77 printContent($message);
78 exit();
79 }
80 // check password strength
81 $userDn = $_SESSION['ldap']->getUserName();
82 $rdnAttr = extractRDNAttribute($userDn);
83 $userName = null;
84 if ($rdnAttr === 'uid') {
85 $userName = extractRDNValue($userDn);
86 }
87 $pwdPolicyResult = checkPasswordStrength($password1, $userName, []);
88 if (!$pwdPolicyResult->isValid()) {
89 $message = new htmlStatusMessage('ERROR', $pwdPolicyResult->getTextMessage());
90 printContent($message);
91 exit();
92 }
93 // set new password
94 $modifyResult = @ldap_exop_passwd($_SESSION['ldap']->server(), $userDn, $_SESSION['ldap']->getPassword(), $password1);
95 if ($modifyResult === true) {
96 $_SESSION['ldap']->tryAndApplyNewPassword($password1);
97 $message = new htmlStatusMessage('INFO', _('Password changed.'));
98 printContent($message, false);
99 exit();
100 }
101 else {
102 $message = new htmlStatusMessage('ERROR', _('Unable to set password'), getExtendedLDAPErrorMessage($_SESSION['ldap']->server()));
103 printContent($message);
104 exit();
105 }
106 }
107
108 printContent($message);
109
110 /**
111 * Displays the content area
112 *
113 * @param htmlStatusMessage $message status message
114 * @param bool $showPasswordInputs show password input fields
115 */
116 function printContent($message = null, $showPasswordInputs = true): void {
117 include __DIR__ . '/../lib/adminHeader.inc';
118 echo '<div class="smallPaddingContent">';
119 echo "<form action=\"changePassword.php\" method=\"post\">\n";
120 $container = new htmlResponsiveRow();
121 if ($message !== null) {
122 $container->addVerticalSpacer('1rem');
123 $container->add($message, 12);
124 }
125 $container->addVerticalSpacer('2rem');
126 if ($showPasswordInputs) {
127 $container->add(new htmlOutputText(_("It seems your password expired. You can set a new one here.")), 12, 12, 12, 'text-center');
128 $container->addVerticalSpacer('2rem');
129 $pwdInput1 = new htmlResponsiveInputField(_('New password'), 'password1', '');
130 $pwdInput1->setIsPassword(true, true, true);
131 $container->add($pwdInput1, 12);
132 $pwdInput2 = new htmlResponsiveInputField(_('Repeat password'), 'password2', '');
133 $pwdInput2->setIsPassword(true);
134 $pwdInput2->setSameValueFieldID('password1');
135 $container->add($pwdInput2, 12);
136 $container->addVerticalSpacer('1rem');
137 $container->add(new htmlButton('changePassword', _("Submit")), 12, 12, 12, 'text-center');
138 addSecurityTokenToMetaHTML($container);
139 }
140
141 parseHtml(null, $container, [], false, 'user');
142
143 echo "</form><br>\n";
144 echo "</div>\n";
145 include __DIR__ . '/../lib/adminFooter.inc';
146 }