Skip to content

Commit

Permalink
Fixed the export and import issues, snapshots restoring, recommendati…
Browse files Browse the repository at this point in the history
…ons import with positions.
  • Loading branch information
ruslanbaidan committed Aug 20, 2024
1 parent 9c79282 commit 80f64f3
Show file tree
Hide file tree
Showing 20 changed files with 196 additions and 98 deletions.
11 changes: 10 additions & 1 deletion src/Entity/Anr.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class Anr extends AnrSuperClass
/**
* @var Snapshot|null
*
* @ORM\OneToOne(targetEntity="Snapshot", mappedBy="anr")
* @ORM\OneToOne(targetEntity="Snapshot", mappedBy="anr", cascade={"remove"})
*/
protected $snapshot;

Expand Down Expand Up @@ -377,6 +377,15 @@ public function addReferencedSnapshot(Snapshot $snapshot): self
return $this;
}

public function removeReferencedSnapshot(Snapshot $snapshot): self
{
if ($this->referencedSnapshots->contains($snapshot)) {
$this->referencedSnapshots->removeElement($snapshot);
}

return $this;
}

public function getRecommendationSets()
{
return $this->recommendationSets;
Expand Down
6 changes: 4 additions & 2 deletions src/Entity/Instance.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,10 @@ public function addInstanceMetadata(InstanceMetadata $instanceMetadata): self
public function getInstanceMetadataByMetadataFieldLink(AnrInstanceMetadataField $metadataField): ?InstanceMetadata
{
foreach ($this->instanceMetadata as $instanceMetadata) {
if ($instanceMetadata->getAnrInstanceMetadataField()->getLabel() === $metadataField->getLabel()) {
return $instanceMetadata->getAnrInstanceMetadataField();
if ($this->id === $instanceMetadata->getInstance()->getId()
&& $instanceMetadata->getAnrInstanceMetadataField()->getLabel() === $metadataField->getLabel()
) {
return $instanceMetadata;
}
}

Expand Down
21 changes: 10 additions & 11 deletions src/Entity/InstanceRiskOwner.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,6 @@ class InstanceRiskOwner
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;

/**
* @var Anr
*
* @ORM\ManyToOne(targetEntity="Anr")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="anr_id", referencedColumnName="id", onDelete="CASCADE")
* })
*/
protected $anr;

/**
* @var InstanceRisk[]|ArrayCollection
*
Expand All @@ -59,6 +48,16 @@ class InstanceRiskOwner
*/
protected $operationalInstanceRisks;

/**
* @var Anr
*
* @ORM\ManyToOne(targetEntity="Anr")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="anr_id", referencedColumnName="id", onDelete="CASCADE")
* })
*/
protected $anr;

/**
* @var string
*
Expand Down
16 changes: 8 additions & 8 deletions src/Entity/Measure.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,6 @@ class Measure extends MeasureSuperClass
*/
protected $uuid;

/**
* @var Anr
*
* @ORM\ManyToOne(targetEntity="Anr")
* @ORM\JoinColumns({@ORM\JoinColumn(name="anr_id", referencedColumnName="id", nullable=false)})
*/
protected $anr;

/**
* @var ArrayCollection|Amv[]
*
Expand Down Expand Up @@ -100,6 +92,14 @@ class Measure extends MeasureSuperClass
*/
protected $soa;

/**
* @var Anr
*
* @ORM\ManyToOne(targetEntity="Anr")
* @ORM\JoinColumns({@ORM\JoinColumn(name="anr_id", referencedColumnName="id", nullable=false)})
*/
protected $anr;

public function getId()
{
return $this->id;
Expand Down
2 changes: 1 addition & 1 deletion src/Entity/Snapshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Snapshot
*
* @ORM\ManyToOne(targetEntity="Anr")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="anr_reference_id", referencedColumnName="id", nullable=false, onDelete="CASCADE")
* @ORM\JoinColumn(name="anr_reference_id", referencedColumnName="id", nullable=false)
* })
*/
protected $anrReference;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,21 @@

use Laminas\Diactoros\Response;
use Psr\Http\Message\ResponseInterface;

use function strlen;

trait ExportResponseControllerTrait
{
private function prepareExportResponse(string $filename, string $output, bool $isEncrypted): ResponseInterface
{
$dataType = 'application/json';
$contentType = 'application/json; charset=utf-8';
$extension = '.json';
if ($isEncrypted) {
$dataType = 'text/plain';
$contentType = 'text/plain; charset=utf-8';
$extension = '.bin';
}
$stream = fopen('data://' . $dataType . ',' . $output, 'rb+');
$stream = fopen('php://memory', 'rb+');
fwrite($stream, $output);
rewind($stream);

return new Response($stream, 200, [
'Content-Type' => $contentType,
Expand Down
23 changes: 15 additions & 8 deletions src/Export/Service/AnrExportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,14 @@ private function prepareExportData(Entity\Anr $anr, array $exportParams): array
!$withKnowledgeBase
) : [],
'library' => $withLibrary ? $this->prepareLibraryData($anr, !$withKnowledgeBase) : [],
'instances' => $this
->prepareInstancesData($anr, !$withLibrary, $withEval, $withControls, $withRecommendations),
'instances' => $this->prepareInstancesData(
$anr,
!$withLibrary,
!$withKnowledgeBase,
$withEval,
$withControls,
$withRecommendations
),
'anrInstanceMetadataFields' => $this->prepareAnrInstanceMetadataFieldsData($anr),
'scales' => $withEval ? $this->prepareScalesData($anr) : [],
'operationalRiskScales' => $withEval ? $this->prepareOperationalRiskScalesData($anr) : [],
Expand Down Expand Up @@ -165,7 +171,7 @@ private function prepareKnowledgeBaseData(
'informationRisks' => $this->prepareInformationRisksData($anr, $withEval, $withControls),
'rolfTags' => $this->prepareRolfTagsData($anr),
'operationalRisks' => $this->prepareOperationalRisksData($anr, $withControls),
'recommendationSets' => $withRecommendations ? $this->prepareRecommendationSetsData($anr) : [],
'recommendationSets' => $this->prepareRecommendationSetsData($anr, $withRecommendations),
];
}

Expand Down Expand Up @@ -266,14 +272,14 @@ private function prepareReferentialsData(Entity\Anr $anr): array
return $result;
}

private function prepareRecommendationSetsData(Entity\Anr $anr): array
private function prepareRecommendationSetsData(Entity\Anr $anr, bool $includePositions): array
{
$result = [];
/** @var Entity\RecommendationSet $recommendationSet */
foreach ($this->recommendationSetTable->findByAnr($anr) as $recommendationSet) {
$recommendationsData = [];
foreach ($recommendationSet->getRecommendations() as $recommendation) {
$recommendationsData[] = $this->prepareRecommendationData($recommendation, false);
$recommendationsData[] = $this->prepareRecommendationData($recommendation, false, $includePositions);
}
if (!empty($recommendationsData)) {
$result[] = [
Expand Down Expand Up @@ -348,7 +354,8 @@ private function prepareCategoryData(

private function prepareInstancesData(
Entity\Anr $anr,
bool $includeObjectDataInTheResult,
bool $includeObjectData,
bool $includeCompleteInformationRisksData,
bool $withEval,
bool $withControls,
bool $withRecommendations
Expand All @@ -360,8 +367,8 @@ private function prepareInstancesData(
$result[] = $this->prepareInstanceData(
$instance,
$languageIndex,
$includeObjectDataInTheResult,
false,
$includeObjectData,
$includeCompleteInformationRisksData,
$withEval,
$withControls,
$withRecommendations,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ private function prepareOperationalInstanceRiskData(
'operationalRisk' => $operationalRisk !== null
? $this->prepareOperationalRiskData($operationalRisk, $languageIndex, $withControls)
: null,
'riskCacheCode' => $operationalInstanceRisk->getRiskCacheCode(),
'riskCacheLabel' => $operationalInstanceRisk->getRiskCacheLabel($languageIndex),
'riskCacheDescription' => $operationalInstanceRisk->getRiskCacheDescription($languageIndex),
'brutProb' => $withEval ? $operationalInstanceRisk->getBrutProb() : -1,
Expand Down
6 changes: 5 additions & 1 deletion src/Export/Service/Traits/RecommendationExportTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ trait RecommendationExportTrait
{
private function prepareRecommendationData(
Entity\Recommendation $recommendation,
bool $includeRecommendationSetData = true
bool $includeRecommendationSetData = true,
bool $includePosition = true
): array {
$result = [
'uuid' => $recommendation->getUuid(),
Expand All @@ -32,6 +33,9 @@ private function prepareRecommendationData(
'label' => $recommendation->getRecommendationSet()->getLabel(),
];
}
if ($includePosition) {
$result['position'] = $recommendation->getPosition();
}

return $result;
}
Expand Down
5 changes: 3 additions & 2 deletions src/Import/Processor/InformationRiskImportProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public function processInformationRiskData(Entity\Anr $anr, array $informationRi
'setOnlyExactPosition' => true,
'position' => ++$this->maxPositionsPerAsset[$asset->getUuid()],
], false, false);

$this->importCacheHelper
->addItemToArrayCache('amvs_by_uuid', $informationRisk, $informationRisk->getUuid());
}

$saveInformationRisk = false;
Expand All @@ -80,8 +83,6 @@ public function processInformationRiskData(Entity\Anr $anr, array $informationRi

if ($saveInformationRisk) {
$this->amvTable->save($informationRisk, false);
$this->importCacheHelper
->addItemToArrayCache('amvs_by_uuid', $informationRisk, $informationRisk->getUuid());
}

return $informationRisk;
Expand Down
2 changes: 2 additions & 0 deletions src/Import/Processor/InstanceRiskImportProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@ private function processInstanceRiskData(Entity\Instance $instance, array $insta
/** @var Entity\Anr $anr */
$anr = $instance->getAnr();
if (!empty($instanceRiskData['informationRisk'])) {
/* The case of normal instance risk, where threat and vulnerability are taken from AMV. */
$amv = $this->informationRiskImportProcessor
->processInformationRiskData($anr, $instanceRiskData['informationRisk']);
$threat = null;
$vulnerability = null;
} else {
/* The case of specific instance risk that has no relation to AMV. */
$amv = null;
$threat = $this->threatImportProcessor->processThreatData($anr, $instanceRiskData['threat']);
$vulnerability = $this->vulnerabilityImportProcessor
Expand Down
52 changes: 41 additions & 11 deletions src/Import/Processor/ObjectImportProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Monarc\FrontOffice\Import\Processor;

use Monarc\Core\Entity\InstanceRiskOpSuperClass;
use Monarc\Core\Entity\InstanceRiskSuperClass;
use Monarc\Core\Entity\ObjectSuperClass;
use Monarc\FrontOffice\Entity;
Expand All @@ -22,15 +23,18 @@ public function __construct(
private Table\MonarcObjectTable $monarcObjectTable,
private Table\ObjectObjectTable $objectObjectTable,
private Table\InstanceRiskTable $instanceRiskTable,
private Table\InstanceRiskOpTable $instanceRiskOpTable,
private Table\AmvTable $amvTable,
private ImportCacheHelper $importCacheHelper,
private Service\AnrObjectService $anrObjectService,
private Service\AnrObjectObjectService $anrObjectObjectService,
private Service\AnrInstanceRiskService $anrInstanceRiskService,
private Service\AnrInstanceRiskOpService $anrInstanceRiskOpService,
private AssetImportProcessor $assetImportProcessor,
private RolfTagImportProcessor $rolfTagImportProcessor,
private ObjectCategoryImportProcessor $objectCategoryImportProcessor,
private InformationRiskImportProcessor $informationRiskImportProcessor
private InformationRiskImportProcessor $informationRiskImportProcessor,
private OperationalRiskScaleImportProcessor $operationalRiskScaleImportProcessor
) {
}

Expand Down Expand Up @@ -216,27 +220,53 @@ private function mergeAssetInformationRisks(Entity\MonarcObject $object, array $
foreach (array_diff_key($existingAmvs, $importingAmvsData) as $existingAmvToRemove) {
/* Recreated instance risks if the object is instantiated. */
foreach ($existingAmvToRemove->getInstanceRisks() as $instanceRiskToSetSpecific) {
$instanceRiskToSetSpecific->setSpecific(InstanceRiskSuperClass::TYPE_SPECIFIC)
->setAmv(null);
$instanceRiskToSetSpecific->setSpecific(InstanceRiskSuperClass::TYPE_SPECIFIC)->setAmv(null);
$this->instanceRiskTable->save($instanceRiskToSetSpecific, false);
}
$this->amvTable->remove($existingAmvToRemove, false);
}
}

/** Merges the rolfRisks (operational risks) of the existing object. */
private function mergeRolfTagOperationalRisks(Entity\MonarcObject $object, ?array $rolfTagData): void
{
private function mergeRolfTagOperationalRisks(
Entity\Anr $anr,
Entity\MonarcObject $object,
?array $rolfTagData
): void {
/* NOTE. If rolfTag stays the same, then the rolf risks could be validated and updated if different. */
if (!empty($rolfTagData) && $object->getRolfTag() === null) {
// TODO: 1:
/* if there was no rolfTag and a new one is set. */

$rolfTag = $this->rolfTagImportProcessor->processRolfTagData($anr, $rolfTagData);
$object->setRolfTag($rolfTag);
$this->monarcObjectTable->save($object, false);
} elseif (empty($rolfTagData) && $object->getRolfTag() !== null) {
// TODO: 2. was a rolf tag and now removed, then remove all the instance risks op have to be removed.
/* if there was a rolfTag and now removed, then all the InstanceRiskOp have to be set as specific. */
$this->setOperationalRisksSpecific($object);
$object->setRolfTag(null);
$this->monarcObjectTable->save($object, false);
} elseif ($object->getRolfTag()->getCode() !== $rolfTagData['code']) {
// TODO: 3. if a new rolf is changed, then all the op risks have to be updated.
} else {
// TODO: 4. rolfTag stays the same (set) then we have to validate the difference of rolf risks.
/* If rolfTag is changed, then all the op risks have to be updated. */
$this->setOperationalRisksSpecific($object);
$rolfTag = $this->rolfTagImportProcessor->processRolfTagData($anr, $rolfTagData);
$object->setRolfTag($rolfTag);
foreach ($object->getInstances() as $instance) {
foreach ($rolfTag->getRisks() as $rolfRisk) {
$this->anrInstanceRiskOpService->createInstanceRiskOpWithScales($instance, $object, $rolfRisk);
}
}
$this->monarcObjectTable->save($object, false);
}
}

private function setOperationalRisksSpecific(Entity\MonarcObject $object): void
{
if ($object->getRolfTag() !== null) {
foreach ($object->getRolfTag()->getRisks() as $rolfRisk) {
foreach ($rolfRisk->getOperationalInstanceRisks() as $operationalInstanceRisk) {
$operationalInstanceRisk->setSpecific(InstanceRiskOpSuperClass::TYPE_SPECIFIC)->setRolfRisk(null);
$this->instanceRiskOpTable->save($operationalInstanceRisk, false);
}
}
}
}
}
10 changes: 7 additions & 3 deletions src/Import/Processor/OperationalInstanceRiskImportProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,16 @@ public function processOperationalInstanceRisksData(
);
}
foreach ($operationalInstanceRisksData as $operationalInstanceRiskData) {
$operationalRisk = $this->operationalRiskImportProcessor
->processOperationalRiskData($anr, $operationalInstanceRiskData['operationalRisk']);
$operationalRisk = empty($operationalInstanceRiskData['operationalRisk'])
? null
: $this->operationalRiskImportProcessor->processOperationalRiskData(
$anr,
$operationalInstanceRiskData['operationalRisk']
);
/** @var Entity\MonarcObject $object */
$object = $instance->getObject();
$operationalInstanceRisk = $this->anrInstanceRiskOpService
->createInstanceRiskOpObject($instance, $object, $operationalRisk);
->createInstanceRiskOpObject($instance, $object, $operationalRisk, $operationalInstanceRiskData);
if ($this->importCacheHelper->getValueFromArrayCache('with_eval')) {
$operationalInstanceRisk
->setBrutProb((int)$operationalInstanceRiskData['brutProb'])
Expand Down
Loading

0 comments on commit 80f64f3

Please sign in to comment.