-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathNewPasswordForm.class.php
More file actions
198 lines (175 loc) · 6.21 KB
/
NewPasswordForm.class.php
File metadata and controls
198 lines (175 loc) · 6.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
namespace wcf\form;
use wcf\data\user\User;
use wcf\data\user\UserAction;
use wcf\system\exception\IllegalLinkException;
use wcf\system\exception\NamedUserException;
use wcf\system\exception\PermissionDeniedException;
use wcf\system\exception\SystemException;
use wcf\system\form\builder\container\FormContainer;
use wcf\system\form\builder\field\PasswordFormField;
use wcf\system\form\builder\field\validation\FormFieldValidationError;
use wcf\system\form\builder\field\validation\FormFieldValidator;
use wcf\system\form\builder\FormDocument;
use wcf\system\request\LinkHandler;
use wcf\system\WCF;
use wcf\util\HeaderUtil;
use wcf\util\HtmlString;
use wcf\util\JSON;
use wcf\util\StringUtil;
use wcf\util\UserRegistrationUtil;
/**
* Shows the new password form.
*
* @author Marcel Werk
* @copyright 2001-2023 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
*
* @extends AbstractFormBuilderForm<null>
*/
final class NewPasswordForm extends AbstractFormBuilderForm
{
const AVAILABLE_DURING_OFFLINE_MODE = true;
public int $userID;
public string $lostPasswordKey;
public User $user;
/**
* @inheritDoc
*/
public function readParameters()
{
parent::readParameters();
if (isset($_GET['id']) && isset($_GET['k'])) {
$this->userID = \intval($_GET['id']);
$this->lostPasswordKey = StringUtil::trim($_GET['k']);
if (!$this->userID || !$this->lostPasswordKey) {
throw new IllegalLinkException();
}
$this->user = new User($this->userID);
if (!$this->user->userID) {
throw new IllegalLinkException();
}
if (!$this->user->lostPasswordKey) {
$this->throwInvalidLinkException();
}
if (!\hash_equals($this->user->lostPasswordKey, $this->lostPasswordKey)) {
$this->throwInvalidLinkException();
}
// expire lost password requests after a day
if ($this->user->lastLostPasswordRequestTime < TIME_NOW - 86400) {
$this->throwInvalidLinkException();
}
WCF::getSession()->register('lostPasswordRequest', [
'userID' => $this->user->userID,
'key' => $this->user->lostPasswordKey,
]);
} else {
if (!\is_array(WCF::getSession()->getVar('lostPasswordRequest'))) {
throw new PermissionDeniedException();
}
$this->userID = \intval(WCF::getSession()->getVar('lostPasswordRequest')['userID']);
$this->user = new User($this->userID);
if (!$this->user->userID) {
throw new IllegalLinkException();
}
if (!\hash_equals($this->user->lostPasswordKey, WCF::getSession()->getVar('lostPasswordRequest')['key'])) {
$this->throwInvalidLinkException();
}
}
}
/**
* @inheritDoc
*/
protected function createForm()
{
// We have to create the form manually here to avoid the form getting the ID 'newPassword'.
$this->form = FormDocument::create('newPasswordForm');
$this->form->appendChild(
FormContainer::create('data')
->appendChildren([
PasswordFormField::create('newPassword')
->label('wcf.user.newPassword')
->required()
->autoFocus()
->removeFieldClass('medium')
->addFieldClass('long')
->autocomplete('new-password')
->fieldAttribute('passwordrules', UserRegistrationUtil::getPasswordRulesAttributeValue())
->addStaticDictionary([$this->user->username, $this->user->email])
->addValidator(new FormFieldValidator(
'passwordValidator',
$this->validatePassword(...)
)),
])
);
}
private function validatePassword(PasswordFormField $formField): void
{
if (isset($_POST['newPassword_passwordStrengthVerdict'])) {
try {
$newPasswordStrengthVerdict = JSON::decode($_POST['newPassword_passwordStrengthVerdict']);
} catch (SystemException $e) {
// ignore
}
}
if (($newPasswordStrengthVerdict['score'] ?? 4) < PASSWORD_MIN_SCORE) {
$formField->addValidationError(
new FormFieldValidationError(
'notSecure',
'wcf.user.password.error.notSecure'
)
);
}
}
/**
* @inheritDoc
*/
public function assignVariables()
{
parent::assignVariables();
WCF::getTPL()->assign([
'user' => $this->user,
]);
}
/**
* @inheritDoc
*/
public function save()
{
AbstractForm::save();
WCF::getSession()->unregister('lostPasswordRequest');
$this->updateUser();
$this->saved();
$this->forwardToIndexPage();
exit;
}
private function updateUser(): void
{
$formData = $this->form->getData()['data'];
$this->objectAction = new UserAction([$this->user], 'update', [
'data' => \array_merge($this->additionalFields, [
'password' => $formData['newPassword'],
'lastLostPasswordRequestTime' => 0,
'lostPasswordKey' => '',
]),
]);
$this->objectAction->executeAction();
}
private function forwardToIndexPage(): void
{
HeaderUtil::delayedRedirect(
LinkHandler::getInstance()->getLink(),
WCF::getLanguage()->getDynamicVariable('wcf.user.newPassword.success', ['user' => $this->user]),
10,
'success',
true
);
exit;
}
private function throwInvalidLinkException(): void
{
throw new NamedUserException(HtmlString::fromSafeHtml(
WCF::getLanguage()->getDynamicVariable('wcf.user.newPassword.error.invalidLink')
));
}
}