Skip to content

Commit 1355e76

Browse files
fix: [PROD-14987] sharing child scenarios remove access
1 parent 27fee05 commit 1355e76

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

src/state/sagas/scenario/ApplyScenarioSharingChanges/ApplyScenarioSharingChanges.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ export function* applyScenarioSharingChanges(action) {
5656
console.warn(`Unable to find dataset part ${datasetId}, you may lack "read" permission on this dataset.`);
5757
continue;
5858
}
59-
60-
const newDatasetSecurity = SecurityUtils.forgeDatasetSecurityFromScenarioSecurity(newScenarioSecurity);
59+
const forgedDatasetSecurity = SecurityUtils.forgeDatasetSecurityFromScenarioSecurity(newScenarioSecurity);
60+
const newDatasetSecurity = SecurityUtils.mergeDatasetSecurity(dataset.security, forgedDatasetSecurity);
6161
yield call(DatasetService.updateSecurity, organizationId, datasetId, dataset.security, newDatasetSecurity);
6262

6363
const datasetPermissionsMapping = yield select(getDatasetPermissionMapping);

src/utils/SecurityUtils.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
/*
115153
Transpose a dict whose values are arrays into another dict where the arrays values are now the dict keys.
116154
Example: { 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

Comments
 (0)