From 1c2cdf207cf880f395c89dc073177abaad263da0 Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Mon, 11 Mar 2024 09:13:07 +0100 Subject: [PATCH] add check for @Version field --- .../hsadminng/rbac/rbacdef/RbacView.java | 14 ++++++++++++-- .../hsadminng/test/cust/TestCustomerEntity.java | 2 +- .../db/changelog/113-test-customer-rbac.md | 2 +- .../db/changelog/113-test-customer-rbac.sql | 2 +- .../db/changelog/123-test-package-rbac.md | 2 +- .../db/changelog/123-test-package-rbac.sql | 2 +- .../db/changelog/133-test-domain-rbac.sql | 2 +- 7 files changed, 18 insertions(+), 8 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 ba1d741d..caa2405d 100644 --- a/src/main/java/net/hostsharing/hsadminng/rbac/rbacdef/RbacView.java +++ b/src/main/java/net/hostsharing/hsadminng/rbac/rbacdef/RbacView.java @@ -20,6 +20,7 @@ import net.hostsharing.hsadminng.test.dom.TestDomainEntity; import net.hostsharing.hsadminng.test.pac.TestPackageEntity; import jakarta.persistence.Table; +import jakarta.persistence.Version; import jakarta.validation.constraints.NotNull; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -29,6 +30,7 @@ import java.util.function.Consumer; import java.util.stream.Stream; import static java.lang.reflect.Modifier.isStatic; +import static java.util.Arrays.stream; import static java.util.Optional.ofNullable; import static net.hostsharing.hsadminng.rbac.rbacdef.RbacView.RbacUserReference.UserRole.CREATOR; import static net.hostsharing.hsadminng.rbac.rbacdef.RbacView.SQL.autoFetched; @@ -76,7 +78,7 @@ public class RbacView { public RbacView withUpdatableColumns(final String... columnNames) { Collections.addAll(updatableColumns, columnNames); - // TODO: automatically add @Version column, otherwise optimistic locking won't work + verifyVersionColumnExists(); return this; } @@ -214,6 +216,14 @@ public class RbacView { return this; } + private void verifyVersionColumnExists() { + if (stream(rootEntityAlias.entityClass.getDeclaredFields()) + .noneMatch(f -> f.getAnnotation(Version.class) != null)) { + // TODO: convert this into throw Exception once RbacEntity is a base class with @Version field + System.err.println("@Version field required in updatable entity " + rootEntityAlias.entityClass); + } + } + public RbacGrantBuilder toRole(final String entityAlias, final Role role) { return new RbacGrantBuilder(entityAlias, role); } @@ -801,7 +811,7 @@ public class RbacView { HsOfficeCoopSharesTransactionEntity.class, HsOfficeMembershipEntity.class ).forEach(c -> { - final Method mainMethod = Arrays.stream(c.getMethods()).filter( + final Method mainMethod = stream(c.getMethods()).filter( m -> isStatic(m.getModifiers()) && m.getName().equals("main") ) .findFirst() diff --git a/src/main/java/net/hostsharing/hsadminng/test/cust/TestCustomerEntity.java b/src/main/java/net/hostsharing/hsadminng/test/cust/TestCustomerEntity.java index bc6c7ae8..99b0fb3c 100644 --- a/src/main/java/net/hostsharing/hsadminng/test/cust/TestCustomerEntity.java +++ b/src/main/java/net/hostsharing/hsadminng/test/cust/TestCustomerEntity.java @@ -41,7 +41,7 @@ public class TestCustomerEntity implements HasUuid { .withIdentityView(SQL.projection("prefix")) .withRestrictedViewOrderBy(SQL.expression("reference")) .withUpdatableColumns("reference", "prefix", "adminUserName") - // TODO: do we want explicit specification of parent-indenpendent insert permissions? + // TODO: do we want explicit specification of parent-independent insert permissions? // .toRole("global", ADMIN).grantPermission("customer", INSERT) .createRole(OWNER, (with) -> { diff --git a/src/main/resources/db/changelog/113-test-customer-rbac.md b/src/main/resources/db/changelog/113-test-customer-rbac.md index a585b153..f126a216 100644 --- a/src/main/resources/db/changelog/113-test-customer-rbac.md +++ b/src/main/resources/db/changelog/113-test-customer-rbac.md @@ -1,4 +1,4 @@ -### rbac customer 2024-03-10T11:42:41.089596517 +### rbac customer 2024-03-11T09:06:04.484587070 ```mermaid %%{init:{'flowchart':{'htmlLabels':false}}}%% diff --git a/src/main/resources/db/changelog/113-test-customer-rbac.sql b/src/main/resources/db/changelog/113-test-customer-rbac.sql index 25128963..da758029 100644 --- a/src/main/resources/db/changelog/113-test-customer-rbac.sql +++ b/src/main/resources/db/changelog/113-test-customer-rbac.sql @@ -1,5 +1,5 @@ --liquibase formatted sql --- This code generated was by RbacViewPostgresGenerator at 2024-03-10T11:42:41.121556631. +-- This code generated was by RbacViewPostgresGenerator at 2024-03-11T09:06:04.497071201. -- ============================================================================ --changeset test-customer-rbac-OBJECT:1 endDelimiter:--// diff --git a/src/main/resources/db/changelog/123-test-package-rbac.md b/src/main/resources/db/changelog/123-test-package-rbac.md index a8c0d66b..4b56651a 100644 --- a/src/main/resources/db/changelog/123-test-package-rbac.md +++ b/src/main/resources/db/changelog/123-test-package-rbac.md @@ -1,4 +1,4 @@ -### rbac package 2024-03-10T11:42:41.162678472 +### rbac package 2024-03-11T09:06:04.536081351 ```mermaid %%{init:{'flowchart':{'htmlLabels':false}}}%% diff --git a/src/main/resources/db/changelog/123-test-package-rbac.sql b/src/main/resources/db/changelog/123-test-package-rbac.sql index ad0359ff..371a28ff 100644 --- a/src/main/resources/db/changelog/123-test-package-rbac.sql +++ b/src/main/resources/db/changelog/123-test-package-rbac.sql @@ -1,5 +1,5 @@ --liquibase formatted sql --- This code generated was by RbacViewPostgresGenerator at 2024-03-10T11:42:41.163393064. +-- This code generated was by RbacViewPostgresGenerator at 2024-03-11T09:06:04.536525766. -- ============================================================================ --changeset test-package-rbac-OBJECT:1 endDelimiter:--// diff --git a/src/main/resources/db/changelog/133-test-domain-rbac.sql b/src/main/resources/db/changelog/133-test-domain-rbac.sql index a29a1b5a..c230bcde 100644 --- a/src/main/resources/db/changelog/133-test-domain-rbac.sql +++ b/src/main/resources/db/changelog/133-test-domain-rbac.sql @@ -1,5 +1,5 @@ --liquibase formatted sql --- This code generated was by RbacViewPostgresGenerator at 2024-03-10T11:42:41.186902574. +-- This code generated was by RbacViewPostgresGenerator at 2024-03-11T09:06:04.558752062. -- ============================================================================ --changeset test-domain-rbac-OBJECT:1 endDelimiter:--//