|
3 | 3 | use Kirby\Cms\App;
|
4 | 4 | use Kirby\Cms\Find;
|
5 | 5 | use Kirby\Cms\UserRules;
|
| 6 | +use Kirby\Exception\Exception; |
6 | 7 | use Kirby\Exception\InvalidArgumentException;
|
7 | 8 | use Kirby\Panel\Field;
|
8 | 9 | use Kirby\Panel\Panel;
|
|
180 | 181 | 'user.changePassword' => [
|
181 | 182 | 'pattern' => 'users/(:any)/changePassword',
|
182 | 183 | '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 | + } |
184 | 202 |
|
185 | 203 | return [
|
186 | 204 | 'component' => 'k-form-dialog',
|
187 | 205 | '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, |
196 | 207 | 'submitButton' => I18n::translate('change'),
|
197 | 208 | ]
|
198 | 209 | ];
|
199 | 210 | },
|
200 | 211 | 'submit' => function (string $id) {
|
201 |
| - $request = App::instance()->request(); |
| 212 | + $kirby = App::instance(); |
| 213 | + $request = $kirby->request(); |
202 | 214 |
|
203 | 215 | $user = Find::user($id);
|
204 | 216 | $password = $request->get('password');
|
205 | 217 | $passwordConfirmation = $request->get('passwordConfirmation');
|
206 | 218 |
|
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 |
208 | 236 | UserRules::validPassword($user, $password ?? '');
|
209 | 237 |
|
210 | 238 | // compare passwords
|
|
0 commit comments