Skip to content

Commit

Permalink
FIxed, reports generation, CSV risks export, 4th step validation, op …
Browse files Browse the repository at this point in the history
…risks scale types creation and others.
  • Loading branch information
ruslanbaidan committed Aug 26, 2024
1 parent d60283d commit e7d0f53
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 62 deletions.
16 changes: 4 additions & 12 deletions src/Controller/ApiAnrInstancesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@

namespace Monarc\FrontOffice\Controller;

use Laminas\Http\Response;
use Monarc\Core\Controller\Handler\AbstractRestfulControllerRequestHandler;
use Monarc\Core\Controller\Handler\ControllerRequestResponseHandlerTrait;
use Monarc\Core\Validator\InputValidator\Instance\CreateInstanceDataInputValidator;
use Monarc\Core\Validator\InputValidator\Instance\PatchInstanceDataInputValidator;
use Monarc\Core\Validator\InputValidator\Instance\UpdateInstanceDataInputValidator;
use Monarc\FrontOffice\Entity\Anr;
use Monarc\FrontOffice\Export\Controller\Traits\ExportResponseControllerTrait;
use Monarc\FrontOffice\Service\AnrInstanceRiskOpService;
use Monarc\FrontOffice\Service\AnrInstanceRiskService;
use Monarc\FrontOffice\Service\AnrInstanceService;

class ApiAnrInstancesController extends AbstractRestfulControllerRequestHandler
{
use ControllerRequestResponseHandlerTrait;
use ExportResponseControllerTrait;

public function __construct(
private AnrInstanceService $anrInstanceService,
Expand Down Expand Up @@ -50,13 +51,13 @@ public function get($id)
$anr = $this->getRequest()->getAttribute('anr');

if ($this->params()->fromQuery('csv', false)) {
return $this->setCsvResponse(
return $this->prepareCsvDataResponse(
$this->anrInstanceRiskOpService->getOperationalRisksInCsv($anr, (int)$id, $this->parseParams())
);
}

if ($this->params()->fromQuery('csvInfoInst', false)) {
return $this->setCsvResponse(
return $this->prepareCsvDataResponse(
$this->anrInstanceRiskService->getInstanceRisksInCsv($anr, (int)$id, $this->parseParams())
);
}
Expand Down Expand Up @@ -142,13 +143,4 @@ private function parseParams(): array
'limit' => (int)$params->fromQuery('limit', 0),
];
}

private function setCsvResponse(string $content): Response
{
$response = $this->getResponse();
$response->getHeaders()->addHeaderLine('Content-Type', 'text/csv; charset=utf-8');
$response->setContent($content);

return $response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function patch($id, $data)
/** @var Anr $anr */
$anr = $this->getRequest()->getAttribute('anr');

$this->anrRecommendationRiskService->validateFor($anr, $id, $data);
$this->anrRecommendationRiskService->validateFor($anr, (int)$id, $data);

return $this->getSuccessfulJsonResponse();
}
Expand Down
11 changes: 5 additions & 6 deletions src/Controller/ApiAnrRisksController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Monarc\Core\Controller\Handler\AbstractRestfulControllerRequestHandler;
use Monarc\Core\Controller\Handler\ControllerRequestResponseHandlerTrait;
use Monarc\FrontOffice\Entity\Anr;
use Monarc\FrontOffice\Export\Controller\Traits\ExportResponseControllerTrait;
use Monarc\FrontOffice\Service\AnrInstanceRiskService;
use Laminas\Http\Response;

Expand All @@ -19,6 +20,7 @@
class ApiAnrRisksController extends AbstractRestfulControllerRequestHandler
{
use ControllerRequestResponseHandlerTrait;
use ExportResponseControllerTrait;

public function __construct(private AnrInstanceRiskService $anrInstanceRiskService)
{
Expand All @@ -37,12 +39,9 @@ public function get($id)
$id = $id === null ? null : (int)$id;

if ($this->params()->fromQuery('csv', false)) {
/** @var Response $response */
$response = $this->getResponse();
$response->getHeaders()?->addHeaderLine('Content-Type', 'text/csv; charset=utf-8');
$response->setContent($this->anrInstanceRiskService->getInstanceRisksInCsv($anr, (int)$id, $params));

return $response;
return $this->prepareCsvDataResponse(
$this->anrInstanceRiskService->getInstanceRisksInCsv($anr, $id, $params)
);
}

$risks = $this->anrInstanceRiskService->getInstanceRisks($anr, $id, $params);
Expand Down
10 changes: 3 additions & 7 deletions src/Controller/ApiAnrRisksOpController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

namespace Monarc\FrontOffice\Controller;

use Laminas\Http\Response;
use Monarc\Core\Controller\Handler\AbstractRestfulControllerRequestHandler;
use Monarc\Core\Controller\Handler\ControllerRequestResponseHandlerTrait;
use Monarc\FrontOffice\Entity\Anr;
use Monarc\FrontOffice\Export\Controller\Traits\ExportResponseControllerTrait;
use Monarc\FrontOffice\Service\AnrInstanceRiskOpService;

/**
Expand All @@ -19,6 +19,7 @@
class ApiAnrRisksOpController extends AbstractRestfulControllerRequestHandler
{
use ControllerRequestResponseHandlerTrait;
use ExportResponseControllerTrait;

public function __construct(private AnrInstanceRiskOpService $anrInstanceRiskOpService)
{
Expand All @@ -37,14 +38,9 @@ public function get($id)
$id = $id === null ? null : (int)$id;

if ($this->params()->fromQuery('csv', false)) {
/** @var Response $response */
$response = $this->getResponse();
$response->getHeaders()?->addHeaderLine('Content-Type', 'text/csv; charset=utf-8');
$response->setContent(
return $this->prepareCsvDataResponse(
$this->anrInstanceRiskOpService->getOperationalRisksInCsv($anr, $id, $params)
);

return $response;
}

$risks = $this->anrInstanceRiskOpService->getOperationalRisks($anr, $id, $params);
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/ApiAnrScalesTypesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function patch($id, $data)
/** @var Anr $anr */
$anr = $this->getRequest()->getAttribute('anr');

$this->scaleImpactTypeService->patch($anr, $id, $data);
$this->scaleImpactTypeService->patch($anr, (int)$id, $data);

return $this->getSuccessfulJsonResponse();
}
Expand Down
12 changes: 12 additions & 0 deletions src/Export/Controller/Traits/ExportResponseControllerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,16 @@ private function prepareExportResponse(string $filename, string $output, bool $i
'Content-Disposition' => 'attachment; filename="' . $filename . $extension . '"',
]);
}

private function prepareCsvDataResponse(string $output): ResponseInterface
{
$stream = fopen('php://memory', 'rb+');
fwrite($stream, $output);
rewind($stream);

return new Response($stream, 200, [
'Content-Type' => 'text/csv; charset=utf-8',
'Content-Length' => strlen($output),
]);
}
}
34 changes: 18 additions & 16 deletions src/Service/AnrInstanceRiskOpService.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,22 +369,22 @@ public function getOperationalRisksInCsv(Entity\Anr $anr, int $instanceId = null
if ($anr->showRolfBrut()) {
$translatedRiskValueDescription = $this->translateService->translate('Inherent risk', $anrLanguage);
$tableHeaders['brutProb'] = $this->translateService->translate('Prob.', $anrLanguage)
. "(" . $translatedRiskValueDescription . ")";
. '(' . $translatedRiskValueDescription . ')';
foreach ($operationalRiskScaleTypes as $operationalRiskScaleType) {
$label = $operationalRiskScaleType->getLabel();
$tableHeaders[$label . " (" . $translatedRiskValueDescription . ")"] = $label . " ("
. $translatedRiskValueDescription . ")";
$tableHeaders[$label . ' (' . $translatedRiskValueDescription . ')'] = $label . ' ('
. $translatedRiskValueDescription . ')';
}
$tableHeaders['cacheBrutRisk'] = $translatedRiskValueDescription;
}

$translatedNetRiskDescription = $this->translateService->translate('Net risk', $anrLanguage);
$tableHeaders['netProb'] = $this->translateService->translate('Prob.', $anrLanguage) . "("
. $translatedNetRiskDescription . ")";
$tableHeaders['netProb'] = $this->translateService->translate('Prob.', $anrLanguage) . '('
. $translatedNetRiskDescription . ')';
foreach ($operationalRiskScaleTypes as $operationalRiskScaleType) {
$label = $operationalRiskScaleType->getLabel();
$tableHeaders[$label . " (" . $translatedNetRiskDescription . ")"] = $label . " ("
. $translatedNetRiskDescription . ")";
$tableHeaders[$label . ' (' . $translatedNetRiskDescription . ')'] = $label . ' ('
. $translatedNetRiskDescription . ')';
}
$tableHeaders['cacheNetRisk'] = $translatedNetRiskDescription;
$tableHeaders['comment'] = $this->translateService->translate('Existing controls', $anrLanguage);
Expand Down Expand Up @@ -426,12 +426,12 @@ public function getOperationalRisksInCsv(Entity\Anr $anr, int $instanceId = null
$values[] = CoreEntity\InstanceRiskOpSuperClass::getAvailableMeasureTypes()[
$operationalInstanceRisk->getKindOfMeasure()
];
$values[] = $operationalInstanceRisk->getCacheTargetedRisk() === -1 ?
$operationalInstanceRisk->getCacheNetRisk() :
$operationalInstanceRisk->getCacheTargetedRisk();
$values[] = $operationalInstanceRisk->getCacheTargetedRisk() === -1
? $operationalInstanceRisk->getCacheNetRisk()
: $operationalInstanceRisk->getCacheTargetedRisk();
$values[] = $operationalInstanceRisk->getInstanceRiskOwner()?->getName();
$values[] = $operationalInstanceRisk->getContext();
$values[] = $this->getCsvRecommendations($anr, $operationalInstanceRisk);
$values[] = $this->getCsvRecommendations($operationalInstanceRisk);
$values[] = $this->getCsvMeasures($anrLanguage, $operationalInstanceRisk);


Expand Down Expand Up @@ -473,12 +473,12 @@ public function createOperationalInstanceRiskScaleObject(
return $operationalInstanceRiskScale;
}

protected function getCsvRecommendations(Entity\Anr $anr, Entity\InstanceRiskOp $operationalInstanceRisk): string
protected function getCsvRecommendations(Entity\InstanceRiskOp $operationalInstanceRisk): string
{
$csvData = [];
foreach ($operationalInstanceRisk->getRecommendationRisks() as $recommendationRisk) {
$recommendation = $recommendationRisk->getRecommendation();
$csvData[] = $recommendation->getCode() . " - " . $recommendation->getDescription();
$csvData[] = $recommendation->getCode() . ' - ' . $recommendation->getDescription();
}

return implode("\n", $csvData);
Expand All @@ -487,9 +487,11 @@ protected function getCsvRecommendations(Entity\Anr $anr, Entity\InstanceRiskOp
protected function getCsvMeasures(int $anrLanguage, Entity\InstanceRiskOp $operationalInstanceRisk): string
{
$csvData = [];
foreach ($operationalInstanceRisk->getRolfRisk()->getMeasures() as $measure) {
$csvData[] = "[" . $measure->getReferential()->getLabel($anrLanguage) . "] " .
$measure->getCode() . " - " . $measure->getLabel($anrLanguage);
if ($operationalInstanceRisk->getRolfRisk() !== null) {
foreach ($operationalInstanceRisk->getRolfRisk()->getMeasures() as $measure) {
$csvData[] = '[' . $measure->getReferential()->getLabel($anrLanguage) . '] '
. $measure->getCode() . ' - ' . $measure->getLabel($anrLanguage);
}
}

return implode("\n", $csvData);
Expand Down
14 changes: 7 additions & 7 deletions src/Service/AnrInstanceRiskService.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ public function recalculateRiskRatesAndUpdateRecommendationsPositions(Entity\Ins
$this->updateInstanceRiskRecommendationsPositions($instanceRisk);
}

public function getInstanceRisksInCsv(Entity\Anr $anr, $instanceId = null, $params = []): string
public function getInstanceRisksInCsv(Entity\Anr $anr, int $instanceId = null, array $params = []): string
{
$languageIndex = $anr->getLanguage();

Expand All @@ -358,9 +358,9 @@ public function getInstanceRisksInCsv(Entity\Anr $anr, $instanceId = null, $para
$this->translateService->translate('Vulnerability', $languageIndex),
$this->translateService->translate('Existing controls', $languageIndex),
$this->translateService->translate('Qualif.', $languageIndex),
$this->translateService->translate('Current risk', $languageIndex). " C",
$this->translateService->translate('Current risk', $languageIndex) . " I",
$this->translateService->translate('Current risk', $languageIndex) . " "
$this->translateService->translate('Current risk', $languageIndex). ' C',
$this->translateService->translate('Current risk', $languageIndex) . ' I',
$this->translateService->translate('Current risk', $languageIndex) . ' '
. $this->translateService->translate('A', $languageIndex),
$this->translateService->translate('Treatment', $languageIndex),
$this->translateService->translate('Residual risk', $languageIndex),
Expand All @@ -383,14 +383,14 @@ public function getInstanceRisksInCsv(Entity\Anr $anr, $instanceId = null, $para
$instance = $instanceRisk->getInstance();
$recommendationData = [];
foreach ($instanceRisk->getRecommendationRisks() as $recommendationRisk) {
$recommendationData[] = $recommendationRisk->getRecommendation()->getCode() . " - "
$recommendationData[] = $recommendationRisk->getRecommendation()->getCode() . ' - '
. $recommendationRisk->getRecommendation()->getDescription();
}
$measuresData = [];
if ($instanceRisk->getAmv() !== null) {
foreach ($instanceRisk->getAmv()->getMeasures() as $measure) {
$measuresData[] = "[" . $measure->getReferential()->getLabel($anr->getLanguage()) . "] "
. $measure->getCode() . " - " . $measure->getLabel($anr->getLanguage());
$measuresData[] = '[' . $measure->getReferential()->getLabel($anr->getLanguage()) . '] '
. $measure->getCode() . ' - ' . $measure->getLabel($anr->getLanguage());
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Service/AnrRecommendationHistoryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function createFromRecommendationRisk(
->setRiskInstance($recommendationRisk->getInstance()->getName($languageIndex))
->setRiskInstanceContext($recommendationRisk->getInstance()->getHierarchyString())
->setCacheCommentAfter($recommendationRisk->getCommentAfter())
->setCreator($this->connectedUser->getEmail());
->setCreator($this->connectedUser->getFirstname() . ' ' . $this->connectedUser->getLastname());

$instanceRisk = $recommendationRisk->getInstanceRisk();
$instanceRiskOp = $recommendationRisk->getInstanceRiskOp();
Expand Down
11 changes: 7 additions & 4 deletions src/Service/AnrRecommendationRiskService.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __construct(

public function getList(FormattedInputParams $formattedInputParams): array
{
$hasRecommendationFilter = $formattedInputParams->hasFilterFor('recommendation');
$hasRecommendationFilter = $formattedInputParams->hasFilterFor('recommendation.uuid');
$recommendationRisksData = [];
$globalObjectsUuids = [];
/** @var Entity\RecommendationRisk $recommendationRisk */
Expand Down Expand Up @@ -533,9 +533,12 @@ private function getPreparedRecommendationRiskData(
'commentAfter' => $recommendationRisk->getCommentAfter(),
];
if ($extendedFormat) {
$recommendationRiskData['instance'] = array_merge([
'id' => $recommendationRisk->getInstance()->getId(),
], $recommendationRisk->getInstance()->getNames());
$instance = $recommendationRisk->getInstance();
$recommendationRiskData['instance'] = array_merge(['id' => $instance->getId()], $instance->getNames());
$recommendationRiskData['asset'] = array_merge([
'uuid' => $instance->getAsset()->getUuid(),
'type' => $instance->getAsset()->getType(),
], $instance->getAsset()->getLabels());
if ($recommendationRisk->getThreat() !== null && $recommendationRisk->getVulnerability() !== null) {
$recommendationRiskData['threat'] = array_merge([
'code' => $recommendationRisk->getThreat()->getCode(),
Expand Down
15 changes: 8 additions & 7 deletions src/Service/DeliverableGenerationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -842,23 +842,24 @@ private function generateTrendAssessmentTable(): PhpWord\Element\Table

// Fill in each row
foreach ($questions as $question) {
$response = null;
$response = '';
if ($question['type'] === 1) {
// Simple text
$response = $question['response'];
} else {
// Choice, either simple or multiple
if ($question['multichoice']) {
$responseIds = json_decode($question['response'], true);
$responseIds = empty($question['response']) ? [] : json_decode($question['response'], true);
$responses = [];

foreach ($questionsChoices as $choice) {
if (!is_null($responseIds) && array_search($choice['id'], $responseIds) !== false) {
$responses[] = '- ' . $choice['label' . $this->currentLangAnrIndex];
if (!empty($responseIds)) {
foreach ($questionsChoices as $choice) {
if (in_array($choice['id'], $responseIds, true)) {
$responses[] = '- ' . $choice['label' . $this->currentLangAnrIndex];
}
}
$response = implode("\n", $responses);
}

$response = implode("\n", $responses);
} else {
foreach ($questionsChoices as $choice) {
if ($choice['id'] === $question['response']) {
Expand Down
3 changes: 3 additions & 0 deletions src/Service/OperationalRiskScaleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ public function createOperationalRiskScaleType(Entity\Anr $anr, array $data): En
$operationalRiskScale = $this->operationalRiskScaleTable->findByAnrAndScaleId($anr, (int)$data['scaleId']);

$operationalRiskScaleType = $this->getCreatedOperationalRiskScaleTypeObject($anr, $operationalRiskScale);
if (!empty($data['label'][$anr->getLanguageCode()])) {
$operationalRiskScaleType->setLabel($data['label'][$anr->getLanguageCode()]);
}

// Process the scale comments.
if (!empty($data['comments'])) {
Expand Down

0 comments on commit e7d0f53

Please sign in to comment.