add-webspace-gid-and-create-webspace-main-user #94

Merged
hsh-michaelhoennig merged 6 commits from add-webspace-gid-and-create-webspace-main-user into master 2024-09-03 10:28:58 +02:00
5 changed files with 61 additions and 5 deletions
Showing only changes of commit 0f070ab8bb - Show all commits

View File

@ -34,6 +34,7 @@ import jakarta.persistence.OneToOne;
import jakarta.persistence.PostLoad; import jakarta.persistence.PostLoad;
import jakarta.persistence.Transient; import jakarta.persistence.Transient;
import jakarta.persistence.Version; import jakarta.persistence.Version;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -115,6 +116,13 @@ public abstract class HsHostingAsset implements Stringifyable, BaseEntity<HsHost
this.isLoaded = true; this.isLoaded = true;
} }
public List<HsHostingAssetRealEntity> getSubHostingAssets() {
if (subHostingAssets == null) {
subHostingAssets = new ArrayList<>();
}
return subHostingAssets;
}
public PatchableMapWrapper<Object> getConfig() { public PatchableMapWrapper<Object> getConfig() {
return PatchableMapWrapper.of(configWrapper, (newWrapper) -> {configWrapper = newWrapper;}, config); return PatchableMapWrapper.of(configWrapper, (newWrapper) -> {configWrapper = newWrapper;}, config);
} }

View File

@ -66,6 +66,8 @@ public class HostingAssetEntitySaveProcessor {
public HostingAssetEntitySaveProcessor saveUsing(final Function<HsHostingAsset, HsHostingAsset> saveFunction) { public HostingAssetEntitySaveProcessor saveUsing(final Function<HsHostingAsset, HsHostingAsset> saveFunction) {
step("saveUsing", "validateContext"); step("saveUsing", "validateContext");
entity = saveFunction.apply(entity); entity = saveFunction.apply(entity);
em.flush(); // makes RbacEntity available as RealEntity if needed
validator.postPersist(em, entity);
return this; return this;
} }

View File

@ -1,10 +1,13 @@
package net.hostsharing.hsadminng.hs.hosting.asset.validators; package net.hostsharing.hsadminng.hs.hosting.asset.validators;
import net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAsset; import net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAsset;
import net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetRealEntity;
import jakarta.persistence.EntityManager;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.MANAGED_WEBSPACE; import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.MANAGED_WEBSPACE;
import static net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType.UNIX_USER;
class HsManagedWebspaceHostingAssetValidator extends HostingAssetEntityValidator { class HsManagedWebspaceHostingAssetValidator extends HostingAssetEntityValidator {
public HsManagedWebspaceHostingAssetValidator() { public HsManagedWebspaceHostingAssetValidator() {
@ -22,4 +25,32 @@ class HsManagedWebspaceHostingAssetValidator extends HostingAssetEntityValidator
: "[a-z][a-z0-9][a-z0-9]"; : "[a-z][a-z0-9][a-z0-9]";
return Pattern.compile("^" + prefixPattern + "[0-9][0-9]$"); return Pattern.compile("^" + prefixPattern + "[0-9][0-9]$");
} }
@Override
public void postPersist(final EntityManager em, final HsHostingAsset webspaceAsset) {
if (!webspaceAsset.isLoaded()) {
final var unixUserAsset = HsHostingAssetRealEntity.builder()
.type(UNIX_USER)
.parentAsset(em.find(HsHostingAssetRealEntity.class, webspaceAsset.getUuid()))
.identifier(webspaceAsset.getIdentifier())
.caption(webspaceAsset.getIdentifier() + " webspace user")
.build();
webspaceAsset.getSubHostingAssets().add(unixUserAsset);
final var emw = new EntityManagerWrapper(em);
new HostingAssetEntitySaveProcessor(em, unixUserAsset)
.preprocessEntity()
hsh-michaelhoennig marked this conversation as resolved
Review

kann weg

kann weg
.validateEntity()
.prepareForSave()
.saveUsing(emw::persist)
.validateContext();
}
}
}
record EntityManagerWrapper(EntityManager em) {
HsHostingAsset persist(HsHostingAsset hsHostingAsset) {
em.persist(hsHostingAsset);
return hsHostingAsset;
}
} }

View File

@ -160,4 +160,7 @@ public abstract class HsEntityValidator<E extends PropertiesProvider> {
public ValidatableProperty<?, ?> getProperty(final String propertyName) { public ValidatableProperty<?, ?> getProperty(final String propertyName) {
return stream(propertyValidators).filter(pv -> pv.propertyName().equals(propertyName)).findFirst().orElse(null); return stream(propertyValidators).filter(pv -> pv.propertyName().equals(propertyName)).findFirst().orElse(null);
} }
public void postPersist(final EntityManager em, final E entity) {
}
} }

View File

@ -182,11 +182,23 @@ class HsHostingAssetControllerAcceptanceTest extends ContextBasedTestWithCleanup
.header("Location", matchesRegex("http://localhost:[1-9][0-9]*/api/hs/hosting/assets/[^/]*")) .header("Location", matchesRegex("http://localhost:[1-9][0-9]*/api/hs/hosting/assets/[^/]*"))
.extract().header("Location"); // @formatter:on .extract().header("Location"); // @formatter:on
// finally, the new asset can be accessed under the generated UUID // the new asset can be accessed under the generated UUID
final var newWebspace = UUID.fromString( final var newWebspaceUuid = UUID.fromString(
location.substring(location.lastIndexOf('/') + 1)); location.substring(location.lastIndexOf('/') + 1));
assertThat(newWebspace).isNotNull(); assertThat(newWebspaceUuid).isNotNull();
toCleanup(HsHostingAssetRbacEntity.class, newWebspace); toCleanup(HsHostingAssetRbacEntity.class, newWebspaceUuid);
// and a default user got created
final var webspaceUnixUser = em.createQuery("SELECT ha FROM HsHostingAssetRealEntity ha WHERE ha.parentAsset.uuid=:webspaceUUID")
.setParameter("webspaceUUID", newWebspaceUuid)
.getSingleResult();
assertThat(webspaceUnixUser).isNotNull().extracting(Object::toString)
.isEqualTo("""
HsHostingAsset(UNIX_USER, fir10, fir10 webspace user, MANAGED_WEBSPACE:fir10, {
"password" : null,
"userid" : 1000000
})
""".trim());
} }
@Test @Test
@ -325,7 +337,7 @@ class HsHostingAssetControllerAcceptanceTest extends ContextBasedTestWithCleanup
jpaAttempt.transacted(() -> { jpaAttempt.transacted(() -> {
context.define("superuser-alex@hostsharing.net"); context.define("superuser-alex@hostsharing.net");
for (int n = 0; n < UNIX_USER_PER_MULTI_OPTION -preExistingUnixUserCount+1; ++n) { for (int n = 0; n < UNIX_USER_PER_MULTI_OPTION-preExistingUnixUserCount; ++n) {
toCleanup(realAssetRepo.save( toCleanup(realAssetRepo.save(
HsHostingAssetRealEntity.builder() HsHostingAssetRealEntity.builder()
.type(UNIX_USER) .type(UNIX_USER)