Skip to content

Commit

Permalink
[backend/frontend] template by entity type (#3402)
Browse files Browse the repository at this point in the history
  • Loading branch information
Archidoit committed Oct 25, 2024
1 parent 84bc274 commit 2288170
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
22 changes: 17 additions & 5 deletions opencti-platform/opencti-graphql/src/domain/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { isFeatureEnabled } from '../config/conf';
import { ENTITY_TYPE_CONTAINER_FEEDBACK } from '../modules/case/feedback/feedback-types';
import { paginatedForPathWithEnrichment } from '../modules/internal/document/document-domain';
import { isEnterpriseEdition } from '../utils/ee';
import { usedTemplates } from '../utils/template/__template';
import { usedTemplatesByEntityType } from '../utils/template/__template';
import { hardcodedTemplateWidgets } from '../utils/template/__widget';

export const findById = async (context, user, containerId) => {
Expand Down Expand Up @@ -258,13 +258,25 @@ export const getContentsFromTemplate = async (context, user, container, args) =>
return paginatedForPathWithEnrichment(context, context.user, `fromTemplate/${container.entity_type}/${container.id}`, container.id, opts);
};

export const getTemplates = (context, user, containerId) => {
return usedTemplates;
export const getTemplates = async (context, user, container) => {
const isEE = await isEnterpriseEdition(context);
const isContentFromTemplateEnabled = isFeatureEnabled('CONTENT_FROM_TEMPLATE');
if (!isEE || !isContentFromTemplateEnabled) {
return null;
}
const entityType = container.entity_type;
return usedTemplatesByEntityType[entityType] ?? [];
};

export const getTemplateAndUtils = (context, user, containerId, templateId) => {
export const getTemplateAndUtils = async (context, user, container, templateId) => {
// check feature is enabled
const isEE = await isEnterpriseEdition(context);
const isContentFromTemplateEnabled = isFeatureEnabled('CONTENT_FROM_TEMPLATE');
if (!isEE || !isContentFromTemplateEnabled) {
return null;
}
// fetch template (hardcoded for the moment)
const template = usedTemplates.find((t) => t.id === templateId);
const template = (usedTemplatesByEntityType[container.entity_type] ?? []).find((t) => t.id === templateId);
const { template_widgets_names } = template;
// fetch the widgets used in the template (hardcoded for the moment)
const template_widgets = hardcodedTemplateWidgets
Expand Down
4 changes: 2 additions & 2 deletions opencti-platform/opencti-graphql/src/resolvers/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ const containerResolvers = {
objects: (container, args, context) => objects(context, context.user, container.id, args),
relatedContainers: (container, args, context) => relatedContainers(context, context.user, container.id, args),
contentsFromTemplate: (container, { first, prefixMimeType }, context) => getContentsFromTemplate(context, context.user, container, { first, prefixMimeType }),
templates: (container, _, context) => getTemplates(context, context.user, container.id),
templateAndUtils: (container, args, context) => getTemplateAndUtils(context, context.user, container.id, args.templateId),
templates: (container, _, context) => getTemplates(context, context.user, container),
templateAndUtils: (container, args, context) => getTemplateAndUtils(context, context.user, container, args.templateId),
},
// TODO Reactivate after official release of graphQL 17
// StixObjectOrStixRelationshipRefConnection: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const content = `
</body>
`;

export const templateIncidentCase: Template = {
export const templateIncidentResponse: Template = {
name: 'Incident Response Report',
id: 'templateIncidentCase-id',
content,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// text //
import type { Template } from '../../generated/graphql';
import { templateIncidentCase } from './__incidentCase.template';
import { templateIncidentResponse } from './__incidentCase.template';

// templates //

Expand Down Expand Up @@ -53,4 +53,8 @@ const templateGraph: Template = {
`,
};

export const usedTemplates: Template[] = [templateText, templateAttribute, templateList, templateGraph, templateIncidentCase];
export const usedTemplatesByEntityType = {
Report: [templateText, templateAttribute, templateList, templateGraph],
Grouping: [templateText, templateGraph],
'Case-Incident': [templateIncidentResponse],
};
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const widgetReportMultiAttributes: TemplateWidget = {
{ label: 'ModificationDate', attribute: 'modified', variableName: 'containerModificationDate' },
{ label: 'Name', attribute: 'name', variableName: 'containerName' },
{ label: 'Publication date', attribute: 'published', variableName: 'reportPublicationDate' },
{ label: 'External references', attribute: 'externalReferences.edges.node.external_id', displayStyle: 'list', variableName: 'containerReferences' },
{ label: 'External references', attribute: 'externalReferences.edges.node.url', displayStyle: 'list', variableName: 'containerReferences' },
],
instance_id: 'CONTAINER_ID',
}],
Expand All @@ -37,7 +37,7 @@ const widgetIncidentResponseMultiAttributes: TemplateWidget = {
{ label: 'Labels', attribute: 'objectLabel.value', variableName: 'containerLabels' },
{ label: 'Markings', attribute: 'objectMarking.definition', variableName: 'containerMarkings' },
{ label: 'ModificationDate', attribute: 'modified', variableName: 'containerModificationDate' },
{ label: 'External references', attribute: 'externalReferences.edges.node.external_id', displayStyle: 'list', variableName: 'containerReferences' },
{ label: 'External references', attribute: 'externalReferences.edges.node.url', displayStyle: 'list', variableName: 'containerReferences' },
{ label: 'Priority', attribute: 'priority', variableName: 'incidentPriority' },
{ label: 'Severity', attribute: 'severity', variableName: 'incidentSeverity' },
{ label: 'Incident type', attribute: 'incident_type', variableName: 'incidentType' },
Expand Down

0 comments on commit 2288170

Please sign in to comment.