add-unix-user-hosting-asset-validation #66

Merged
hsh-michaelhoennig merged 11 commits from add-unix-user-hosting-asset-validation into master 2024-06-27 12:39:45 +02:00
2 changed files with 19 additions and 16 deletions
Showing only changes of commit 680b67f162 - Show all commits

View File

@ -23,6 +23,7 @@ import java.util.List;
import java.util.UUID;
import java.util.function.BiConsumer;
import static net.hostsharing.hsadminng.hs.hosting.asset.validators.HsHostingAssetEntityValidatorRegistry.cleanup;
import static net.hostsharing.hsadminng.hs.hosting.asset.validators.HsHostingAssetEntityValidatorRegistry.validated;
@RestController
@ -71,7 +72,7 @@ public class HsHostingAssetController implements HsHostingAssetsApi {
final var entityToSave = mapper.map(body, HsHostingAssetEntity.class, RESOURCE_TO_ENTITY_POSTMAPPER);
final var saved = saveAndValidate(entityToSave);
final var saved = validated(assetRepo.save(entityToSave));
final var uri =
MvcUriComponentsBuilder.fromController(getClass())
@ -126,8 +127,8 @@ public class HsHostingAssetController implements HsHostingAssetsApi {
new HsHostingAssetEntityPatcher(em, current).apply(body);
final var saved = saveAndValidate(current);
final var mapped = mapper.map(saved, HsHostingAssetResource.class);
final var saved = validated(assetRepo.save(current));
final var mapped = mapper.map(saved, HsHostingAssetResource.class, ENTITY_TO_RESOURCE_POSTMAPPER);
return ResponseEntity.ok(mapped);
}
@ -145,11 +146,7 @@ public class HsHostingAssetController implements HsHostingAssetsApi {
}
};
HsHostingAssetEntity saveAndValidate(final HsHostingAssetEntity entity) {
final var saved = assetRepo.save(entity);
// FIXME: this is hacky, better remove the properties from the mapped resource object
em.flush();
em.detach(saved); // validated(...) is going to remove writeOnly properties
return validated(saved);
}
final BiConsumer<HsHostingAssetEntity, HsHostingAssetResource> ENTITY_TO_RESOURCE_POSTMAPPER = (entity, resource) -> {
cleanup(entity, resource);
};
}

View File

@ -2,6 +2,7 @@ package net.hostsharing.hsadminng.hs.hosting.asset.validators;
import net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetEntity;
import net.hostsharing.hsadminng.hs.hosting.asset.HsHostingAssetType;
import net.hostsharing.hsadminng.hs.hosting.generated.api.v1.model.HsHostingAssetResource;
import net.hostsharing.hsadminng.hs.validation.HsEntityValidator;
import net.hostsharing.hsadminng.errors.MultiValidationException;
@ -41,12 +42,7 @@ public class HsHostingAssetEntityValidatorRegistry {
public static List<String> doValidate(final HsHostingAssetEntity hostingAsset) {
final var validator = HsHostingAssetEntityValidatorRegistry.forType(hostingAsset.getType());
final var validated = validator.validate(hostingAsset);
//validator.cleanup()
hostingAsset.getConfig().remove("password"); // FIXME
hostingAsset.getConfig().remove("totpKey"); // FIXME
return validated;
return validator.validate(hostingAsset);
}
public static HsHostingAssetEntity validated(final HsHostingAssetEntity entityToSave) {
@ -54,4 +50,14 @@ public class HsHostingAssetEntityValidatorRegistry {
return entityToSave;
}
public static void cleanup(final HsHostingAssetEntity entity, final HsHostingAssetResource resource) {
final var validator = HsHostingAssetEntityValidatorRegistry.forType(entity.getType());
// final var validated = validator.cleanup(hostingAsset);
//validator.cleanup()
final var config = new HashMap<>((Map<String, Object>) resource.getConfig());
config.remove("password"); // FIXME
config.remove("totpKey"); // FIXME
resource.setConfig(config);
}
}