From 9c79282a1ed965c4bab36732005cb99c07daef4e Mon Sep 17 00:00:00 2001 From: Ruslan Baidan Date: Fri, 26 Jul 2024 17:25:00 +0200 Subject: [PATCH] Fixed the import conditions and added referentials when soa is exported but not the knowledge base, removed the extra db column. --- ...0230901112005_fix_positions_cleanup_db.php | 1 + src/Export/Service/AnrExportService.php | 28 +++++++-- src/Import/Processor/AssetImportProcessor.php | 34 +++++------ .../InformationRiskImportProcessor.php | 59 ++++++++++--------- .../OperationalRiskImportProcessor.php | 46 +++++++++------ src/Import/Service/InstanceImportService.php | 6 +- src/Table/AssetTable.php | 1 - .../Anr/CreateAnrDataInputValidator.php | 1 - 8 files changed, 102 insertions(+), 74 deletions(-) diff --git a/migrations/db/20230901112005_fix_positions_cleanup_db.php b/migrations/db/20230901112005_fix_positions_cleanup_db.php index 74d16580..652ddf65 100644 --- a/migrations/db/20230901112005_fix_positions_cleanup_db.php +++ b/migrations/db/20230901112005_fix_positions_cleanup_db.php @@ -277,6 +277,7 @@ public function change() $this->table('measures_rolf_risks') ->addForeignKey('measure_id', 'measures', 'id', ['delete' => 'CASCADE', 'update' => 'RESTRICT']) ->removeColumn('measure_uuid') + ->removeColumn('anr_id') ->update(); /* Rename column of owner_id to risk_owner_id. */ diff --git a/src/Export/Service/AnrExportService.php b/src/Export/Service/AnrExportService.php index c3c48542..445dd29a 100644 --- a/src/Export/Service/AnrExportService.php +++ b/src/Export/Service/AnrExportService.php @@ -101,9 +101,13 @@ private function prepareExportData(Entity\Anr $anr, array $exportParams): array 'withKnowledgeBase' => $withKnowledgeBase, 'languageCode' => $anr->getLanguageCode(), 'languageIndex' => $anr->getLanguage(), - 'knowledgeBase' => $withKnowledgeBase - ? $this->prepareKnowledgeBaseData($anr, $withEval, $withControls, $withRecommendations) - : [], + 'knowledgeBase' => $withKnowledgeBase || $withSoas ? $this->prepareKnowledgeBaseData( + $anr, + $withEval, + $withControls, + $withRecommendations, + !$withKnowledgeBase + ) : [], 'library' => $withLibrary ? $this->prepareLibraryData($anr, !$withKnowledgeBase) : [], 'instances' => $this ->prepareInstancesData($anr, !$withLibrary, $withEval, $withControls, $withRecommendations), @@ -137,13 +141,27 @@ private function prepareKnowledgeBaseData( Entity\Anr $anr, bool $withEval, bool $withControls, - bool $withRecommendations + bool $withRecommendations, + bool $onlyWithReferentials ): array { + if ($onlyWithReferentials) { + return [ + 'assets' => [], + 'threats' => [], + 'vulnerabilities' => [], + 'referentials' => $this->prepareReferentialsData($anr), + 'informationRisks' => [], + 'rolfTags' => [], + 'operationalRisks' => [], + 'recommendationSets' => [], + ]; + } + return [ 'assets' => $this->prepareAssetsData($anr), 'threats' => $this->prepareThreatsData($anr, $withEval), 'vulnerabilities' => $this->prepareVulnerabilitiesData($anr), - 'referentials' => $withControls ? $this->prepareReferentialsData($anr) : [], + 'referentials' => $this->prepareReferentialsData($anr), 'informationRisks' => $this->prepareInformationRisksData($anr, $withEval, $withControls), 'rolfTags' => $this->prepareRolfTagsData($anr), 'operationalRisks' => $this->prepareOperationalRisksData($anr, $withControls), diff --git a/src/Import/Processor/AssetImportProcessor.php b/src/Import/Processor/AssetImportProcessor.php index 5ea7027b..6a3e04a3 100644 --- a/src/Import/Processor/AssetImportProcessor.php +++ b/src/Import/Processor/AssetImportProcessor.php @@ -32,29 +32,27 @@ public function processAssetsData(Entity\Anr $anr, array $assetsData): void public function processAssetData(Entity\Anr $anr, array $assetData): Entity\Asset { $asset = $this->getAssetFromCache($anr, $assetData['uuid']); - if ($asset !== null) { - return $asset; - } + if ($asset === null) { + /* The code should be unique. */ + if ($this->importCacheHelper->isItemInArrayCache('assets_codes', $assetData['code'])) { + $assetData['code'] .= '-' . time(); + } - /* The code should be unique. */ - if ($this->importCacheHelper->isItemInArrayCache('assets_codes', $assetData['code'])) { - $assetData['code'] .= '-' . time(); - } + /* In the new data structure there is only "label" field set. */ + if (isset($assetData['label'])) { + $assetData['label' . $anr->getLanguage()] = $assetData['label']; + } + if (isset($assetData['description'])) { + $assetData['description' . $anr->getLanguage()] = $assetData['description']; + } - /* In the new data structure there is only "label" field set. */ - if (isset($assetData['label'])) { - $assetData['label' . $anr->getLanguage()] = $assetData['label']; - } - if (isset($assetData['description'])) { - $assetData['description' . $anr->getLanguage()] = $assetData['description']; + $asset = $this->anrAssetService->create($anr, $assetData, false); + $this->importCacheHelper->addItemToArrayCache('assets_by_uuid', $asset, $asset->getUuid()); } - $asset = $this->anrAssetService->create($anr, $assetData, false); - $this->importCacheHelper->addItemToArrayCache('assets_by_uuid', $asset, $asset->getUuid()); - /* In case if the process is called from the object then process information risks data. */ - if (!empty($assetsData['informationRisks'])) { - $this->informationRiskImportProcessor->processInformationRisksData($anr, $assetsData['informationRisks']); + if (!empty($assetData['informationRisks'])) { + $this->informationRiskImportProcessor->processInformationRisksData($anr, $assetData['informationRisks']); } return $asset; diff --git a/src/Import/Processor/InformationRiskImportProcessor.php b/src/Import/Processor/InformationRiskImportProcessor.php index 22a9fe38..39ccd4f4 100644 --- a/src/Import/Processor/InformationRiskImportProcessor.php +++ b/src/Import/Processor/InformationRiskImportProcessor.php @@ -37,34 +37,33 @@ public function processInformationRisksData(Entity\Anr $anr, array $informationR public function processInformationRiskData(Entity\Anr $anr, array $informationRiskData): Entity\Amv { $informationRisk = $this->getInformationRiskFromCache($anr, $informationRiskData['uuid']); - if ($informationRisk !== null) { - return $informationRisk; - } - - $asset = $this->assetImportProcessor->processAssetData($anr, $informationRiskData['asset']); - $threat = $this->threatImportProcessor->processThreatData($anr, $informationRiskData['threat']); - $vulnerability = $this->vulnerabilityImportProcessor - ->processVulnerabilityData($anr, $informationRiskData['vulnerability']); + if ($informationRisk === null) { + $asset = $this->assetImportProcessor->processAssetData($anr, $informationRiskData['asset']); + $threat = $this->threatImportProcessor->processThreatData($anr, $informationRiskData['threat']); + $vulnerability = $this->vulnerabilityImportProcessor + ->processVulnerabilityData($anr, $informationRiskData['vulnerability']); - /* Prepare the max positions per asset as the objects are not saved in the DB to be able to determine on fly. */ - if (!isset($this->maxPositionsPerAsset[$asset->getUuid()])) { - $this->maxPositionsPerAsset[$asset->getUuid()] = $this->amvTable->findMaxPosition([ - 'anr' => $anr, - 'asset' => [ - 'uuid' => $asset->getUuid(), + /* Prepare the max positions per asset as the objects are not saved in the DB. */ + if (!isset($this->maxPositionsPerAsset[$asset->getUuid()])) { + $this->maxPositionsPerAsset[$asset->getUuid()] = $this->amvTable->findMaxPosition([ 'anr' => $anr, - ], - ]); - } + 'asset' => [ + 'uuid' => $asset->getUuid(), + 'anr' => $anr, + ], + ]); + } - $amv = $this->anrAmvService->createAmvFromPreparedData($anr, $asset, $threat, $vulnerability, [ - 'uuid' => $informationRiskData['uuid'], - 'status' => $informationRiskData['status'], - 'setOnlyExactPosition' => true, - 'position' => ++$this->maxPositionsPerAsset[$asset->getUuid()], - ], false, false); + $informationRisk = $this->anrAmvService->createAmvFromPreparedData($anr, $asset, $threat, $vulnerability, [ + 'uuid' => $informationRiskData['uuid'], + 'status' => $informationRiskData['status'], + 'setOnlyExactPosition' => true, + 'position' => ++$this->maxPositionsPerAsset[$asset->getUuid()], + ], false, false); + } - foreach ($informationRiskData['measures'] as $measureData) { + $saveInformationRisk = false; + foreach ($informationRiskData['measures'] ?? [] as $measureData) { $measure = $this->referentialImportProcessor->getMeasureFromCache($anr, $measureData['uuid']); if ($measure === null && !empty($measureData['referential'])) { $referential = $this->referentialImportProcessor->processReferentialData( @@ -74,14 +73,18 @@ public function processInformationRiskData(Entity\Anr $anr, array $informationRi $measure = $this->referentialImportProcessor->processMeasureData($anr, $referential, $measureData); } if ($measure !== null) { - $amv->addMeasure($measure); + $informationRisk->addMeasure($measure); + $saveInformationRisk = true; } } - $this->amvTable->save($amv, false); - $this->importCacheHelper->addItemToArrayCache('amvs_by_uuid', $amv, $amv->getUuid()); + if ($saveInformationRisk) { + $this->amvTable->save($informationRisk, false); + $this->importCacheHelper + ->addItemToArrayCache('amvs_by_uuid', $informationRisk, $informationRisk->getUuid()); + } - return $amv; + return $informationRisk; } private function getInformationRiskFromCache(Entity\Anr $anr, string $uuid): ?Entity\Amv diff --git a/src/Import/Processor/OperationalRiskImportProcessor.php b/src/Import/Processor/OperationalRiskImportProcessor.php index 993fe0fa..bca36e95 100644 --- a/src/Import/Processor/OperationalRiskImportProcessor.php +++ b/src/Import/Processor/OperationalRiskImportProcessor.php @@ -33,24 +33,24 @@ public function processOperationalRisksData(Entity\Anr $anr, array $operationalR public function processOperationalRiskData(Entity\Anr $anr, array $operationalRiskData): Entity\RolfRisk { $operationalRisk = $this->getRolfRiskFromCache($anr, $operationalRiskData['code']); - if ($operationalRisk !== null) { - return $operationalRisk; + if ($operationalRisk === null) { + $operationalRisk = $this->anrRolfRiskService->create($anr, [ + 'code' => $operationalRiskData['code'], + 'label' . $anr->getLanguage() => + $operationalRiskData['label'] ?? $operationalRiskData['label' . $anr->getLanguage()], + 'description' . $anr->getLanguage() => + $operationalRiskData['label'] ?? $operationalRiskData['description' . $anr->getLanguage()], + ], false); + $this->importCacheHelper->addItemToArrayCache( + 'rolf_risks_by_code', + $operationalRisk, + $operationalRisk->getCode() + ); } - $operationalRisk = $this->anrRolfRiskService->create($anr, [ - 'code' => $operationalRiskData['code'], - 'label' . $anr->getLanguage() => - $operationalRiskData['label'] ?? $operationalRiskData['label' . $anr->getLanguage()], - 'description' . $anr->getLanguage() => - $operationalRiskData['label'] ?? $operationalRiskData['description' . $anr->getLanguage()], - ], false); - $this->importCacheHelper->addItemToArrayCache( - 'rolf_risks_by_code', - $operationalRisk, - $operationalRisk->getCode() - ); + $saveOperationalRisk = false; foreach ($operationalRiskData['measures'] as $measureData) { - $measure = $this->referentialImportProcessor->getMeasureFromCache($anr, $measureData); + $measure = $this->referentialImportProcessor->getMeasureFromCache($anr, $measureData['uuid']); if ($measure === null && !empty($measureData['referential'])) { $referential = $this->referentialImportProcessor->processReferentialData( $anr, @@ -60,12 +60,22 @@ public function processOperationalRiskData(Entity\Anr $anr, array $operationalRi } if ($measure !== null) { $operationalRisk->addMeasure($measure); + $saveOperationalRisk = true; } } - foreach ($operationalRiskData['rolfTags'] as $rolfTagData) { - $operationalRisk->addTag($this->rolfTagImportProcessor->processRolfTagData($anr, $rolfTagData)); + foreach ($operationalRiskData['rolfTags'] ?? [] as $rolfTagData) { + $rolfTag = $this->rolfTagImportProcessor->processRolfTagData($anr, $rolfTagData); + if (!$operationalRisk->hasRolfTag($rolfTag)) { + $saveOperationalRisk = true; + } + $operationalRisk->addTag($rolfTag); + } + + if ($saveOperationalRisk) { + $this->rolfRiskTable->save($operationalRisk, false); + $this->importCacheHelper + ->addItemToArrayCache('rolf_risks_by_code', $operationalRisk, $operationalRisk->getCode()); } - $this->rolfRiskTable->save($operationalRisk, false); return $operationalRisk; } diff --git a/src/Import/Service/InstanceImportService.php b/src/Import/Service/InstanceImportService.php index 7112fa91..9a69ff8f 100755 --- a/src/Import/Service/InstanceImportService.php +++ b/src/Import/Service/InstanceImportService.php @@ -167,15 +167,15 @@ private function processAnrImport( $this->operationalRiskScaleImportProcessor->adjustOperationalRisksScaleValuesBasedOnNewScales($anr, $data); $this->operationalRiskScaleImportProcessor->updateOperationalRisksScalesAndRelatedInstances($anr, $data); } - if ($data['withInterviews']) { + if (!empty($data['interviews'])) { /* Process the interviews' data. */ $this->anrMethodStepImportProcessor->processInterviewsData($anr, $data['interviews']); } - if ($data['withKnowledgeBase']) { + if (!empty($data['knowledgeBase'])) { /* Process the Knowledge Base data. */ $this->processKnowledgeBaseData($anr, $data['knowledgeBase']); } - if ($data['withLibrary']) { + if (!empty($data['library'])) { /* Process the Assets Library data. */ $this->objectCategoryImportProcessor ->processObjectCategoriesData($anr, $data['library']['categories'], $importMode); diff --git a/src/Table/AssetTable.php b/src/Table/AssetTable.php index a20b8213..22834838 100755 --- a/src/Table/AssetTable.php +++ b/src/Table/AssetTable.php @@ -11,7 +11,6 @@ use Monarc\Core\Table\AbstractTable; use Monarc\Core\Table\Interfaces\UniqueCodeTableInterface; use Monarc\Core\Table\Traits\CodeExistenceValidationTableTrait; -use Monarc\FrontOffice\Entity\Anr; use Monarc\FrontOffice\Entity\Asset; class AssetTable extends AbstractTable implements UniqueCodeTableInterface diff --git a/src/Validator/InputValidator/Anr/CreateAnrDataInputValidator.php b/src/Validator/InputValidator/Anr/CreateAnrDataInputValidator.php index 62fb6fe9..e511d734 100644 --- a/src/Validator/InputValidator/Anr/CreateAnrDataInputValidator.php +++ b/src/Validator/InputValidator/Anr/CreateAnrDataInputValidator.php @@ -42,7 +42,6 @@ protected function getRules(): array ], ], 'validators' => [ - // TODO: Add unique label validation. [ 'name' => StringLength::class, 'options' => [