Skip to content

Commit f06da83

Browse files
committed
fix: use flatmap to get ownerInstances in HR scoping
1 parent e6c5f55 commit f06da83

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

src/core/hierarchicalScope.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -192,25 +192,31 @@ export const checkHierarchicalScope = async (ruleTarget: Target,
192192
}
193193
const reducedHRScopes = context?.subject?.hierarchical_scopes?.filter((hrObj) => hrObj?.role === ruleRole);
194194
for (let [resourceId, owners] of resourceIdOwnersMap) {
195-
// validate scoping Entity first
196-
let ownerInstances: string[] = [];
197-
const entityMatch = owners?.some((ownerObj) => {
198-
return reducedUserRoleAssocs?.some((roleObj) => {
199-
if (roleObj?.attributes?.some((roleAttributeObject) => roleAttributeObject?.id === urns.get('roleScopingEntity')
200-
&& ownerObj?.id === urns.get('ownerEntity') && ownerObj.value === ruleRoleScopingEntity && ownerObj.value === roleAttributeObject?.value)) {
201-
ownerObj?.attributes?.forEach((obj) => ownerInstances.push(obj.value));
202-
return true;
203-
}
204-
});
205-
});
195+
const ownerInstances: string[] = owners.filter(
196+
owner => reducedUserRoleAssocs?.some((roleObj) => {
197+
return roleObj?.attributes?.some(
198+
(roleAttributeObject) => roleAttributeObject?.id === urns.get('roleScopingEntity')
199+
&& owner?.id === urns.get('ownerEntity')
200+
&& owner?.value === ruleRoleScopingEntity
201+
&& owner?.value === roleAttributeObject?.value
202+
);
203+
}
204+
)
205+
).flatMap(
206+
owner => owner?.attributes?.filter(
207+
attr => attr?.id === urns.get('ownerInstance')
208+
).map(
209+
attr => attr?.value
210+
)
211+
);
206212
// validate the ownerInstance from HR scope tree for matched scoping entity
207-
if (entityMatch && ownerInstances?.length > 0) {
208-
traverse(reducedHRScopes).forEach((node: any) => { // depth-first search
213+
traverse(reducedHRScopes).forEach(
214+
(node: any) => { // depth-first search
209215
if (ownerInstances.includes(node?.id)) {
210216
deleteMapEntries.push(resourceId);
211217
}
212-
});
213-
}
218+
}
219+
);
214220
}
215221
}
216222

0 commit comments

Comments
 (0)