@@ -111,6 +111,44 @@ const forgeDatasetSecurityFromScenarioSecurity = (scenarioSecurity) => {
111111 } ;
112112} ;
113113
114+ const mergeDatasetSecurity = ( currentDatasetSecurity , newDatasetSecurity ) => {
115+ const roleHierarchy = {
116+ [ ACL_ROLES . DATASET . NONE ] : 0 ,
117+ [ ACL_ROLES . DATASET . VIEWER ] : 1 ,
118+ [ ACL_ROLES . DATASET . USER ] : 2 ,
119+ [ ACL_ROLES . DATASET . EDITOR ] : 3 ,
120+ [ ACL_ROLES . DATASET . ADMIN ] : 4 ,
121+ } ;
122+
123+ const getRoleLevel = ( role ) => roleHierarchy [ role ] ?? - 1 ;
124+ const getHighestRole = ( role1 , role2 ) => ( getRoleLevel ( role1 ) > getRoleLevel ( role2 ) ? role1 : role2 ) ;
125+
126+ const mergedACL = new Map ( ) ;
127+
128+ for ( const userEntry of currentDatasetSecurity ?. accessControlList ?? [ ] ) {
129+ mergedACL . set ( userEntry . id , userEntry . role ) ;
130+ }
131+
132+ for ( const newUser of newDatasetSecurity ?. accessControlList ?? [ ] ) {
133+ const currentRole = mergedACL . get ( newUser . id ) ;
134+ if ( ! currentRole ) {
135+ mergedACL . set ( newUser . id , newUser . role ) ;
136+ } else {
137+ const highestRole = getHighestRole ( currentRole , newUser . role ) ;
138+ mergedACL . set ( newUser . id , highestRole ) ;
139+ }
140+ }
141+
142+ const currentDefault = currentDatasetSecurity ?. default ?? ACL_ROLES . DATASET . NONE ;
143+ const newDefault = newDatasetSecurity ?. default ?? ACL_ROLES . DATASET . NONE ;
144+ const mergedDefault = getHighestRole ( currentDefault , newDefault ) ;
145+
146+ return {
147+ default : mergedDefault ,
148+ accessControlList : [ ...mergedACL . entries ( ) ] . map ( ( [ id , role ] ) => ( { id, role } ) ) ,
149+ } ;
150+ } ;
151+
114152/*
115153Transpose a dict whose values are arrays into another dict where the arrays values are now the dict keys.
116154Example: { A: [1,2,3], B:[1,2] } will become { 1:['A','B'], 2:['A','B'], 3:['A'] }
@@ -309,6 +347,7 @@ export const SecurityUtils = {
309347 areAccessControlListsIdentical,
310348 compareAccessControlLists,
311349 forgeDatasetSecurityFromScenarioSecurity,
350+ mergeDatasetSecurity,
312351 getPermissionsFromRole,
313352 getRolesGrantingPermission,
314353 getUserPermissionsForResource,
0 commit comments