Skip to content

Commit

Permalink
Fixed the import conditions and added referentials when soa is export…
Browse files Browse the repository at this point in the history
…ed but not the knowledge base, removed the extra db column.
  • Loading branch information
ruslanbaidan committed Jul 26, 2024
1 parent 208aac1 commit 9c79282
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 74 deletions.
1 change: 1 addition & 0 deletions migrations/db/20230901112005_fix_positions_cleanup_db.php
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
28 changes: 23 additions & 5 deletions src/Export/Service/AnrExportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down
34 changes: 16 additions & 18 deletions src/Import/Processor/AssetImportProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
59 changes: 31 additions & 28 deletions src/Import/Processor/InformationRiskImportProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down
46 changes: 28 additions & 18 deletions src/Import/Processor/OperationalRiskImportProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Import/Service/InstanceImportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion src/Table/AssetTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ protected function getRules(): array
],
],
'validators' => [
// TODO: Add unique label validation.
[
'name' => StringLength::class,
'options' => [
Expand Down

0 comments on commit 9c79282

Please sign in to comment.