Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
SwitchUserEndpoint
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
7
100.00% covered (success)
100.00%
1 / 1
 handle
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
7
1<?php
2
3namespace Olz\Api\Endpoints;
4
5use Olz\Api\OlzTypedEndpoint;
6use Olz\Entity\Users\User;
7use PhpTypeScriptApi\HttpError;
8
9/**
10 * @extends OlzTypedEndpoint<
11 *   array{
12 *     userId: int<1, max>,
13 *   },
14 *   array{
15 *     status: 'OK',
16 *   }
17 * >
18 */
19class 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        $root = $user->getRoot() !== '' ? $user->getRoot() : './';
35        $this->session()->set('auth', $user->getPermissions());
36        $this->session()->set('root', $root);
37        $this->session()->set('user', $user->getUsername());
38        $this->session()->set('user_id', "{$user->getId()}");
39        return [
40            'status' => 'OK',
41        ];
42    }
43}