|
1 |
| -<?php |
| 1 | +<?php declare(strict_types=1); |
2 | 2 | /**
|
3 | 3 | * @link https://github.com/monarc-project for the canonical source repository
|
4 |
| - * @copyright Copyright (c) 2016-2022 SMILE GIE Securitymadein.lu - Licensed under GNU Affero GPL v3 |
| 4 | + * @copyright Copyright (c) 2016-2024 Luxembourg House of Cybersecurity LHC.lu - Licensed under GNU Affero GPL v3 |
5 | 5 | * @license MONARC is licensed under GNU Affero General Public License version 3
|
6 | 6 | */
|
7 | 7 |
|
8 | 8 | namespace Monarc\FrontOffice;
|
9 | 9 |
|
10 |
| -use DateTime; |
11 | 10 | use Laminas\Stdlib\ResponseInterface;
|
12 |
| -use Monarc\Core\Model\Entity\AnrSuperClass; |
13 | 11 | use Monarc\Core\Service\ConnectedUserService;
|
14 |
| -use Monarc\FrontOffice\CronTask\Service\CronTaskService; |
15 |
| -use Monarc\FrontOffice\Model\Entity\Anr; |
16 |
| -use Monarc\FrontOffice\Model\Entity\CronTask; |
17 |
| -use Monarc\FrontOffice\Model\Entity\Snapshot; |
18 |
| -use Monarc\FrontOffice\Model\Table\InstanceTable; |
19 |
| -use Monarc\FrontOffice\Model\Table\SnapshotTable; |
20 |
| -use Monarc\FrontOffice\Table\AnrTable; |
21 |
| -use Monarc\FrontOffice\Table\UserAnrTable; |
22 | 12 | use Laminas\Http\Request;
|
23 | 13 | use Laminas\Mvc\ModuleRouteListener;
|
24 | 14 | use Laminas\Mvc\MvcEvent;
|
@@ -166,164 +156,14 @@ public function checkRbac(MvcEvent $mvcEvent)
|
166 | 156 | $roles = $connectedUser->getRoles();
|
167 | 157 | }
|
168 | 158 |
|
169 |
| - $isGranted = false; |
170 |
| - /** @var SnapshotTable $snapshotTable */ |
171 |
| - $snapshotTable = $serviceManager->get(SnapshotTable::class); |
172 |
| - /** @var UserAnrTable $userAnrTable */ |
173 |
| - $userAnrTable = $serviceManager->get(UserAnrTable::class); |
174 | 159 | foreach ($roles as $role) {
|
175 | 160 | if ($mvcEvent->getViewModel()->rbac->isGranted($role, $route)) {
|
176 |
| - $anrId = (int)$mvcEvent->getRouteMatch()->getParam('id'); |
177 |
| - if (($route === 'monarc_api_client_anr' && $anrId !== 0) |
178 |
| - || strncmp($route, 'monarc_api_global_client_anr/', 29) === 0 |
179 |
| - ) { |
180 |
| - if ($route !== 'monarc_api_client_anr') { |
181 |
| - $anrId = (int)$mvcEvent->getRouteMatch()->getParam('anrid'); |
182 |
| - } |
183 |
| - if ($anrId === 0) { |
184 |
| - break; |
185 |
| - } |
186 |
| - |
187 |
| - $result = $this->validateAnrStatusAndGetResponseIfInvalid($anrId, $mvcEvent, $route); |
188 |
| - if ($result !== null) { |
189 |
| - return $result; |
190 |
| - } |
191 |
| - |
192 |
| - $userAnr = $userAnrTable->findByAnrIdAndUser($anrId, $connectedUser); |
193 |
| - if ($userAnr === null) { |
194 |
| - // We authorise the access for snapshot, but for read only (GET). |
195 |
| - if ($mvcEvent->getRequest()->getMethod() !== 'GET' |
196 |
| - && !$this->authorizedPost($route, $mvcEvent->getRequest()->getMethod()) |
197 |
| - ) { |
198 |
| - break; |
199 |
| - } |
200 |
| - /** @var Snapshot|false $snapshot */ |
201 |
| - $snapshot = current($snapshotTable->getEntityByFields(['anr' => $anrId])); |
202 |
| - if ($snapshot === false) { |
203 |
| - break; |
204 |
| - } |
205 |
| - $userAnr = $userAnrTable->findByAnrAndUser($snapshot->getAnrReference(), $connectedUser); |
206 |
| - if ($userAnr === null) { |
207 |
| - // the user did not have access to the anr, from which this snapshot was created. |
208 |
| - break; |
209 |
| - } |
210 |
| - $isGranted = true; |
211 |
| - break; |
212 |
| - } |
213 |
| - |
214 |
| - if (!$userAnr->hasWriteAccess() && $mvcEvent->getRequest()->getMethod() !== 'GET') { |
215 |
| - // We authorize POST for the specific actions. |
216 |
| - if ($this->authorizedPost($route, $mvcEvent->getRequest()->getMethod())) { |
217 |
| - $isGranted = true; |
218 |
| - } |
219 |
| - break; |
220 |
| - } |
221 |
| - } |
222 |
| - |
223 |
| - $isGranted = true; |
224 |
| - break; |
225 |
| - } |
226 |
| - } |
227 |
| - |
228 |
| - if (!$isGranted) { |
229 |
| - $response = $mvcEvent->getResponse(); |
230 |
| - $response->setStatusCode($connectedUser === null ? 401 : 403); |
231 |
| - |
232 |
| - return $response; |
233 |
| - } |
234 |
| - } |
235 |
| - |
236 |
| - private function authorizedPost($route, $method) |
237 |
| - { |
238 |
| - return $method === 'POST' |
239 |
| - && ($route === 'monarc_api_global_client_anr/export' // export ANR |
240 |
| - || $route === 'monarc_api_global_client_anr/instance_export' // export Instance |
241 |
| - || $route === 'monarc_api_global_client_anr/objects_export' // export Object |
242 |
| - || $route === 'monarc_api_global_client_anr/deliverable' // generate a report |
243 |
| - ); |
244 |
| - } |
245 |
| - |
246 |
| - /** |
247 |
| - * Validates the anr status for NON GET method requests exclude DELETE (cancellation of background import). |
248 |
| - */ |
249 |
| - private function validateAnrStatusAndGetResponseIfInvalid( |
250 |
| - int $anrId, |
251 |
| - MvcEvent $mvcEvent, |
252 |
| - string $route |
253 |
| - ): ?ResponseInterface { |
254 |
| - /* GET requests are always allowed and cancellation of import (delete import process -> PID). */ |
255 |
| - if ($mvcEvent->getRequest()->getMethod() === Request::METHOD_GET |
256 |
| - || ( |
257 |
| - $mvcEvent->getRequest()->getMethod() === Request::METHOD_DELETE |
258 |
| - && $route === 'monarc_api_global_client_anr/instance_import' |
259 |
| - ) |
260 |
| - ) { |
261 |
| - return null; |
262 |
| - } |
263 |
| - |
264 |
| - $serviceManager = $mvcEvent->getApplication()->getServiceManager(); |
265 |
| - |
266 |
| - /** @var Anr $anr */ |
267 |
| - $anr = $serviceManager->get(AnrTable::class)->findById($anrId); |
268 |
| - if ($anr->isActive()) { |
269 |
| - return null; |
270 |
| - } |
271 |
| - |
272 |
| - /* Allow deleting anr if the status is waiting for import or there is an import error. */ |
273 |
| - if ($route === 'monarc_api_client_anr' |
274 |
| - && $mvcEvent->getRequest()->getMethod() === Request::METHOD_DELETE |
275 |
| - && ($anr->getStatus() === AnrSuperClass::STATUS_IMPORT_ERROR |
276 |
| - || $anr->getStatus() === AnrSuperClass::STATUS_AWAITING_OF_IMPORT |
277 |
| - ) |
278 |
| - ) { |
279 |
| - return null; |
280 |
| - } |
281 |
| - |
282 |
| - /* Allow to restore a snapshot if there is an import error. */ |
283 |
| - if ($route === 'monarc_api_global_client_anr/snapshot_restore' |
284 |
| - && $anr->getStatus() === AnrSuperClass::STATUS_IMPORT_ERROR |
285 |
| - && $mvcEvent->getRequest()->getMethod() === Request::METHOD_POST |
286 |
| - ) { |
287 |
| - return null; |
288 |
| - } |
289 |
| - |
290 |
| - $result = [ |
291 |
| - 'status' => $anr->getStatusName(), |
292 |
| - 'importStatus' => [], |
293 |
| - ]; |
294 |
| - /** @var CronTaskService $cronTaskService */ |
295 |
| - $cronTaskService = $serviceManager->get(CronTaskService::class); |
296 |
| - |
297 |
| - if ($anr->getStatus() === AnrSuperClass::STATUS_UNDER_IMPORT) { |
298 |
| - $importCronTask = $cronTaskService->getLatestTaskByNameWithParam( |
299 |
| - CronTask::NAME_INSTANCE_IMPORT, |
300 |
| - ['anrId' => $anrId] |
301 |
| - ); |
302 |
| - if ($importCronTask !== null && $importCronTask->getStatus() === CronTask::STATUS_IN_PROGRESS) { |
303 |
| - /** @var InstanceTable $instanceTable */ |
304 |
| - $instanceTable = $serviceManager->get(InstanceTable::class); |
305 |
| - $timeDiff = $importCronTask->getUpdatedAt()->diff(new DateTime()); |
306 |
| - $instancesNumber = $instanceTable->countByAnrIdFromDate($anrId, $importCronTask->getUpdatedAt()); |
307 |
| - $result['importStatus'] = [ |
308 |
| - 'executionTime' => $timeDiff->h . ' hours ' . $timeDiff->i . ' min ' . $timeDiff->s . ' sec', |
309 |
| - 'createdInstances' => $instancesNumber, |
310 |
| - ]; |
311 |
| - } |
312 |
| - } elseif ($anr->getStatus() === AnrSuperClass::STATUS_IMPORT_ERROR) { |
313 |
| - $importCronTask = $cronTaskService->getLatestTaskByNameWithParam( |
314 |
| - CronTask::NAME_INSTANCE_IMPORT, |
315 |
| - ['anrId' => $anrId] |
316 |
| - ); |
317 |
| - if ($importCronTask !== null && $importCronTask->getStatus() === CronTask::STATUS_FAILURE) { |
318 |
| - $result['importStatus'] = [ |
319 |
| - 'errorMessage' => $importCronTask->getResultMessage(), |
320 |
| - ]; |
| 161 | + return; |
321 | 162 | }
|
322 | 163 | }
|
323 | 164 |
|
324 | 165 | $response = $mvcEvent->getResponse();
|
325 |
| - $response->setContent(json_encode($result, JSON_THROW_ON_ERROR)); |
326 |
| - $response->setStatusCode(409); |
| 166 | + $response->setStatusCode($connectedUser === null ? 401 : 403); |
327 | 167 |
|
328 | 168 | return $response;
|
329 | 169 | }
|
|
0 commit comments