argos/dmp-backend/web/src/main/java/eu/eudat/controllers/LockController.java

225 lines
11 KiB
Java
Raw Normal View History

2024-02-13 08:53:33 +01:00
package eu.eudat.controllers;
2023-12-08 17:05:48 +01:00
import eu.eudat.audit.AuditableAction;
2024-03-19 16:21:50 +01:00
import eu.eudat.authorization.AffiliatedResource;
2023-12-08 17:05:48 +01:00
import eu.eudat.authorization.AuthorizationFlags;
import eu.eudat.authorization.Permission;
2024-03-14 10:23:46 +01:00
import eu.eudat.authorization.authorizationcontentresolver.AuthorizationContentResolver;
import eu.eudat.commons.enums.LockTargetType;
import eu.eudat.model.LockStatus;
2024-01-17 10:20:02 +01:00
import gr.cite.tools.validation.ValidationFilterAnnotation;
2023-12-08 17:05:48 +01:00
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));
2023-12-08 17:05:48 +01:00
private final BuilderFactory builderFactory;
2023-12-08 17:05:48 +01:00
private final AuditService auditService;
2023-12-08 17:05:48 +01:00
private final LockService lockService;
2023-12-08 17:05:48 +01:00
private final CensorFactory censorFactory;
2023-12-08 17:05:48 +01:00
private final QueryFactory queryFactory;
2023-12-08 17:05:48 +01:00
private final MessageSource messageSource;
2023-12-08 17:05:48 +01:00
private final AuthorizationService authService;
2024-03-14 10:23:46 +01:00
private final AuthorizationContentResolver authorizationContentResolver;
@Autowired
public LockController(BuilderFactory builderFactory,
AuditService auditService,
LockService lockService,
CensorFactory censorFactory,
QueryFactory queryFactory,
2024-03-14 10:23:46 +01:00
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;
2024-03-14 10:23:46 +01:00
this.authorizationContentResolver = authorizationContentResolver;
}
2023-12-08 17:05:48 +01:00
@PostMapping("query")
public QueryResult<Lock> query(@RequestBody LockLookup lookup) throws MyApplicationException, MyForbiddenException, InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException {
logger.debug("querying {}", Lock.class.getSimpleName());
2023-12-08 17:05:48 +01:00
this.censorFactory.censor(LockCensor.class).censor(lookup.getProject(), null);
2023-12-08 17:05:48 +01:00
2024-03-12 17:27:16 +01:00
LockQuery query = lookup.enrich(this.queryFactory).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission);
List<LockEntity> data = query.collectAs(lookup.getProject());
2024-03-12 17:27:16 +01:00
List<Lock> 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();
2023-12-08 17:05:48 +01:00
this.auditService.track(AuditableAction.Lock_Query, "lookup", lookup);
2023-12-08 17:05:48 +01:00
return new QueryResult<>(models, count);
}
2023-12-08 17:05:48 +01:00
@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));
2023-12-08 17:05:48 +01:00
this.censorFactory.censor(LockCensor.class).censor(fieldSet, null);
2023-12-08 17:05:48 +01:00
2024-03-12 17:27:16 +01:00
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()));
2023-12-08 17:05:48 +01:00
this.auditService.track(AuditableAction.Lock_Lookup, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("id", id),
new AbstractMap.SimpleEntry<String, Object>("fields", fieldSet)
));
2023-12-08 17:05:48 +01:00
return model;
}
2023-12-08 17:05:48 +01:00
@PostMapping("persist")
@Transactional
@ValidationFilterAnnotation(validator = LockPersist.LockPersistValidator.ValidatorName, argumentName = "model")
2024-01-17 10:20:02 +01:00
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);
2023-12-08 17:05:48 +01:00
Lock persisted = this.lockService.persist(model, fieldSet);
2023-12-08 17:05:48 +01:00
this.auditService.track(AuditableAction.Lock_Persist, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("model", model),
new AbstractMap.SimpleEntry<String, Object>("fields", fieldSet)
));
2023-12-08 17:05:48 +01:00
return persisted;
}
2023-12-08 17:05:48 +01:00
@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));
2023-12-08 17:05:48 +01:00
this.censorFactory.censor(LockCensor.class).censor(fieldSet, null);
2023-12-08 17:05:48 +01:00
2024-03-12 17:27:16 +01:00
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()));
2023-12-08 17:05:48 +01:00
this.auditService.track(AuditableAction.Lock_Lookup, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("targetId", targetId),
new AbstractMap.SimpleEntry<String, Object>("fields", fieldSet)
));
return model;
}
@Transactional
@GetMapping("target/status/{id}")
public LockStatus getLocked(@PathVariable("id") UUID targetId, FieldSet fieldSet) throws Exception {
logger.debug(new MapLogEntry("is locked" + Lock.class.getSimpleName()).And("targetId", targetId).And("fields", fieldSet));
2024-03-19 16:21:50 +01:00
this.authService.authorizeForce(Permission.BrowseLock);
LockStatus lockStatus = this.lockService.isLocked(targetId, fieldSet);
this.auditService.track(AuditableAction.Lock_IsLocked, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("targetId", targetId),
new AbstractMap.SimpleEntry<String, Object>("fields", fieldSet)
));
return lockStatus;
}
@Transactional
@GetMapping("target/lock/{id}/{targetType}")
public boolean lock(@PathVariable("id") UUID targetId, @PathVariable("targetType") int targetType) 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.lock(targetId, LockTargetType.of((short) targetType));
this.auditService.track(AuditableAction.Lock_Locked, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("targetId", targetId),
new AbstractMap.SimpleEntry<String, Object>("targetType", targetType)
));
return true;
}
@Transactional
@DeleteMapping("target/touch/{id}")
public boolean touch(@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.touch(targetId);
this.auditService.track(AuditableAction.Lock_Touched, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("targetId", targetId)
));
return true;
}
@Transactional
@DeleteMapping("target/unlock/{id}")
public boolean unlock(@PathVariable("id") UUID targetId) throws Exception {
2024-03-19 16:21:50 +01:00
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<String, Object>("targetId", targetId)
));
return true;
}
2024-03-22 14:18:51 +01:00
@DeleteMapping("{id}/{target}")
@Transactional
public void delete(@PathVariable("id") UUID id, @PathVariable("target") UUID target) throws MyForbiddenException, InvalidApplicationException {
logger.debug(new MapLogEntry("retrieving" + Lock.class.getSimpleName()).And("id", id));
2024-03-22 14:18:51 +01:00
this.lockService.deleteAndSave(id, target);
this.auditService.track(AuditableAction.Lock_Delete, "id", id);
}
2023-12-08 17:05:48 +01:00
}