Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
96.55% |
28 / 29 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
GetRoleInfoEndpoint | |
96.55% |
28 / 29 |
|
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 1 |
handle | |
96.55% |
28 / 29 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | |
3 | namespace Olz\Roles\Endpoints; |
4 | |
5 | use Olz\Api\OlzTypedEndpoint; |
6 | use Olz\Entity\Roles\Role; |
7 | use PhpTypeScriptApi\HttpError; |
8 | |
9 | /** |
10 | * @phpstan-type OlzRoleId int |
11 | * @phpstan-type OlzRoleInfoData array{ |
12 | * name?: ?non-empty-string, |
13 | * username?: ?non-empty-string, |
14 | * assignees: array<array{ |
15 | * firstName: non-empty-string, |
16 | * lastName: non-empty-string, |
17 | * email?: ?array<non-empty-string>, |
18 | * avatarImageId?: array<string, string>, |
19 | * }> |
20 | * } |
21 | * |
22 | * @extends OlzTypedEndpoint< |
23 | * array{id: OlzRoleId, captchaToken?: ?non-empty-string}, |
24 | * OlzRoleInfoData |
25 | * > |
26 | */ |
27 | class GetRoleInfoEndpoint extends OlzTypedEndpoint { |
28 | protected function handle(mixed $input): mixed { |
29 | $has_access = $this->authUtils()->hasPermission('any'); |
30 | $token = $input['captchaToken'] ?? null; |
31 | $is_valid_token = $token ? $this->captchaUtils()->validateToken($token) : false; |
32 | if (!$has_access && !$is_valid_token) { |
33 | throw new HttpError(403, 'Captcha token invalid'); |
34 | } |
35 | |
36 | $id = $input['id']; |
37 | $repo = $this->entityManager()->getRepository(Role::class); |
38 | $role = $repo->findOneBy(['id' => $id]); |
39 | if (!$role) { |
40 | throw new HttpError(404, "Nicht gefunden."); |
41 | } |
42 | |
43 | $assignees = $role->getUsers(); |
44 | $assignee_infos = []; |
45 | foreach ($assignees as $assignee) { |
46 | $has_official_email = $this->authUtils()->hasPermission('user_email', $assignee); |
47 | $host = $this->envUtils()->getEmailForwardingHost(); |
48 | $email = $has_official_email |
49 | ? "{$assignee->getUsername()}@{$host}" |
50 | : ($assignee->getEmail() ? $assignee->getEmail() : null); |
51 | |
52 | $assignee_infos[] = [ |
53 | 'firstName' => $assignee->getFirstName() ?: '-', |
54 | 'lastName' => $assignee->getLastName() ?: '-', |
55 | 'email' => $this->emailUtils()->obfuscateEmail($email), |
56 | 'avatarImageId' => $this->authUtils()->getUserAvatar($assignee), |
57 | ]; |
58 | } |
59 | |
60 | return [ |
61 | 'name' => $role->getName() ? $role->getName() : null, |
62 | 'username' => $role->getUsername() ? $role->getUsername() : null, |
63 | 'assignees' => $assignee_infos, |
64 | ]; |
65 | } |
66 | } |