RBAC Diagram+PostgreSQL Generator #21

Merged
hsh-michaelhoennig merged 54 commits from experimental-rbacview-generator into master 2024-03-11 12:30:44 +01:00
3 changed files with 111 additions and 33 deletions
Showing only changes of commit b4d6930fbe - Show all commits

View File

@ -1,7 +1,5 @@
package net.hostsharing.hsadminng.rbac.rbacdef;
import net.hostsharing.hsadminng.hs.office.bankaccount.HsOfficeBankAccountEntity;
import net.hostsharing.hsadminng.hs.office.debitor.HsOfficeDebitorEntity;
import net.hostsharing.hsadminng.hs.office.relationship.HsOfficeRelationshipEntity;
import org.apache.commons.lang3.StringUtils;
@ -16,11 +14,11 @@ public class RbacViewMermaidFlowchart {
public static final String HOSTSHARING_ORANGE = "#dd4901";
public static final String HOSTSHARING_LIGHTBLUE = "#99bcdb";
private final RbacView rbacDef;
private final StringBuilder flowchart = new StringBuilder();
private final StringWriter flowchart = new StringWriter();
public RbacViewMermaidFlowchart(final RbacView rbacDef) {
this.rbacDef = rbacDef;
flowchart.append("""
flowchart.writeLn("""
%%{init:{'flowchart':{'htmlLabels':false}}}%%
flowchart TB
""");
@ -36,16 +34,16 @@ public class RbacViewMermaidFlowchart {
private void renderEntitySubgraph(final RbacView.EntityAlias entity) {
final var color = rbacDef.isMainEntityAlias(entity) ? HOSTSHARING_ORANGE : HOSTSHARING_LIGHTBLUE;
flowchart.append("""
flowchart.writeLn("""
subgraph %{aliasName}["`**%{aliasName}**`"]
direction TB
style %{aliasName} fill:%{color},stroke:darkblue,stroke-width:8px
"""
.replace("%{aliasName}", entity.aliasName())
.replace("%{color}", color ));
flowchart.indented( () -> {
rbacDef.getEntityAliases().values().stream()
.filter(e -> e.aliasName().startsWith(entity.aliasName() + "."))
.forEach(this::renderEntitySubgraph);
@ -66,24 +64,33 @@ public class RbacViewMermaidFlowchart {
renderEntitySubgraph(rbacDef.getEntityAliasProxy());
}
flowchart.append("end\n\n");
});
flowchart.chopEmptyLines();
flowchart.writeLn("end");
flowchart.writeLn();
}
private void wrapOutputInSubgraph(final String name, final String color, final String content) {
if (!StringUtils.isEmpty(content)) {
flowchart.append("subgraph " + name + "[ ]\n");
flowchart.append("style %{aliasName} fill: %{color}\n\n"
flowchart.emptyLine();
flowchart.writeLn("subgraph " + name + "[ ]\n");
flowchart.indented(() -> {
flowchart.writeLn("style %{aliasName} fill: %{color}"
.replace("%{aliasName}", name)
.replace("%{color}", color));
flowchart.append(content);
flowchart.append("\nend\n\n");
flowchart.writeLn();
flowchart.writeLn(content);
});
flowchart.chopEmptyLines();
flowchart.writeLn("end");
flowchart.writeLn();
}
}
private void renderGrants() {
rbacDef.getGrantDefs()
.forEach(g -> {
flowchart.append(grantDef(g) + "\n" );
flowchart.writeLn(grantDef(g) + "\n");
});
}
@ -141,8 +148,8 @@ public class RbacViewMermaidFlowchart {
}
public static void main(String[] args) throws IOException {
new RbacViewMermaidFlowchart(HsOfficeBankAccountEntity.rbac()).generateToMarkdownFile();
// new RbacViewMermaidFlowchart(HsOfficeBankAccountEntity.rbac()).generateToMarkdownFile();
new RbacViewMermaidFlowchart(HsOfficeRelationshipEntity.rbac()).generateToMarkdownFile();
new RbacViewMermaidFlowchart(HsOfficeDebitorEntity.rbac()).generateToMarkdownFile();
// new RbacViewMermaidFlowchart(HsOfficeDebitorEntity.rbac()).generateToMarkdownFile();
}
}

View File

@ -0,0 +1,71 @@
package net.hostsharing.hsadminng.rbac.rbacdef;
import org.apache.commons.lang3.StringUtils;
import static java.util.Arrays.stream;
import static java.util.stream.Collectors.joining;
public class StringWriter {
private final StringBuilder string = new StringBuilder();
private int indentLevel = 0;
void writeLn(final String text) {
string.append( indented(text));
writeLn();
}
void writeLn() {
string.append( "\n");
}
private String indented(final String text) {
if ( indentLevel == 0) {
return text.trim();
}
final var indentation = StringUtils.repeat(" ", indentLevel);
final var indented = stream(text.split("\n"))
.map(line -> line.trim().isBlank() ? "" : indentation + line.trim())
.collect(joining("\n"));
return indented;
}
void indent() {
++indentLevel;
}
void unindent() {
--indentLevel;
}
void indented(final Runnable indented) {
indent();
indented.run();
unindent();
}
boolean chopTail(final String tail) {
if (string.toString().endsWith(tail)) {
string.setLength(string.length() - tail.length());
return true;
}
return false;
}
void chopEmptyLines() {
while (string.toString().endsWith("\n\n")) {
string.setLength(string.length() - 1);
};
}
void emptyLine() {
if (!string.toString().endsWith("\n\n")) {
writeLn();
}
}
@Override
public String toString() {
return string.toString();
}
}