From bc44887675ab345ab5259027200d1951e8b7aeea Mon Sep 17 00:00:00 2001 From: Thomas Georgios Giannos Date: Thu, 4 Apr 2024 11:43:04 +0300 Subject: [PATCH] Added maintenance tasks for sending touch events (users and tenants) --- .../java/eu/eudat/audit/AuditableAction.java | 2 + .../eu/eudat/authorization/Permission.java | 2 + .../TenantTouchedIntegrationEventHandler.java | 2 - ...antTouchedIntegrationEventHandlerImpl.java | 16 ++-- .../controllers/MaintenanceController.java | 87 +++++++++++++++++-- .../src/main/resources/config/permissions.yml | 8 ++ 6 files changed, 103 insertions(+), 14 deletions(-) diff --git a/dmp-backend/core/src/main/java/eu/eudat/audit/AuditableAction.java b/dmp-backend/core/src/main/java/eu/eudat/audit/AuditableAction.java index c614f310d..39e172336 100644 --- a/dmp-backend/core/src/main/java/eu/eudat/audit/AuditableAction.java +++ b/dmp-backend/core/src/main/java/eu/eudat/audit/AuditableAction.java @@ -145,6 +145,8 @@ public class AuditableAction { public static final EventId Maintenance_GenerateElastic = new EventId(220000, "Maintenance_GenerateElastic"); public static final EventId Maintenance_ClearElastic = new EventId(230000, "Maintenance_ClearElastic"); + public static final EventId Maintenance_SendUserTouchEvents = new EventId(230001, "Maintenance_SendUserTouchEvents"); + public static final EventId Maintenance_SendTenantTouchEvents = new EventId(230002, "Maintenance_SendTenantTouchEvents"); public static final EventId Principal_Lookup = new EventId(240000, "Principal_Lookup"); public static final EventId Principal_MyTenants = new EventId(240001, "Principal_MyTenants"); diff --git a/dmp-backend/core/src/main/java/eu/eudat/authorization/Permission.java b/dmp-backend/core/src/main/java/eu/eudat/authorization/Permission.java index f031e632a..b7de28665 100644 --- a/dmp-backend/core/src/main/java/eu/eudat/authorization/Permission.java +++ b/dmp-backend/core/src/main/java/eu/eudat/authorization/Permission.java @@ -22,6 +22,8 @@ public final class Permission { public static String PublicBrowseReferenceType = "PublicBrowseReferenceType"; //Elastic public static String ManageElastic = "ManageElastic"; + //Queue Events + public static String ManageQueueEvents = "ManageQueueEvents"; //Deposit diff --git a/dmp-backend/core/src/main/java/eu/eudat/integrationevent/outbox/tenanttouched/TenantTouchedIntegrationEventHandler.java b/dmp-backend/core/src/main/java/eu/eudat/integrationevent/outbox/tenanttouched/TenantTouchedIntegrationEventHandler.java index 4ef529ac2..579b2e9c7 100644 --- a/dmp-backend/core/src/main/java/eu/eudat/integrationevent/outbox/tenanttouched/TenantTouchedIntegrationEventHandler.java +++ b/dmp-backend/core/src/main/java/eu/eudat/integrationevent/outbox/tenanttouched/TenantTouchedIntegrationEventHandler.java @@ -1,7 +1,5 @@ package eu.eudat.integrationevent.outbox.tenanttouched; -import javax.management.InvalidApplicationException; - public interface TenantTouchedIntegrationEventHandler { void handle(TenantTouchedIntegrationEvent event); diff --git a/dmp-backend/core/src/main/java/eu/eudat/integrationevent/outbox/tenanttouched/TenantTouchedIntegrationEventHandlerImpl.java b/dmp-backend/core/src/main/java/eu/eudat/integrationevent/outbox/tenanttouched/TenantTouchedIntegrationEventHandlerImpl.java index 06cd22296..8d788478c 100644 --- a/dmp-backend/core/src/main/java/eu/eudat/integrationevent/outbox/tenanttouched/TenantTouchedIntegrationEventHandlerImpl.java +++ b/dmp-backend/core/src/main/java/eu/eudat/integrationevent/outbox/tenanttouched/TenantTouchedIntegrationEventHandlerImpl.java @@ -1,18 +1,24 @@ package eu.eudat.integrationevent.outbox.tenanttouched; -import eu.eudat.commons.scope.tenant.TenantScope; import eu.eudat.integrationevent.outbox.OutboxIntegrationEvent; import eu.eudat.integrationevent.outbox.OutboxService; +import org.springframework.beans.factory.config.ConfigurableBeanFactory; +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Component; + import java.util.UUID; +@Component("outboxtenanttouchedintegrationeventhandler") +@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) public class TenantTouchedIntegrationEventHandlerImpl implements TenantTouchedIntegrationEventHandler { + private final OutboxService outboxService; - public TenantTouchedIntegrationEventHandlerImpl(OutboxService outboxService) { - this.outboxService = outboxService; - } + public TenantTouchedIntegrationEventHandlerImpl(OutboxService outboxService) { + this.outboxService = outboxService; + } - @Override + @Override public void handle(TenantTouchedIntegrationEvent event) { OutboxIntegrationEvent message = new OutboxIntegrationEvent(); message.setMessageId(UUID.randomUUID()); diff --git a/dmp-backend/web/src/main/java/eu/eudat/controllers/MaintenanceController.java b/dmp-backend/web/src/main/java/eu/eudat/controllers/MaintenanceController.java index 768633f43..049cbb992 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/controllers/MaintenanceController.java +++ b/dmp-backend/web/src/main/java/eu/eudat/controllers/MaintenanceController.java @@ -2,32 +2,67 @@ package eu.eudat.controllers; import eu.eudat.audit.AuditableAction; import eu.eudat.authorization.Permission; +import eu.eudat.commons.enums.IsActive; +import eu.eudat.data.TenantEntity; +import eu.eudat.data.TenantEntityManager; +import eu.eudat.data.UserEntity; +import eu.eudat.integrationevent.outbox.tenanttouched.TenantTouchedIntegrationEvent; +import eu.eudat.integrationevent.outbox.tenanttouched.TenantTouchedIntegrationEventHandler; +import eu.eudat.integrationevent.outbox.usertouched.UserTouchedIntegrationEventHandler; +import eu.eudat.query.TenantQuery; +import eu.eudat.query.UserQuery; import eu.eudat.service.elastic.ElasticService; import gr.cite.commons.web.authz.service.AuthorizationService; import gr.cite.tools.auditing.AuditService; +import gr.cite.tools.data.query.QueryFactory; import gr.cite.tools.logging.LoggerService; +import jakarta.persistence.EntityManager; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; +import javax.management.InvalidApplicationException; +import java.util.List; + @RestController @RequestMapping(path = "api/maintenance") public class MaintenanceController { private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(MaintenanceController.class)); + private final AuthorizationService authorizationService; + private final ElasticService elasticService; + private final AuditService auditService; - @Autowired - public MaintenanceController(AuthorizationService authorizationService, ElasticService elasticService, AuditService auditService) { - this.authorizationService = authorizationService; - this.elasticService = elasticService; - this.auditService = auditService; - } + private final QueryFactory queryFactory; + private final TenantEntityManager entityManager; + + private final UserTouchedIntegrationEventHandler userTouchedIntegrationEventHandler; + + private final TenantTouchedIntegrationEventHandler tenantTouchedIntegrationEventHandler; + + @Autowired + public MaintenanceController( + AuthorizationService authorizationService, + ElasticService elasticService, + AuditService auditService, + QueryFactory queryFactory, + TenantEntityManager entityManager, + UserTouchedIntegrationEventHandler userTouchedIntegrationEventHandler, + TenantTouchedIntegrationEventHandler tenantTouchedIntegrationEventHandler) { + this.authorizationService = authorizationService; + this.elasticService = elasticService; + this.auditService = auditService; + this.queryFactory = queryFactory; + this.entityManager = entityManager; + this.userTouchedIntegrationEventHandler = userTouchedIntegrationEventHandler; + this.tenantTouchedIntegrationEventHandler = tenantTouchedIntegrationEventHandler; + } @RequestMapping(method = RequestMethod.POST, value = {"/index/elastic"}) public void generateIndex() throws Exception { @@ -36,6 +71,7 @@ public class MaintenanceController { elasticService.resetDmpIndex(); elasticService.resetDescriptionIndex(); + this.auditService.track(AuditableAction.Maintenance_GenerateElastic); } @@ -43,9 +79,46 @@ public class MaintenanceController { public void clearIndex() throws Exception { logger.debug("clear elastic"); this.authorizationService.authorizeForce(Permission.ManageElastic); - + elasticService.deleteDescriptionIndex(); elasticService.deleteDmpIndex(); + this.auditService.track(AuditableAction.Maintenance_ClearElastic); } + + @RequestMapping(method = RequestMethod.DELETE, value = {"/events/users/touch"}) + public void sendUserTouchEvents() throws InvalidApplicationException { + logger.debug("send user touch queue events"); + this.authorizationService.authorizeForce(Permission.ManageQueueEvents); + + this.entityManager.disableTenantFilters(); + UserQuery userQuery = queryFactory.query(UserQuery.class).isActive(IsActive.Active); + List users = userQuery.collect(); + this.entityManager.enableTenantFilters(); + + for(UserEntity user : users) + this.userTouchedIntegrationEventHandler.handle(user.getId()); + + this.auditService.track(AuditableAction.Maintenance_SendUserTouchEvents); + } + + @RequestMapping(method = RequestMethod.DELETE, value = {"/events/tenants/touch"}) + public void sendTenantTouchEvents() throws InvalidApplicationException { + logger.debug("send tenant touch queue events"); + this.authorizationService.authorizeForce(Permission.ManageQueueEvents); + + this.entityManager.disableTenantFilters(); + TenantQuery tenantQuery = queryFactory.query(TenantQuery.class).isActive(IsActive.Active); + List tenants = tenantQuery.collect(); + this.entityManager.enableTenantFilters(); + + for (TenantEntity tenant : tenants) { + TenantTouchedIntegrationEvent event = new TenantTouchedIntegrationEvent(); + event.setId(tenant.getId()); + event.setCode(tenant.getCode()); + this.tenantTouchedIntegrationEventHandler.handle(event); + } + + this.auditService.track(AuditableAction.Maintenance_SendTenantTouchEvents); + } } diff --git a/dmp-backend/web/src/main/resources/config/permissions.yml b/dmp-backend/web/src/main/resources/config/permissions.yml index c7369a0ac..c9253be3e 100644 --- a/dmp-backend/web/src/main/resources/config/permissions.yml +++ b/dmp-backend/web/src/main/resources/config/permissions.yml @@ -88,6 +88,14 @@ permissions: clients: [ ] allowAnonymous: false allowAuthenticated: false + + # Queue Events + ManageQueueEvents: + roles: + - Admin + clients: [ ] + allowAnonymous: false + allowAuthenticated: false # Deposit BrowseDeposit: