From b37674d8773480b83ff49833ec31466936b8a563 Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Mon, 6 Jan 2025 13:02:04 +0100 Subject: [PATCH] fix circular dependency --- .../hs/office/person/HsOfficePerson.java | 3 ++- .../hs/office/relation/HsOfficeRelation.java | 3 ++- .../hsadminng/persistence/BaseEntity.java | 11 ---------- .../hsadminng/rbac/role/WithRoleId.java | 20 +++++++++++++++++++ ...fficeDebitorRepositoryIntegrationTest.java | 10 +++++----- ...fficePartnerRepositoryIntegrationTest.java | 2 +- 6 files changed, 30 insertions(+), 19 deletions(-) create mode 100644 src/main/java/net/hostsharing/hsadminng/rbac/role/WithRoleId.java diff --git a/src/main/java/net/hostsharing/hsadminng/hs/office/person/HsOfficePerson.java b/src/main/java/net/hostsharing/hsadminng/hs/office/person/HsOfficePerson.java index 8842dac2..9b130566 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/office/person/HsOfficePerson.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/office/person/HsOfficePerson.java @@ -9,6 +9,7 @@ import lombok.experimental.FieldNameConstants; import lombok.experimental.SuperBuilder; import net.hostsharing.hsadminng.errors.DisplayAs; import net.hostsharing.hsadminng.persistence.BaseEntity; +import net.hostsharing.hsadminng.rbac.role.WithRoleId; import net.hostsharing.hsadminng.repr.Stringify; import net.hostsharing.hsadminng.repr.Stringifyable; import org.apache.commons.lang3.StringUtils; @@ -30,7 +31,7 @@ import static net.hostsharing.hsadminng.repr.Stringify.stringify; @SuperBuilder(toBuilder = true) @FieldNameConstants @DisplayAs("Person") -public class HsOfficePerson & BaseEntity> implements BaseEntity, Stringifyable { +public class HsOfficePerson & BaseEntity> implements BaseEntity, Stringifyable, WithRoleId { private static Stringify toString = stringify(HsOfficePerson.class, "person") .withProp(Fields.personType, HsOfficePerson::getPersonType) diff --git a/src/main/java/net/hostsharing/hsadminng/hs/office/relation/HsOfficeRelation.java b/src/main/java/net/hostsharing/hsadminng/hs/office/relation/HsOfficeRelation.java index 7a687a3b..47e19ce6 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/office/relation/HsOfficeRelation.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/office/relation/HsOfficeRelation.java @@ -6,6 +6,7 @@ import lombok.experimental.SuperBuilder; import net.hostsharing.hsadminng.hs.office.contact.HsOfficeContactRealEntity; import net.hostsharing.hsadminng.hs.office.person.HsOfficePersonRealEntity; import net.hostsharing.hsadminng.persistence.BaseEntity; +import net.hostsharing.hsadminng.rbac.role.WithRoleId; import net.hostsharing.hsadminng.repr.Stringify; import net.hostsharing.hsadminng.repr.Stringifyable; @@ -22,7 +23,7 @@ import static net.hostsharing.hsadminng.repr.Stringify.stringify; @Setter @SuperBuilder(toBuilder = true) @FieldNameConstants -public class HsOfficeRelation implements BaseEntity, Stringifyable { +public class HsOfficeRelation implements BaseEntity, Stringifyable, WithRoleId { private static Stringify toString = stringify(HsOfficeRelation.class, "rel") .withProp(Fields.anchor, HsOfficeRelation::getAnchor) diff --git a/src/main/java/net/hostsharing/hsadminng/persistence/BaseEntity.java b/src/main/java/net/hostsharing/hsadminng/persistence/BaseEntity.java index 5998407d..b3e5a535 100644 --- a/src/main/java/net/hostsharing/hsadminng/persistence/BaseEntity.java +++ b/src/main/java/net/hostsharing/hsadminng/persistence/BaseEntity.java @@ -1,10 +1,8 @@ package net.hostsharing.hsadminng.persistence; -import net.hostsharing.hsadminng.rbac.role.RbacRoleType; import org.hibernate.Hibernate; -import jakarta.persistence.Table; import java.util.UUID; public interface BaseEntity> { @@ -17,13 +15,4 @@ public interface BaseEntity> { //noinspection unchecked return (T) this; }; - - default String role(RbacRoleType rbacRoleType) { - if ( getUuid() == null ) { - throw new IllegalStateException("UUID missing => role can't be determined"); - } - final Table tableAnnot = getClass().getAnnotation(Table.class); - final var qualifiedTableName = tableAnnot.schema() + "." + tableAnnot.name(); - return qualifiedTableName + "#" + getUuid() + ":" + rbacRoleType.name(); - } } diff --git a/src/main/java/net/hostsharing/hsadminng/rbac/role/WithRoleId.java b/src/main/java/net/hostsharing/hsadminng/rbac/role/WithRoleId.java new file mode 100644 index 00000000..b703cc17 --- /dev/null +++ b/src/main/java/net/hostsharing/hsadminng/rbac/role/WithRoleId.java @@ -0,0 +1,20 @@ +package net.hostsharing.hsadminng.rbac.role; + +import jakarta.persistence.Table; +import java.util.UUID; + +public interface WithRoleId { + UUID getUuid(); + + /** + * @return the RBAC-Role-Id of the given `rbacRoleType` for this entity instance. + */ + default String roleId(final RbacRoleType rbacRoleType) { + if ( getUuid() == null ) { + throw new IllegalStateException("UUID missing => role can't be determined"); + } + final Table tableAnnot = getClass().getAnnotation(Table.class); + final var qualifiedTableName = tableAnnot.schema() + "." + tableAnnot.name(); + return qualifiedTableName + "#" + getUuid() + ":" + rbacRoleType.name(); + } +} diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorRepositoryIntegrationTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorRepositoryIntegrationTest.java index 846b6b3d..948ec958 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorRepositoryIntegrationTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorRepositoryIntegrationTest.java @@ -340,7 +340,7 @@ class HsOfficeDebitorRepositoryIntegrationTest extends ContextBasedTestWithClean assertThatDebitorIsVisibleForUserWithRole( givenDebitor, - givenDebitor.getDebitorRel().role(ADMIN), true); + givenDebitor.getDebitorRel().roleId(ADMIN), true); final var givenNewPartnerPerson = one(personRepo.findPersonByOptionalNameLike("First")); final var givenNewBillingPerson = one(personRepo.findPersonByOptionalNameLike("Firby")); final var givenNewContact = one(contactRealRepo.findContactByOptionalCaptionLike("sixth contact")); @@ -373,10 +373,10 @@ class HsOfficeDebitorRepositoryIntegrationTest extends ContextBasedTestWithClean // ... partner role was reassigned: assertThatDebitorIsNotVisibleForUserWithRole( result.returnedValue(), - originalDebitorRel.role(ADMIN)); + originalDebitorRel.roleId(ADMIN)); assertThatDebitorIsVisibleForUserWithRole( result.returnedValue(), - result.returnedValue().getDebitorRel().role(ADMIN), true); + result.returnedValue().getDebitorRel().roleId(ADMIN), true); // ... contact role was reassigned: assertThatDebitorIsNotVisibleForUserWithRole( @@ -402,7 +402,7 @@ class HsOfficeDebitorRepositoryIntegrationTest extends ContextBasedTestWithClean final var givenDebitor = givenSomeTemporaryDebitor("Fourth", "tenth contact", null, "fig"); assertThatDebitorIsVisibleForUserWithRole( givenDebitor, - givenDebitor.getDebitorRel().role(ADMIN), true); + givenDebitor.getDebitorRel().roleId(ADMIN), true); assertThatDebitorActuallyInDatabase(givenDebitor, true); final var givenNewBankAccount = one(bankAccountRepo.findByOptionalHolderLike("first")); @@ -566,7 +566,7 @@ class HsOfficeDebitorRepositoryIntegrationTest extends ContextBasedTestWithClean // when final var result = jpaAttempt.transacted(() -> { - context("superuser-alex@hostsharing.net", givenDebitor.getDebitorRel().role(ADMIN)); + context("superuser-alex@hostsharing.net", givenDebitor.getDebitorRel().roleId(ADMIN)); assertThat(debitorRepo.findByUuid(givenDebitor.getUuid())).isPresent(); debitorRepo.deleteByUuid(givenDebitor.getUuid()); diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerRepositoryIntegrationTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerRepositoryIntegrationTest.java index 8a2b8d44..c8a709a1 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerRepositoryIntegrationTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/partner/HsOfficePartnerRepositoryIntegrationTest.java @@ -282,7 +282,7 @@ class HsOfficePartnerRepositoryIntegrationTest extends ContextBasedTestWithClean "rbac.global#global:ADMIN"); assertThatPartnerIsVisibleForUserWithRole( givenPartner, - givenPartner.getPartnerRel().getHolder().role(ADMIN)); + givenPartner.getPartnerRel().getHolder().roleId(ADMIN)); assertThatPartnerIsNotVisibleForUserWithRole( givenPartner, "hs_office.person#ErbenBesslerMelBessler:ADMIN");