Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.88% covered (success)
96.88%
31 / 32
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
GetRoleInfoEndpoint
96.88% covered (success)
96.88%
31 / 32
0.00% covered (danger)
0.00%
0 / 1
14
0.00% covered (danger)
0.00%
0 / 1
 handle
96.88% covered (success)
96.88%
31 / 32
0.00% covered (danger)
0.00%
0 / 1
14
1<?php
2
3namespace Olz\Roles\Endpoints;
4
5use Olz\Api\OlzTypedEndpoint;
6use Olz\Entity\Roles\Role;
7use 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 *   email?: ?array<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 */
28class GetRoleInfoEndpoint extends OlzTypedEndpoint {
29    protected function handle(mixed $input): mixed {
30        $has_access = $this->authUtils()->hasPermission('any');
31        $token = $input['captchaToken'] ?? null;
32        $is_valid_token = $token ? $this->captchaUtils()->validateToken($token) : false;
33        if (!$has_access && !$is_valid_token) {
34            throw new HttpError(400, 'Bot-Prüfung nicht bestanden!');
35        }
36
37        $id = $input['id'];
38        $repo = $this->entityManager()->getRepository(Role::class);
39        $role = $repo->findOneBy(['id' => $id]);
40        if (!$role) {
41            throw new HttpError(404, "Nicht gefunden.");
42        }
43
44        $host = $this->envUtils()->getEmailForwardingHost();
45        $assignees = $role->getUsers();
46        $assignee_infos = [];
47        foreach ($assignees as $assignee) {
48            $has_official_email = $this->authUtils()->hasPermission('user_email', $assignee);
49            $email = $has_official_email
50                ? "{$assignee->getUsername()}@{$host}"
51                : ($assignee->getEmail() ? $assignee->getEmail() : null);
52
53            $assignee_infos[] = [
54                'firstName' => $assignee->getFirstName() ?: '-',
55                'lastName' => $assignee->getLastName() ?: '-',
56                'email' => $this->emailUtils()->obfuscateEmail($email),
57                'avatarImageId' => $this->authUtils()->getUserAvatar($assignee),
58            ];
59        }
60
61        $has_role_email = $this->authUtils()->hasRolePermission('role_email', $role);
62        $role_email = $has_role_email ? "{$role->getUsername()}@{$host}" : null;
63        return [
64            'name' => $role->getName() ? (html_entity_decode($role->getName()) ?: null) : null,
65            'username' => $role->getUsername() ? $role->getUsername() : null,
66            'email' => $this->emailUtils()->obfuscateEmail($role_email),
67            'assignees' => $assignee_infos,
68        ];
69    }
70}