1
1
import type { AuthContext , AuthUser } from '../../../types/user' ;
2
- import { createEntity } from '../../../database/middleware' ;
2
+ import { createEntity , patchAttribute } from '../../../database/middleware' ;
3
3
import type { EntityOptions } from '../../../database/middleware-loader' ;
4
4
import { internalLoadById , listEntitiesPaginated , storeLoadById } from '../../../database/middleware-loader' ;
5
5
import { BUS_TOPICS } from '../../../config/conf' ;
6
- import { ABSTRACT_STIX_DOMAIN_OBJECT , buildRefRelationKey } from '../../../schema/general' ;
6
+ import { ABSTRACT_STIX_CORE_OBJECT , ABSTRACT_STIX_DOMAIN_OBJECT , buildRefRelationKey } from '../../../schema/general' ;
7
7
import { notify } from '../../../database/redis' ;
8
8
import { now } from '../../../utils/format' ;
9
9
import { userAddIndividual } from '../../../domain/user' ;
@@ -12,10 +12,13 @@ import { upsertTemplateForCase } from '../case-domain';
12
12
import type { BasicStoreEntityCaseIncident } from './case-incident-types' ;
13
13
import { ENTITY_TYPE_CONTAINER_CASE_INCIDENT } from './case-incident-types' ;
14
14
import type { DomainFindById } from '../../../domain/domainTypes' ;
15
- import type { CaseIncidentAddInput } from '../../../generated/graphql' ;
15
+ import type { CaseIncidentAddInput , MemberAccessInput } from '../../../generated/graphql' ;
16
16
import { isStixId } from '../../../schema/schemaUtils' ;
17
17
import { RELATION_OBJECT } from '../../../schema/stixRefRelationship' ;
18
18
import { FilterMode } from '../../../generated/graphql' ;
19
+ import { isValidMemberAccessRight } from '../../../utils/access' ;
20
+ import { containsValidAdmin } from '../../../utils/authorizedMembers' ;
21
+ import { FunctionalError } from '../../../config/errors' ;
19
22
20
23
export const findById : DomainFindById < BasicStoreEntityCaseIncident > = ( context : AuthContext , user : AuthUser , caseIncidentId : string ) => {
21
24
return storeLoadById ( context , user , caseIncidentId , ENTITY_TYPE_CONTAINER_CASE_INCIDENT ) ;
@@ -59,3 +62,34 @@ export const caseIncidentContainsStixObjectOrStixRelationship = async (context:
59
62
const caseIncidentFound = await findAll ( context , user , args ) ;
60
63
return caseIncidentFound . edges . length > 0 ;
61
64
} ;
65
+
66
+ export const caseIncidentEditAuthorizedMembers = async (
67
+ context : AuthContext ,
68
+ user : AuthUser ,
69
+ entityId : string ,
70
+ input : MemberAccessInput [ ] | undefined | null
71
+ ) => {
72
+ let authorized_members : { id : string , access_right : string } [ ] | null = null ;
73
+
74
+ if ( input ) {
75
+ // validate input (validate access right) and remove duplicates
76
+ const filteredInput = input . filter ( ( value , index , array ) => {
77
+ return isValidMemberAccessRight ( value . access_right ) && array . findIndex ( ( e ) => e . id === value . id ) === index ;
78
+ } ) ;
79
+
80
+ const hasValidAdmin = await containsValidAdmin (
81
+ context ,
82
+ filteredInput ,
83
+ [ 'KNOWLEDGE_KNUPDATE_KNMANAGEAUTHMEMBERS' ]
84
+ ) ;
85
+ if ( ! hasValidAdmin ) {
86
+ throw FunctionalError ( 'It should have at least one valid member with admin access' ) ;
87
+ }
88
+
89
+ authorized_members = filteredInput . map ( ( { id, access_right } ) => ( { id, access_right } ) ) ;
90
+ }
91
+
92
+ const patch = { authorized_members } ;
93
+ const { element } = await patchAttribute ( context , user , entityId , ENTITY_TYPE_CONTAINER_CASE_INCIDENT , patch ) ;
94
+ return notify ( BUS_TOPICS [ ABSTRACT_STIX_CORE_OBJECT ] . EDIT_TOPIC , element , user ) ;
95
+ } ;
0 commit comments