Skip to content

Commit

Permalink
[backend/frontend] Fix removing a team from the context doesn't work (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
RomuDeuxfois authored Oct 19, 2024
1 parent 9274617 commit 0f6cfee
Show file tree
Hide file tree
Showing 19 changed files with 344 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.openbas.rest.exercise.exports.VariableMixin;
import io.openbas.rest.exercise.exports.VariableWithValueMixin;
import io.openbas.rest.exercise.form.*;
import io.openbas.rest.exercise.service.ExerciseService;
import io.openbas.rest.helper.RestBehavior;
import io.openbas.rest.helper.TeamHelper;
import io.openbas.rest.inject.form.InjectExpectationResultsByAttackPattern;
Expand Down Expand Up @@ -231,14 +232,7 @@ public Iterable<Team> addExerciseTeams(
@PreAuthorize("isExercisePlanner(#exerciseId)")
public Iterable<Team> removeExerciseTeams(@PathVariable String exerciseId,
@Valid @RequestBody ExerciseUpdateTeamsInput input) {
Exercise exercise = this.exerciseService.exercise(exerciseId);
// Remove teams from exercise
List<Team> teams = exercise.getTeams().stream().filter(team -> !input.getTeamIds().contains(team.getId())).toList();
exercise.setTeams(new ArrayList<>(teams));
this.exerciseService.updateExercise(exercise);
// Remove all association between users / exercises / teams
input.getTeamIds().forEach(exerciseTeamUserRepository::deleteTeamFromAllReferences);
return teamRepository.findAllById(input.getTeamIds());
return this.exerciseService.removeTeams(exerciseId, input.getTeamIds());
}

@Transactional(rollbackOn = Exception.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.openbas.database.model.ImportMapper;
import io.openbas.database.repository.ImportMapperRepository;
import io.openbas.rest.exception.ElementNotFoundException;
import io.openbas.rest.exercise.service.ExerciseService;
import io.openbas.rest.helper.RestBehavior;
import io.openbas.rest.scenario.form.InjectsImportInput;
import io.openbas.rest.scenario.response.ImportTestSummary;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package io.openbas.rest.exercise;
package io.openbas.rest.exercise.service;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import io.openbas.config.OpenBASConfig;
import io.openbas.database.model.*;
import io.openbas.database.raw.RawExerciseSimple;
import io.openbas.database.repository.ArticleRepository;
import io.openbas.database.repository.ExerciseRepository;
import io.openbas.database.repository.TeamRepository;
import io.openbas.database.repository.*;
import io.openbas.rest.exception.ElementNotFoundException;
import io.openbas.rest.exercise.form.ExerciseSimple;
import io.openbas.rest.inject.service.InjectDuplicateService;
Expand All @@ -23,7 +21,6 @@
import jakarta.persistence.Tuple;
import jakarta.persistence.TypedQuery;
import jakarta.persistence.criteria.*;
import jakarta.transaction.Transactional;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
Expand All @@ -33,6 +30,7 @@
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.time.Instant;
Expand Down Expand Up @@ -67,6 +65,9 @@ public class ExerciseService {
private final ArticleRepository articleRepository;
private final ExerciseRepository exerciseRepository;
private final TeamRepository teamRepository;
private final ExerciseTeamUserRepository exerciseTeamUserRepository;
private final InjectRepository injectRepository;
private final LessonsCategoryRepository lessonsCategoryRepository;

// region properties
@Value("${openbas.mail.imap.enabled}")
Expand All @@ -79,18 +80,19 @@ public class ExerciseService {
private OpenBASConfig openBASConfig;
// endregion

public List<ExerciseSimple> exercises(){
public List<ExerciseSimple> exercises() {
// We get the exercises depending on whether or not we are granted
List<RawExerciseSimple> exercises = currentUser().isAdmin() ? exerciseRepository.rawAll()
: exerciseRepository.rawAllGranted(currentUser().getId());

return exercises.stream().map(exercise->exerciseMapper.fromRawExerciseSimple(exercise)).collect(Collectors.toList());
return exercises.stream().map(exercise -> exerciseMapper.fromRawExerciseSimple(exercise))
.collect(Collectors.toList());
}

public Page<ExerciseSimple> exercises(
Specification<Exercise> specification,
Specification<Exercise> specificationCount,
Pageable pageable) {
Specification<Exercise> specification,
Specification<Exercise> specificationCount,
Pageable pageable) {
CriteriaBuilder cb = this.entityManager.getCriteriaBuilder();

CriteriaQuery<Tuple> cq = cb.createTupleQuery();
Expand Down Expand Up @@ -120,9 +122,11 @@ public Page<ExerciseSimple> exercises(
List<ExerciseSimple> exercises = execution(query);

for (ExerciseSimple exercise : exercises) {
if (exercise.getInjectIds() != null) {
exercise.setExpectationResultByTypes(resultUtils.getResultsByTypes(new HashSet<>(Arrays.asList(exercise.getInjectIds()))));
exercise.setTargets(resultUtils.getInjectTargetWithResults(new HashSet<>(Arrays.asList(exercise.getInjectIds()))));
if (exercise.getInjectIds() != null) {
exercise.setExpectationResultByTypes(
resultUtils.getResultsByTypes(new HashSet<>(Arrays.asList(exercise.getInjectIds()))));
exercise.setTargets(
resultUtils.getInjectTargetWithResults(new HashSet<>(Arrays.asList(exercise.getInjectIds()))));
}
}

Expand Down Expand Up @@ -189,7 +193,7 @@ private List<ExerciseSimple> execution(TypedQuery<Tuple> query) {

// -- CREATION --

@Transactional(rollbackOn = Exception.class)
@Transactional(rollbackFor = Exception.class)
public Exercise createExercise(@NotNull final Exercise exercise) {
if (imapEnabled) {
exercise.setFrom(imapUsername);
Expand Down Expand Up @@ -410,4 +414,20 @@ public Iterable<ExerciseSimple> scenarioExercises(@NotBlank String scenarioId) {
public List<AtomicTestingMapper.ExpectationResultsByType> getGlobalResults(@NotBlank String exerciseId) {
return resultUtils.getResultsByTypes(exerciseRepository.findInjectsByExercise(exerciseId));
}

// -- TEAMS --

@Transactional(rollbackFor = Exception.class)
public Iterable<Team> removeTeams(@NotBlank final String exerciseId, @NotNull final List<String> teamIds) {
// Remove teams from exercise
this.exerciseRepository.removeTeams(exerciseId, teamIds);
// Remove all association between users / exercises / teams
this.exerciseTeamUserRepository.deleteTeamsFromAllReferences(teamIds);
// Remove all association between injects and teams
this.injectRepository.removeTeamsForExercise(exerciseId, teamIds);
// Remove all association between lessons learned and teams
this.lessonsCategoryRepository.removeTeamsForExercise(exerciseId, teamIds);
return teamRepository.findAllById(teamIds);
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.openbas.rest.report;

import io.openbas.database.model.*;
import io.openbas.rest.exercise.ExerciseService;
import io.openbas.rest.exercise.service.ExerciseService;
import io.openbas.rest.helper.RestBehavior;
import io.openbas.rest.report.form.ReportInjectCommentInput;
import io.openbas.rest.report.form.ReportInput;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import io.openbas.aop.LogExecutionTime;
import io.openbas.database.model.Exercise;
import io.openbas.rest.exercise.ExerciseService;
import io.openbas.rest.exercise.service.ExerciseService;
import io.openbas.rest.exercise.form.ExerciseSimple;
import io.openbas.utils.pagination.SearchPaginationInput;
import jakarta.validation.Valid;
Expand Down
16 changes: 10 additions & 6 deletions openbas-api/src/main/java/io/openbas/service/ScenarioService.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ public class ScenarioService {
private final TeamService teamService;
private final FileService fileService;
private final InjectDuplicateService injectDuplicateService;

private final InjectRepository injectRepository;
private final LessonsCategoryRepository lessonsCategoryRepository;

@Transactional
public Scenario createScenario(@NotNull final Scenario scenario) {
Expand Down Expand Up @@ -460,13 +461,16 @@ public Iterable<Team> addTeams(@NotBlank final String scenarioId, @NotNull final
return teamsToAdd;
}

@Transactional(rollbackFor = Exception.class)
public Iterable<Team> removeTeams(@NotBlank final String scenarioId, @NotNull final List<String> teamIds) {
Scenario scenario = this.scenario(scenarioId);
List<Team> teams = scenario.getTeams().stream().filter(team -> !teamIds.contains(team.getId())).toList();
scenario.setTeams(new ArrayList<>(teams));
this.updateScenario(scenario);
// Remove teams from exercise
this.scenarioRepository.removeTeams(scenarioId, teamIds);
// Remove all association between users / exercises / teams
teamIds.forEach(this.scenarioTeamUserRepository::deleteTeamFromAllReferences);
this.scenarioTeamUserRepository.deleteTeamFromAllReferences(teamIds);
// Remove all association between injects and teams
this.injectRepository.removeTeamsForScenario(scenarioId, teamIds);
// Remove all association between lessons learned and teams
this.lessonsCategoryRepository.removeTeamsForScenario(scenarioId, teamIds);
return teamRepository.findAllById(teamIds);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package io.openbas.rest;

import io.openbas.IntegrationTest;
import io.openbas.database.model.*;
import io.openbas.database.model.Exercise;
import io.openbas.database.model.LessonsCategory;
import io.openbas.database.model.Team;
import io.openbas.database.model.User;
import io.openbas.database.repository.ExerciseRepository;
import io.openbas.database.repository.LessonsCategoryRepository;
import io.openbas.database.repository.TeamRepository;
import io.openbas.database.repository.UserRepository;
import io.openbas.rest.exercise.ExerciseService;
import io.openbas.rest.exercise.service.ExerciseService;
import io.openbas.rest.lessons.form.LessonsSendInput;
import io.openbas.service.MailingService;
import io.openbas.utils.mockUser.WithMockPlannerUser;
Expand All @@ -26,9 +29,9 @@
import static io.openbas.utils.fixtures.ExerciseLessonsCategoryFixture.getLessonsCategory;
import static io.openbas.utils.fixtures.TeamFixture.getTeam;
import static io.openbas.utils.fixtures.UserFixture.getUser;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.mockito.Mockito.verify;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.mockito.Mockito.*;


@SpringBootTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import io.openbas.database.model.InjectorContract;
import io.openbas.database.model.*;
import io.openbas.database.repository.*;
import io.openbas.rest.exercise.ExerciseService;
import io.openbas.rest.exercise.service.ExerciseService;
import io.openbas.rest.inject.form.InjectInput;
import io.openbas.service.ScenarioService;
import io.openbas.utils.fixtures.InjectExpectationFixture;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jayway.jsonpath.JsonPath;
import io.openbas.database.model.*;
import io.openbas.rest.exercise.ExerciseService;
import io.openbas.rest.exercise.service.ExerciseService;
import io.openbas.rest.mapper.MapperApi;
import io.openbas.rest.report.ReportApi;
import io.openbas.rest.report.form.ReportInjectCommentInput;
Expand Down
Loading

0 comments on commit 0f6cfee

Please sign in to comment.