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

222 lines
10 KiB
Java
Raw Normal View History

2023-12-08 17:05:48 +01:00
package eu.eudat.controllers.v2;
import eu.eudat.audit.AuditableAction;
import eu.eudat.authorization.AuthorizationFlags;
2023-12-11 17:47:35 +01:00
import eu.eudat.authorization.OwnedResource;
2023-12-08 17:05:48 +01:00
import eu.eudat.authorization.Permission;
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.models.data.helpers.responses.ResponseItem;
import eu.eudat.query.LockQuery;
import eu.eudat.query.lookup.LockLookup;
import eu.eudat.service.lock.LockService;
import eu.eudat.types.ApiMessageCode;
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 gr.cite.tools.validation.MyValidate;
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.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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 authorizationService;
2023-12-11 17:47:35 +01:00
private final AuthorizationService authService;
2023-12-08 17:05:48 +01:00
@Autowired
public LockController(BuilderFactory builderFactory,
AuditService auditService,
LockService lockService,
CensorFactory censorFactory,
QueryFactory queryFactory,
MessageSource messageSource,
2023-12-11 17:47:35 +01:00
AuthorizationService authorizationService, AuthorizationService authService) {
2023-12-08 17:05:48 +01:00
this.builderFactory = builderFactory;
this.auditService = auditService;
this.lockService = lockService;
this.censorFactory = censorFactory;
this.queryFactory = queryFactory;
this.messageSource = messageSource;
this.authorizationService = authorizationService;
2023-12-11 17:47:35 +01:00
this.authService = authService;
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());
this.censorFactory.censor(LockCensor.class).censor(lookup.getProject(), null);
LockQuery query = lookup.enrich(this.queryFactory).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermissionOrPublic);
List<LockEntity> data = query.collectAs(lookup.getProject());
List<Lock> models = this.builderFactory.builder(LockBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermissionOrPublic).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.OwnerOrDmpAssociatedOrPermissionOrPublic).ids(id);
Lock model = this.builderFactory.builder(LockBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermissionOrPublic).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<String, Object>("id", id),
new AbstractMap.SimpleEntry<String, Object>("fields", fieldSet)
));
return model;
}
@PostMapping("persist")
@Transactional
public Lock persist(@MyValidate @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<String, Object>("model", model),
new AbstractMap.SimpleEntry<String, Object>("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.OwnerOrDmpAssociatedOrPermissionOrPublic).targetIds(targetId);
Lock model = this.builderFactory.builder(LockBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermissionOrPublic).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<String, Object>("targetId", targetId),
new AbstractMap.SimpleEntry<String, Object>("fields", fieldSet)
));
return model;
}
@Transactional
@PostMapping("target/status/{id}")
2023-12-11 17:47:35 +01:00
public @ResponseBody ResponseEntity<ResponseItem<Boolean>> getLocked(@PathVariable("id") UUID targetId) throws Exception {
this.authService.authorizeAtLeastOneForce(targetId != null ? List.of(new OwnedResource(targetId)) : null, Permission.BrowseDmp);
2023-12-08 17:05:48 +01:00
2023-12-11 17:47:35 +01:00
boolean locked = this.lockService.isLocked(targetId);
2023-12-08 17:05:48 +01:00
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<Boolean>().status(ApiMessageCode.SUCCESS_MESSAGE).message("locked").payload(locked));
}
@Transactional
@PostMapping("target/unlock/{id}")
2023-12-11 17:47:35 +01:00
public @ResponseBody ResponseEntity<ResponseItem<String>> unlock(@PathVariable("id") UUID targetId) throws Exception {
this.authService.authorizeAtLeastOneForce(targetId != null ? List.of(new OwnedResource(targetId)) : null, Permission.BrowseDmp);
2023-12-08 17:05:48 +01:00
2023-12-11 17:47:35 +01:00
this.lockService.unlock(targetId);
2023-12-08 17:05:48 +01:00
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<String>().status(ApiMessageCode.SUCCESS_MESSAGE).message("Created").payload("Lock Removed"));
}
@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);
}
// @Transactional
// @RequestMapping(method = RequestMethod.GET, path = "target/status/{id}")
// public @ResponseBody ResponseEntity<ResponseItem<Boolean>> getLocked(@PathVariable String id) throws Exception {
// this.authorizationService.authorizeForce(Permission.AuthenticatedRole);
//
// boolean locked = this.lockManager.isLocked(id);
// return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<Boolean>().status(ApiMessageCode.SUCCESS_MESSAGE).message("locked").payload(locked));
// }
//
// @Transactional
// @RequestMapping(method = RequestMethod.DELETE, path = "target/unlock/{id}")
// public @ResponseBody ResponseEntity<ResponseItem<String>> unlock(@PathVariable String id) throws Exception {
// this.authorizationService.authorizeForce(Permission.AuthenticatedRole);
//
// this.lockManager.unlock(id);
// return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<String>().status(ApiMessageCode.SUCCESS_MESSAGE).message("Created").payload("Lock Removed"));
// }
//
// @Transactional
// @RequestMapping(method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
// public @ResponseBody ResponseEntity<ResponseItem<UUID>> createOrUpdate(@RequestBody Lock lock) throws Exception {
// this.authorizationService.authorizeForce(Permission.AuthenticatedRole);
//
// eu.eudat.data.old.Lock result = this.lockManager.createOrUpdate(lock);
// return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<UUID>().status(ApiMessageCode.SUCCESS_MESSAGE).message("Created").payload(result.getId()));
// }
//
// @RequestMapping(method = RequestMethod.GET, path = "target/{id}")
// public @ResponseBody ResponseEntity<ResponseItem<Lock>> getSingle(@PathVariable String id) throws Exception {
// this.authorizationService.authorizeForce(Permission.AuthenticatedRole);
//
// Lock lock = this.lockManager.getFromTarget(id);
// return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<Lock>().status(ApiMessageCode.NO_MESSAGE).payload(lock));
// }
}