create webspace main user for new webspaces
This commit is contained in:
parent
0c9931d73a
commit
ce81c4a0c1
@ -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);
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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()
|
||||||
|
.validateEntity()
|
||||||
|
.prepareForSave()
|
||||||
|
.saveUsing(emw::persist)
|
||||||
|
.validateContext();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
record EntityManagerWrapper(EntityManager em) {
|
||||||
|
|
||||||
|
HsHostingAsset persist(HsHostingAsset hsHostingAsset) {
|
||||||
|
em.persist(hsHostingAsset);
|
||||||
|
return hsHostingAsset;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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)
|
||||||
|
Loading…
Reference in New Issue
Block a user