Repository: OpenAEV-Platform/openaev (java)

Issue:
RBAC: Remove auto-planner/auto-observer
## Context
Auto grants are being removed as we will be able to assign the new assessment capabilities to give access to all the scenario/simulation

## Goal
- Auto-planner/Auto-observer options are removed from the front end
- Auto grants logic is removed from the back


Discussion:

Patch:
diff --git a/openbas-api/src/main/java/io/openbas/migration/V4_25__Remove_Default_Grants_Table.java b/openbas-api/src/main/java/io/openbas/migration/V4_25__Remove_Default_Grants_Table.java
new file mode 100644
--- /dev/null
+++ b/openbas-api/src/main/java/io/openbas/migration/V4_25__Remove_Default_Grants_Table.java
@@ -0,0 +1,18 @@
+package io.openbas.migration;
+
+import java.sql.Connection;
+import java.sql.Statement;
+import org.flywaydb.core.api.migration.BaseJavaMigration;
+import org.flywaydb.core.api.migration.Context;
+import org.springframework.stereotype.Component;
+
+@Component
+public class V4_25__Remove_Default_Grants_Table extends BaseJavaMigration {
+
+  @Override
+  public void migrate(Context context) throws Exception {
+    Connection connection = context.getConnection();
+    Statement stmt = connection.createStatement();
+    stmt.execute("DROP TABLE IF EXISTS groups_default_grants CASCADE;");
+  }
+}
diff --git a/openbas-api/src/main/java/io/openbas/rest/exercise/service/ExerciseService.java b/openbas-api/src/main/java/io/openbas/rest/exercise/service/ExerciseService.java
--- a/openbas-api/src/main/java/io/openbas/rest/exercise/service/ExerciseService.java
+++ b/openbas-api/src/main/java/io/openbas/rest/exercise/service/ExerciseService.java
@@ -135,7 +135,6 @@ public Exercise createExercise(@NotNull final Exercise exercise) {
       }
     }
 
-    this.grantService.computeGrant(exercise);
     actionMetricCollector.addSimulationCreatedCount();
     return exerciseRepository.save(exercise);
   }
diff --git a/openbas-api/src/main/java/io/openbas/rest/group/GroupApi.java b/openbas-api/src/main/java/io/openbas/rest/group/GroupApi.java
--- a/openbas-api/src/main/java/io/openbas/rest/group/GroupApi.java
+++ b/openbas-api/src/main/java/io/openbas/rest/group/GroupApi.java
@@ -64,7 +64,6 @@ public Group group(@PathVariable String groupId) {
   public Group createGroup(@Valid @RequestBody GroupCreateInput input) {
     Group group = new Group();
     group.setUpdateAttributes(input);
-    group.setDefaultGrants(input.getDefaultGrants());
     return groupRepository.save(group);
   }
 
@@ -127,7 +126,6 @@ public Group updateGroupInformation(
       @PathVariable String groupId, @Valid @RequestBody GroupCreateInput input) {
     Group group = groupRepository.findById(groupId).orElseThrow(ElementNotFoundException::new);
     group.setUpdateAttributes(input);
-    group.setDefaultGrants(input.getDefaultGrants());
     return groupRepository.save(group);
   }
 
diff --git a/openbas-api/src/main/java/io/openbas/rest/group/form/GroupCreateInput.java b/openbas-api/src/main/java/io/openbas/rest/group/form/GroupCreateInput.java
--- a/openbas-api/src/main/java/io/openbas/rest/group/form/GroupCreateInput.java
+++ b/openbas-api/src/main/java/io/openbas/rest/group/form/GroupCreateInput.java
@@ -3,10 +3,7 @@
 import static io.openbas.config.AppConfig.MANDATORY_MESSAGE;
 
 import com.fasterxml.jackson.annotation.JsonProperty;
-import io.openbas.database.model.DefaultGrant;
 import jakarta.validation.constraints.NotBlank;
-import java.util.HashSet;
-import java.util.Set;
 import lombok.Getter;
 import lombok.Setter;
 
@@ -21,9 +18,6 @@ public class GroupCreateInput {
   @JsonProperty("group_description")
   private String description;
 
-  @JsonProperty("group_default_grants")
-  private Set<DefaultGrant> defaultGrants = new HashSet<>();
-
   @JsonProperty("group_default_user_assign")
   private boolean defaultUserAssignation;
 }
diff --git a/openbas-api/src/main/java/io/openbas/rest/payload/service/PayloadCreationService.java b/openbas-api/src/main/java/io/openbas/rest/payload/service/PayloadCreationService.java
--- a/openbas-api/src/main/java/io/openbas/rest/payload/service/PayloadCreationService.java
+++ b/openbas-api/src/main/java/io/openbas/rest/payload/service/PayloadCreationService.java
@@ -63,7 +63,6 @@ private Payload create(PayloadCreateInput input, List<AttackPattern> attackPatte
       fileDrop.setFileDropFile(documentService.document(input.getFileDropFile()));
     }
 
-    this.grantService.computeGrant(payload);
     Payload saved = payloadRepository.save(payload);
     payloadService.updateInjectorContractsForPayload(saved);
     return saved;
diff --git a/openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java b/openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java
--- a/openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java
+++ b/openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java
@@ -57,10 +57,9 @@ public class AtomicTestingService {
   private final DocumentRepository documentRepository;
   private final AssetGroupService assetGroupService;
   private final UserService userService;
-  private final GrantService grantService;
-
   private final InjectSearchService injectSearchService;
   private final InjectService injectService;
+  private final GrantService grantService;
 
   // -- CRUD --
 
diff --git a/openbas-api/src/main/java/io/openbas/service/GrantService.java b/openbas-api/src/main/java/io/openbas/service/GrantService.java
--- a/openbas-api/src/main/java/io/openbas/service/GrantService.java
+++ b/openbas-api/src/main/java/io/openbas/service/GrantService.java
@@ -1,19 +1,14 @@
 package io.openbas.service;
 
-import static io.openbas.helper.StreamHelper.fromIterable;
-
+import io.openbas.database.model.*;
 import io.openbas.database.model.Grant;
-import io.openbas.database.model.GrantableBase;
-import io.openbas.database.model.Group;
 import io.openbas.database.model.User;
 import io.openbas.database.repository.*;
 import jakarta.validation.constraints.NotBlank;
 import jakarta.validation.constraints.NotNull;
-import java.util.List;
 import lombok.RequiredArgsConstructor;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.stereotype.Service;
-import reactor.util.function.Tuples;
 
 @Service
 @RequiredArgsConstructor
@@ -26,42 +21,6 @@ public class GrantService {
   private final InjectRepository injectRepository;
   private final PayloadRepository payloadRepository;
 
-  public <T extends GrantableBase> void computeGrant(@NotNull T resource) {
-    // Extract grant resource type
-    Grant.GRANT_RESOURCE_TYPE grantResourceType =
-        GrantableBase.getGrantResourceType(resource.getClass());
-    // Find automatic groups to grants
-    List<Group> groups = fromIterable(this.groupRepository.findAll());
-    List<Grant> grants =
-        groups.stream()
-            .filter(
-                group -> {
-                  List<Grant.GRANT_TYPE> defaultGrants =
-                      group.getDefaultGrantsMap().get(grantResourceType);
-                  return defaultGrants != null && !defaultGrants.isEmpty();
-                })
-            .flatMap(
-                group ->
-                    group.getDefaultGrantsMap().get(grantResourceType).stream()
-                        .distinct()
-                        .map(s -> Tuples.of(group, s)))
-            .map(
-                tuple -> {
-                  Grant grant = new Grant();
-                  grant.setGroup(tuple.getT1());
-                  grant.setName(tuple.getT2());
-                  grant.setResourceId(resource.getId());
-                  grant.setGrantResourceType(grantResourceType);
-                  return grant;
-                })
-            .toList();
-
-    if (!grants.isEmpty()) {
-      Iterable<Grant> savedGrants = this.grantRepository.saveAll(grants);
-      resource.setGrants(fromIterable(savedGrants));
-    }
-  }
-
   public boolean hasReadGrant(@NotBlank final String resourceId, @NotNull final User user) {
     return hasGrant(resourceId, user, Grant.GRANT_TYPE.OBSERVER);
   }
diff --git a/openbas-api/src/main/java/io/openbas/service/ScenarioService.java b/openbas-api/src/main/java/io/openbas/service/ScenarioService.java
--- a/openbas-api/src/main/java/io/openbas/service/ScenarioService.java
+++ b/openbas-api/src/main/java/io/openbas/service/ScenarioService.java
@@ -105,7 +105,6 @@ public class ScenarioService {
   private final LicenseCacheManager licenseCacheManager;
 
   private final Ee eeService;
-  private final GrantService grantService;
   private final VariableService variableService;
   private final ChallengeService challengeService;
   private final TeamService teamService;
@@ -121,7 +120,6 @@ public class ScenarioService {
   @Transactional
   public Scenario createScenario(@NotNull final Scenario scenario) {
     computeEmails(scenario);
-    this.grantService.computeGrant(scenario);
     this.actionMetricCollector.addScenarioCreatedCount();
     return this.scenarioRepository.save(scenario);
   }
diff --git a/openbas-model/src/main/java/io/openbas/database/model/Group.java b/openbas-model/src/main/java/io/openbas/database/model/Group.java
--- a/openbas-model/src/main/java/io/openbas/database/model/Group.java
+++ b/openbas-model/src/main/java/io/openbas/database/model/Group.java
@@ -1,7 +1,6 @@
 package io.openbas.database.model;
 
 import com.fasterxml.jackson.annotation.JsonIgnore;
-import com.fasterxml.jackson.annotation.JsonInclude;
 import com.fasterxml.jackson.annotation.JsonProperty;
 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
 import io.openbas.annotation.Queryable;
@@ -13,7 +12,6 @@
 import jakarta.persistence.*;
 import jakarta.validation.constraints.NotBlank;
 import java.util.*;
-import java.util.stream.Collectors;
 import lombok.Getter;
 import lombok.Setter;
 import org.hibernate.annotations.Fetch;
@@ -50,12 +48,6 @@ public class Group implements Base {
   @JsonProperty("group_default_user_assign")
   private boolean defaultUserAssignation;
 
-  @ElementCollection(fetch = FetchType.EAGER)
-  @CollectionTable(name = "groups_default_grants", joinColumns = @JoinColumn(name = "group_id"))
-  @JsonProperty("group_default_grants")
-  @JsonInclude(JsonInclude.Include.NON_NULL)
-  private Set<DefaultGrant> defaultGrants = new HashSet<>();
-
   @OneToMany(
       mappedBy = "group",
       fetch = FetchType.EAGER,
@@ -101,36 +93,6 @@ public class Group implements Base {
   @Transient
   private final ResourceType resourceType = ResourceType.USER_GROUP;
 
-  // region transient
-  // Convert to Map for easier access
-  @Transient
-  @JsonIgnore
-  public Map<Grant.GRANT_RESOURCE_TYPE, List<Grant.GRANT_TYPE>> getDefaultGrantsMap() {
-    return defaultGrants.stream()
-        .collect(
-            Collectors.groupingBy(
-                DefaultGrant::getGrantResourceType,
-                Collectors.mapping(DefaultGrant::getGrantType, Collectors.toList())));
-  }
-
-  @JsonIgnore
-  @Transient
-  public List<Grant.GRANT_TYPE> getSimulationsDefaultGrants() {
-    return defaultGrants.stream()
-        .filter(dg -> dg.getGrantResourceType() == Grant.GRANT_RESOURCE_TYPE.SIMULATION)
-        .map(DefaultGrant::getGrantType)
-        .collect(Collectors.toList());
-  }
-
-  @JsonIgnore
-  @Transient
-  public List<Grant.GRANT_TYPE> getScenariosDefaultGrants() {
-    return defaultGrants.stream()
-        .filter(dg -> dg.getGrantResourceType() == Grant.GRANT_RESOURCE_TYPE.SCENARIO)
-        .map(DefaultGrant::getGrantType)
-        .collect(Collectors.toList());
-  }
-
   // endregion
 
   @Override


Test patch:
diff --git a/openbas-api/src/test/java/io/openbas/rest/scenario/ScenarioToExerciseServiceTest.java b/openbas-api/src/test/java/io/openbas/rest/scenario/ScenarioToExerciseServiceTest.java
--- a/openbas-api/src/test/java/io/openbas/rest/scenario/ScenarioToExerciseServiceTest.java
+++ b/openbas-api/src/test/java/io/openbas/rest/scenario/ScenarioToExerciseServiceTest.java
@@ -247,8 +247,6 @@ void scenarioToExerciseTest() {
     // -- ASSERT --
     assertNotNull(exerciseSaved);
     assertEquals(name, exerciseSaved.getName());
-    // Grants
-    assertEquals(3, (long) exerciseSaved.getGrants().size());
     // User & Teams
     assertEquals(2, (long) exerciseSaved.getTeams().size());
     assertTrue(
diff --git a/openbas-api/src/test/java/io/openbas/service/ScenarioServiceTest.java b/openbas-api/src/test/java/io/openbas/service/ScenarioServiceTest.java
--- a/openbas-api/src/test/java/io/openbas/service/ScenarioServiceTest.java
+++ b/openbas-api/src/test/java/io/openbas/service/ScenarioServiceTest.java
@@ -82,7 +82,6 @@ void setUp() {
             actionMetricCollector,
             licenseCacheManager,
             eeService,
-            grantService,
             variableService,
             challengeService,
             teamService,
@@ -108,7 +107,6 @@ void setUpWithMockRepository() {
             actionMetricCollector,
             licenseCacheManager,
             eeService,
-            grantService,
             variableService,
             challengeService,
             teamService,
diff --git a/openbas-api/src/test/java/io/openbas/utils/mockUser/WithMockObserverUserSecurityContextFactory.java b/openbas-api/src/test/java/io/openbas/utils/mockUser/WithMockObserverUserSecurityContextFactory.java
--- a/openbas-api/src/test/java/io/openbas/utils/mockUser/WithMockObserverUserSecurityContextFactory.java
+++ b/openbas-api/src/test/java/io/openbas/utils/mockUser/WithMockObserverUserSecurityContextFactory.java
@@ -3,7 +3,6 @@
 import static io.openbas.database.model.Grant.GRANT_TYPE.OBSERVER;
 import static io.openbas.service.UserService.buildAuthenticationToken;
 
-import io.openbas.database.model.DefaultGrant;
 import io.openbas.database.model.Grant;
 import io.openbas.database.model.Group;
 import io.openbas.database.model.User;
@@ -65,12 +64,6 @@ private void createObserverMockUser() {
     if (groupOpt.isEmpty()) {
       Group newGroup = new Group();
       newGroup.setName(MOCK_OBSERVER_GROUP);
-      newGroup
-          .getDefaultGrants()
-          .add(new DefaultGrant(OBSERVER, Grant.GRANT_RESOURCE_TYPE.SCENARIO));
-      newGroup
-          .getDefaultGrants()
-          .add(new DefaultGrant(OBSERVER, Grant.GRANT_RESOURCE_TYPE.SIMULATION));
       group = this.groupRepository.save(newGroup);
       // Create grant
       Grant grant = new Grant();
diff --git a/openbas-api/src/test/java/io/openbas/utils/mockUser/WithMockPlannerUserSecurityContextFactory.java b/openbas-api/src/test/java/io/openbas/utils/mockUser/WithMockPlannerUserSecurityContextFactory.java
--- a/openbas-api/src/test/java/io/openbas/utils/mockUser/WithMockPlannerUserSecurityContextFactory.java
+++ b/openbas-api/src/test/java/io/openbas/utils/mockUser/WithMockPlannerUserSecurityContextFactory.java
@@ -3,7 +3,6 @@
 import static io.openbas.database.model.Grant.GRANT_TYPE.PLANNER;
 import static io.openbas.service.UserService.buildAuthenticationToken;
 
-import io.openbas.database.model.DefaultGrant;
 import io.openbas.database.model.Grant;
 import io.openbas.database.model.Group;
 import io.openbas.database.model.User;
@@ -64,12 +63,6 @@ private void createPlannerMockUser() {
     if (groupOpt.isEmpty()) {
       Group newGroup = new Group();
       newGroup.setName(MOCK_PLANNER_GROUP);
-      newGroup
-          .getDefaultGrants()
-          .add(new DefaultGrant(Grant.GRANT_TYPE.PLANNER, Grant.GRANT_RESOURCE_TYPE.SCENARIO));
-      newGroup
-          .getDefaultGrants()
-          .add(new DefaultGrant(Grant.GRANT_TYPE.PLANNER, Grant.GRANT_RESOURCE_TYPE.SIMULATION));
       group = this.groupRepository.save(newGroup);
       // Create grant
       Grant grant = new Grant();
diff --git a/openbas-api/src/test/java/io/openbas/utils/mockUser/WithMockUserFullPermissionsSecurityContextFactory.java b/openbas-api/src/test/java/io/openbas/utils/mockUser/WithMockUserFullPermissionsSecurityContextFactory.java
--- a/openbas-api/src/test/java/io/openbas/utils/mockUser/WithMockUserFullPermissionsSecurityContextFactory.java
+++ b/openbas-api/src/test/java/io/openbas/utils/mockUser/WithMockUserFullPermissionsSecurityContextFactory.java
@@ -105,12 +105,6 @@ public void createPlannerMockUser() {
     if (groupOpt.isEmpty()) {
       Group newGroup = new Group();
       newGroup.setName(MOCK_FULL_PERMISSION_GROUP);
-      newGroup
-          .getDefaultGrants()
-          .add(new DefaultGrant(Grant.GRANT_TYPE.PLANNER, Grant.GRANT_RESOURCE_TYPE.SCENARIO));
-      newGroup
-          .getDefaultGrants()
-          .add(new DefaultGrant(Grant.GRANT_TYPE.PLANNER, Grant.GRANT_RESOURCE_TYPE.SIMULATION));
       newGroup.setRoles(List.of(role));
       group = this.groupRepository.save(newGroup);
       // Create grant