Skip to content

Commit 6c24ab3

Browse files
committed
[backend] handle orga delete in retention manager
1 parent fcb555e commit 6c24ab3

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

opencti-platform/opencti-graphql/src/manager/retentionManager.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import type { FileEdge, RetentionRule } from '../generated/graphql';
1616
import { RetentionRuleScope, RetentionUnit } from '../generated/graphql';
1717
import { deleteFile } from '../database/file-storage';
1818
import { DELETABLE_FILE_STATUSES, paginatedForPathWithEnrichment } from '../modules/internal/document/document-domain';
19+
import { ENTITY_TYPE_IDENTITY_ORGANIZATION } from '../modules/organization/organization-types';
20+
import { organizationDelete } from '../modules/organization/organization-domain';
1921
import type { BasicStoreCommonEdge, StoreObject } from '../types/store';
2022
import { ALREADY_DELETED_ERROR } from '../config/errors';
2123

@@ -33,7 +35,12 @@ export const RETENTION_UNIT_VALUES = Object.values(RetentionUnit);
3335

3436
export const deleteElement = async (context: AuthContext, scope: string, nodeId: string, nodeEntityType?: string) => {
3537
if (scope === 'knowledge') {
36-
await deleteElementById(context, RETENTION_MANAGER_USER, nodeId, nodeEntityType, { forceDelete: true });
38+
if (nodeEntityType === ENTITY_TYPE_IDENTITY_ORGANIZATION) {
39+
// call organizationDelete which will ensure protections (for platform organization & members)
40+
await organizationDelete(context, RETENTION_MANAGER_USER, nodeId);
41+
} else {
42+
await deleteElementById(context, RETENTION_MANAGER_USER, nodeId, nodeEntityType, { forceDelete: true });
43+
}
3744
} else if (scope === 'file' || scope === 'workbench') {
3845
await deleteFile(context, RETENTION_MANAGER_USER, nodeId);
3946
} else {

opencti-platform/opencti-graphql/tests/02-integration/04-manager/retentionManager-test.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { describe, expect, it, beforeAll, afterAll } from 'vitest';
22
import gql from 'graphql-tag';
33
import { Readable } from 'stream';
4-
import { ADMIN_USER, queryAsAdmin, testContext } from '../../utils/testQuery';
4+
import { ADMIN_USER, queryAsAdmin, TEST_ORGANIZATION, testContext } from '../../utils/testQuery';
55
import { utcDate } from '../../../src/utils/format';
66
import { deleteElement, getElementsToDelete } from '../../../src/manager/retentionManager';
77
import { allFilesForPaths } from '../../../src/modules/internal/document/document-domain';
8+
import { ENTITY_TYPE_IDENTITY_ORGANIZATION } from '../../../src/modules/organization/organization-types';
89
import { uploadToStorage } from '../../../src/database/file-storage-helper';
910
import { elLoadById, elRawUpdateByQuery } from '../../../src/database/engine';
1011
import { READ_INDEX_INTERNAL_OBJECTS, READ_INDEX_STIX_DOMAIN_OBJECTS } from '../../../src/database/utils';
@@ -322,4 +323,8 @@ describe('Retention Manager tests ', () => {
322323
const report1deleted = await elLoadById(testContext, ADMIN_USER, report1Id);
323324
expect(report1deleted).toBeUndefined();
324325
});
326+
it('should not delete organization with members', async () => {
327+
await expect(() => deleteElement(context, 'knowledge', TEST_ORGANIZATION.id, ENTITY_TYPE_IDENTITY_ORGANIZATION))
328+
.rejects.toThrowError('Cannot delete the organization that has members.');
329+
});
325330
});

0 commit comments

Comments
 (0)