improve RbacGrantsMermaidService formatting

This commit is contained in:
Michael Hoennig 2024-02-15 17:20:50 +01:00
parent fb00b36b2f
commit 85ad05a77e

View File

@ -11,9 +11,9 @@ import java.io.BufferedWriter;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Stream;
import static java.lang.String.join; import static java.util.stream.Collectors.joining;
import static net.hostsharing.hsadminng.rbac.rbacgrant.RbacGrantsMermaidService.Include.*; import static net.hostsharing.hsadminng.rbac.rbacgrant.RbacGrantsMermaidService.Include.*;
@Service @Service
@ -103,11 +103,22 @@ public class RbacGrantsMermaidService {
private String toMermaidFlowchart(final HashSet<RawRbacGrantEntity> graph) { private String toMermaidFlowchart(final HashSet<RawRbacGrantEntity> graph) {
return "%%{init:{'flowchart':{'htmlLabels':false}}}%%\n" + return "%%{init:{'flowchart':{'htmlLabels':false}}}%%\n" +
"flowchart TB\n\n" + graph.stream().sorted() "flowchart TB\n\n"
.map(g -> node(g.getAscendantIdName(), g.getAscendingUuid()) + + graph.stream()
.flatMap(g -> Stream.of(
new Node(g.getAscendantIdName(), g.getAscendingUuid()),
new Node(g.getAscendantIdName(), g.getAscendingUuid()))
)
.map(n -> node(n.idName(), n.uuid()))
.sorted()
.collect(joining("\n\n"))
+ "\n"
+ graph.stream()
.map(g -> quoted(g.getAscendantIdName()) +
(g.isAssumed() ? " --> " : " -.-> ") + (g.isAssumed() ? " --> " : " -.-> ") +
node(g.getDescendantIdName(), g.getDescendantUuid())) quoted(g.getDescendantIdName()))
.collect(Collectors.joining("\n")); .sorted()
.collect(joining("\n"));
} }
private String node(final String idName, final UUID uuid) { private String node(final String idName, final UUID uuid) {
@ -128,7 +139,7 @@ public class RbacGrantsMermaidService {
final var objectName = idName.substring(refType.length()+1, idName.length() - roleType.length() - 1); final var objectName = idName.substring(refType.length()+1, idName.length() - roleType.length() - 1);
final var tableName = objectName.contains("#") ? objectName.split("#")[0] : ""; final var tableName = objectName.contains("#") ? objectName.split("#")[0] : "";
final var instanceName = objectName.contains("#") ? objectName.split("#", 2)[1] : objectName; final var instanceName = objectName.contains("#") ? objectName.split("#", 2)[1] : objectName;
final var displayName = "\n" + (tableName.equals("global") ? "" : tableName) + "\n" + instanceName + "\n" + uuid + "\n" + roleType; final var displayName = (tableName.equals("global") ? "" : tableName) + "\n" + instanceName + "\n" + uuid + "\n" + roleType;
return "[" + displayName + "]"; return "[" + displayName + "]";
} }
if (refType.equals("perm")) { if (refType.equals("perm")) {
@ -146,3 +157,7 @@ public class RbacGrantsMermaidService {
return idName.replace(" ", ":").replaceAll("@.*", ""); return idName.replace(" ", ":").replaceAll("@.*", "");
} }
} }
record Node(String idName, UUID uuid) {
}