Skip to content

Commit

Permalink
[frontend/backend] fix auto_new_marking update (#7944)
Browse files Browse the repository at this point in the history
  • Loading branch information
Archidoit committed Nov 19, 2024
1 parent e9a32c8 commit 70dc0c8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ interface MarkingsSelectFieldInternalValue {
[key: string]: string
}

interface MarkingsSelectFieldProps extends FieldProps<MarkingsSelectFieldValue> {
interface MaxShareableMarkingsSelectFieldProps extends FieldProps<MarkingsSelectFieldValue> {
markingDefinitions: EntityMarkingDefinition[]
onChange?: (type: string, val: string) => void
}

const ALL_ID = 'all';
const NOT_SHAREABLE_ID = 'none';

const MarkingsSelectField = ({
const MaxShareableMarkingsSelectField = ({
form,
field,
markingDefinitions,
onChange,
}: MarkingsSelectFieldProps) => {
}: MaxShareableMarkingsSelectFieldProps) => {
const { t_i18n } = useFormatter();
const { setFieldValue } = form;
const { value, name } = field;
Expand Down Expand Up @@ -97,4 +97,4 @@ const MarkingsSelectField = ({
);
};

export default MarkingsSelectField;
export default MaxShareableMarkingsSelectField;
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Alert from '@mui/lab/Alert';
import makeStyles from '@mui/styles/makeStyles';
import { Field, Form, Formik } from 'formik';
import Typography from '@mui/material/Typography';
import MarkingsSelectField from '@components/common/form/MarkingsSelectField';
import MaxShareableMarkingsSelectField from '@components/common/form/MaxShareableMarkingsSelectField';
import { QueryRenderer } from '../../../../relay/environment';
import { useFormatter } from '../../../../components/i18n';
import { markingDefinitionsLinesSearchQuery } from '../marking_definitions/MarkingDefinitionsLines';
Expand Down Expand Up @@ -344,7 +344,7 @@ const GroupEditionMarkingsComponent = ({
)}
</Alert>
<Field
component={MarkingsSelectField}
component={MaxShareableMarkingsSelectField}
markingDefinitions={proposedShareableMarkings}
name="shareableMarkings"
onChange={(type: string, markingId: string) => handleToggleMaxShareableMarkings(type, markingId)}
Expand Down
18 changes: 13 additions & 5 deletions opencti-platform/opencti-graphql/src/domain/markingDefinition.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { ENTITY_TYPE_GROUP } from '../schema/internalObject';
import { SYSTEM_USER } from '../utils/access';
import { RELATION_ACCESSES_TO } from '../schema/internalRelationship';
import { groupAddRelation, groupEditField } from './group';
import { EditOperation } from '../generated/graphql';

export const findById = (context, user, markingDefinitionId) => {
return storeLoadById(context, user, markingDefinitionId, ENTITY_TYPE_MARKING_DEFINITION);
Expand Down Expand Up @@ -37,22 +36,31 @@ export const addAllowedMarkingDefinition = async (context, user, markingDefiniti
// Bypass current right to read group
const groupsWithAutoNewMarking = await listEntities(context, SYSTEM_USER, [ENTITY_TYPE_GROUP], { filters, connectionFormat: false });
if (groupsWithAutoNewMarking && groupsWithAutoNewMarking.length > 0) {
const markingType = element.definition_type;
const markingId = element.id;
// add marking in allowed markings
await Promise.all(
groupsWithAutoNewMarking.map((group) => {
return groupAddRelation(context, SYSTEM_USER, group.id, {
relationship_type: RELATION_ACCESSES_TO,
toId: element.id,
toId: markingId,
});
})
);
// add marking in max shareable markings
const groupsWithShareableMarkingToUpdate = groupsWithAutoNewMarking
.filter((g) => !g.max_shareable_markings
.find((m) => m.definition_type === markingType && m.x_opencti_order > element.x_opencti_order));
await Promise.all(
groupsWithAutoNewMarking.map((group) => {
groupsWithShareableMarkingToUpdate.map((group) => {
const currentMarkings = group.max_shareable_markings;
const finalMarkings = [
...currentMarkings.filter(({ type: t }) => t !== markingType),
...[{ type: markingType, value: markingId }],
];
return groupEditField(context, SYSTEM_USER, group.id, [{
key: 'max_shareable_markings',
value: [{ type: element.definition_type, value: element.id }],
operation: EditOperation.Add,
value: finalMarkings,
}]);
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('Raw streams tests', () => {
const createEvents = events.filter((e) => e.type === EVENT_TYPE_CREATE);
// Check some events count
const createEventsByTypes = R.groupBy((e) => e.data.data.type, createEvents);
expect(createEventsByTypes['marking-definition'].length).toBe(19);
expect(createEventsByTypes['marking-definition'].length).toBe(20);
expect(createEventsByTypes['external-reference'].length).toBe(17);
expect(createEventsByTypes['kill-chain-phase'].length).toBe(3);
expect(createEventsByTypes['course-of-action'].length).toBe(3);
Expand Down Expand Up @@ -46,7 +46,7 @@ describe('Raw streams tests', () => {
expect(createEventsByTypes.tool.length).toBe(2);
expect(createEventsByTypes.vocabulary.length).toBe(342); // 328 created at init + 2 created in tests + 5 vocabulary organizations types + 7 persona
expect(createEventsByTypes.vulnerability.length).toBe(7);
expect(createEvents.length).toBe(798);
expect(createEvents.length).toBe(799);
for (let createIndex = 0; createIndex < createEvents.length; createIndex += 1) {
const { data: insideData, origin, type } = createEvents[createIndex];
expect(origin).toBeDefined();
Expand Down

0 comments on commit 70dc0c8

Please sign in to comment.