Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 14 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
IsWorkspacePermitted | |
0.00% |
0 / 14 |
|
0.00% |
0 / 2 |
56 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
__invoke | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
42 |
1 | <?php |
2 | /** @noinspection PhpUnhandledExceptionInspection */ |
3 | declare(strict_types=1); |
4 | // TODO unit test |
5 | |
6 | use Psr\Http\Message\ResponseInterface; |
7 | use Slim\Exception\HttpForbiddenException; |
8 | use Slim\Exception\HttpNotFoundException; |
9 | use Slim\Http\ServerRequest as Request; |
10 | use Psr\Http\Server\RequestHandlerInterface as RequestHandler; |
11 | use Slim\Routing\RouteContext; |
12 | |
13 | class IsWorkspacePermitted { |
14 | private string $_necessaryRole; |
15 | |
16 | function __construct(string $necessaryRole = '') { |
17 | $this->_necessaryRole = $necessaryRole; |
18 | } |
19 | |
20 | function __invoke(Request $request, RequestHandler $handler): ResponseInterface { |
21 | $routeContext = RouteContext::fromRequest($request); |
22 | $route = $routeContext->getRoute(); |
23 | $params = $route->getArguments(); |
24 | |
25 | if (!isset($params['ws_id']) or ((int) $params['ws_id'] < 1)) { |
26 | throw new HttpNotFoundException($request, "No valid workspace: `{$params['ws_id']}`"); |
27 | } |
28 | |
29 | /* @var $authToken AuthToken */ |
30 | $authToken = $request->getAttribute('AuthToken'); |
31 | |
32 | $adminDAO = new AdminDAO(); |
33 | |
34 | if (!$adminDAO->hasAdminAccessToWorkspace($authToken->getToken(), (int) $params['ws_id'])) { |
35 | throw new HttpNotFoundException($request, "Workspace `{$params['ws_id']}` not found."); |
36 | } |
37 | |
38 | $userRoleOnWorkspace = $adminDAO->getWorkspaceRole($authToken->getToken(), (int) $params['ws_id']); |
39 | |
40 | if ($this->_necessaryRole and (!in_array($this->_necessaryRole, Mode::withChildren($userRoleOnWorkspace)))) { |
41 | throw new HttpForbiddenException($request, "Access Denied: Role `{$this->_necessaryRole}` on workspace `ws_{$params['ws_id']}`, needed. Only `{$userRoleOnWorkspace}` provided."); |
42 | } |
43 | |
44 | return $handler->handle($request); |
45 | |
46 | } |
47 | } |