Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[backend/frontend] Merge tabs in simulation form (#1515) #1827

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ public Exercise removeExerciseTeamPlayers(

// region exercises
@PostMapping(EXERCISE_URI)
public Exercise createExercise(@Valid @RequestBody ExerciseCreateInput input) {
public Exercise createExercise(@Valid @RequestBody ExerciseInput input) {
if (input == null) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Exercise input cannot be null");
}
Expand All @@ -394,7 +394,7 @@ public Exercise duplicateExercise(@PathVariable @NotBlank final String exerciseI
@PreAuthorize("isExercisePlanner(#exerciseId)")
@Transactional(rollbackOn = Exception.class)
public Exercise updateExerciseInformation(
@PathVariable String exerciseId, @Valid @RequestBody ExerciseUpdateInput input) {
@PathVariable String exerciseId, @Valid @RequestBody ExerciseInput input) {
Exercise exercise =
exerciseRepository.findById(exerciseId).orElseThrow(ElementNotFoundException::new);
exercise.setTags(iterableToSet(this.tagRepository.findAllById(input.getTagIds())));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package io.openbas.rest.exercise.form;

import static io.openbas.config.AppConfig.MANDATORY_MESSAGE;
import static io.openbas.config.AppConfig.NOW_FUTURE_MESSAGE;
import static io.openbas.config.AppConfig.*;
import static lombok.AccessLevel.NONE;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.annotation.Nullable;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.FutureOrPresent;
import jakarta.validation.constraints.NotBlank;
import java.time.Instant;
Expand All @@ -15,9 +15,12 @@
import java.util.List;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Data
public class ExerciseCreateInput {
public class ExerciseInput {

@NotBlank(message = MANDATORY_MESSAGE)
@JsonProperty("exercise_name")
Expand Down Expand Up @@ -51,6 +54,19 @@ public class ExerciseCreateInput {
@JsonProperty("exercise_tags")
private List<String> tagIds = new ArrayList<>();

@Email(message = EMAIL_FORMAT)
@JsonProperty("exercise_mail_from")
private String from;

@JsonProperty("exercise_mails_reply_to")
private List<String> replyTos;

@JsonProperty("exercise_message_header")
private String header;

@JsonProperty("exercise_message_footer")
private String footer;

public Instant getStart() {
return start != null ? start.truncatedTo(ChronoUnit.MINUTES) : null;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,11 @@ public Map<String, List<String>> getPlatformBannerByLevel() {
@NotNull
@JsonProperty("expectation_manual_default_score_value")
private int expectationDefaultScoreValue;

// EMAIL CONFIG
@JsonProperty("default_mailer")
private String defaultMailer;

@JsonProperty("default_reply_to")
private String defaultReplyTo;
}
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ public PlatformSettings findSettings() {
ofNullable(dbSettings.get(DEFAULT_LANG.key()))
.map(Setting::getValue)
.orElse(DEFAULT_LANG.defaultValue()));
platformSettings.setDefaultMailer(openBASConfig.getDefaultMailer());
platformSettings.setDefaultReplyTo(openBASConfig.getDefaultReplyTo());

// Build authenticated user settings
OpenBASPrincipal user = currentUser();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -578,8 +578,7 @@ const Injects: FunctionComponent<Props> = ({
edge="start"
checked={
(selectAll && !(inject.inject_id
in (deSelectedElements || {})))
|| inject.inject_id in (selectedElements || {})
in (deSelectedElements || {}))) || inject.inject_id in (selectedElements || {})
}
disableRipple
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ const RulesContractContent: React.FC<Props> = ({
}, []);

const onChangeInjectorContractId = () => {
directFetchInjectorContract(methods.getValues(`import_mapper_inject_importers.${index}.inject_importer_injector_contract`)).then((result: { data: InjectorContractConverted }) => {
directFetchInjectorContract(methods.getValues(`import_mapper_inject_importers.${index}.inject_importer_injector_contract`)).then((result: {
data: InjectorContractConverted;
}) => {
const injectorContract = result.data;
setInjectorContractLabel(tPick(injectorContract.injector_contract_labels));
const tmp = injectorContract?.convertedContent?.fields
Expand Down Expand Up @@ -244,8 +246,7 @@ const RulesContractContent: React.FC<Props> = ({
<Badge
color="secondary"
variant="dot"
invisible={(!methods.getValues(`import_mapper_inject_importers.${index}.inject_importer_rule_attributes.${rulesIndex}.rule_attribute_default_value`)
|| methods.getValues(`import_mapper_inject_importers.${index}.inject_importer_rule_attributes.${rulesIndex}.rule_attribute_default_value`)?.length === 0)
invisible={(!methods.getValues(`import_mapper_inject_importers.${index}.inject_importer_rule_attributes.${rulesIndex}.rule_attribute_default_value`) || methods.getValues(`import_mapper_inject_importers.${index}.inject_importer_rule_attributes.${rulesIndex}.rule_attribute_default_value`)?.length === 0)
&& (!methods.getValues(`import_mapper_inject_importers.${index}.inject_importer_rule_attributes.${rulesIndex}.rule_attribute_additional_config.timePattern`)
|| methods.getValues(`import_mapper_inject_importers.${index}.inject_importer_rule_attributes.${rulesIndex}.rule_attribute_additional_config`)?.timePattern?.length === 0)}
>
Expand All @@ -257,8 +258,7 @@ const RulesContractContent: React.FC<Props> = ({
<Badge
color="secondary"
variant="dot"
invisible={(!methods.getValues(`import_mapper_inject_importers.${index}.inject_importer_rule_attributes.${rulesIndex}.rule_attribute_default_value`)
|| methods.getValues(`import_mapper_inject_importers.${index}.inject_importer_rule_attributes.${rulesIndex}.rule_attribute_default_value`)?.length === 0)
invisible={(!methods.getValues(`import_mapper_inject_importers.${index}.inject_importer_rule_attributes.${rulesIndex}.rule_attribute_default_value`) || methods.getValues(`import_mapper_inject_importers.${index}.inject_importer_rule_attributes.${rulesIndex}.rule_attribute_default_value`)?.length === 0)
&& (!methods.getValues(`import_mapper_inject_importers.${index}.inject_importer_rule_attributes.${rulesIndex}.rule_attribute_additional_config.allTeamsValue`)
|| methods.getValues(`import_mapper_inject_importers.${index}.inject_importer_rule_attributes.${rulesIndex}.rule_attribute_additional_config`)?.allTeamsValue?.length === 0)}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,48 @@ import { useNavigate } from 'react-router-dom';

import { addExercise } from '../../../../actions/Exercise';
import type { ExerciseStore } from '../../../../actions/exercises/Exercise';
import type { LoggedHelper } from '../../../../actions/helper';
import ButtonCreate from '../../../../components/common/ButtonCreate';
import Drawer from '../../../../components/common/Drawer';
import { useFormatter } from '../../../../components/i18n';
import type { ExerciseCreateInput } from '../../../../utils/api-types';
import { useHelper } from '../../../../store';
import type { ExerciseInput, PlatformSettings } from '../../../../utils/api-types';
import { useAppDispatch } from '../../../../utils/hooks';
import ExerciseCreationForm from './ExerciseCreationForm';
import ExerciseForm from './ExerciseForm';

const ExerciseCreation = () => {
// Standard hooks
const [open, setOpen] = useState(false);
const { t } = useFormatter();
const navigate = useNavigate();
const dispatch = useAppDispatch();
const onSubmit = (data: ExerciseCreateInput) => {
const onSubmit = (data: ExerciseInput) => {
dispatch(addExercise(data)).then((result: { result: string; entities: { scenarios: Record<string, ExerciseStore> } }) => {
setOpen(false);
navigate(`/admin/exercises/${result.result}`);
});
};

const { settings }: { settings: PlatformSettings } = useHelper((helper: LoggedHelper) => ({
settings: helper.getPlatformSettings(),
}));

// Form
const initialValues: ExerciseInput = {
exercise_name: '',
exercise_subtitle: '',
exercise_description: '',
exercise_category: 'attack-scenario',
exercise_main_focus: 'incident-response',
exercise_severity: 'high',
exercise_tags: [],
exercise_start_date: null,
exercise_mail_from: settings.default_mailer,
exercise_mails_reply_to: [settings.default_reply_to ? settings.default_reply_to : ''],
exercise_message_header: t('SIMULATION HEADER'),
exercise_message_footer: t('SIMULATION FOOTER'),
};

return (
<>
<ButtonCreate onClick={() => setOpen(true)} />
Expand All @@ -30,9 +53,11 @@ const ExerciseCreation = () => {
handleClose={() => setOpen(false)}
title={t('Create a new simulation')}
>
<ExerciseCreationForm
<ExerciseForm
onSubmit={onSubmit}
handleClose={() => setOpen(false)}
initialValues={initialValues}
edit={false}
/>
</Drawer>
</>
Expand Down
Loading