From ded5cc55fa22ab369814e32899a11cbb6c16b3b9 Mon Sep 17 00:00:00 2001 From: Mohamed-Hacene Date: Thu, 5 Dec 2024 12:01:34 +0100 Subject: [PATCH 01/16] feat: workshop 2 --- frontend/messages/en.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/messages/en.json b/frontend/messages/en.json index a58960796..00f2403cd 100644 --- a/frontend/messages/en.json +++ b/frontend/messages/en.json @@ -385,7 +385,7 @@ "exportDatabase": "Export database", "upload": "Upload", "add": "Add", - "undefined": "--", + "undefined": "undefined", "production": "Production", "design": "Design", "development": "Development", From e52ce61e3e25dae4245708b10bc3d1f2f124f10c Mon Sep 17 00:00:00 2001 From: Mohamed-Hacene Date: Thu, 5 Dec 2024 13:38:47 +0100 Subject: [PATCH 02/16] feat: workshop 4 --- frontend/src/lib/utils/schemas.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/frontend/src/lib/utils/schemas.ts b/frontend/src/lib/utils/schemas.ts index a7429aa00..ace465917 100644 --- a/frontend/src/lib/utils/schemas.ts +++ b/frontend/src/lib/utils/schemas.ts @@ -1,4 +1,5 @@ // schema for the validation of forms +import { description, threats } from '$paraglide/messages'; import { z, type AnyZodObject } from 'zod'; const toArrayPreprocessor = (value: unknown) => { @@ -462,6 +463,16 @@ export const operationalScenarioSchema = z.object({ justification: z.string().optional() }); +export const operationalScenarioSchema = z.object({ + ebios_rm_study: z.string(), + attack_paths: z.string().uuid().array(), + threats: z.string().uuid().optional().array(), + description: z.string(), + likelihood: z.number().optional().default(-1), + is_selected: z.boolean().optional().default(false), + justification: z.string().optional(), +}); + const SCHEMA_MAP: Record = { folders: FolderSchema, projects: ProjectSchema, From 6e53cbcde19fadd443e6300fa4c1aadbbe2484f1 Mon Sep 17 00:00:00 2001 From: Mohamed-Hacene Date: Thu, 5 Dec 2024 13:39:24 +0100 Subject: [PATCH 03/16] chore: run format --- frontend/src/lib/utils/schemas.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/src/lib/utils/schemas.ts b/frontend/src/lib/utils/schemas.ts index ace465917..849f3200e 100644 --- a/frontend/src/lib/utils/schemas.ts +++ b/frontend/src/lib/utils/schemas.ts @@ -470,7 +470,7 @@ export const operationalScenarioSchema = z.object({ description: z.string(), likelihood: z.number().optional().default(-1), is_selected: z.boolean().optional().default(false), - justification: z.string().optional(), + justification: z.string().optional() }); const SCHEMA_MAP: Record = { From 571407f68f4c3bc8493861b70961d8c1a1e985ad Mon Sep 17 00:00:00 2001 From: Mohamed-Hacene Date: Fri, 6 Dec 2024 12:58:42 +0100 Subject: [PATCH 04/16] feat: add likelihood endpoint --- backend/ebios_rm/models.py | 1 + backend/ebios_rm/views.py | 2 +- frontend/messages/en.json | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/backend/ebios_rm/models.py b/backend/ebios_rm/models.py index 7b8853494..bf1e4f9fd 100644 --- a/backend/ebios_rm/models.py +++ b/backend/ebios_rm/models.py @@ -237,6 +237,7 @@ class Meta: verbose_name = _("RO/TO couple") verbose_name_plural = _("RO/TO couples") ordering = ["created_at"] + def save(self, *args, **kwargs): self.folder = self.ebios_rm_study.folder diff --git a/backend/ebios_rm/views.py b/backend/ebios_rm/views.py index bf5ec10b4..aaa1b5203 100644 --- a/backend/ebios_rm/views.py +++ b/backend/ebios_rm/views.py @@ -138,7 +138,7 @@ class OperationalScenarioViewSet(BaseModelViewSet): filterset_fields = [ "ebios_rm_study", ] - + @action(detail=True, name="Get risk matrix", url_path="risk-matrix") def risk_matrix(self, request, pk=None): attack_path = self.get_object() diff --git a/frontend/messages/en.json b/frontend/messages/en.json index 00f2403cd..a58960796 100644 --- a/frontend/messages/en.json +++ b/frontend/messages/en.json @@ -385,7 +385,7 @@ "exportDatabase": "Export database", "upload": "Upload", "add": "Add", - "undefined": "undefined", + "undefined": "--", "production": "Production", "design": "Design", "development": "Development", From d94aef9dde9b729b9b609a8f49ddcc24f2cdacb7 Mon Sep 17 00:00:00 2001 From: Mohamed-Hacene Date: Fri, 6 Dec 2024 12:59:23 +0100 Subject: [PATCH 05/16] chore: format --- backend/ebios_rm/models.py | 1 - backend/ebios_rm/views.py | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/backend/ebios_rm/models.py b/backend/ebios_rm/models.py index bf1e4f9fd..7b8853494 100644 --- a/backend/ebios_rm/models.py +++ b/backend/ebios_rm/models.py @@ -237,7 +237,6 @@ class Meta: verbose_name = _("RO/TO couple") verbose_name_plural = _("RO/TO couples") ordering = ["created_at"] - def save(self, *args, **kwargs): self.folder = self.ebios_rm_study.folder diff --git a/backend/ebios_rm/views.py b/backend/ebios_rm/views.py index aaa1b5203..bf5ec10b4 100644 --- a/backend/ebios_rm/views.py +++ b/backend/ebios_rm/views.py @@ -138,7 +138,7 @@ class OperationalScenarioViewSet(BaseModelViewSet): filterset_fields = [ "ebios_rm_study", ] - + @action(detail=True, name="Get risk matrix", url_path="risk-matrix") def risk_matrix(self, request, pk=None): attack_path = self.get_object() From d69b58ec0d10f696f3f5dc82a4c0f5e24b33a8fe Mon Sep 17 00:00:00 2001 From: Mohamed-Hacene Date: Fri, 6 Dec 2024 14:37:41 +0100 Subject: [PATCH 06/16] feat: create risk analysis from ebios detailed view --- .../Forms/ModelForm/RiskAssessmentForm.svelte | 1 + .../ebios-rm/[id=uuid]/+layout.server.ts | 7 +++ .../ebios-rm/[id=uuid]/+page.server.ts | 46 +++++++++++++-- .../ebios-rm/[id=uuid]/+page.svelte | 57 ++++++++++++++++++- .../(internal)/ebios-rm/[id=uuid]/Tile.svelte | 23 +++++--- 5 files changed, 119 insertions(+), 15 deletions(-) create mode 100644 frontend/src/routes/(app)/(internal)/ebios-rm/[id=uuid]/+layout.server.ts diff --git a/frontend/src/lib/components/Forms/ModelForm/RiskAssessmentForm.svelte b/frontend/src/lib/components/Forms/ModelForm/RiskAssessmentForm.svelte index 1a5f2d850..edbfe6f8d 100644 --- a/frontend/src/lib/components/Forms/ModelForm/RiskAssessmentForm.svelte +++ b/frontend/src/lib/components/Forms/ModelForm/RiskAssessmentForm.svelte @@ -65,6 +65,7 @@ bind:cachedValue={formDataCache['risk_matrix']} label={m.riskMatrix()} helpText={m.riskAssessmentMatrixHelpText()} + hidden={initialData.risk_matrix} /> { + return await loadDetail({ event, model: getModelInfo('ebios-rm'), id: event.params.id }); +}; diff --git a/frontend/src/routes/(app)/(internal)/ebios-rm/[id=uuid]/+page.server.ts b/frontend/src/routes/(app)/(internal)/ebios-rm/[id=uuid]/+page.server.ts index cb361f306..52f5ce694 100644 --- a/frontend/src/routes/(app)/(internal)/ebios-rm/[id=uuid]/+page.server.ts +++ b/frontend/src/routes/(app)/(internal)/ebios-rm/[id=uuid]/+page.server.ts @@ -1,7 +1,45 @@ -import { loadDetail } from '$lib/utils/load'; +import { defaultDeleteFormAction, defaultWriteFormAction } from '$lib/utils/actions'; +import { BASE_API_URL } from '$lib/utils/constants'; +import { + getModelInfo, + urlParamModelForeignKeyFields, + urlParamModelSelectFields +} from '$lib/utils/crud'; +import { modelSchema } from '$lib/utils/schemas'; +import type { ModelInfo } from '$lib/utils/types'; +import { type Actions } from '@sveltejs/kit'; +import { superValidate } from 'sveltekit-superforms'; +import { zod } from 'sveltekit-superforms/adapters'; import type { PageServerLoad } from './$types'; -import { getModelInfo } from '$lib/utils/crud'; -export const load: PageServerLoad = async (event) => { - return await loadDetail({ event, model: getModelInfo('ebios-rm'), id: event.params.id }); +export const load: PageServerLoad = async ({ params, fetch }) => { + const URLModel = 'ebios-rm'; + const model: ModelInfo = getModelInfo(URLModel); + + const endpoint = model.endpointUrl + ? `${BASE_API_URL}/${model.endpointUrl}/${params.id}/` + : `${BASE_API_URL}/${model.urlModel}/${params.id}/`; + const res = await fetch(endpoint); + const data = await res.json(); + + const initialData = { + risk_matrix: data.risk_matrix.id + } + + const createSchema = modelSchema('risk-assessments'); + const createRiskAnalysisForm = await superValidate(initialData, zod(createSchema), { errors: false }); + + return { createRiskAnalysisForm, model: getModelInfo('risk-assessments') }; }; + +export const actions: Actions = { + create: async (event) => { + // const redirectToWrittenObject = Boolean(event.params.model === 'entity-assessments'); + return defaultWriteFormAction({ + event, + urlModel: 'risk-assessments', + action: 'create' + // redirectToWrittenObject: redirectToWrittenObject + }); + }, +}; \ No newline at end of file diff --git a/frontend/src/routes/(app)/(internal)/ebios-rm/[id=uuid]/+page.svelte b/frontend/src/routes/(app)/(internal)/ebios-rm/[id=uuid]/+page.svelte index 59ef62bad..5416d192f 100644 --- a/frontend/src/routes/(app)/(internal)/ebios-rm/[id=uuid]/+page.svelte +++ b/frontend/src/routes/(app)/(internal)/ebios-rm/[id=uuid]/+page.svelte @@ -5,11 +5,19 @@ import { page } from '$app/stores'; import type { PageData } from './$types'; import { breadcrumbObject } from '$lib/utils/stores'; + import type { ModalComponent, ModalSettings, ModalStore } from '@skeletonlabs/skeleton'; + import { getModalStore } from '@skeletonlabs/skeleton'; + import CreateModal from '$lib/components/Modals/CreateModal.svelte'; + import MissingConstraintsModal from '$lib/components/Modals/MissingConstraintsModal.svelte'; + import { checkConstraints } from '$lib/utils/crud'; + const modalStore: ModalStore = getModalStore(); export let data: PageData; $: breadcrumbObject.set(data.data); + const riskAnalysisCreated: boolean = data.data.risk_assessments.length > 0; + const dummydata = { ws1: [ { @@ -80,13 +88,46 @@ } ], ws5: [ - { title: safeTranslate(m.ebiosWs5_1()), status: 'done', href: '#' }, + { title: safeTranslate(m.ebiosWs5_1()), status: riskAnalysisCreated ? 'done' : 'to_do', href: '#' }, { title: safeTranslate(m.ebiosWs5_2()), status: 'done', href: '#' }, { title: safeTranslate(m.ebiosWs5_3()), status: 'to_do', href: '#' }, { title: safeTranslate(m.ebiosWs5_4()), status: 'to_do', href: '#' }, { title: safeTranslate(m.ebiosWs5_5()), status: 'done', href: '#' } ] }; + + function modalCreateForm(): void { + let modalComponent: ModalComponent = { + ref: CreateModal, + props: { + form: data.createRiskAnalysisForm, + model: data.model + } + }; + let modal: ModalSettings = { + type: 'component', + component: modalComponent, + // Data + title: safeTranslate('add-' + data.model.localName) + }; + if ( + checkConstraints( + data.createRiskAnalysisForm.constraints, data.model.foreignKeys) + .length > 0 + ) { + modalComponent = { + ref: MissingConstraintsModal + }; + modal = { + type: 'component', + component: modalComponent, + title: m.warning(), + body: safeTranslate('add-' + data.model.localName).toLowerCase(), + value: checkConstraints(data.createRiskAnalysisForm.constraints, data.model.foreignKeys) + }; + } + modalStore.trigger(modal); + }
@@ -97,7 +138,19 @@ - + +
+ +
+
diff --git a/frontend/src/routes/(app)/(internal)/ebios-rm/[id=uuid]/Tile.svelte b/frontend/src/routes/(app)/(internal)/ebios-rm/[id=uuid]/Tile.svelte index 00fe2faee..766d33520 100644 --- a/frontend/src/routes/(app)/(internal)/ebios-rm/[id=uuid]/Tile.svelte +++ b/frontend/src/routes/(app)/(internal)/ebios-rm/[id=uuid]/Tile.svelte @@ -7,6 +7,7 @@ export let meta = null; export let href = '#'; export let accent_color = ''; + export let createRiskAnalysis = false;
@@ -42,15 +43,19 @@ {:else}
  • - - - - -

    {m.activity()} {i + 1}

    -

    {step.title}

    -
    + {#if createRiskAnalysis && i == 0} + + {:else} + + + + +

    {m.activity()} {i + 1}

    +

    {step.title}

    +
    + {/if}
  • {/if} {/each} From a63259cdc2669b1d1d99c19de7ec59c40a95b3b3 Mon Sep 17 00:00:00 2001 From: Mohamed-Hacene Date: Fri, 6 Dec 2024 14:45:11 +0100 Subject: [PATCH 07/16] feat: link risk analysis created to ebios study --- backend/core/serializers.py | 8 +++++++ .../Forms/ModelForm/RiskAssessmentForm.svelte | 12 ++++++++++ frontend/src/lib/utils/crud.ts | 3 ++- frontend/src/lib/utils/schemas.ts | 3 ++- .../ebios-rm/[id=uuid]/+page.server.ts | 3 ++- .../(internal)/ebios-rm/[id=uuid]/Tile.svelte | 22 +++++++++++-------- 6 files changed, 39 insertions(+), 12 deletions(-) diff --git a/backend/core/serializers.py b/backend/core/serializers.py index e55df2c53..25ef7029b 100644 --- a/backend/core/serializers.py +++ b/backend/core/serializers.py @@ -191,6 +191,14 @@ class Meta: class RiskAssessmentWriteSerializer(BaseModelSerializer): + ebios_rm_studies = serializers.PrimaryKeyRelatedField( + many=True, + queryset=EbiosRMStudy.objects.all(), + required=False, + allow_null=True, + write_only=True, + ) + class Meta: model = RiskAssessment exclude = ["created_at", "updated_at"] diff --git a/frontend/src/lib/components/Forms/ModelForm/RiskAssessmentForm.svelte b/frontend/src/lib/components/Forms/ModelForm/RiskAssessmentForm.svelte index edbfe6f8d..e1e4510a6 100644 --- a/frontend/src/lib/components/Forms/ModelForm/RiskAssessmentForm.svelte +++ b/frontend/src/lib/components/Forms/ModelForm/RiskAssessmentForm.svelte @@ -110,4 +110,16 @@ cacheLock={cacheLocks['observation']} bind:cachedValue={formDataCache['observation']} /> + {#if initialData.ebios_rm_studies} +