performance optimization for RBAC runtime graph generation

This commit is contained in:
Michael Hoennig 2024-06-12 16:00:12 +02:00
parent 0045c62a27
commit 5cae64af4c

View File

@ -62,6 +62,8 @@ public class RbacGrantsDiagramService {
@PersistenceContext @PersistenceContext
private EntityManager em; private EntityManager em;
private Map<UUID, List<RawRbacGrantEntity>> descendantsByUuid = new HashMap<>();
public String allGrantsToCurrentUser(final EnumSet<Include> includes) { public String allGrantsToCurrentUser(final EnumSet<Include> includes) {
final var graph = new LimitedHashSet<RawRbacGrantEntity>(); final var graph = new LimitedHashSet<RawRbacGrantEntity>();
for ( UUID subjectUuid: context.currentSubjectsUuids() ) { for ( UUID subjectUuid: context.currentSubjectsUuids() ) {
@ -102,7 +104,7 @@ public class RbacGrantsDiagramService {
} }
private void traverseGrantsFrom(final Set<RawRbacGrantEntity> graph, final UUID refUuid, final EnumSet<Include> option) { private void traverseGrantsFrom(final Set<RawRbacGrantEntity> graph, final UUID refUuid, final EnumSet<Include> option) {
final var grants = rawGrantRepo.findByDescendantUuid(refUuid); final var grants = findDescendantsByUuid(refUuid);
grants.forEach(g -> { grants.forEach(g -> {
if (!option.contains(USERS) && g.getAscendantIdName().startsWith("user:")) { if (!option.contains(USERS) && g.getAscendantIdName().startsWith("user:")) {
return; return;
@ -114,6 +116,11 @@ public class RbacGrantsDiagramService {
}); });
} }
private List<RawRbacGrantEntity> findDescendantsByUuid(final UUID refUuid) {
// TODO.impl: if that UUID already got processed, do we need to return anything at all?
return descendantsByUuid.computeIfAbsent(refUuid, uuid -> rawGrantRepo.findByDescendantUuid(uuid));
}
private String toMermaidFlowchart(final HashSet<RawRbacGrantEntity> graph, final EnumSet<Include> includes) { private String toMermaidFlowchart(final HashSet<RawRbacGrantEntity> graph, final EnumSet<Include> includes) {
final var entities = final var entities =
includes.contains(DETAILS) includes.contains(DETAILS)