fix tenant scope

This commit is contained in:
Efstratios Giannopoulos 2024-04-22 10:00:04 +03:00
parent 2be94e39ff
commit c523bda52b
4 changed files with 134 additions and 3 deletions

View File

@ -11,6 +11,8 @@ import javax.management.InvalidApplicationException;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicReference;
@Component
@RequestScope
public class TenantScope {
public static final String TenantReplaceParameter = "::TenantCode::";
private final MultitenancyProperties multitenancy;

View File

@ -0,0 +1,129 @@
package eu.eudat.controllers;
import eu.eudat.audit.AuditableAction;
import eu.eudat.authorization.AuthorizationFlags;
import gr.cite.tools.validation.ValidationFilterAnnotation;
import eu.eudat.data.EntityDoiEntity;
import eu.eudat.model.DescriptionTemplateType;
import eu.eudat.model.EntityDoi;
import eu.eudat.model.builder.EntityDoiBuilder;
import eu.eudat.model.censorship.EntityDoiCensor;
import eu.eudat.model.persist.EntityDoiPersist;
import eu.eudat.model.result.QueryResult;
import eu.eudat.query.EntityDoiQuery;
import eu.eudat.query.lookup.EntityDoiLookup;
import eu.eudat.service.deposit.DepositService;
import eu.eudat.service.entitydoi.EntityDoiService;
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 org.slf4j.LoggerFactory;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import javax.management.InvalidApplicationException;
import java.util.*;
@RestController
@RequestMapping(path = "api/entity-doi")
public class EntityDoiController {
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(DescriptionTemplateTypeController.class));
private final BuilderFactory builderFactory;
private final AuditService auditService;
private final EntityDoiService entityDoiService;
private final CensorFactory censorFactory;
private final QueryFactory queryFactory;
private final MessageSource messageSource;
public EntityDoiController(
BuilderFactory builderFactory,
AuditService auditService,
EntityDoiService entityDoiService, CensorFactory censorFactory,
QueryFactory queryFactory,
MessageSource messageSource) {
this.builderFactory = builderFactory;
this.auditService = auditService;
this.entityDoiService = entityDoiService;
this.censorFactory = censorFactory;
this.queryFactory = queryFactory;
this.messageSource = messageSource;
}
@PostMapping("query")
public QueryResult<EntityDoi> Query(@RequestBody EntityDoiLookup lookup) throws MyApplicationException, MyForbiddenException {
logger.debug("querying {}", EntityDoi.class.getSimpleName());
this.censorFactory.censor(EntityDoiCensor.class).censor(lookup.getProject(), null);
EntityDoiQuery query = lookup.enrich(this.queryFactory).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission);
List<EntityDoiEntity> data = query.collectAs(lookup.getProject());
List<EntityDoi> models = this.builderFactory.builder(EntityDoiBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).build(lookup.getProject(), data);
long count = (lookup.getMetadata() != null && lookup.getMetadata().getCountAll()) ? query.count() : models.size();
this.auditService.track(AuditableAction.EntityDoi_Query, "lookup", lookup);
return new QueryResult<>(models, count);
}
@GetMapping("{id}")
public EntityDoi Get(@PathVariable("id") UUID id, FieldSet fieldSet, Locale locale) throws MyApplicationException, MyForbiddenException, MyNotFoundException {
logger.debug(new MapLogEntry("retrieving" + EntityDoi.class.getSimpleName()).And("id", id).And("fields", fieldSet));
this.censorFactory.censor(EntityDoiCensor.class).censor(fieldSet, null);
EntityDoiQuery query = this.queryFactory.query(EntityDoiQuery.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).ids(id);
EntityDoi model = this.builderFactory.builder(EntityDoiBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).build(fieldSet, query.firstAs(fieldSet));
if (model == null)
throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{id, EntityDoi.class.getSimpleName()}, LocaleContextHolder.getLocale()));
this.auditService.track(AuditableAction.EntityDoi_Lookup, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("id", id),
new AbstractMap.SimpleEntry<String, Object>("fields", fieldSet)
));
return model;
}
@PostMapping("persist")
@Transactional
@ValidationFilterAnnotation(validator = EntityDoiPersist.EntityDoiPersistValidator.ValidatorName, argumentName = "model")
public EntityDoi Persist(@RequestBody EntityDoiPersist model, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException, InvalidApplicationException {
logger.debug(new MapLogEntry("persisting" + DescriptionTemplateType.class.getSimpleName()).And("model", model).And("fieldSet", fieldSet));
EntityDoi persisted = this.entityDoiService.persist(model, fieldSet);
this.auditService.track(AuditableAction.EntityDoi_Persist, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("model", model),
new AbstractMap.SimpleEntry<String, Object>("fields", fieldSet)
));
return persisted;
}
@DeleteMapping("{id}")
@Transactional
public void Delete(@PathVariable("id") UUID id) throws MyForbiddenException, InvalidApplicationException {
logger.debug(new MapLogEntry("retrieving" + EntityDoi.class.getSimpleName()).And("id", id));
this.entityDoiService.deleteAndSave(id);
this.auditService.track(AuditableAction.EntityDoi_Delete, "id", id);
}
}

View File

@ -11,6 +11,8 @@ import javax.management.InvalidApplicationException;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicReference;
@Component
@RequestScope
public class TenantScope {
public static final String TenantReplaceParameter = "::TenantCode::";
private final MultitenancyProperties multitenancy;
@ -120,6 +122,3 @@ public class TenantScope {
}
}
}

View File

@ -14,6 +14,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import javax.management.InvalidApplicationException;
import java.util.UUID;
public class TenantListener {
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(TenantListener.class));
private final TenantScope tenantScope;