Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 42 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
UrlParameterSigning | |
0.00% |
0 / 42 |
|
0.00% |
0 / 4 |
650 | |
0.00% |
0 / 1 |
readResponse | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
72 | |||
testData | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
12 | |||
testScopeList | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
56 | |||
testClusterList | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
56 |
1 | <?php |
2 | |
3 | /** |
4 | * @copyright BerlinOnline Stadtportal GmbH & Co. KG |
5 | **/ |
6 | |
7 | declare(strict_types=1); |
8 | |
9 | namespace BO\Zmsadmin; |
10 | |
11 | use BO\Mellon\Validator; |
12 | use BO\Slim\Helper; |
13 | use BO\Slim\Render; |
14 | use BO\Zmsadmin\Exception\BadRequest; |
15 | use BO\Zmsadmin\Exception\NotAllowed; |
16 | use BO\Zmsentities\Collection\DepartmentList; |
17 | use BO\Zmsentities\Department; |
18 | use BO\Zmsentities\Exception\UserAccountAccessRightsFailed; |
19 | use BO\Zmsentities\Helper\Property; |
20 | use Psr\Http\Message\RequestInterface; |
21 | use Psr\Http\Message\ResponseInterface; |
22 | use Slim\Http\Request as SlimRequest; |
23 | |
24 | /** |
25 | * returning Signatures for signing requests |
26 | */ |
27 | class UrlParameterSigning extends BaseController |
28 | { |
29 | /** |
30 | * @SuppressWarnings(UnusedFormalParameter) |
31 | * @param SlimRequest $request |
32 | * @return String |
33 | */ |
34 | public function readResponse( |
35 | RequestInterface $request, |
36 | ResponseInterface $response, |
37 | array $args |
38 | ) { |
39 | $validator = $request->getAttribute('validator'); |
40 | $data = $validator->getInput()->isJson()->assertValid()->getValue(); |
41 | $this->testData($data); |
42 | |
43 | $workstation = \App::$http->readGetResult('/workstation/', ['resolveReferences' => 0])->getEntity(); |
44 | $collections = isset($data['parameters']['collections']) ? $data['parameters']['collections'] : []; |
45 | |
46 | $hasScopeList = (isset($collections['scopelist']) && strlen($collections['scopelist']) > 0); |
47 | $hasClusterList = (isset($collections['clusterlist']) && strlen($collections['clusterlist']) > 0); |
48 | $hasValidScopeId = ( |
49 | isset($workstation['scope']['id']) && |
50 | !Validator::value($workstation['scope']['id'])->isNumber()->hasFailed() |
51 | ); |
52 | |
53 | if (($hasScopeList || $hasClusterList) && $hasValidScopeId) { |
54 | $organisation = \App::$http->readGetResult( |
55 | '/scope/' . $workstation['scope']['id'] . '/organisation/', |
56 | ['resolveReferences' => 3] |
57 | )->getEntity(); |
58 | |
59 | $this->testScopeList($organisation, $collections); |
60 | $this->testClusterList($organisation, $collections); |
61 | } |
62 | |
63 | $data['hmac'] = Helper::hashQueryParameters($data['section'], $data['parameters'], ['collections', 'queue']); |
64 | return Render::withJson($response, $data); |
65 | } |
66 | |
67 | private function testData($data) |
68 | { |
69 | if (!isset($data['section']) || !isset($data['parameters'])) { |
70 | throw new BadRequest(); |
71 | } |
72 | } |
73 | |
74 | private function testScopeList($organisation, $collections) |
75 | { |
76 | $scopeIds = []; |
77 | foreach ($organisation->departments as $departmentData) { |
78 | $department = (new Department($departmentData))->withCompleteScopeList(); |
79 | if (Property::__keyExists('scopes', $department)) { |
80 | /** @var \BO\Zmsentities\Scope $scope */ |
81 | foreach ($department['scopes'] as $scope) { |
82 | $scopeIds[$scope['id']] = $scope['id']; |
83 | } |
84 | } |
85 | } |
86 | if (isset($collections['scopelist']) && strlen($collections['scopelist']) > 0) { |
87 | $requestedIds = explode(',', $collections['scopelist']); |
88 | if (count(array_diff($requestedIds, $scopeIds)) > 0) { |
89 | throw new UserAccountAccessRightsFailed(); |
90 | } |
91 | } |
92 | } |
93 | |
94 | private function testClusterList($organisation, $collections) |
95 | { |
96 | $clusterIds = []; |
97 | foreach ($organisation->departments as $departmentData) { |
98 | $department = (new Department($departmentData))->withCompleteScopeList(); |
99 | if (Property::__keyExists('clusters', $department)) { |
100 | /** @var \BO\Zmsentities\Cluster $scope */ |
101 | foreach ($department['clusters'] as $cluster) { |
102 | $clusterIds[$cluster['id']] = $cluster['id']; |
103 | } |
104 | } |
105 | } |
106 | if (isset($collections['clusterlist']) && strlen($collections['clusterlist']) > 0) { |
107 | $requestedIds = explode(',', $collections['clusterlist']); |
108 | if (count(array_diff($requestedIds, $clusterIds)) > 0) { |
109 | throw new UserAccountAccessRightsFailed(); |
110 | } |
111 | } |
112 | } |
113 | } |