From 9a48ca5c4042d760574847e71929a14b415b42ad Mon Sep 17 00:00:00 2001 From: Mohamed-Hacene Date: Mon, 16 Dec 2024 18:57:40 +0100 Subject: [PATCH] feat: auto fill risk assessment with ebios rm study --- backend/core/views.py | 31 +++++++++++++++++++++++++++++++ backend/ebios_rm/models.py | 19 +++++++++++++------ 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/backend/core/views.py b/backend/core/views.py index 6f4301571..b8d3f7927 100644 --- a/backend/core/views.py +++ b/backend/core/views.py @@ -69,10 +69,16 @@ AppliedControl, ComplianceAssessment, RequirementMappingSet, + RiskAssessment, ) from core.serializers import ComplianceAssessmentReadSerializer from core.utils import RoleCodename, UserGroupCodename +from ebios_rm.models import ( + EbiosRMStudy, + OperationalScenario, +) + from .models import * from .serializers import * @@ -578,6 +584,31 @@ class RiskAssessmentViewSet(BaseModelViewSet): "ebios_rm_study", ] + def perform_create(self, serializer): + instance: RiskAssessment = serializer.save() + if instance.ebios_rm_study: + instance.risk_matrix = instance.ebios_rm_study.risk_matrix + ebios_rm_study = EbiosRMStudy.objects.get(id=instance.ebios_rm_study.id) + for operational_scenario in ebios_rm_study.operational_scenarios.all(): + risk_scenario = RiskScenario.objects.create( + risk_assessment=instance, + name=operational_scenario.name, + ref_id=operational_scenario.ref_id + if operational_scenario.ref_id + else RiskScenario.get_default_ref_id(instance), + description=operational_scenario.operating_modes_description, + current_proba=operational_scenario.likelihood, + current_impact=operational_scenario.gravity, + ) + risk_scenario.assets.set(operational_scenario.get_assets()) + risk_scenario.threats.set(operational_scenario.threats.all()) + risk_scenario.existing_applied_controls.set( + operational_scenario.get_applied_controls() + ) + risk_scenario.save() + instance.save() + return super().perform_create(serializer) + @action(detail=False, name="Risk assessments per status") def per_status(self, request): data = assessment_per_status(request.user, RiskAssessment) diff --git a/backend/ebios_rm/models.py b/backend/ebios_rm/models.py index 7eab4f5d2..165d25560 100644 --- a/backend/ebios_rm/models.py +++ b/backend/ebios_rm/models.py @@ -458,12 +458,11 @@ def parsed_matrix(self): @property def ref_id(self): - sorted_operational_scenarios = list( - OperationalScenario.objects.filter( - ebios_rm_study=self.ebios_rm_study - ).order_by("created_at") - ) - return sorted_operational_scenarios.index(self) + 1 + return self.attack_path.ref_id + + @property + def name(self): + return self.attack_path.name @property def gravity(self): @@ -477,6 +476,14 @@ def stakeholders(self): def ro_to(self): return self.attack_path.ro_to_couple + def get_assets(self): + return Asset.objects.filter( + feared_events__in=self.attack_path.ro_to_couple.feared_events.all() + ) + + def get_applied_controls(self): + return AppliedControl.objects.filter(stakeholders__in=self.stakeholders.all()) + def get_likelihood_display(self): if self.likelihood < 0: return {