Skip to content

Commit

Permalink
feat: auto fill risk assessment with ebios rm study
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohamed-Hacene committed Dec 16, 2024
1 parent 8f035e3 commit 9a48ca5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
31 changes: 31 additions & 0 deletions backend/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 *

Expand Down Expand Up @@ -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)
Expand Down
19 changes: 13 additions & 6 deletions backend/ebios_rm/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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 {
Expand Down

0 comments on commit 9a48ca5

Please sign in to comment.