Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
95.56% |
43 / 45 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
UpdateRoleEndpoint | |
95.56% |
43 / 45 |
|
50.00% |
1 / 2 |
14 | |
0.00% |
0 / 1 |
configure | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
handle | |
95.35% |
41 / 43 |
|
0.00% |
0 / 1 |
13 |
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 | public function configure(): void { |
21 | parent::configure(); |
22 | $this->phpStanUtils->registerTypeImport(RoleEndpointTrait::class); |
23 | } |
24 | |
25 | protected function handle(mixed $input): mixed { |
26 | $user_repo = $this->entityManager()->getRepository(User::class); |
27 | $role_repo = $this->entityManager()->getRepository(Role::class); |
28 | $entity = $this->getEntityById($input['id']); |
29 | |
30 | $is_superior = $this->authUtils()->hasRoleEditPermission($input['id']); |
31 | $is_owner = $this->entityUtils()->canUpdateOlzEntity($entity, null, 'roles'); |
32 | if (!$is_superior && !$is_owner) { |
33 | throw new HttpError(403, "Kein Zugriff!"); |
34 | } |
35 | |
36 | // Username validation |
37 | $new_username = $input['data']['username']; |
38 | $is_username_updated = $new_username !== $entity->getUsername(); |
39 | if (!$this->authUtils()->isUsernameAllowed($new_username)) { |
40 | throw new ValidationError(['username' => ["Der Benutzername darf nur Buchstaben, Zahlen, und die Zeichen -_. enthalten."]]); |
41 | } |
42 | if ($is_username_updated) { |
43 | $same_username_user = $user_repo->findOneBy(['username' => $new_username]); |
44 | $same_old_username_user = $user_repo->findOneBy(['old_username' => $new_username]); |
45 | $same_username_role = $role_repo->findOneBy(['username' => $new_username]); |
46 | $same_old_username_role = $role_repo->findOneBy(['old_username' => $new_username]); |
47 | $is_existing_username = (bool) ( |
48 | $same_username_user || $same_old_username_user |
49 | || $same_username_role || $same_old_username_role |
50 | ); |
51 | if ($is_existing_username) { |
52 | throw new ValidationError(['username' => ["Dieser Benutzername ist bereits vergeben."]]); |
53 | } |
54 | } |
55 | |
56 | // TODO Do this more elegantly? |
57 | $old_data = $this->getEntityData($entity); |
58 | $this->log()->notice('OLD:', [$old_data]); |
59 | |
60 | $this->entityUtils()->updateOlzEntity($entity, $input['meta']); |
61 | if ($is_username_updated) { |
62 | $entity->setOldUsername($entity->getUsername()); |
63 | } |
64 | $role_repo = $this->entityManager()->getRepository(Role::class); |
65 | $parent_role_id = $entity->getParentRoleId(); |
66 | $parent_role = $role_repo->findOneBy(['id' => $parent_role_id]); |
67 | $is_parent_superior = $this->authUtils()->hasRoleEditPermission($parent_role_id); |
68 | $is_parent_owner = $parent_role && $this->entityUtils()->canUpdateOlzEntity($parent_role, null, 'roles'); |
69 | if ($is_parent_superior || $is_parent_owner) { |
70 | $this->updateEntityWithData($entity, $input['data']); |
71 | } else { |
72 | $this->updateEntityWithNonParentData($entity, $input['data']); |
73 | } |
74 | |
75 | // TODO Do this more elegantly? |
76 | $new_data = $this->getEntityData($entity); |
77 | $this->log()->notice('NEW:', [$new_data]); |
78 | |
79 | $this->entityManager()->persist($entity); |
80 | $this->entityManager()->flush(); |
81 | $this->persistUploads($entity, $input['data']); |
82 | |
83 | return [ |
84 | 'id' => $entity->getId() ?? 0, |
85 | ]; |
86 | } |
87 | } |