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