Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 80 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
MonitorController | |
0.00% |
0 / 80 |
|
0.00% |
0 / 6 |
342 | |
0.00% |
0 / 1 |
getGroup | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
getTestSessions | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
20 | |||
putCommand | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
20 | |||
postUnlock | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
6 | |||
postLock | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
6 | |||
getProfile | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | |
3 | /** @noinspection PhpUnhandledExceptionInspection */ |
4 | declare(strict_types=1); |
5 | |
6 | // TODO unit tests ! |
7 | |
8 | use Slim\Exception\HttpForbiddenException; |
9 | use Slim\Exception\HttpNotFoundException; |
10 | use Slim\Http\Response; |
11 | use Slim\Http\ServerRequest as Request; |
12 | |
13 | class MonitorController extends Controller { |
14 | /** |
15 | * @deprecated |
16 | */ |
17 | public static function getGroup(Request $request, Response $response): Response { |
18 | /* @var $authToken AuthToken */ |
19 | $authToken = $request->getAttribute('AuthToken'); |
20 | $groupName = $request->getAttribute('group_name'); |
21 | |
22 | $group = self::adminDAO()->getGroup($groupName); |
23 | |
24 | if (!$group) { |
25 | throw new HttpNotFoundException($request, "Group `$groupName` not found."); |
26 | } |
27 | |
28 | // currently a group-monitor can always only monitor it's own group |
29 | if ($groupName !== $authToken->getGroup()) { |
30 | throw new HttpForbiddenException($request, "Group `$groupName` not allowed."); |
31 | } |
32 | |
33 | return $response |
34 | ->withHeader("Deprecation", "true") |
35 | ->withJson($group); |
36 | } |
37 | |
38 | public static function getTestSessions(Request $request, Response $response): Response { |
39 | /* @var $authToken AuthToken */ |
40 | $authToken = $request->getAttribute('AuthToken'); |
41 | $groupName = $request->getAttribute('group_name'); |
42 | $groupNames = $groupName ? [$groupName] : array_keys($request->getAttribute('groups')); |
43 | |
44 | $sessionChangeMessages = self::adminDAO()->getTestSessions($authToken->getWorkspaceId(), $groupNames); |
45 | |
46 | $bsUrl = BroadcastService::registerChannel('monitor', ["groups" => $groupNames]); |
47 | |
48 | if ($bsUrl !== null) { |
49 | foreach ($sessionChangeMessages as $sessionChangeMessage) { |
50 | BroadcastService::sessionChange($sessionChangeMessage); |
51 | } |
52 | |
53 | $response = $response->withHeader('SubscribeURI', $bsUrl); |
54 | } |
55 | |
56 | return $response->withJson($sessionChangeMessages->asArray()); |
57 | } |
58 | |
59 | public static function putCommand(Request $request, Response $response): Response { |
60 | /* @var $authToken AuthToken */ |
61 | $authToken = $request->getAttribute('AuthToken'); |
62 | $personId = $authToken->getId(); |
63 | |
64 | $body = RequestBodyParser::getElementsFromRequest($request, [ |
65 | 'keyword' => 'REQUIRED', |
66 | 'arguments' => [], |
67 | 'timestamp' => 'REQUIRED', |
68 | 'testIds' => [] |
69 | ]); |
70 | |
71 | $command = new Command(-1, $body['keyword'], (int) $body['timestamp'], ...$body['arguments']); |
72 | |
73 | foreach (array_unique($body['testIds']) as $testId) { |
74 | if (!self::adminDAO()->getTest($testId)) { |
75 | throw new HttpNotFoundException( |
76 | $request, "Test `$testId` not found. `{$command->getKeyword()}` not committed." |
77 | ); |
78 | } |
79 | } |
80 | |
81 | foreach ($body['testIds'] as $testId) { |
82 | $commandId = self::adminDAO()->storeCommand($personId, (int) $testId, $command); |
83 | $command->setId($commandId); |
84 | } |
85 | |
86 | BroadcastService::send('command', json_encode([ |
87 | 'command' => $command, |
88 | 'testIds' => $body['testIds'] |
89 | ])); |
90 | |
91 | return $response->withStatus(201); |
92 | } |
93 | |
94 | public static function postUnlock(Request $request, Response $response): Response { |
95 | $groupName = $request->getAttribute('group_name'); |
96 | $testIds = RequestBodyParser::getElementWithDefault($request, 'testIds', []); |
97 | |
98 | foreach ($testIds as $testId) { |
99 | // TODO check if test is in group |
100 | self::testDAO()->changeTestLockStatus((int) $testId); |
101 | |
102 | $testSession = self::testDAO()->getTestSession($testId); |
103 | BroadcastService::sessionChange( |
104 | SessionChangeMessage::testState( |
105 | $groupName, |
106 | (int) $testSession['person_id'], |
107 | $testId, |
108 | $testSession['laststate'] |
109 | ) |
110 | ); |
111 | } |
112 | |
113 | return $response->withStatus(200); |
114 | } |
115 | |
116 | public static function postLock(Request $request, Response $response): Response { |
117 | /* @var $authToken AuthToken */ |
118 | $authToken = $request->getAttribute('AuthToken'); |
119 | |
120 | $groupName = $request->getAttribute('group_name'); |
121 | $testIds = RequestBodyParser::getElementWithDefault($request, 'testIds', []); |
122 | |
123 | foreach ($testIds as $testId) { |
124 | // TODO check if test is in group |
125 | self::testDAO()->changeTestLockStatus((int) $testId, false); |
126 | |
127 | $testSession = self::testDAO()->getTestSession($testId); |
128 | self::testDAO()->addTestLog($testId, 'locked by monitor', 0, (string) $authToken->getId()); |
129 | BroadcastService::sessionChange( |
130 | SessionChangeMessage::testState( |
131 | $groupName, |
132 | (int) $testSession['person_id'], |
133 | $testId, |
134 | $testSession['laststate'] |
135 | ) |
136 | ); |
137 | } |
138 | |
139 | return $response->withStatus(200); |
140 | } |
141 | |
142 | public static function getProfile(Request $request, Response $response): Response { |
143 | $authToken = $request->getAttribute('AuthToken'); |
144 | /** @var $authToken AuthToken */ |
145 | |
146 | $profileId = $groupName = $request->getAttribute('profile_id'); |
147 | $session = self::sessionDAO()->getPersonSessionByToken($authToken->getToken()); |
148 | $profiles = $session->getLoginSession()->getLogin()->getProfiles(); |
149 | foreach ($profiles as $profile) { |
150 | if ($profile['id'] == $profileId) { |
151 | return $response->withJson((object) $profile); |
152 | } |
153 | } |
154 | throw new HttpNotFoundException($request, "Profile not found `$profileId`"); |
155 | } |
156 | } |