Skip to content

Commit

Permalink
[backend] Avoid creation groups relation when marking already exists
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelHassine committed Sep 20, 2023
1 parent dd9fe4b commit 03e290e
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions opencti-platform/opencti-graphql/src/domain/markingDefinition.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,22 @@ export const findAll = (context, user, args) => {
export const addMarkingDefinition = async (context, user, markingDefinition) => {
const markingColor = markingDefinition.x_opencti_color ? markingDefinition.x_opencti_color : '#ffffff';
const markingToCreate = R.assoc('x_opencti_color', markingColor, markingDefinition);
const element = await createEntity(context, user, markingToCreate, ENTITY_TYPE_MARKING_DEFINITION);
const filters = [{ key: 'auto_new_marking', values: [true] }];
// Bypass current right to read group
const groups = await listEntities(context, SYSTEM_USER, [ENTITY_TYPE_GROUP], { filters, connectionFormat: false });
if (groups && groups.length > 0) {
await Promise.all(
groups.map((group) => {
return groupAddRelation(context, SYSTEM_USER, group.id, {
relationship_type: RELATION_ACCESSES_TO,
toId: element.id,
});
})
);
const result = await createEntity(context, user, markingToCreate, ENTITY_TYPE_MARKING_DEFINITION, { complete: true });
const { element } = result;
if (result.isCreation) {
const filters = [{ key: 'auto_new_marking', values: [true] }];
// Bypass current right to read group
const groups = await listEntities(context, SYSTEM_USER, [ENTITY_TYPE_GROUP], { filters, connectionFormat: false });
if (groups && groups.length > 0) {
await Promise.all(
groups.map((group) => {
return groupAddRelation(context, SYSTEM_USER, group.id, {
relationship_type: RELATION_ACCESSES_TO,
toId: element.id,
});
})
);
}
}
return notify(BUS_TOPICS[ENTITY_TYPE_MARKING_DEFINITION].ADDED_TOPIC, element, user);
};
Expand Down

0 comments on commit 03e290e

Please sign in to comment.