Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
UpdateUserPasswordEndpoint
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
30
0.00% covered (danger)
0.00%
0 / 1
 handle
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2
3namespace Olz\Api\Endpoints;
4
5use Olz\Api\OlzTypedEndpoint;
6use Olz\Entity\Users\User;
7use PhpTypeScriptApi\Fields\ValidationError;
8
9/**
10 * @extends OlzTypedEndpoint<
11 *   array{
12 *     id: int,
13 *     oldPassword: non-empty-string,
14 *     newPassword: non-empty-string,
15 *   },
16 *   array{
17 *     status: 'OK'|'OTHER_USER'|'INVALID_OLD',
18 *   }
19 * >
20 */
21class UpdateUserPasswordEndpoint extends OlzTypedEndpoint {
22    protected function handle(mixed $input): mixed {
23        $auth_username = $this->session()->get('user');
24
25        $old_password = $input['oldPassword'];
26        $new_password = $input['newPassword'];
27
28        if (!$this->authUtils()->isPasswordAllowed($new_password)) {
29            throw new ValidationError(['newPassword' => ["Das neue Passwort muss mindestens 8 Zeichen lang sein."]]);
30        }
31
32        $user_repo = $this->entityManager()->getRepository(User::class);
33        $user = $user_repo->findOneBy(['id' => $input['id']]);
34        $this->generalUtils()->checkNotNull($user, "No such user: {$input['id']}");
35
36        if ($user->getUsername() !== $auth_username) {
37            return ['status' => 'OTHER_USER'];
38        }
39
40        $hash = $user->getPasswordHash();
41        if (!$hash || !$this->authUtils()->verifyPassword($old_password, $hash)) {
42            return ['status' => 'INVALID_OLD'];
43        }
44
45        $now_datetime = new \DateTime($this->dateUtils()->getIsoNow());
46        $user->setPasswordHash($this->authUtils()->hashPassword($new_password));
47        $user->setLastModifiedAt($now_datetime);
48        $this->entityManager()->flush();
49
50        return ['status' => 'OK'];
51    }
52}