From 6143aa4860db8fc6a96e549cc4c48e0a9bce2967 Mon Sep 17 00:00:00 2001 From: Gael Leblan Date: Fri, 18 Oct 2024 01:01:05 +0200 Subject: [PATCH] [backend/frontend] Prevent users from deleting SP logos (#1660) --- .../java/io/openbas/rest/document/DocumentApi.java | 13 +++++++++++-- .../components/documents/DocumentPopover.js | 2 +- openbas-front/src/utils/api-types.d.ts | 1 + .../openbas/database/raw/RawPaginationDocument.java | 1 + .../repository/SecurityPlatformRepository.java | 7 +++++++ 5 files changed, 21 insertions(+), 3 deletions(-) diff --git a/openbas-api/src/main/java/io/openbas/rest/document/DocumentApi.java b/openbas-api/src/main/java/io/openbas/rest/document/DocumentApi.java index c9895b6526..fdbcde2a23 100644 --- a/openbas-api/src/main/java/io/openbas/rest/document/DocumentApi.java +++ b/openbas-api/src/main/java/io/openbas/rest/document/DocumentApi.java @@ -269,13 +269,18 @@ public List documents() { @PostMapping("/api/documents/search") public Page searchDocuments(@RequestBody @Valid final SearchPaginationInput searchPaginationInput) { OpenBASPrincipal user = currentUser(); + List securityPlatformLogos = securityPlatformRepository.securityPlatformLogo(); if (user.isAdmin()) { return buildPaginationJPA( (Specification specification, Pageable pageable) -> this.documentRepository.findAll( specification, pageable), searchPaginationInput, Document.class - ).map(RawPaginationDocument::new); + ).map((document) -> { + var rawPaginationDocument = new RawPaginationDocument(document); + rawPaginationDocument.setDocument_can_be_deleted(!securityPlatformLogos.contains(document)); + return rawPaginationDocument; + }); } else { return buildPaginationJPA( (Specification specification, Pageable pageable) -> this.documentRepository.findAll( @@ -284,7 +289,11 @@ public Page searchDocuments(@RequestBody @Valid final Sea ), searchPaginationInput, Document.class - ).map(RawPaginationDocument::new); + ).map((document) -> { + var rawPaginationDocument = new RawPaginationDocument(document); + rawPaginationDocument.setDocument_can_be_deleted(!securityPlatformLogos.contains(document)); + return rawPaginationDocument; + }); } } diff --git a/openbas-front/src/admin/components/components/documents/DocumentPopover.js b/openbas-front/src/admin/components/components/documents/DocumentPopover.js index 2302f91c4f..8ddbb8a224 100644 --- a/openbas-front/src/admin/components/components/documents/DocumentPopover.js +++ b/openbas-front/src/admin/components/components/documents/DocumentPopover.js @@ -164,7 +164,7 @@ const DocumentPopover = (props) => { )} {!onRemoveDocument && ( - + {t('Delete')} )} diff --git a/openbas-front/src/utils/api-types.d.ts b/openbas-front/src/utils/api-types.d.ts index 2f7824239f..d73b8022be 100644 --- a/openbas-front/src/utils/api-types.d.ts +++ b/openbas-front/src/utils/api-types.d.ts @@ -2598,6 +2598,7 @@ export interface RawDocument { } export interface RawPaginationDocument { + document_can_be_deleted?: boolean; document_description?: string; document_exercises?: string[]; document_id?: string; diff --git a/openbas-model/src/main/java/io/openbas/database/raw/RawPaginationDocument.java b/openbas-model/src/main/java/io/openbas/database/raw/RawPaginationDocument.java index 92cdc8b63d..e1c8dfae81 100644 --- a/openbas-model/src/main/java/io/openbas/database/raw/RawPaginationDocument.java +++ b/openbas-model/src/main/java/io/openbas/database/raw/RawPaginationDocument.java @@ -18,6 +18,7 @@ public class RawPaginationDocument { List document_scenarios; String document_type; List document_tags; + boolean document_can_be_deleted = true; public RawPaginationDocument(final Document document) { this.document_id = document.getId(); diff --git a/openbas-model/src/main/java/io/openbas/database/repository/SecurityPlatformRepository.java b/openbas-model/src/main/java/io/openbas/database/repository/SecurityPlatformRepository.java index 3b0ab2f30b..c8b29948c2 100644 --- a/openbas-model/src/main/java/io/openbas/database/repository/SecurityPlatformRepository.java +++ b/openbas-model/src/main/java/io/openbas/database/repository/SecurityPlatformRepository.java @@ -1,5 +1,6 @@ package io.openbas.database.repository; +import io.openbas.database.model.Document; import io.openbas.database.model.SecurityPlatform; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.Query; @@ -8,6 +9,7 @@ import org.springframework.stereotype.Repository; import java.time.Instant; +import java.util.List; import java.util.Optional; @Repository @@ -29,4 +31,9 @@ public interface SecurityPlatformRepository extends CrudRepository securityPlatformLogo(); }