From 56f316ef80223e07fbd50075bd6408c2428cb6fd Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Sun, 7 Apr 2024 13:20:44 +0200 Subject: [PATCH] CaseDef extends ColumnValue --- .../hsadminng/rbac/rbacdef/RbacView.java | 30 ++++++------------- .../RolesGrantsAndPermissionsGenerator.java | 4 +-- 2 files changed, 11 insertions(+), 23 deletions(-) diff --git a/src/main/java/net/hostsharing/hsadminng/rbac/rbacdef/RbacView.java b/src/main/java/net/hostsharing/hsadminng/rbac/rbacdef/RbacView.java index 90bddc20..d052b958 100644 --- a/src/main/java/net/hostsharing/hsadminng/rbac/rbacdef/RbacView.java +++ b/src/main/java/net/hostsharing/hsadminng/rbac/rbacdef/RbacView.java @@ -394,7 +394,7 @@ public class RbacView { }); importedRbacView.getGrantDefs().forEach(grantDef -> { if ( grantDef.grantType() == RbacGrantDefinition.GrantType.ROLE_TO_ROLE && - grantDef.matchesCase(forCase) ) { + (grantDef.forCases == null || grantDef.matchesCase(forCase)) ) { final var importedGrantDef = findOrCreateGrantDef( findRbacRole( mapper.map(grantDef.getSubRoleDef().entityAlias.aliasName), @@ -483,7 +483,7 @@ public class RbacView { public void generateWithBaseFileName(final String baseFileName) { if (allCases.size() > 1) { allCases.forEach(caseDef -> { - final var fileName = baseFileName + (caseDef.isDefaultCase() ? "" : "-" + caseDef.val) + ".md"; + final var fileName = baseFileName + (caseDef.isDefaultCase() ? "" : "-" + caseDef.value) + ".md"; new RbacViewMermaidFlowchartGenerator(this, caseDef) .generateToMarkdownFile(Path.of(OUTPUT_BASEDIR, fileName)); }); @@ -596,22 +596,12 @@ public class RbacView { } boolean matchesCase(final ColumnValue requestedCase) { - if (forCases == null) { - return true; - } final var noCasesDefined = forCases.isEmpty(); final var generateForAllCases = requestedCase == null; final boolean isGrantedForRequestedCase = forCases.stream().anyMatch(c -> c.isCase(requestedCase)); return noCasesDefined || generateForAllCases || isGrantedForRequestedCase; } - boolean matchesCase(final CaseDef requestedCase) { - final var noCasesDefined = forCases.isEmpty(); - final var generateForAllCases = requestedCase == null; - final boolean isGrantedForRequestedCase = forCases.contains(requestedCase); - return noCasesDefined || generateForAllCases || isGrantedForRequestedCase; - } - boolean isToCreate() { return toCreate; } @@ -1132,14 +1122,12 @@ public class RbacView { } } - // FIXME: subclass of ColumnValue - public static class CaseDef { + public static class CaseDef extends ColumnValue { - final String val; final Consumer def; private CaseDef(final String discriminatorColumnValue, final Consumer def) { - this.val = discriminatorColumnValue; + super(discriminatorColumnValue); this.def = def; } @@ -1154,7 +1142,7 @@ public class RbacView { @Override public int hashCode() { - return ofNullable(val).map(String::hashCode).orElse(0); + return ofNullable(value).map(String::hashCode).orElse(0); } @Override @@ -1164,22 +1152,22 @@ public class RbacView { if (other == null || getClass() != other.getClass()) return false; final CaseDef caseDef = (CaseDef) other; - return Objects.equals(val, caseDef.val); + return Objects.equals(value, caseDef.value); } boolean isDefaultCase() { - return val == null; + return value == null; } @Override public String toString() { return isDefaultCase() ? "inOtherCases" - : "inCaseOf:" + val; + : "inCaseOf:" + value; } public boolean isCase(final ColumnValue requestedCase) { - return Objects.equals(requestedCase.value, this.val); + return Objects.equals(requestedCase.value, this.value); } } diff --git a/src/main/java/net/hostsharing/hsadminng/rbac/rbacdef/RolesGrantsAndPermissionsGenerator.java b/src/main/java/net/hostsharing/hsadminng/rbac/rbacdef/RolesGrantsAndPermissionsGenerator.java index 38632e27..2089d4d9 100644 --- a/src/main/java/net/hostsharing/hsadminng/rbac/rbacdef/RolesGrantsAndPermissionsGenerator.java +++ b/src/main/java/net/hostsharing/hsadminng/rbac/rbacdef/RolesGrantsAndPermissionsGenerator.java @@ -197,8 +197,8 @@ class RolesGrantsAndPermissionsGenerator { plPgSql.writeLn(); final var ifOrElsIf = new AtomicReference<>("IF "); rbacDef.getAllCases().forEach(caseDef -> { - if (caseDef.val != null) { - plPgSql.writeLn(ifOrElsIf + "NEW." + rbacDef.getDiscriminatorColumName() + " = '" + caseDef.val + "' THEN"); + if (caseDef.value != null) { + plPgSql.writeLn(ifOrElsIf + "NEW." + rbacDef.getDiscriminatorColumName() + " = '" + caseDef.value + "' THEN"); } else { plPgSql.writeLn("ELSE"); }