Skip to content

Commit cba5eb2

Browse files
committed
Require current password when user changes own pwd
1 parent 3cd91ea commit cba5eb2

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

config/areas/users/dialogs.php

+39-11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use Kirby\Cms\App;
44
use Kirby\Cms\Find;
55
use Kirby\Cms\UserRules;
6+
use Kirby\Exception\Exception;
67
use Kirby\Exception\InvalidArgumentException;
78
use Kirby\Panel\Field;
89
use Kirby\Panel\Panel;
@@ -180,31 +181,58 @@
180181
'user.changePassword' => [
181182
'pattern' => 'users/(:any)/changePassword',
182183
'load' => function (string $id) {
183-
$user = Find::user($id);
184+
$user = Find::user($id);
185+
$fields = [
186+
'password' => Field::password([
187+
'label' => I18n::translate('user.changePassword.new'),
188+
]),
189+
'passwordConfirmation' => Field::password([
190+
'label' => I18n::translate('user.changePassword.new.confirm'),
191+
])
192+
];
193+
194+
if ($user->is($user->kirby()->user()) === true) {
195+
$fields = [
196+
'currentPassword' => Field::password([
197+
'label' => I18n::translate('user.changePassword.current'),
198+
]),
199+
...$fields
200+
];
201+
}
184202

185203
return [
186204
'component' => 'k-form-dialog',
187205
'props' => [
188-
'fields' => [
189-
'password' => Field::password([
190-
'label' => I18n::translate('user.changePassword.new'),
191-
]),
192-
'passwordConfirmation' => Field::password([
193-
'label' => I18n::translate('user.changePassword.new.confirm'),
194-
])
195-
],
206+
'fields' => $fields,
196207
'submitButton' => I18n::translate('change'),
197208
]
198209
];
199210
},
200211
'submit' => function (string $id) {
201-
$request = App::instance()->request();
212+
$kirby = App::instance();
213+
$request = $kirby->request();
202214

203215
$user = Find::user($id);
204216
$password = $request->get('password');
205217
$passwordConfirmation = $request->get('passwordConfirmation');
206218

207-
// validate the password
219+
// validate the current password,
220+
// if current user is changing their own password
221+
if ($user->is($kirby->user()) === true) {
222+
$currentPassword = $request->get('currentPassword');
223+
224+
// catching and re-throwing exception to avoid automatic
225+
// sign-out of current user from the Panel
226+
try {
227+
$user->validatePassword($currentPassword);
228+
} catch (Exception) {
229+
throw new InvalidArgumentException([
230+
'key' => 'user.password.wrong'
231+
]);
232+
}
233+
}
234+
235+
// validate the new password
208236
UserRules::validPassword($user, $password ?? '');
209237

210238
// compare passwords

i18n/translations/en.json

+1
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,7 @@
714714
"user.changeLanguage": "Change language",
715715
"user.changeName": "Rename this user",
716716
"user.changePassword": "Change password",
717+
"user.changePassword.current": "Current password",
717718
"user.changePassword.new": "New password",
718719
"user.changePassword.new.confirm": "Confirm the new password…",
719720
"user.changeRole": "Change role",

src/Cms/UserActions.php

+3
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ public function changeName(string $name): static
9696

9797
/**
9898
* Changes the user password
99+
*
100+
* If this method is used with user input, it is recommended to also
101+
* confirm the current password by the user via `::validatePassword()`
99102
*/
100103
public function changePassword(
101104
#[SensitiveParameter]

0 commit comments

Comments
 (0)