Skip to content

Commit 93018e9

Browse files
authored
[frontend] display virtual types in entity type filter list (#7637)
1 parent b2228d2 commit 93018e9

File tree

4 files changed

+66
-5
lines changed

4 files changed

+66
-5
lines changed

opencti-platform/opencti-front/src/utils/filters/useSearchEntities.tsx

+16-2
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,8 @@ const useSearchEntities = ({
746746
...result,
747747
];
748748
// if there are not only stix cyber observables in the entity types list, add the 'Stix Cyber Observable' abstract type
749-
if (availableEntityTypes && (availableEntityTypes.length > 1 || availableEntityTypes.includes('Stix-Core-Object'))) {
749+
if (!availableEntityTypes
750+
|| (availableEntityTypes && (availableEntityTypes.length > 1 || availableEntityTypes.includes('Stix-Core-Object')))) {
750751
result = [
751752
{
752753
label: t_i18n('entity_Stix-Cyber-Observable'),
@@ -772,7 +773,8 @@ const useSearchEntities = ({
772773
...result,
773774
];
774775
// if there are not only stix domain objects in the entity types list, add the 'Stix Domain Object' abstract type
775-
if (availableEntityTypes && (availableEntityTypes.length > 1 || availableEntityTypes.includes('Stix-Core-Object'))) {
776+
if (!availableEntityTypes
777+
|| (availableEntityTypes && (availableEntityTypes.length > 1 || availableEntityTypes.includes('Stix-Core-Object')))) {
776778
result = [
777779
{
778780
label: t_i18n('entity_Stix-Domain-Object'),
@@ -796,6 +798,18 @@ const useSearchEntities = ({
796798
})),
797799
...result,
798800
];
801+
// if there are not only stix core relationships in the entity types list, add the 'Stix Core Relationship' abstract type
802+
if (!availableEntityTypes
803+
|| (availableEntityTypes && availableEntityTypes.length > 1)) {
804+
result = [
805+
{
806+
label: t_i18n('entity_Stix-Core-Relationship'),
807+
value: 'Stix-Core-Relationship',
808+
type: 'Stix-Core-Relationship',
809+
},
810+
...result,
811+
];
812+
}
799813
}
800814
// push the sighting relationship
801815
if (

opencti-platform/opencti-graphql/src/schema/schemaUtils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ export const keepMostRestrictiveTypes = (entityTypes) => {
188188
let restrictedEntityTypes = [...entityTypes];
189189
for (let i = 0; i < entityTypes.length; i += 1) {
190190
const type = entityTypes[i];
191-
const parentTypes = getParentTypes(type);
191+
const parentTypes = getParentTypes(type).filter((p) => p !== type);
192192
restrictedEntityTypes = restrictedEntityTypes.filter((t) => !parentTypes.includes(t));
193193
}
194194
return restrictedEntityTypes;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { keepMostRestrictiveTypes } from '../../../src/schema/schemaUtils';
3+
4+
describe('Schema Utils tests', () => {
5+
it('keepMostRestrictiveTypes should keep all types if no overlap', () => {
6+
const types = keepMostRestrictiveTypes(['Report', 'City']);
7+
expect(types.length).toEqual(2);
8+
expect(types).toContain('Report');
9+
expect(types).toContain('City');
10+
});
11+
it('keepMostRestrictiveTypes should keep most restrictive types', () => {
12+
const types = keepMostRestrictiveTypes(['Report', 'Stix-Domain-Object', 'City']);
13+
expect(types.length).toEqual(2);
14+
expect(types).toContain('Report');
15+
expect(types).toContain('City');
16+
});
17+
it('keepMostRestrictiveTypes should keep abstract types', () => {
18+
const types = keepMostRestrictiveTypes(['Stix-Domain-Object']);
19+
expect(types.length).toEqual(1);
20+
expect(types).toContain('Stix-Domain-Object');
21+
});
22+
it('keepMostRestrictiveTypes should keep most restrictive abstract types', () => {
23+
const types = keepMostRestrictiveTypes(['Stix-Domain-Object', 'Stix-Core-Object']);
24+
expect(types.length).toEqual(1);
25+
expect(types).toContain('Stix-Domain-Object');
26+
});
27+
});

opencti-platform/opencti-graphql/tests/02-integration/01-database/filterGroup-test.js

+22-2
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,7 @@ describe('Complex filters combinations for elastic queries', () => {
958958
}
959959
});
960960
expect(queryResult.data.globalSearch.edges.length).toEqual(5); // 5 reports
961-
// (entity_type = Report AND container AND Stix-Core-Object)
961+
// (entity_type = Report AND Container AND Stix-Core-Object)
962962
queryResult = await queryAsAdmin({
963963
query: LIST_QUERY,
964964
variables: {
@@ -969,7 +969,7 @@ describe('Complex filters combinations for elastic queries', () => {
969969
{
970970
key: 'entity_type',
971971
operator: 'eq',
972-
values: [ABSTRACT_STIX_CORE_OBJECT, ENTITY_TYPE_CONTAINER_REPORT, ENTITY_TYPE_CONTAINER, ABSTRACT_INTERNAL_OBJECT],
972+
values: [ABSTRACT_STIX_CORE_OBJECT, ENTITY_TYPE_CONTAINER_REPORT, ENTITY_TYPE_CONTAINER],
973973
mode: 'and',
974974
}
975975
],
@@ -978,6 +978,26 @@ describe('Complex filters combinations for elastic queries', () => {
978978
}
979979
});
980980
expect(queryResult.data.globalSearch.edges.length).toEqual(5); // 5 reports
981+
// (entity_type = Report AND Container AND Internal-Object)
982+
queryResult = await queryAsAdmin({
983+
query: LIST_QUERY,
984+
variables: {
985+
first: 10,
986+
filters: {
987+
mode: 'or',
988+
filters: [
989+
{
990+
key: 'entity_type',
991+
operator: 'eq',
992+
values: [ENTITY_TYPE_CONTAINER_REPORT, ENTITY_TYPE_CONTAINER, ABSTRACT_INTERNAL_OBJECT],
993+
mode: 'and',
994+
}
995+
],
996+
filterGroups: [],
997+
},
998+
}
999+
});
1000+
expect(queryResult.data.globalSearch.edges.length).toEqual(0); // reports are not internal objects
9811001
// (entity_type = Malware OR Software)
9821002
queryResult = await queryAsAdmin({
9831003
query: LIST_QUERY,

0 commit comments

Comments
 (0)