package eu.eudat.controllers; import eu.eudat.audit.AuditableAction; import eu.eudat.authorization.AffiliatedResource; import eu.eudat.authorization.AuthorizationFlags; import eu.eudat.authorization.Permission; import eu.eudat.authorization.authorizationcontentresolver.AuthorizationContentResolver; import gr.cite.tools.validation.ValidationFilterAnnotation; import eu.eudat.data.LockEntity; import eu.eudat.model.Lock; import eu.eudat.model.builder.LockBuilder; import eu.eudat.model.censorship.LockCensor; import eu.eudat.model.persist.LockPersist; import eu.eudat.model.result.QueryResult; import eu.eudat.query.LockQuery; import eu.eudat.query.lookup.LockLookup; import eu.eudat.service.lock.LockService; import gr.cite.commons.web.authz.service.AuthorizationService; import gr.cite.tools.auditing.AuditService; import gr.cite.tools.data.builder.BuilderFactory; import gr.cite.tools.data.censor.CensorFactory; import gr.cite.tools.data.query.QueryFactory; import gr.cite.tools.exception.MyApplicationException; import gr.cite.tools.exception.MyForbiddenException; import gr.cite.tools.exception.MyNotFoundException; import gr.cite.tools.fieldset.FieldSet; import gr.cite.tools.logging.LoggerService; import gr.cite.tools.logging.MapLogEntry; import jakarta.transaction.Transactional; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.MessageSource; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.web.bind.annotation.*; import javax.crypto.BadPaddingException; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import javax.management.InvalidApplicationException; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.util.AbstractMap; import java.util.List; import java.util.Map; import java.util.UUID; @RestController @RequestMapping(path = {"api/lock"}) public class LockController { private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(LockController.class)); private final BuilderFactory builderFactory; private final AuditService auditService; private final LockService lockService; private final CensorFactory censorFactory; private final QueryFactory queryFactory; private final MessageSource messageSource; private final AuthorizationService authService; private final AuthorizationContentResolver authorizationContentResolver; @Autowired public LockController(BuilderFactory builderFactory, AuditService auditService, LockService lockService, CensorFactory censorFactory, QueryFactory queryFactory, MessageSource messageSource, AuthorizationService authService, AuthorizationContentResolver authorizationContentResolver) { this.builderFactory = builderFactory; this.auditService = auditService; this.lockService = lockService; this.censorFactory = censorFactory; this.queryFactory = queryFactory; this.messageSource = messageSource; this.authService = authService; this.authorizationContentResolver = authorizationContentResolver; } @PostMapping("query") public QueryResult query(@RequestBody LockLookup lookup) throws MyApplicationException, MyForbiddenException, InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException { logger.debug("querying {}", Lock.class.getSimpleName()); this.censorFactory.censor(LockCensor.class).censor(lookup.getProject(), null); LockQuery query = lookup.enrich(this.queryFactory).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission); List data = query.collectAs(lookup.getProject()); List models = this.builderFactory.builder(LockBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).build(lookup.getProject(), data); long count = (lookup.getMetadata() != null && lookup.getMetadata().getCountAll()) ? query.count() : models.size(); this.auditService.track(AuditableAction.Lock_Query, "lookup", lookup); return new QueryResult<>(models, count); } @GetMapping("{id}") public Lock get(@PathVariable("id") UUID id, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException { logger.debug(new MapLogEntry("retrieving" + Lock.class.getSimpleName()).And("id", id).And("fields", fieldSet)); this.censorFactory.censor(LockCensor.class).censor(fieldSet, null); LockQuery query = this.queryFactory.query(LockQuery.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).ids(id); Lock model = this.builderFactory.builder(LockBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).build(fieldSet, query.firstAs(fieldSet)); if (model == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{id, Lock.class.getSimpleName()}, LocaleContextHolder.getLocale())); this.auditService.track(AuditableAction.Lock_Lookup, Map.ofEntries( new AbstractMap.SimpleEntry("id", id), new AbstractMap.SimpleEntry("fields", fieldSet) )); return model; } @PostMapping("persist") @Transactional @ValidationFilterAnnotation(validator = LockPersist.LockPersistValidator.ValidatorName, argumentName = "model") public Lock persist(@RequestBody LockPersist model, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException, InvalidApplicationException { logger.debug(new MapLogEntry("persisting" + Lock.class.getSimpleName()).And("model", model).And("fieldSet", fieldSet)); this.censorFactory.censor(LockCensor.class).censor(fieldSet, null); Lock persisted = this.lockService.persist(model, fieldSet); this.auditService.track(AuditableAction.Lock_Persist, Map.ofEntries( new AbstractMap.SimpleEntry("model", model), new AbstractMap.SimpleEntry("fields", fieldSet) )); return persisted; } @GetMapping("target/{id}") public Lock getWithTarget(@PathVariable("id") UUID targetId, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException { logger.debug(new MapLogEntry("retrieving" + Lock.class.getSimpleName()).And("targetId", targetId).And("fields", fieldSet)); this.censorFactory.censor(LockCensor.class).censor(fieldSet, null); LockQuery query = this.queryFactory.query(LockQuery.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).targetIds(targetId); Lock model = this.builderFactory.builder(LockBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).build(fieldSet, query.firstAs(fieldSet)); if (model == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{targetId, Lock.class.getSimpleName()}, LocaleContextHolder.getLocale())); this.auditService.track(AuditableAction.Lock_Lookup, Map.ofEntries( new AbstractMap.SimpleEntry("targetId", targetId), new AbstractMap.SimpleEntry("fields", fieldSet) )); return model; } @Transactional @GetMapping("target/status/{id}") public Boolean getLocked(@PathVariable("id") UUID targetId) throws Exception { logger.debug(new MapLogEntry("is locked" + Lock.class.getSimpleName()).And("targetId", targetId)); this.authService.authorizeForce(Permission.BrowseLock); Boolean isLocked = this.lockService.isLocked(targetId); this.auditService.track(AuditableAction.Lock_IsLocked, Map.ofEntries( new AbstractMap.SimpleEntry("targetId", targetId) )); return isLocked; } @Transactional @DeleteMapping("target/unlock/{id}") public boolean unlock(@PathVariable("id") UUID targetId) throws Exception { AffiliatedResource affiliatedResourceDmp = this.authorizationContentResolver.dmpAffiliation(targetId); AffiliatedResource affiliatedResourceDescription = this.authorizationContentResolver.descriptionAffiliation(targetId); this.authService.authorizeAtLeastOneForce(List.of(affiliatedResourceDmp, affiliatedResourceDescription), Permission.EditLock); this.lockService.unlock(targetId); this.auditService.track(AuditableAction.Lock_UnLocked, Map.ofEntries( new AbstractMap.SimpleEntry("targetId", targetId) )); return true; } @DeleteMapping("{id}") @Transactional public void delete(@PathVariable("id") UUID id) throws MyForbiddenException, InvalidApplicationException { logger.debug(new MapLogEntry("retrieving" + Lock.class.getSimpleName()).And("id", id)); this.lockService.deleteAndSave(id); this.auditService.track(AuditableAction.Lock_Delete, "id", id); } }