From a0473976d5fdfa83fb142ed8d38890802175ab30 Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Wed, 21 Feb 2024 13:22:45 +0100 Subject: [PATCH] improve readability for defineEntityAlias --- .../HsOfficeBankAccountEntity.java | 15 +++++---- .../hsadminng/rbac/rbacdef/RbacView.java | 31 +++++++++++++++++-- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/main/java/net/hostsharing/hsadminng/hs/office/bankaccount/HsOfficeBankAccountEntity.java b/src/main/java/net/hostsharing/hsadminng/hs/office/bankaccount/HsOfficeBankAccountEntity.java index 94cd60e4..b424f3d1 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/office/bankaccount/HsOfficeBankAccountEntity.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/office/bankaccount/HsOfficeBankAccountEntity.java @@ -16,10 +16,9 @@ import jakarta.persistence.Id; import jakarta.persistence.Table; import java.util.UUID; -import static net.hostsharing.hsadminng.rbac.rbacdef.RbacView.GLOBAL; +import static net.hostsharing.hsadminng.rbac.rbacdef.RbacView.*; import static net.hostsharing.hsadminng.rbac.rbacdef.RbacView.Permission.*; import static net.hostsharing.hsadminng.rbac.rbacdef.RbacView.Role.*; -import static net.hostsharing.hsadminng.rbac.rbacdef.RbacView.rbacViewFor; import static net.hostsharing.hsadminng.stringify.Stringify.stringify; @Entity @@ -104,28 +103,28 @@ public class HsOfficeBankAccountEntity implements HasUuid, Stringifyable { "defaultPrefix" /* TODO: do we want that updatable? */ ) .createPermission(extraPermission("new-debitor")).grantedTo("global", ADMIN).pop() - .defineEntityAlias("debitorRel", HsOfficeRelationshipEntity.class, """ + .defineEntityAlias("debitorRel", HsOfficeRelationshipEntity.class, fetchedBySql(""" SELECT * FROM hs_office_relationship AS r WHERE r.relType = 'ACCOUNTING' AND r.relHolderUuid = ${REF}.debitorRelUuid; - """, "debitorRelUuid") + """), dependsOnColumn("debitorRelUuid")) .createPermission(ALL).grantedTo("hsOfficeRelationship:DEBITOR", OWNER).pop() .createPermission(UPDATE).grantedTo("hsOfficeRelationship:DEBITOR", ADMIN).pop() .createPermission(READ).grantedTo("hsOfficeRelationship:DEBITOR", TENANT).pop() - .defineEntityAlias("bankAccount", HsOfficeBankAccountEntity.class, """ + .defineEntityAlias("bankAccount", HsOfficeBankAccountEntity.class, fetchedBySql(""" SELECT * FROM hs_office_relationship AS r WHERE r.relType = 'ACCOUNTING' AND r.relHolderUuid = ${REF}.debitorRelUuid; - """, "bankAccountUuid") + """), dependsOnColumn("bankAccountUuid")) .toRole("hsOfficeBankAccount", ADMIN).grantRole("debitorRel", AGENT) .toRole("debitorRel", AGENT).grantRole("hsOfficeBankAccount", REFERRER) - .defineEntityAlias("partnerRel", HsOfficeRelationshipEntity.class, """ + .defineEntityAlias("partnerRel", HsOfficeRelationshipEntity.class, fetchedBySql(""" SELECT * FROM hs_office_relationship AS partnerRel WHERE ${debitorRel}.relAnchorUuid = partnerRel.relHolderUuid; - """, "debitorRelUuid") + """), dependsOnColumn("debitorRelUuid")) .toRole("partnerRel", ADMIN).grantRole("debitorRel", ADMIN) .toRole("debitorRel", ADMIN).grantRole("partnerRel", AGENT) .toRole("partnerRel", AGENT).grantRole("debitorRel", AGENT) 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 1b6af664..57d7e846 100644 --- a/src/main/java/net/hostsharing/hsadminng/rbac/rbacdef/RbacView.java +++ b/src/main/java/net/hostsharing/hsadminng/rbac/rbacdef/RbacView.java @@ -7,6 +7,14 @@ public class RbacView { public static final String GLOBAL = "global"; + public static SQL fetchedBySql(final String sql) { + return new SQL(sql); + } + + public static Column dependsOnColumn(final String column) { + return new Column(column); + } + public static RbacView rbacViewFor(final Class entityClass) { return new RbacView<>(entityClass); } @@ -39,7 +47,7 @@ public class RbacView { } public RbacView defineEntityAlias( - final String alias, final Class entityClass, final String fetchSql, final String dependsOnColum) { + final String alias, final Class entityClass, final SQL fetchSql, final Column dependsOnColum) { return this; } @@ -123,9 +131,10 @@ public class RbacView { public static final Role AGENT = new Role("agent"); public static final Role TENANT = new Role("tenant"); public static final Role REFERRER = new Role("referrer"); + private final String roleName; public Role(final String roleName) { - + this.roleName = roleName; } } @@ -144,4 +153,22 @@ public class RbacView { this.permission = permission; } } + + public static class SQL { + + public final String sql; + + public SQL(final String sql) { + this.sql = sql; + } + } + + public static class Column { + + public final String column; + + public Column(final String column) { + this.column = column; + } + } }