Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| SwitchUserEndpoint | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
42 | |
0.00% |
0 / 1 |
| handle | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
42 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Olz\Api\Endpoints; |
| 4 | |
| 5 | use Olz\Api\OlzTypedEndpoint; |
| 6 | use Olz\Entity\Users\User; |
| 7 | use PhpTypeScriptApi\HttpError; |
| 8 | |
| 9 | /** |
| 10 | * @extends OlzTypedEndpoint< |
| 11 | * array{ |
| 12 | * userId: int<1, max>, |
| 13 | * }, |
| 14 | * array{ |
| 15 | * status: 'OK', |
| 16 | * } |
| 17 | * > |
| 18 | */ |
| 19 | class SwitchUserEndpoint extends OlzTypedEndpoint { |
| 20 | protected function handle(mixed $input): mixed { |
| 21 | $user_repo = $this->entityManager()->getRepository(User::class); |
| 22 | $user = $user_repo->findOneBy(['id' => $input['userId']]); |
| 23 | if (!$user) { |
| 24 | throw new HttpError(403, "Kein Zugriff!"); |
| 25 | } |
| 26 | |
| 27 | $auth_user_id = $this->session()->get('auth_user_id'); |
| 28 | $is_parent = $auth_user_id && intval($user->getParentUserId()) === intval($auth_user_id); |
| 29 | $is_self = $auth_user_id && intval($user->getId()) === intval($auth_user_id); |
| 30 | if (!$is_self && !$is_parent) { |
| 31 | throw new HttpError(403, "Kein Zugriff!"); |
| 32 | } |
| 33 | |
| 34 | $this->authUtils()->setSessionUser($user); |
| 35 | |
| 36 | return [ |
| 37 | 'status' => 'OK', |
| 38 | ]; |
| 39 | } |
| 40 | } |