Skip to content

Commit

Permalink
Added multiple import processors, refactored soa functionality and im…
Browse files Browse the repository at this point in the history
…proved many other things.
  • Loading branch information
ruslanbaidan committed Jul 4, 2024
1 parent b8d07ea commit 727e1c8
Show file tree
Hide file tree
Showing 60 changed files with 3,248 additions and 2,902 deletions.
27 changes: 13 additions & 14 deletions config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -1104,7 +1104,7 @@
'instance_metadata' => [
'type' => 'segment',
'options' => [
'route' => 'instances/:instanceid/instances-metadata[/:id]',
'route' => 'instances/:instanceid/metadata[/:id]',
'constraints' => [
'instanceid' => '[0-9]+',
'id' => '[0-9]+',
Expand Down Expand Up @@ -1404,23 +1404,23 @@
Controller\ApiAnrInstancesRisksController::class => AutowireFactory::class,
Controller\ApiAnrInstancesRisksOpController::class => AutowireFactory::class,
Controller\ApiSnapshotController::class => AutowireFactory::class,
Import\Controller\ApiAnrInstancesImportController::class => AutowireFactory::class,
Export\Controller\ApiAnrExportController::class => AutowireFactory::class,
Export\Controller\ApiAnrInstancesExportController::class => AutowireFactory::class,
Controller\ApiAnrObjectsCategoriesController::class => AutowireFactory::class,
Export\Controller\ApiAnrObjectsExportController::class => AutowireFactory::class,
Import\Controller\ApiAnrObjectsImportController::class => AutowireFactory::class,
Controller\ApiAnrDeliverableController::class => AutowireFactory::class,
Controller\ApiAnrInstancesConsequencesController::class => AutowireFactory::class,
Controller\ApiModelVerifyLanguageController::class => AutowireFactory::class,
Stats\Controller\StatsController::class => AutowireFactory::class,
Stats\Controller\StatsAnrsSettingsController::class => AutowireFactory::class,
Stats\Controller\StatsGeneralSettingsController::class => AutowireFactory::class,
Controller\ApiOperationalRisksScalesController::class => AutowireFactory::class,
Controller\ApiOperationalRisksScalesCommentsController::class => AutowireFactory::class,
Controller\ApiAnrInstancesMetadataFieldsController::class => AutowireFactory::class,
Controller\ApiInstanceMetadataController::class => AutowireFactory::class,
Controller\ApiSoaScaleCommentController::class => AutowireFactory::class,
Export\Controller\ApiAnrExportController::class => AutowireFactory::class,
Export\Controller\ApiAnrInstancesExportController::class => AutowireFactory::class,
Export\Controller\ApiAnrObjectsExportController::class => AutowireFactory::class,
Import\Controller\ApiAnrInstancesImportController::class => AutowireFactory::class,
Import\Controller\ApiAnrObjectsImportController::class => AutowireFactory::class,
Stats\Controller\StatsController::class => AutowireFactory::class,
Stats\Controller\StatsAnrsSettingsController::class => AutowireFactory::class,
Stats\Controller\StatsGeneralSettingsController::class => AutowireFactory::class,
],
],

Expand Down Expand Up @@ -1459,7 +1459,6 @@
DeprecatedTable\RecordProcessorTable::class => AutowireFactory::class,
DeprecatedTable\RecordRecipientTable::class => AutowireFactory::class,
DeprecatedTable\RecordTable::class => AutowireFactory::class,
DeprecatedTable\SoaTable::class => AutowireFactory::class,
DeprecatedTable\QuestionTable::class => AutowireFactory::class,
DeprecatedTable\QuestionChoiceTable::class => AutowireFactory::class,
Table\AnrTable::class => ClientEntityManagerFactory::class,
Expand Down Expand Up @@ -1493,6 +1492,7 @@
Table\RolfTagTable::class => ClientEntityManagerFactory::class,
Table\ReferentialTable::class => ClientEntityManagerFactory::class,
Table\SoaCategoryTable::class => ClientEntityManagerFactory::class,
Table\SoaTable::class => ClientEntityManagerFactory::class,
Table\SnapshotTable::class => ClientEntityManagerFactory::class,
Table\SoaScaleCommentTable::class => ClientEntityManagerFactory::class,
Table\ThemeTable::class => ClientEntityManagerFactory::class,
Expand All @@ -1513,7 +1513,6 @@
Entity\RecordProcessor::class => ModelFactory\RecordProcessorServiceModelEntity::class,
Entity\RecordRecipient::class => ModelFactory\RecordRecipientServiceModelEntity::class,
Entity\Record::class => ModelFactory\RecordServiceModelEntity::class,
Entity\Soa::class => ModelFactory\SoaServiceModelEntity::class,
Entity\Question::class => ModelFactory\QuestionServiceModelEntity::class,
Entity\QuestionChoice::class => ModelFactory\QuestionChoiceServiceModelEntity::class,

Expand All @@ -1527,7 +1526,6 @@
Service\AnrRecordProcessorService::class => Service\AnrRecordProcessorServiceFactory::class,
Service\AnrRecordRecipientService::class => Service\AnrRecordRecipientServiceFactory::class,
Service\AnrRecordService::class => Service\AnrRecordServiceFactory::class,
Service\SoaService::class => Service\SoaServiceFactory::class,
Service\AnrQuestionService::class => Service\AnrQuestionServiceFactory::class,
Service\AnrQuestionChoiceService::class => Service\AnrQuestionChoiceServiceFactory::class,
Service\AnrMeasureService::class => AutowireFactory::class,
Expand Down Expand Up @@ -1561,14 +1559,15 @@
Service\AnrInstanceRiskOpService::class => AutowireFactory::class,
Service\AnrInstanceRiskService::class => AutowireFactory::class,
Service\AnrInstanceService::class => AutowireFactory::class,
Stats\Service\StatsAnrService::class => ReflectionBasedAbstractFactory::class,
Stats\Service\StatsSettingsService::class => AutowireFactory::class,
Service\OperationalRiskScaleService::class => AutowireFactory::class,
Service\InstanceRiskOwnerService::class => AutowireFactory::class,
Service\OperationalRiskScaleCommentService::class => AutowireFactory::class,
Service\AnrInstanceMetadataFieldService::class => AutowireFactory::class,
Service\InstanceMetadataService::class => AutowireFactory::class,
Service\SoaService::class => AutowireFactory::class,
Service\SoaScaleCommentService::class => AutowireFactory::class,
Stats\Service\StatsAnrService::class => ReflectionBasedAbstractFactory::class,
Stats\Service\StatsSettingsService::class => AutowireFactory::class,
CronTask\Service\CronTaskService::class => AutowireFactory::class,
/* Export services. */
Export\Service\AnrExportService::class => AutowireFactory::class,
Expand Down
8 changes: 6 additions & 2 deletions migrations/db/20230901112005_fix_positions_cleanup_db.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,11 @@ public function change()

/* Clean up unused columns. */
$this->table('clients')->removeColumn('model_id')->update();
$this->table('instances')->removeColumn('disponibility')->update();
$this->table('instances')
->removeColumn('disponibility')
->removeColumn('asset_type')
->removeColumn('exportable')
->update();
$this->table('objects')
->removeColumn('disponibility')
->removeColumn('token_import')
Expand Down Expand Up @@ -222,7 +226,7 @@ public function change()
->dropForeignKey(['measure_uuid', 'anr_id'])
->update();
$this->table('soa')
->addColumn('measure_id', 'integer', ['signed' => false, 'after' => MysqlAdapter::FIRST])
->addColumn('measure_id', 'integer', ['signed' => false, 'after' => 'id'])
->update();
$this->execute('UPDATE soa s INNER JOIN measures m '
. 'ON s.measure_uuid = m.`uuid` AND s.anr_id = m.anr_id SET s.measure_id = m.id;');
Expand Down
20 changes: 0 additions & 20 deletions src/Controller/ApiAnrInstancesMetadataFieldsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,6 @@ public function __construct(AnrInstanceMetadataFieldService $anrInstanceMetadata
$this->anrInstanceMetadataFieldService = $anrInstanceMetadataFieldService;
}

public function getList()
{
/** @var Anr $anr */
$anr = $this->getRequest()->getAttribute('anr');

return $this->getPreparedJsonResponse([
'data' => $this->anrInstanceMetadataFieldService->getList($anr),
]);
}

public function get($id)
{
/** @var Anr $anr */
$anr = $this->getRequest()->getAttribute('anr');

return $this->getPreparedJsonResponse([
'data' => $this->anrInstanceMetadataFieldService->getAnrInstanceMetadataFieldData($anr, (int)$id),
]);
}

/**
* @param array $data
*/
Expand Down
7 changes: 0 additions & 7 deletions src/Controller/ApiAnrRecordsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,8 @@ public function getList()

public function get($id)
{
$anrId = (int)$this->params()->fromRoute('anrid');
$entity = $this->getService()->getEntity(['id' => $id]);


$this->formatDependencies($entity, $this->dependencies);

return $this->getPreparedJsonResponse($entity);
Expand Down Expand Up @@ -101,11 +99,6 @@ public function formatDependencies(&$entity, $dependencies, $entityDependency =
} else {
$entity[$dependency] = $entity[$dependency]->getJsonArray();
}
unset(
$entity[$dependency][$value]['__initializer__'],
$entity[$dependency][$value]['__cloner__'],
$entity[$dependency][$value]['__isInitialized__']
);
} elseif ($entity[$dependency] instanceof PersistentCollection) {
$entity[$dependency]->initialize();
if ($entity[$dependency]->count()) {
Expand Down
165 changes: 9 additions & 156 deletions src/Controller/ApiSoaController.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php declare(strict_types=1);
/**
* @link https://github.com/monarc-project for the canonical source repository
* @copyright Copyright (c) 2016-2023 Luxembourg House of Cybersecurity LHC.lu - Licensed under GNU Affero GPL v3
* @copyright Copyright (c) 2016-2024 Luxembourg House of Cybersecurity LHC.lu - Licensed under GNU Affero GPL v3
* @license MONARC is licensed under GNU Affero General Public License version 3
*/

Expand All @@ -10,165 +10,30 @@
use Monarc\Core\Controller\Handler\AbstractRestfulControllerRequestHandler;
use Monarc\Core\Controller\Handler\ControllerRequestResponseHandlerTrait;
use Monarc\FrontOffice\Entity\Anr;
use Monarc\FrontOffice\Entity\Measure;
use Monarc\FrontOffice\Entity\SoaScaleComment;
use Monarc\FrontOffice\Service\AnrInstanceRiskOpService;
use Monarc\FrontOffice\Service\AnrInstanceRiskService;
use Monarc\FrontOffice\Service\AnrMeasureService;
use Monarc\FrontOffice\Service\SoaScaleCommentService;
use Monarc\FrontOffice\InputFormatter\Soa\GetSoasInputFormatter;
use Monarc\FrontOffice\Service\SoaService;

class ApiSoaController extends AbstractRestfulControllerRequestHandler
{
use ControllerRequestResponseHandlerTrait;

public function __construct(
private SoaService $soaService,
private AnrMeasureService $anrMeasureService,
private AnrInstanceRiskService $anrInstanceRiskService,
private AnrInstanceRiskOpService $anrInstanceRiskOpService,
private SoaScaleCommentService $soaScaleCommentService
) {
public function __construct(private SoaService $soaService, private GetSoasInputFormatter $getSoasInputFormatter)
{
}

public function getList()
{
$page = (int)$this->params()->fromQuery('page', 1);
$limit = (int)$this->params()->fromQuery('limit', 0);
$order = $this->params()->fromQuery('order');
$filter = $this->params()->fromQuery('filter');
$category = (int)$this->params()->fromQuery('category', 0);
$referential = $this->params()->fromQuery('referential');

/** @var Anr $anr */
$anr = $this->getRequest()->getAttribute('anr');

$filterAnd = ['anr' => $anr->getId()];

if ($referential) {
if ($category !== 0) {
$filterMeasures['category'] = [
'op' => 'IN',
'value' => (array)$category,
];
} elseif ($category === -1) {
$filterMeasures['category'] = null;
}

$filterMeasures['r.anr'] = $anr->getId();
$filterMeasures['r.uuid'] = $referential;

$measuresFiltered = $this->anrMeasureService->getList(1, 0, null, null, $filterMeasures);
$measuresFilteredId = [];
foreach ($measuresFiltered as $key) {
$measuresFilteredId[] = $key['uuid'];
}
$filterAnd['m.uuid'] = [
'op' => 'IN',
'value' => $measuresFilteredId,
];
$filterAnd['m.anr'] = $anr->getId();
}

if ($order === 'measure') {
$order = 'm.code';
} elseif ($order === '-measure') {
$order = '-m.code';
}
$entities = $this->soaService->getList($page, $limit, $order, $filter, $filterAnd);
foreach ($entities as $key => $entity) {
$amvs = [];
$rolfRisks = [];

/** @var SoaScaleComment $soaScaleComment */
$soaScaleComment = $entity['soaScaleComment'];

/** @var Measure $measure */
$measure = $entity['measure'];
foreach ($measure->getAmvs() as $amv) {
$amvs[] = $amv->getUuid();
}
foreach ($measure->getRolfRisks() as $rolfRisk) {
$rolfRisks[] = $rolfRisk->getId();
}
$entity['measure']->rolfRisks = [];
if (!empty($rolfRisks)) {
$entity['measure']->rolfRisks = $this->anrInstanceRiskOpService->getOperationalRisks(
$measure->getAnr(),
null,
[
'rolfRisks' => $rolfRisks,
'limit' => -1,
'order' => 'cacheNetRisk',
'order_direction' => 'desc',
]
);
}
$entity['measure']->amvs = [];
if (!empty($amvs)) {
$entity['measure']->amvs = $this->anrInstanceRiskService->getInstanceRisks($measure->getAnr(), null, [
'amvs' => $amvs,
'limit' => -1,
'order' => 'maxRisk',
'order_direction' => 'desc',
]);
}
$entities[$key]['anr'] = [
'id' => $measure->getAnr()->getId(),
'label' => $measure->getAnr()->getLabel(),
];
$entities[$key]['measure'] = $measure->getJsonArray();
$entities[$key]['measure']['category'] = $measure->getCategory()->getJsonArray();
$entities[$key]['measure']['referential'] = $measure->getReferential()->getJsonArray();
$entities[$key]['measure']['linkedMeasures'] = [];
foreach ($measure->getLinkedMeasures() as $linkedMeasure) {
$entities[$key]['measure']['linkedMeasures'][] = $linkedMeasure->getUuid();
}
if ($soaScaleComment !== null) {
$entities[$key]['soaScaleComment'] = $this->soaScaleCommentService
->getPreparedSoaScaleCommentData($soaScaleComment);
} else {
$entities[$key]['soaScaleComment'] = null;
}
}

return $this->getPreparedJsonResponse([
'count' => $this->soaService->getFilteredCount($filter, $filterAnd),
'soaMeasures' => $entities,
'soaMeasures' => $this->soaService->getList($this->getFormattedInputParams($this->getSoasInputFormatter)),
'count' => $this->soaService->getCount($this->getFormattedInputParams($this->getSoasInputFormatter))
]);
}

public function get($id)
public function patch($id, $data)
{
$entity = $this->soaService->getEntity((int)$id);
/** @var Measure $measure */
$measure = $entity['measure'];
/** @var SoaScaleComment $measure */
$soaScaleComment = $entity['soaScaleComment'];

/** @var Anr $anr */
$anr = $this->getRequest()->getAttribute('anr');

$entity['anr'] = [
'id' => $anr->getId(),
'label' => $anr->getLabel(),
];
$entity['measure'] = $measure->getJsonArray();
$entity['measure']['category'] = $measure->getCategory()->getJsonArray();
$entity['measure']['referential'] = $measure->getReferential()->getJsonArray();
if ($soaScaleComment !== null) {
$entity['soaScaleComment'] = $this->soaScaleCommentService
->getPreparedSoaScaleCommentData($soaScaleComment);
} else {
$entity['soaScaleComment'] = null;
}

return $this->getPreparedJsonResponse($entity);
}

public function patch($id, $data)
{
$this->soaService->patchSoa($id, $data);
$this->soaService->patchSoa($anr, (int)$id, $data);

return $this->getSuccessfulJsonResponse();
}
Expand All @@ -178,18 +43,6 @@ public function patchList($data)
/** @var Anr $anr */
$anr = $this->getRequest()->getAttribute('anr');

$createdObjects = [];
foreach ($data as $newData) {
$newData['anr'] = $anr->getId();
$newData['measure'] = ['anr' => $anr->getId(), 'uuid' => $newData['measure']['uuid']];
$id = $newData['id'];
if (\is_array($newData['soaScaleComment'])) {
$newData['soaScaleComment'] = $newData['soaScaleComment']['id'];
}
$this->soaService->patchSoa($id, $newData);
$createdObjects[] = $id;
}

return $this->getSuccessfulJsonResponse(['id' => $createdObjects]);
return $this->getSuccessfulJsonResponse(['id' => $this->soaService->patchList($anr, $data)]);
}
}
11 changes: 11 additions & 0 deletions src/Entity/Instance.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@ public function addInstanceMetadata(InstanceMetadata $instanceMetadata): self
return $this;
}

public function getInstanceMetadataByMetadataFieldLink(AnrInstanceMetadataField $metadataField): ?InstanceMetadata
{
foreach ($this->instanceMetadata as $instanceMetadata) {
if ($instanceMetadata->getAnrInstanceMetadataField()->getLabel() === $metadataField->getLabel()) {
return $instanceMetadata->getAnrInstanceMetadataField();
}
}

return null;
}

public function getHierarchyString(): string
{
return implode(' > ', array_column($this->getHierarchyArray(), 'name' . $this->anr->getLanguage()));
Expand Down
Loading

0 comments on commit 727e1c8

Please sign in to comment.