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