Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 43 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
UpdateRoleEndpoint | |
0.00% |
0 / 43 |
|
0.00% |
0 / 1 |
182 | |
0.00% |
0 / 1 |
handle | |
0.00% |
0 / 43 |
|
0.00% |
0 / 1 |
182 |
1 | <?php |
2 | |
3 | namespace Olz\Roles\Endpoints; |
4 | |
5 | use Olz\Api\OlzUpdateEntityTypedEndpoint; |
6 | use Olz\Entity\Roles\Role; |
7 | use Olz\Entity\Users\User; |
8 | use PhpTypeScriptApi\Fields\ValidationError; |
9 | use PhpTypeScriptApi\HttpError; |
10 | |
11 | /** |
12 | * @phpstan-import-type OlzRoleId from RoleEndpointTrait |
13 | * @phpstan-import-type OlzRoleData from RoleEndpointTrait |
14 | * |
15 | * @extends OlzUpdateEntityTypedEndpoint<OlzRoleId, OlzRoleData> |
16 | */ |
17 | class UpdateRoleEndpoint extends OlzUpdateEntityTypedEndpoint { |
18 | use RoleEndpointTrait; |
19 | |
20 | protected function handle(mixed $input): mixed { |
21 | $user_repo = $this->entityManager()->getRepository(User::class); |
22 | $role_repo = $this->entityManager()->getRepository(Role::class); |
23 | $entity = $this->getEntityById($input['id']); |
24 | |
25 | $is_superior = $this->authUtils()->hasRoleEditPermission($input['id']); |
26 | $is_owner = $this->entityUtils()->canUpdateOlzEntity($entity, null, 'roles'); |
27 | if (!$is_superior && !$is_owner) { |
28 | throw new HttpError(403, "Kein Zugriff!"); |
29 | } |
30 | |
31 | // Username validation |
32 | $new_username = $input['data']['username']; |
33 | $is_username_updated = $new_username !== $entity->getUsername(); |
34 | if (!$this->authUtils()->isUsernameAllowed($new_username)) { |
35 | throw new ValidationError(['username' => ["Der Benutzername darf nur Buchstaben, Zahlen, und die Zeichen -_. enthalten."]]); |
36 | } |
37 | if ($is_username_updated) { |
38 | $same_username_user = $user_repo->findOneBy(['username' => $new_username]); |
39 | $same_old_username_user = $user_repo->findOneBy(['old_username' => $new_username]); |
40 | $same_username_role = $role_repo->findOneBy(['username' => $new_username]); |
41 | $same_old_username_role = $role_repo->findOneBy(['old_username' => $new_username]); |
42 | $is_existing_username = (bool) ( |
43 | $same_username_user || $same_old_username_user |
44 | || $same_username_role || $same_old_username_role |
45 | ); |
46 | if ($is_existing_username) { |
47 | throw new ValidationError(['username' => ["Dieser Benutzername ist bereits vergeben."]]); |
48 | } |
49 | } |
50 | |
51 | // TODO Do this more elegantly? |
52 | $old_data = $this->getEntityData($entity); |
53 | $this->log()->notice('OLD:', [$old_data]); |
54 | |
55 | $this->entityUtils()->updateOlzEntity($entity, $input['meta']); |
56 | if ($is_username_updated) { |
57 | $entity->setOldUsername($entity->getUsername()); |
58 | } |
59 | $role_repo = $this->entityManager()->getRepository(Role::class); |
60 | $parent_role_id = $entity->getParentRoleId(); |
61 | $parent_role = $role_repo->findOneBy(['id' => $parent_role_id]); |
62 | $is_parent_superior = $this->authUtils()->hasRoleEditPermission($parent_role_id); |
63 | $is_parent_owner = $parent_role && $this->entityUtils()->canUpdateOlzEntity($parent_role, null, 'roles'); |
64 | if ($is_parent_superior || $is_parent_owner) { |
65 | $this->updateEntityWithData($entity, $input['data']); |
66 | } else { |
67 | $this->updateEntityWithNonParentData($entity, $input['data']); |
68 | } |
69 | |
70 | // TODO Do this more elegantly? |
71 | $new_data = $this->getEntityData($entity); |
72 | $this->log()->notice('NEW:', [$new_data]); |
73 | |
74 | $this->entityManager()->persist($entity); |
75 | $this->entityManager()->flush(); |
76 | $this->persistUploads($entity, $input['data']); |
77 | |
78 | return [ |
79 | 'id' => $entity->getId() ?? 0, |
80 | ]; |
81 | } |
82 | } |