tenant fixes

This commit is contained in:
Efstratios Giannopoulos 2024-05-30 15:56:37 +03:00
parent fa6d100b41
commit f156d79ace
29 changed files with 277 additions and 323 deletions

View File

@ -9,9 +9,9 @@ import gr.cite.annotation.integrationevent.TrackedEvent;
import gr.cite.annotation.integrationevent.inbox.annotationentitiesremoval.AnnotationEntitiesRemovalIntegrationEventHandler; import gr.cite.annotation.integrationevent.inbox.annotationentitiesremoval.AnnotationEntitiesRemovalIntegrationEventHandler;
import gr.cite.annotation.integrationevent.inbox.annotationentitiestouch.AnnotationEntitiesTouchedIntegrationEventHandler; import gr.cite.annotation.integrationevent.inbox.annotationentitiestouch.AnnotationEntitiesTouchedIntegrationEventHandler;
import gr.cite.annotation.integrationevent.inbox.tenantremoval.TenantRemovalIntegrationEventHandler; import gr.cite.annotation.integrationevent.inbox.tenantremoval.TenantRemovalIntegrationEventHandler;
import gr.cite.annotation.integrationevent.inbox.tenanttouch.TenantTouchedIntegrationEventHandler; import gr.cite.annotation.integrationevent.inbox.tenanttouched.TenantTouchedIntegrationEventHandler;
import gr.cite.annotation.integrationevent.inbox.userremoval.UserRemovalIntegrationEventHandler; import gr.cite.annotation.integrationevent.inbox.userremoval.UserRemovalIntegrationEventHandler;
import gr.cite.annotation.integrationevent.inbox.usertouch.UserTouchedIntegrationEventHandler; import gr.cite.annotation.integrationevent.inbox.usertouched.UserTouchedIntegrationEventHandler;
import gr.cite.annotation.query.QueueInboxQuery; import gr.cite.annotation.query.QueueInboxQuery;
import gr.cite.queueinbox.entity.QueueInbox; import gr.cite.queueinbox.entity.QueueInbox;
import gr.cite.queueinbox.entity.QueueInboxStatus; import gr.cite.queueinbox.entity.QueueInboxStatus;

View File

@ -1,5 +1,6 @@
package gr.cite.annotation.integrationevent.inbox.tenantremoval; package gr.cite.annotation.integrationevent.inbox.tenantremoval;
import gr.cite.annotation.integrationevent.inbox.IntegrationEventHandler; import gr.cite.annotation.integrationevent.inbox.IntegrationEventHandler;
public interface TenantRemovalIntegrationEventHandler extends IntegrationEventHandler { public interface TenantRemovalIntegrationEventHandler extends IntegrationEventHandler {

View File

@ -1,5 +1,7 @@
package gr.cite.annotation.integrationevent.inbox.tenantremoval; package gr.cite.annotation.integrationevent.inbox.tenantremoval;
import gr.cite.commons.web.oidc.principal.CurrentPrincipalResolver;
import gr.cite.commons.web.oidc.principal.extractor.ClaimExtractorProperties;
import gr.cite.annotation.audit.AuditableAction; import gr.cite.annotation.audit.AuditableAction;
import gr.cite.annotation.common.JsonHandlingService; import gr.cite.annotation.common.JsonHandlingService;
import gr.cite.annotation.data.TenantEntityManager; import gr.cite.annotation.data.TenantEntityManager;
@ -7,8 +9,6 @@ import gr.cite.annotation.integrationevent.inbox.EventProcessingStatus;
import gr.cite.annotation.integrationevent.inbox.InboxPrincipal; import gr.cite.annotation.integrationevent.inbox.InboxPrincipal;
import gr.cite.annotation.integrationevent.inbox.IntegrationEventProperties; import gr.cite.annotation.integrationevent.inbox.IntegrationEventProperties;
import gr.cite.annotation.service.tenant.TenantService; import gr.cite.annotation.service.tenant.TenantService;
import gr.cite.commons.web.oidc.principal.CurrentPrincipalResolver;
import gr.cite.commons.web.oidc.principal.extractor.ClaimExtractorProperties;
import gr.cite.tools.auditing.AuditService; import gr.cite.tools.auditing.AuditService;
import gr.cite.tools.logging.LoggerService; import gr.cite.tools.logging.LoggerService;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;

View File

@ -1,4 +1,5 @@
package gr.cite.annotation.integrationevent.inbox.tenanttouch; package gr.cite.annotation.integrationevent.inbox.tenanttouched;
import gr.cite.annotation.integrationevent.TrackedEvent; import gr.cite.annotation.integrationevent.TrackedEvent;

View File

@ -1,4 +1,4 @@
package gr.cite.annotation.integrationevent.inbox.tenanttouch; package gr.cite.annotation.integrationevent.inbox.tenanttouched;
import gr.cite.annotation.integrationevent.inbox.IntegrationEventHandler; import gr.cite.annotation.integrationevent.inbox.IntegrationEventHandler;

View File

@ -1,5 +1,7 @@
package gr.cite.annotation.integrationevent.inbox.tenanttouch; package gr.cite.annotation.integrationevent.inbox.tenanttouched;
import gr.cite.commons.web.oidc.principal.CurrentPrincipalResolver;
import gr.cite.commons.web.oidc.principal.extractor.ClaimExtractorProperties;
import gr.cite.annotation.audit.AuditableAction; import gr.cite.annotation.audit.AuditableAction;
import gr.cite.annotation.common.JsonHandlingService; import gr.cite.annotation.common.JsonHandlingService;
import gr.cite.annotation.data.TenantEntityManager; import gr.cite.annotation.data.TenantEntityManager;
@ -8,8 +10,6 @@ import gr.cite.annotation.integrationevent.inbox.InboxPrincipal;
import gr.cite.annotation.integrationevent.inbox.IntegrationEventProperties; import gr.cite.annotation.integrationevent.inbox.IntegrationEventProperties;
import gr.cite.annotation.model.persist.TenantTouchedIntegrationEventPersist; import gr.cite.annotation.model.persist.TenantTouchedIntegrationEventPersist;
import gr.cite.annotation.service.tenant.TenantService; import gr.cite.annotation.service.tenant.TenantService;
import gr.cite.commons.web.oidc.principal.CurrentPrincipalResolver;
import gr.cite.commons.web.oidc.principal.extractor.ClaimExtractorProperties;
import gr.cite.tools.auditing.AuditService; import gr.cite.tools.auditing.AuditService;
import gr.cite.tools.logging.LoggerService; import gr.cite.tools.logging.LoggerService;
import gr.cite.tools.validation.ValidatorFactory; import gr.cite.tools.validation.ValidatorFactory;

View File

@ -1,5 +1,6 @@
package gr.cite.annotation.integrationevent.inbox.userremoval; package gr.cite.annotation.integrationevent.inbox.userremoval;
import gr.cite.annotation.integrationevent.inbox.ConsistencyPredicates; import gr.cite.annotation.integrationevent.inbox.ConsistencyPredicates;
import java.util.UUID; import java.util.UUID;

View File

@ -1,5 +1,6 @@
package gr.cite.annotation.integrationevent.inbox.userremoval; package gr.cite.annotation.integrationevent.inbox.userremoval;
import gr.cite.annotation.integrationevent.inbox.IntegrationEventHandler; import gr.cite.annotation.integrationevent.inbox.IntegrationEventHandler;
public interface UserRemovalIntegrationEventHandler extends IntegrationEventHandler { public interface UserRemovalIntegrationEventHandler extends IntegrationEventHandler {

View File

@ -1,23 +1,18 @@
package gr.cite.annotation.integrationevent.inbox.userremoval; package gr.cite.annotation.integrationevent.inbox.userremoval;
import gr.cite.commons.web.oidc.principal.CurrentPrincipalResolver;
import gr.cite.commons.web.oidc.principal.extractor.ClaimExtractorProperties;
import gr.cite.annotation.audit.AuditableAction; import gr.cite.annotation.audit.AuditableAction;
import gr.cite.annotation.common.JsonHandlingService; import gr.cite.annotation.common.JsonHandlingService;
import gr.cite.annotation.common.scope.tenant.TenantScope; import gr.cite.annotation.common.scope.tenant.TenantScope;
import gr.cite.annotation.data.TenantEntity;
import gr.cite.annotation.data.TenantEntityManager; import gr.cite.annotation.data.TenantEntityManager;
import gr.cite.annotation.errorcode.ErrorThesaurusProperties; import gr.cite.annotation.errorcode.ErrorThesaurusProperties;
import gr.cite.annotation.integrationevent.inbox.EventProcessingStatus; import gr.cite.annotation.integrationevent.inbox.EventProcessingStatus;
import gr.cite.annotation.integrationevent.inbox.InboxPrincipal; import gr.cite.annotation.integrationevent.inbox.InboxPrincipal;
import gr.cite.annotation.integrationevent.inbox.IntegrationEventProperties; import gr.cite.annotation.integrationevent.inbox.IntegrationEventProperties;
import gr.cite.annotation.model.Tenant;
import gr.cite.annotation.query.TenantQuery;
import gr.cite.annotation.service.user.UserService; import gr.cite.annotation.service.user.UserService;
import gr.cite.commons.web.oidc.principal.CurrentPrincipalResolver;
import gr.cite.commons.web.oidc.principal.extractor.ClaimExtractorProperties;
import gr.cite.tools.auditing.AuditService; import gr.cite.tools.auditing.AuditService;
import gr.cite.tools.data.query.QueryFactory;
import gr.cite.tools.exception.MyValidationException; import gr.cite.tools.exception.MyValidationException;
import gr.cite.tools.fieldset.BaseFieldSet;
import gr.cite.tools.logging.LoggerService; import gr.cite.tools.logging.LoggerService;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.beans.factory.config.ConfigurableBeanFactory;
@ -26,7 +21,6 @@ import org.springframework.context.annotation.Scope;
import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.management.InvalidApplicationException;
import java.util.AbstractMap; import java.util.AbstractMap;
import java.util.Map; import java.util.Map;
@ -42,7 +36,6 @@ public class UserRemovalIntegrationEventHandlerImpl implements UserRemovalIntegr
private final ErrorThesaurusProperties errors; private final ErrorThesaurusProperties errors;
private final MessageSource messageSource; private final MessageSource messageSource;
private final QueryFactory queryFactory;
private final TenantScope tenantScope; private final TenantScope tenantScope;
private final CurrentPrincipalResolver currentPrincipalResolver; private final CurrentPrincipalResolver currentPrincipalResolver;
private final ClaimExtractorProperties claimExtractorProperties; private final ClaimExtractorProperties claimExtractorProperties;
@ -54,12 +47,11 @@ public class UserRemovalIntegrationEventHandlerImpl implements UserRemovalIntegr
public UserRemovalIntegrationEventHandlerImpl( public UserRemovalIntegrationEventHandlerImpl(
JsonHandlingService jsonHandlingService, JsonHandlingService jsonHandlingService,
ErrorThesaurusProperties errors, ErrorThesaurusProperties errors,
MessageSource messageSource, QueryFactory queryFactory, TenantScope tenantScope, CurrentPrincipalResolver currentPrincipalResolver, ClaimExtractorProperties claimExtractorProperties, UserRemovalConsistencyHandler userRemovalConsistencyHandler, UserService userService, AuditService auditService, TenantEntityManager tenantEntityManager MessageSource messageSource, TenantScope tenantScope, CurrentPrincipalResolver currentPrincipalResolver, ClaimExtractorProperties claimExtractorProperties, UserRemovalConsistencyHandler userRemovalConsistencyHandler, UserService userService, AuditService auditService, TenantEntityManager tenantEntityManager
) { ) {
this.jsonHandlingService = jsonHandlingService; this.jsonHandlingService = jsonHandlingService;
this.errors = errors; this.errors = errors;
this.messageSource = messageSource; this.messageSource = messageSource;
this.queryFactory = queryFactory;
this.tenantScope = tenantScope; this.tenantScope = tenantScope;
this.currentPrincipalResolver = currentPrincipalResolver; this.currentPrincipalResolver = currentPrincipalResolver;
this.claimExtractorProperties = claimExtractorProperties; this.claimExtractorProperties = claimExtractorProperties;
@ -81,21 +73,10 @@ public class UserRemovalIntegrationEventHandlerImpl implements UserRemovalIntegr
EventProcessingStatus status = EventProcessingStatus.Success; EventProcessingStatus status = EventProcessingStatus.Success;
try { try {
if (this.tenantScope.isMultitenant() && properties.getTenantId() != null) {
TenantEntity tenant = queryFactory.query(TenantQuery.class).disableTracking().ids(properties.getTenantId()).firstAs(new BaseFieldSet().ensure(Tenant._id).ensure(Tenant._code));
if (tenant == null) {
logger.error("missing tenant from event message");
return EventProcessingStatus.Error;
}
this.tenantScope.setTempTenant(tenantEntityManager, properties.getTenantId(), tenant.getCode());
} else if (this.tenantScope.isMultitenant()) {
// logger.error("missing tenant from event message");
// return EventProcessingStatus.Error;
this.tenantScope.setTempTenant(tenantEntityManager, null, this.tenantScope.getDefaultTenantCode());
}
currentPrincipalResolver.push(InboxPrincipal.build(properties, claimExtractorProperties)); currentPrincipalResolver.push(InboxPrincipal.build(properties, claimExtractorProperties));
try{
this.tenantEntityManager.disableTenantFilters();
if (!(userRemovalConsistencyHandler.isConsistent(new UserRemovalConsistencyPredicates(event.getUserId())))) { if (!(userRemovalConsistencyHandler.isConsistent(new UserRemovalConsistencyPredicates(event.getUserId())))) {
status = EventProcessingStatus.Postponed; status = EventProcessingStatus.Postponed;
currentPrincipalResolver.pop(); currentPrincipalResolver.pop();
@ -104,6 +85,9 @@ public class UserRemovalIntegrationEventHandlerImpl implements UserRemovalIntegr
} }
userService.deleteAndSave(event.getUserId()); userService.deleteAndSave(event.getUserId());
} finally {
this.tenantEntityManager.reloadTenantFilters();
}
auditService.track(AuditableAction.User_Delete, Map.ofEntries( auditService.track(AuditableAction.User_Delete, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("id", event.getUserId()) new AbstractMap.SimpleEntry<String, Object>("id", event.getUserId())
@ -114,11 +98,6 @@ public class UserRemovalIntegrationEventHandlerImpl implements UserRemovalIntegr
logger.error("Problem getting list of queue outbox. Skipping: {}", ex.getMessage(), ex); logger.error("Problem getting list of queue outbox. Skipping: {}", ex.getMessage(), ex);
} finally { } finally {
currentPrincipalResolver.pop(); currentPrincipalResolver.pop();
try {
tenantScope.removeTempTenant(this.tenantEntityManager);
} catch (InvalidApplicationException e) {
logger.error( e.getMessage(), e);
}
} }
return status; return status;

View File

@ -1,11 +1,10 @@
package gr.cite.annotation.integrationevent.inbox.usertouch; package gr.cite.annotation.integrationevent.inbox.usertouched;
import gr.cite.annotation.common.enums.ContactInfoType; import gr.cite.annotation.common.enums.ContactInfoType;
import gr.cite.annotation.common.validation.BaseValidator; import gr.cite.annotation.common.validation.BaseValidator;
import gr.cite.annotation.convention.ConventionService; import gr.cite.annotation.convention.ConventionService;
import gr.cite.annotation.errorcode.ErrorThesaurusProperties; import gr.cite.annotation.errorcode.ErrorThesaurusProperties;
import gr.cite.annotation.integrationevent.TrackedEvent; import gr.cite.annotation.integrationevent.TrackedEvent;
import gr.cite.annotation.model.persist.UserTouchedIntegrationEventPersist;
import gr.cite.tools.validation.ValidatorFactory; import gr.cite.tools.validation.ValidatorFactory;
import gr.cite.tools.validation.specification.Specification; import gr.cite.tools.validation.specification.Specification;
import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.beans.factory.config.ConfigurableBeanFactory;
@ -162,7 +161,7 @@ public class UserTouchedIntegrationEvent extends TrackedEvent {
this.ordinal = ordinal; this.ordinal = ordinal;
} }
@Component(UserTouchedIntegrationEvent.UserContactInfo.UserTouchedIntegrationUserContactInfoEventValidator.ValidatorName) @Component(UserTouchedIntegrationUserContactInfoEventValidator.ValidatorName)
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public static class UserTouchedIntegrationUserContactInfoEventValidator extends BaseValidator<UserContactInfo> { public static class UserTouchedIntegrationUserContactInfoEventValidator extends BaseValidator<UserContactInfo> {
@ -211,7 +210,7 @@ public class UserTouchedIntegrationEvent extends TrackedEvent {
this.subjectId = subjectId; this.subjectId = subjectId;
} }
@Component(UserCredential.UserTouchedIntegrationUserCredentialEventValidator.ValidatorName) @Component(UserTouchedIntegrationUserCredentialEventValidator.ValidatorName)
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public static class UserTouchedIntegrationUserCredentialEventValidator extends BaseValidator<UserCredential> { public static class UserTouchedIntegrationUserCredentialEventValidator extends BaseValidator<UserCredential> {
@ -253,7 +252,7 @@ public class UserTouchedIntegrationEvent extends TrackedEvent {
this.tenant = tenant; this.tenant = tenant;
} }
@Component(TenantUser.UserTouchedIntegrationTenantUserEventValidator.ValidatorName) @Component(UserTouchedIntegrationTenantUserEventValidator.ValidatorName)
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public static class UserTouchedIntegrationTenantUserEventValidator extends BaseValidator<TenantUser> { public static class UserTouchedIntegrationTenantUserEventValidator extends BaseValidator<TenantUser> {
@ -282,7 +281,7 @@ public class UserTouchedIntegrationEvent extends TrackedEvent {
} }
} }
@Component(UserTouchedIntegrationEvent.UserTouchedIntegrationEventValidator.ValidatorName) @Component(UserTouchedIntegrationEventValidator.ValidatorName)
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public static class UserTouchedIntegrationEventValidator extends BaseValidator<UserTouchedIntegrationEvent> { public static class UserTouchedIntegrationEventValidator extends BaseValidator<UserTouchedIntegrationEvent> {
@ -315,7 +314,7 @@ public class UserTouchedIntegrationEvent extends TrackedEvent {
.failOn(UserTouchedIntegrationEvent._name).failWith(messageSource.getMessage("Validation_Required", new Object[]{UserTouchedIntegrationEvent._name}, LocaleContextHolder.getLocale())), .failOn(UserTouchedIntegrationEvent._name).failWith(messageSource.getMessage("Validation_Required", new Object[]{UserTouchedIntegrationEvent._name}, LocaleContextHolder.getLocale())),
this.spec() this.spec()
.iff(() -> !this.isEmpty(item.getName())) .iff(() -> !this.isEmpty(item.getName()))
.must(() -> this.lessEqualLength(item.getName(), UserTouchedIntegrationEventPersist._nameLength)) .must(() -> this.lessEqualLength(item.getName(), UserTouchedIntegrationEvent._nameLength))
.failOn(UserTouchedIntegrationEvent._name).failWith(messageSource.getMessage("Validation_MaxLength", new Object[]{UserTouchedIntegrationEvent._name}, LocaleContextHolder.getLocale())), .failOn(UserTouchedIntegrationEvent._name).failWith(messageSource.getMessage("Validation_MaxLength", new Object[]{UserTouchedIntegrationEvent._name}, LocaleContextHolder.getLocale())),
this.navSpec() this.navSpec()
.iff(() -> !this.isListNullOrEmpty(item.getUserContactInfo())) .iff(() -> !this.isListNullOrEmpty(item.getUserContactInfo()))

View File

@ -1,4 +1,5 @@
package gr.cite.annotation.integrationevent.inbox.usertouch; package gr.cite.annotation.integrationevent.inbox.usertouched;
import gr.cite.annotation.integrationevent.inbox.IntegrationEventHandler; import gr.cite.annotation.integrationevent.inbox.IntegrationEventHandler;

View File

@ -1,21 +1,17 @@
package gr.cite.annotation.integrationevent.inbox.usertouch; package gr.cite.annotation.integrationevent.inbox.usertouched;
import gr.cite.annotation.data.TenantEntityManager;
import gr.cite.commons.web.oidc.principal.CurrentPrincipalResolver; import gr.cite.commons.web.oidc.principal.CurrentPrincipalResolver;
import gr.cite.commons.web.oidc.principal.extractor.ClaimExtractorProperties;
import gr.cite.annotation.audit.AuditableAction; import gr.cite.annotation.audit.AuditableAction;
import gr.cite.annotation.common.JsonHandlingService; import gr.cite.annotation.common.JsonHandlingService;
import gr.cite.annotation.common.scope.tenant.TenantScope; import gr.cite.annotation.common.scope.tenant.TenantScope;
import gr.cite.annotation.data.TenantEntity; import gr.cite.annotation.data.TenantEntityManager;
import gr.cite.annotation.integrationevent.inbox.EventProcessingStatus; import gr.cite.annotation.integrationevent.inbox.EventProcessingStatus;
import gr.cite.annotation.integrationevent.inbox.InboxPrincipal; import gr.cite.annotation.integrationevent.inbox.InboxPrincipal;
import gr.cite.annotation.integrationevent.inbox.IntegrationEventProperties; import gr.cite.annotation.integrationevent.inbox.IntegrationEventProperties;
import gr.cite.annotation.model.Tenant;
import gr.cite.annotation.query.TenantQuery;
import gr.cite.annotation.service.user.UserService; import gr.cite.annotation.service.user.UserService;
import gr.cite.commons.web.oidc.principal.extractor.ClaimExtractorProperties;
import gr.cite.tools.auditing.AuditService; import gr.cite.tools.auditing.AuditService;
import gr.cite.tools.data.query.QueryFactory; import gr.cite.tools.data.query.QueryFactory;
import gr.cite.tools.fieldset.BaseFieldSet;
import gr.cite.tools.logging.LoggerService; import gr.cite.tools.logging.LoggerService;
import gr.cite.tools.validation.ValidatorFactory; import gr.cite.tools.validation.ValidatorFactory;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -23,7 +19,6 @@ import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope; import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.management.InvalidApplicationException;
import java.util.AbstractMap; import java.util.AbstractMap;
import java.util.Map; import java.util.Map;
@ -71,23 +66,14 @@ public class UserTouchedIntegrationEventHandlerImpl implements UserTouchedIntegr
EventProcessingStatus status = EventProcessingStatus.Success; EventProcessingStatus status = EventProcessingStatus.Success;
try { try {
if (this.tenantScope.isMultitenant() && properties.getTenantId() != null) {
TenantEntity tenant = queryFactory.query(TenantQuery.class).disableTracking().ids(properties.getTenantId()).firstAs(new BaseFieldSet().ensure(Tenant._id).ensure(Tenant._code));
if (tenant == null) {
logger.error("missing tenant from event message");
return EventProcessingStatus.Error;
}
this.tenantScope.setTempTenant(tenantEntityManager, properties.getTenantId(), tenant.getCode());
} else if (this.tenantScope.isMultitenant()) {
// logger.error("missing tenant from event message");
// return EventProcessingStatus.Error;
this.tenantScope.setTempTenant(tenantEntityManager, null, this.tenantScope.getDefaultTenantCode());
}
currentPrincipalResolver.push(InboxPrincipal.build(properties, claimExtractorProperties)); currentPrincipalResolver.push(InboxPrincipal.build(properties, claimExtractorProperties));
try {
this.tenantEntityManager.disableTenantFilters();
userService.persist(event, null); userService.persist(event, null);
} finally {
this.tenantEntityManager.reloadTenantFilters();
}
auditService.track(AuditableAction.User_Persist, Map.ofEntries( auditService.track(AuditableAction.User_Persist, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("model", event) new AbstractMap.SimpleEntry<String, Object>("model", event)
)); ));
@ -97,11 +83,6 @@ public class UserTouchedIntegrationEventHandlerImpl implements UserTouchedIntegr
logger.error("Problem getting list of queue outbox. Skipping: {}", ex.getMessage(), ex); logger.error("Problem getting list of queue outbox. Skipping: {}", ex.getMessage(), ex);
} finally { } finally {
currentPrincipalResolver.pop(); currentPrincipalResolver.pop();
try {
tenantScope.removeTempTenant(this.tenantEntityManager);
} catch (InvalidApplicationException e) {
logger.error(e.getMessage(), e);
}
} }
return status; return status;

View File

@ -3,7 +3,7 @@ package gr.cite.annotation.model.persist;
import gr.cite.annotation.common.validation.BaseValidator; import gr.cite.annotation.common.validation.BaseValidator;
import gr.cite.annotation.convention.ConventionService; import gr.cite.annotation.convention.ConventionService;
import gr.cite.annotation.errorcode.ErrorThesaurusProperties; import gr.cite.annotation.errorcode.ErrorThesaurusProperties;
import gr.cite.annotation.integrationevent.inbox.usertouch.UserTouchedIntegrationEvent; import gr.cite.annotation.integrationevent.inbox.usertouched.UserTouchedIntegrationEvent;
import gr.cite.tools.validation.specification.Specification; import gr.cite.tools.validation.specification.Specification;
import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.MessageSource; import org.springframework.context.MessageSource;

View File

@ -1,7 +1,7 @@
package gr.cite.annotation.service.user; package gr.cite.annotation.service.user;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import gr.cite.annotation.integrationevent.inbox.usertouch.UserTouchedIntegrationEvent; import gr.cite.annotation.integrationevent.inbox.usertouched.UserTouchedIntegrationEvent;
import gr.cite.annotation.model.User; import gr.cite.annotation.model.User;
import gr.cite.tools.exception.MyApplicationException; import gr.cite.tools.exception.MyApplicationException;
import gr.cite.tools.exception.MyForbiddenException; import gr.cite.tools.exception.MyForbiddenException;

View File

@ -8,7 +8,7 @@ import gr.cite.annotation.common.enums.IsActive;
import gr.cite.annotation.common.scope.tenant.TenantScope; import gr.cite.annotation.common.scope.tenant.TenantScope;
import gr.cite.annotation.convention.ConventionService; import gr.cite.annotation.convention.ConventionService;
import gr.cite.annotation.data.*; import gr.cite.annotation.data.*;
import gr.cite.annotation.integrationevent.inbox.usertouch.UserTouchedIntegrationEvent; import gr.cite.annotation.integrationevent.inbox.usertouched.UserTouchedIntegrationEvent;
import gr.cite.annotation.model.Tenant; import gr.cite.annotation.model.Tenant;
import gr.cite.annotation.model.User; import gr.cite.annotation.model.User;
import gr.cite.annotation.model.builder.UserBuilder; import gr.cite.annotation.model.builder.UserBuilder;

View File

@ -5,16 +5,18 @@ import gr.cite.tools.fieldset.BaseFieldSet;
import gr.cite.tools.logging.LoggerService; import gr.cite.tools.logging.LoggerService;
import org.opencdmp.commons.scope.tenant.TenantScope; import org.opencdmp.commons.scope.tenant.TenantScope;
import org.opencdmp.data.DescriptionEntity; import org.opencdmp.data.DescriptionEntity;
import org.opencdmp.data.DmpEntity;
import org.opencdmp.integrationevent.outbox.OutboxIntegrationEvent; import org.opencdmp.integrationevent.outbox.OutboxIntegrationEvent;
import org.opencdmp.integrationevent.outbox.OutboxService; import org.opencdmp.integrationevent.outbox.OutboxService;
import org.opencdmp.model.description.Description; import org.opencdmp.model.description.Description;
import org.opencdmp.model.dmp.Dmp;
import org.opencdmp.query.DescriptionQuery; import org.opencdmp.query.DescriptionQuery;
import org.opencdmp.query.DmpQuery;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope; import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.management.InvalidApplicationException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
@ -28,33 +30,36 @@ public class AnnotationEntityRemovalIntegrationEventHandlerImpl implements Annot
private final OutboxService outboxService; private final OutboxService outboxService;
private final QueryFactory queryFactory; private final QueryFactory queryFactory;
private final TenantScope tenantScope;
public AnnotationEntityRemovalIntegrationEventHandlerImpl(OutboxService outboxService, QueryFactory queryFactory, TenantScope tenantScope) { public AnnotationEntityRemovalIntegrationEventHandlerImpl(OutboxService outboxService, QueryFactory queryFactory, TenantScope tenantScope) {
this.outboxService = outboxService; this.outboxService = outboxService;
this.queryFactory = queryFactory; this.queryFactory = queryFactory;
this.tenantScope = tenantScope;
} }
private void handle(AnnotationEntitiesRemovalIntegrationEvent event) throws InvalidApplicationException { private void handle(AnnotationEntitiesRemovalIntegrationEvent event, UUID tenantId) {
OutboxIntegrationEvent message = new OutboxIntegrationEvent(); OutboxIntegrationEvent message = new OutboxIntegrationEvent();
message.setMessageId(UUID.randomUUID()); message.setMessageId(UUID.randomUUID());
message.setType(OutboxIntegrationEvent.ANNOTATION_ENTITY_REMOVE); message.setType(OutboxIntegrationEvent.ANNOTATION_ENTITY_REMOVE);
message.setEvent(event); message.setEvent(event);
if (this.tenantScope.isSet()) message.setTenantId(this.tenantScope.getTenant());
this.outboxService.publish(message); this.outboxService.publish(message);
} }
@Override @Override
public void handleDescription(UUID descriptionId) throws InvalidApplicationException { public void handleDescription(UUID descriptionId) {
DescriptionEntity description = this.queryFactory.query(DescriptionQuery.class).disableTracking().ids(descriptionId).firstAs(new BaseFieldSet().ensure(Description._id).ensure(DescriptionEntity._tenantId));
if (description == null) return;
AnnotationEntitiesRemovalIntegrationEvent event = new AnnotationEntitiesRemovalIntegrationEvent(); AnnotationEntitiesRemovalIntegrationEvent event = new AnnotationEntitiesRemovalIntegrationEvent();
event.setEntityIds(List.of(descriptionId)); event.setEntityIds(List.of(descriptionId));
this.handle(event); this.handle(event, description.getTenantId());
} }
@Override @Override
public void handleDmp(UUID dmpId) throws InvalidApplicationException { public void handleDmp(UUID dmpId) {
DmpEntity dmp = this.queryFactory.query(DmpQuery.class).disableTracking().ids(dmpId).firstAs(new BaseFieldSet().ensure(Dmp._id).ensure(DmpEntity._tenantId));
if(dmp == null) return;
List<DescriptionEntity> descriptionEntities = this.queryFactory.query(DescriptionQuery.class).disableTracking().dmpIds(dmpId).collectAs(new BaseFieldSet().ensure(Description._id)); List<DescriptionEntity> descriptionEntities = this.queryFactory.query(DescriptionQuery.class).disableTracking().dmpIds(dmpId).collectAs(new BaseFieldSet().ensure(Description._id));
AnnotationEntitiesRemovalIntegrationEvent event = new AnnotationEntitiesRemovalIntegrationEvent(); AnnotationEntitiesRemovalIntegrationEvent event = new AnnotationEntitiesRemovalIntegrationEvent();
@ -63,6 +68,6 @@ public class AnnotationEntityRemovalIntegrationEventHandlerImpl implements Annot
for (DescriptionEntity description : descriptionEntities) event.getEntityIds().add(description.getId()); for (DescriptionEntity description : descriptionEntities) event.getEntityIds().add(description.getId());
this.handle(event); this.handle(event, dmp.getTenantId());
} }
} }

View File

@ -10,7 +10,7 @@ public class AnnotationEntitiesTouchedIntegrationEvent extends TrackedEvent {
private List<AnnotationEntityTouchedIntegrationEvent> events; private List<AnnotationEntityTouchedIntegrationEvent> events;
public List<AnnotationEntityTouchedIntegrationEvent> getEvents() { public List<AnnotationEntityTouchedIntegrationEvent> getEvents() {
return events; return this.events;
} }
public void setEvents(List<AnnotationEntityTouchedIntegrationEvent> events) { public void setEvents(List<AnnotationEntityTouchedIntegrationEvent> events) {
@ -24,7 +24,7 @@ public class AnnotationEntitiesTouchedIntegrationEvent extends TrackedEvent {
private List<UUID> userIds; private List<UUID> userIds;
public UUID getEntityId() { public UUID getEntityId() {
return entityId; return this.entityId;
} }
public void setEntityId(UUID entityId) { public void setEntityId(UUID entityId) {
@ -32,7 +32,7 @@ public class AnnotationEntitiesTouchedIntegrationEvent extends TrackedEvent {
} }
public List<UUID> getUserIds() { public List<UUID> getUserIds() {
return userIds; return this.userIds;
} }
public void setUserIds(List<UUID> userIds) { public void setUserIds(List<UUID> userIds) {

View File

@ -3,8 +3,6 @@ package org.opencdmp.integrationevent.outbox.notification;
import org.opencdmp.commons.scope.tenant.TenantScope; import org.opencdmp.commons.scope.tenant.TenantScope;
import org.opencdmp.integrationevent.outbox.OutboxIntegrationEvent; import org.opencdmp.integrationevent.outbox.OutboxIntegrationEvent;
import org.opencdmp.integrationevent.outbox.OutboxService; import org.opencdmp.integrationevent.outbox.OutboxService;
import gr.cite.tools.logging.LoggerService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.context.annotation.RequestScope; import org.springframework.web.context.annotation.RequestScope;
@ -16,8 +14,6 @@ import java.util.UUID;
@RequestScope @RequestScope
public class NotifyIntegrationEventHandlerImpl implements NotifyIntegrationEventHandler { public class NotifyIntegrationEventHandlerImpl implements NotifyIntegrationEventHandler {
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(NotifyIntegrationEventHandlerImpl.class));
private final OutboxService outboxService; private final OutboxService outboxService;
private final TenantScope tenantScope; private final TenantScope tenantScope;
@ -34,7 +30,7 @@ public class NotifyIntegrationEventHandlerImpl implements NotifyIntegrationEvent
message.setMessageId(UUID.randomUUID()); message.setMessageId(UUID.randomUUID());
message.setType(OutboxIntegrationEvent.NOTIFY); message.setType(OutboxIntegrationEvent.NOTIFY);
message.setEvent(event); message.setEvent(event);
if (this.tenantScope.isSet()) message.setTenantId(tenantScope.getTenant()); if (this.tenantScope.isSet()) message.setTenantId(this.tenantScope.getTenant());
this.outboxService.publish(message); this.outboxService.publish(message);
} }
} }

View File

@ -1,9 +1,8 @@
package org.opencdmp.integrationevent.outbox.tenantdefaultlocaleremoval; package org.opencdmp.integrationevent.outbox.tenantdefaultlocaleremoval;
import org.opencdmp.commons.scope.tenant.TenantScope; import gr.cite.tools.logging.LoggerService;
import org.opencdmp.integrationevent.outbox.OutboxIntegrationEvent; import org.opencdmp.integrationevent.outbox.OutboxIntegrationEvent;
import org.opencdmp.integrationevent.outbox.OutboxService; import org.opencdmp.integrationevent.outbox.OutboxService;
import gr.cite.tools.logging.LoggerService;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -19,13 +18,11 @@ public class TenantDefaultLocaleRemovalIntegrationEventHandlerImpl implements Te
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(TenantDefaultLocaleRemovalIntegrationEventHandlerImpl.class)); private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(TenantDefaultLocaleRemovalIntegrationEventHandlerImpl.class));
private final OutboxService outboxService; private final OutboxService outboxService;
private final TenantScope tenantScope;
@Autowired @Autowired
public TenantDefaultLocaleRemovalIntegrationEventHandlerImpl( public TenantDefaultLocaleRemovalIntegrationEventHandlerImpl(
OutboxService outboxService, TenantScope tenantScope) { OutboxService outboxService) {
this.outboxService = outboxService; this.outboxService = outboxService;
this.tenantScope = tenantScope;
} }
@Override @Override
@ -34,7 +31,7 @@ public class TenantDefaultLocaleRemovalIntegrationEventHandlerImpl implements Te
message.setMessageId(UUID.randomUUID()); message.setMessageId(UUID.randomUUID());
message.setType(OutboxIntegrationEvent.TENANT_DEFAULT_LOCALE_REMOVAL); message.setType(OutboxIntegrationEvent.TENANT_DEFAULT_LOCALE_REMOVAL);
message.setEvent(event); message.setEvent(event);
if (this.tenantScope.isSet()) message.setTenantId(tenantScope.getTenant()); message.setTenantId(event.getTenantId());
this.outboxService.publish(message); this.outboxService.publish(message);
} }
} }

View File

@ -1,9 +1,8 @@
package org.opencdmp.integrationevent.outbox.tenantdefaultlocaletouched; package org.opencdmp.integrationevent.outbox.tenantdefaultlocaletouched;
import org.opencdmp.commons.scope.tenant.TenantScope; import gr.cite.tools.logging.LoggerService;
import org.opencdmp.integrationevent.outbox.OutboxIntegrationEvent; import org.opencdmp.integrationevent.outbox.OutboxIntegrationEvent;
import org.opencdmp.integrationevent.outbox.OutboxService; import org.opencdmp.integrationevent.outbox.OutboxService;
import gr.cite.tools.logging.LoggerService;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -19,13 +18,11 @@ public class TenantDefaultLocaleTouchedIntegrationEventHandlerImpl implements Te
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(TenantDefaultLocaleTouchedIntegrationEventHandlerImpl.class)); private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(TenantDefaultLocaleTouchedIntegrationEventHandlerImpl.class));
private final OutboxService outboxService; private final OutboxService outboxService;
private final TenantScope tenantScope;
@Autowired @Autowired
public TenantDefaultLocaleTouchedIntegrationEventHandlerImpl( public TenantDefaultLocaleTouchedIntegrationEventHandlerImpl(
OutboxService outboxService, TenantScope tenantScope) { OutboxService outboxService) {
this.outboxService = outboxService; this.outboxService = outboxService;
this.tenantScope = tenantScope;
} }
@Override @Override
@ -34,7 +31,7 @@ public class TenantDefaultLocaleTouchedIntegrationEventHandlerImpl implements Te
message.setMessageId(UUID.randomUUID()); message.setMessageId(UUID.randomUUID());
message.setType(OutboxIntegrationEvent.TENANT_DEFAULT_LOCALE_TOUCHED); message.setType(OutboxIntegrationEvent.TENANT_DEFAULT_LOCALE_TOUCHED);
message.setEvent(event); message.setEvent(event);
if (this.tenantScope.isSet()) message.setTenantId(tenantScope.getTenant()); message.setTenantId(event.getTenantId());
this.outboxService.publish(message); this.outboxService.publish(message);
} }
} }

View File

@ -1,9 +1,8 @@
package org.opencdmp.integrationevent.outbox.userremoval; package org.opencdmp.integrationevent.outbox.userremoval;
import org.opencdmp.commons.scope.tenant.TenantScope; import gr.cite.tools.logging.LoggerService;
import org.opencdmp.integrationevent.outbox.OutboxIntegrationEvent; import org.opencdmp.integrationevent.outbox.OutboxIntegrationEvent;
import org.opencdmp.integrationevent.outbox.OutboxService; import org.opencdmp.integrationevent.outbox.OutboxService;
import gr.cite.tools.logging.LoggerService;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
@ -14,7 +13,7 @@ import javax.management.InvalidApplicationException;
import java.util.UUID; import java.util.UUID;
@Component("outboxuserremovalintegrationeventhandler") @Component("outboxuserremovalintegrationeventhandler")
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class UserRemovalIntegrationEventHandlerImpl implements UserRemovalIntegrationEventHandler { public class UserRemovalIntegrationEventHandlerImpl implements UserRemovalIntegrationEventHandler {
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(UserRemovalIntegrationEventHandlerImpl.class)); private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(UserRemovalIntegrationEventHandlerImpl.class));
@ -22,12 +21,10 @@ public class UserRemovalIntegrationEventHandlerImpl implements UserRemovalIntegr
private final OutboxService outboxService; private final OutboxService outboxService;
private final ApplicationContext applicationContext; private final ApplicationContext applicationContext;
private final TenantScope tenantScope;
public UserRemovalIntegrationEventHandlerImpl(OutboxService outboxService, ApplicationContext applicationContext, TenantScope tenantScope) { public UserRemovalIntegrationEventHandlerImpl(OutboxService outboxService, ApplicationContext applicationContext) {
this.outboxService = outboxService; this.outboxService = outboxService;
this.applicationContext = applicationContext; this.applicationContext = applicationContext;
this.tenantScope = tenantScope;
} }
@Override @Override
@ -39,7 +36,6 @@ public class UserRemovalIntegrationEventHandlerImpl implements UserRemovalIntegr
OutboxIntegrationEvent message = new OutboxIntegrationEvent(); OutboxIntegrationEvent message = new OutboxIntegrationEvent();
message.setMessageId(UUID.randomUUID()); message.setMessageId(UUID.randomUUID());
message.setType(OutboxIntegrationEvent.USER_REMOVE); message.setType(OutboxIntegrationEvent.USER_REMOVE);
if (this.tenantScope.isSet()) message.setTenantId(tenantScope.getTenant());
UserRemovalIntegrationEvent event = new UserRemovalIntegrationEvent(); UserRemovalIntegrationEvent event = new UserRemovalIntegrationEvent();
event.setUserId(userId); event.setUserId(userId);
message.setEvent(event); message.setEvent(event);

View File

@ -5,7 +5,6 @@ import gr.cite.tools.exception.MyNotFoundException;
import gr.cite.tools.fieldset.BaseFieldSet; import gr.cite.tools.fieldset.BaseFieldSet;
import gr.cite.tools.logging.LoggerService; import gr.cite.tools.logging.LoggerService;
import org.opencdmp.commons.JsonHandlingService; import org.opencdmp.commons.JsonHandlingService;
import org.opencdmp.commons.scope.tenant.TenantScope;
import org.opencdmp.commons.types.user.AdditionalInfoEntity; import org.opencdmp.commons.types.user.AdditionalInfoEntity;
import org.opencdmp.data.*; import org.opencdmp.data.*;
import org.opencdmp.integrationevent.outbox.OutboxIntegrationEvent; import org.opencdmp.integrationevent.outbox.OutboxIntegrationEvent;
@ -39,16 +38,14 @@ public class UserTouchedIntegrationEventHandlerImpl implements UserTouchedIntegr
private final OutboxService outboxService; private final OutboxService outboxService;
private final JsonHandlingService jsonHandlingService; private final JsonHandlingService jsonHandlingService;
private final MessageSource messageSource; private final MessageSource messageSource;
private final TenantScope tenantScope;
private final TenantEntityManager entityManager; private final TenantEntityManager entityManager;
private final QueryFactory queryFactory; private final QueryFactory queryFactory;
public UserTouchedIntegrationEventHandlerImpl( public UserTouchedIntegrationEventHandlerImpl(
OutboxService outboxService, JsonHandlingService jsonHandlingService, MessageSource messageSource, TenantScope tenantScope, TenantEntityManager entityManager, QueryFactory queryFactory) { OutboxService outboxService, JsonHandlingService jsonHandlingService, MessageSource messageSource, TenantEntityManager entityManager, QueryFactory queryFactory) {
this.outboxService = outboxService; this.outboxService = outboxService;
this.jsonHandlingService = jsonHandlingService; this.jsonHandlingService = jsonHandlingService;
this.messageSource = messageSource; this.messageSource = messageSource;
this.tenantScope = tenantScope;
this.entityManager = entityManager; this.entityManager = entityManager;
this.queryFactory = queryFactory; this.queryFactory = queryFactory;
} }
@ -58,7 +55,6 @@ public class UserTouchedIntegrationEventHandlerImpl implements UserTouchedIntegr
OutboxIntegrationEvent message = new OutboxIntegrationEvent(); OutboxIntegrationEvent message = new OutboxIntegrationEvent();
message.setMessageId(UUID.randomUUID()); message.setMessageId(UUID.randomUUID());
message.setType(OutboxIntegrationEvent.USER_TOUCH); message.setType(OutboxIntegrationEvent.USER_TOUCH);
if (this.tenantScope.isSet()) message.setTenantId(this.tenantScope.getTenant());
try { try {
this.entityManager.disableTenantFilters(); this.entityManager.disableTenantFilters();

View File

@ -428,6 +428,8 @@ public class DmpQuery extends QueryBase<DmpEntity> {
return DmpEntity._publicAfter; return DmpEntity._publicAfter;
else if (item.match(Dmp._versionStatus)) else if (item.match(Dmp._versionStatus))
return DmpEntity._versionStatus; return DmpEntity._versionStatus;
else if (item.match(DmpEntity._tenantId))
return DmpEntity._tenantId;
else if (item.match(Dmp._belongsToCurrentTenant)) else if (item.match(Dmp._belongsToCurrentTenant))
return DmpEntity._tenantId; return DmpEntity._tenantId;
else else

View File

@ -163,32 +163,34 @@ public class TenantServiceImpl implements TenantService {
List<UserRoleEntity> existingItems; List<UserRoleEntity> existingItems;
List<UserCredentialEntity> userCredentialEntities; List<UserCredentialEntity> userCredentialEntities;
try { try {
this.tenantScope.setTempTenant(this.entityManager, null, this.tenantScope.getDefaultTenantCode()); this.entityManager.disableTenantFilters();
existingItems = this.queryFactory.query(UserRoleQuery.class).disableTracking().tenantIsSet(false).roles(this.authorizationProperties.getGlobalAdminRole()).collect(); existingItems = this.queryFactory.query(UserRoleQuery.class).disableTracking().tenantIsSet(false).roles(this.authorizationProperties.getGlobalAdminRole()).collect();
userCredentialEntities = this.queryFactory.query(UserCredentialQuery.class).disableTracking().userIds(existingItems.stream().map(UserRoleEntity::getUserId).distinct().toList()).collect(); userCredentialEntities = this.queryFactory.query(UserCredentialQuery.class).disableTracking().userIds(existingItems.stream().map(UserRoleEntity::getUserId).distinct().toList()).collect();
} finally {
this.tenantScope.removeTempTenant(this.entityManager); List<String> keycloakIdsToAddToTenantGroup = new ArrayList<>();
}
try {
this.tenantScope.setTempTenant(this.entityManager, tenant.getId(), tenant.getCode());
for (UUID userId : existingItems.stream().map(UserRoleEntity::getUserId).distinct().toList()) { for (UUID userId : existingItems.stream().map(UserRoleEntity::getUserId).distinct().toList()) {
UserCredentialEntity userCredential = userCredentialEntities.stream().filter(x-> !this.conventionService.isNullOrEmpty(x.getExternalId()) && x.getUserId().equals(userId)).findFirst().orElse(null); UserCredentialEntity userCredential = userCredentialEntities.stream().filter(x-> !this.conventionService.isNullOrEmpty(x.getExternalId()) && x.getUserId().equals(userId)).findFirst().orElse(null);
if (userCredential == null) continue; if (userCredential == null) continue;
UserRoleEntity item = new UserRoleEntity(); UserRoleEntity item = new UserRoleEntity();
item.setId(UUID.randomUUID()); item.setId(UUID.randomUUID());
item.setUserId(userId); item.setUserId(userId);
item.setTenantId(tenant.getId());
item.setRole(this.authorizationProperties.getTenantAdminRole()); item.setRole(this.authorizationProperties.getTenantAdminRole());
item.setCreatedAt(Instant.now()); item.setCreatedAt(Instant.now());
this.entityManager.persist(item); this.entityManager.persist(item);
keycloakIdsToAddToTenantGroup.add(userCredential.getExternalId());
this.keycloakService.addUserToTenantRoleGroup(userCredential.getExternalId(), this.tenantScope.getTenantCode(), this.authorizationProperties.getTenantAdminRole()); this.keycloakService.addUserToTenantRoleGroup(userCredential.getExternalId(), this.tenantScope.getTenantCode(), this.authorizationProperties.getTenantAdminRole());
} }
} finally {
this.tenantScope.removeTempTenant(this.entityManager);
}
this.entityManager.flush(); this.entityManager.flush();
for (String externalId : keycloakIdsToAddToTenantGroup) {
this.keycloakService.addUserToTenantRoleGroup(externalId, this.tenantScope.getTenantCode(), this.authorizationProperties.getTenantAdminRole());
}
} finally {
this.entityManager.reloadTenantFilters();
}
} }
@Override @Override

View File

@ -280,8 +280,12 @@ public class UserServiceImpl implements UserService {
logger.debug("deleting User: {}", id); logger.debug("deleting User: {}", id);
this.authorizationService.authorizeForce(Permission.DeleteUser); this.authorizationService.authorizeForce(Permission.DeleteUser);
try {
this.entityManager.disableTenantFilters();
this.deleterFactory.deleter(UserDeleter.class).deleteAndSaveByIds(List.of(id)); this.deleterFactory.deleter(UserDeleter.class).deleteAndSaveByIds(List.of(id));
} finally {
this.entityManager.reloadTenantFilters();
}
this.userRemovalIntegrationEventHandler.handle(id); this.userRemovalIntegrationEventHandler.handle(id);
} }
@ -341,7 +345,7 @@ public class UserServiceImpl implements UserService {
private void applyGlobalRoles(UUID userId, UserRolePatchPersist model) throws InvalidApplicationException { private void applyGlobalRoles(UUID userId, UserRolePatchPersist model) throws InvalidApplicationException {
try { try {
this.tenantScope.setTempTenant(this.entityManager, null, this.tenantScope.getDefaultTenantCode()); this.entityManager.disableTenantFilters();
List<UserRoleEntity> existingItems = this.queryFactory.query(UserRoleQuery.class).userIds(userId).tenantIsSet(false).roles(this.authorizationProperties.getAllowedGlobalRoles()).collect(); List<UserRoleEntity> existingItems = this.queryFactory.query(UserRoleQuery.class).userIds(userId).tenantIsSet(false).roles(this.authorizationProperties.getAllowedGlobalRoles()).collect();
List<UUID> foundIds = new ArrayList<>(); List<UUID> foundIds = new ArrayList<>();
@ -365,7 +369,7 @@ public class UserServiceImpl implements UserService {
this.entityManager.flush(); this.entityManager.flush();
} finally { } finally {
this.tenantScope.removeTempTenant(this.entityManager); this.entityManager.reloadTenantFilters();
} }
} }
@ -623,7 +627,9 @@ public class UserServiceImpl implements UserService {
} }
private void syncKeycloakRoles(UUID userId){ private void syncKeycloakRoles(UUID userId) throws InvalidApplicationException {
try {
this.entityManager.disableTenantFilters();
List<UserCredentialEntity> userCredentials = this.queryFactory.query(UserCredentialQuery.class).disableTracking().userIds(userId).collect(); List<UserCredentialEntity> userCredentials = this.queryFactory.query(UserCredentialQuery.class).disableTracking().userIds(userId).collect();
List<UserRoleEntity> userRoles = this.queryFactory.query(UserRoleQuery.class).disableTracking().userIds(userId).collect(); List<UserRoleEntity> userRoles = this.queryFactory.query(UserRoleQuery.class).disableTracking().userIds(userId).collect();
List<TenantEntity> tenants = this.queryFactory.query(TenantQuery.class).disableTracking().ids(userRoles.stream().map(TenantScopedBaseEntity::getTenantId).filter(Objects::nonNull).toList()).collect(); List<TenantEntity> tenants = this.queryFactory.query(TenantQuery.class).disableTracking().ids(userRoles.stream().map(TenantScopedBaseEntity::getTenantId).filter(Objects::nonNull).toList()).collect();
@ -639,9 +645,15 @@ public class UserServiceImpl implements UserService {
} }
} }
} }
} finally {
this.entityManager.reloadTenantFilters();
}
} }
private void mergeNewUserToOld(UserEntity newUser, UserEntity oldUser) throws IOException, InvalidApplicationException { private void mergeNewUserToOld(UserEntity newUser, UserEntity oldUser) throws IOException, InvalidApplicationException {
try {
this.entityManager.disableTenantFilters();
List<UserCredentialEntity> userCredentials = this.queryFactory.query(UserCredentialQuery.class).userIds(oldUser.getId()).collect(); List<UserCredentialEntity> userCredentials = this.queryFactory.query(UserCredentialQuery.class).userIds(oldUser.getId()).collect();
for (UserCredentialEntity userCredential : userCredentials) { for (UserCredentialEntity userCredential : userCredentials) {
userCredential.setUserId(newUser.getId()); userCredential.setUserId(newUser.getId());
@ -664,7 +676,7 @@ public class UserServiceImpl implements UserService {
List<UserRoleEntity> newUserRoles = this.queryFactory.query(UserRoleQuery.class).userIds(newUser.getId()).collect(); List<UserRoleEntity> newUserRoles = this.queryFactory.query(UserRoleQuery.class).userIds(newUser.getId()).collect();
List<UserRoleEntity> rolesToDelete = new ArrayList<>(); List<UserRoleEntity> rolesToDelete = new ArrayList<>();
for (UserRoleEntity userRole : userRoles) { for (UserRoleEntity userRole : userRoles) {
if (newUserRoles.stream().anyMatch(x-> x.getRole().equals(userRole.getRole()))) { if (newUserRoles.stream().anyMatch(x -> Objects.equals(x.getTenantId(), userRole.getTenantId()) && x.getRole().equals(userRole.getRole()))) {
rolesToDelete.add(userRole); rolesToDelete.add(userRole);
} else { } else {
userRole.setUserId(newUser.getId()); userRole.setUserId(newUser.getId());
@ -673,11 +685,24 @@ public class UserServiceImpl implements UserService {
} }
this.deleterFactory.deleter(UserRoleDeleter.class).delete(rolesToDelete); this.deleterFactory.deleter(UserRoleDeleter.class).delete(rolesToDelete);
List<TenantUserEntity> userTenantUsers = this.queryFactory.query(TenantUserQuery.class).userIds(oldUser.getId()).collect();
List<TenantUserEntity> newTenantUsers = this.queryFactory.query(TenantUserQuery.class).userIds(newUser.getId()).collect();
List<TenantUserEntity> tenantUsersToDelete = new ArrayList<>();
for (TenantUserEntity userTenantUser : userTenantUsers) {
if (newTenantUsers.stream().anyMatch(x -> Objects.equals(x.getTenantId(), userTenantUser.getTenantId()))) {
tenantUsersToDelete.add(userTenantUser);
} else {
userTenantUser.setUserId(newUser.getId());
this.entityManager.merge(userTenantUser);
}
}
this.deleterFactory.deleter(TenantUserDeleter.class).delete(tenantUsersToDelete);
List<UserSettingsEntity> userSettings = this.queryFactory.query(UserSettingsQuery.class).entityIds(oldUser.getId()).collect(); List<UserSettingsEntity> userSettings = this.queryFactory.query(UserSettingsQuery.class).entityIds(oldUser.getId()).collect();
List<UserSettingsEntity> newUserSettings = this.queryFactory.query(UserSettingsQuery.class).entityIds(newUser.getId()).collect(); List<UserSettingsEntity> newUserSettings = this.queryFactory.query(UserSettingsQuery.class).entityIds(newUser.getId()).collect();
List<UserSettingsEntity> userSettingsToDelete = new ArrayList<>(); List<UserSettingsEntity> userSettingsToDelete = new ArrayList<>();
for (UserSettingsEntity userSetting : userSettings) { for (UserSettingsEntity userSetting : userSettings) {
if (newUserSettings.stream().anyMatch(x-> x.getKey().equals(userSetting.getKey()))) { if (newUserSettings.stream().anyMatch(x -> Objects.equals(x.getTenantId(), userSetting.getTenantId()) &&x.getKey().equals(userSetting.getKey()))) {
userSettingsToDelete.add(userSetting); userSettingsToDelete.add(userSetting);
} else { } else {
userSetting.setEntityId(newUser.getId()); userSetting.setEntityId(newUser.getId());
@ -749,6 +774,9 @@ public class UserServiceImpl implements UserService {
for (DescriptionEntity description : descriptions) { for (DescriptionEntity description : descriptions) {
this.annotationEntityTouchedIntegrationEventHandler.handleDescription(description.getId()); this.annotationEntityTouchedIntegrationEventHandler.handleDescription(description.getId());
} }
} finally {
this.entityManager.reloadTenantFilters();
}
} }
public void confirmRemoveCredential(String token) throws InvalidApplicationException { public void confirmRemoveCredential(String token) throws InvalidApplicationException {

View File

@ -42,7 +42,6 @@ public class UserRemovalIntegrationEventHandlerImpl implements UserRemovalIntegr
private final ErrorThesaurusProperties errors; private final ErrorThesaurusProperties errors;
private final MessageSource messageSource; private final MessageSource messageSource;
private final QueryFactory queryFactory;
private final TenantScope tenantScope; private final TenantScope tenantScope;
private final CurrentPrincipalResolver currentPrincipalResolver; private final CurrentPrincipalResolver currentPrincipalResolver;
private final ClaimExtractorProperties claimExtractorProperties; private final ClaimExtractorProperties claimExtractorProperties;
@ -54,12 +53,11 @@ public class UserRemovalIntegrationEventHandlerImpl implements UserRemovalIntegr
public UserRemovalIntegrationEventHandlerImpl( public UserRemovalIntegrationEventHandlerImpl(
JsonHandlingService jsonHandlingService, JsonHandlingService jsonHandlingService,
ErrorThesaurusProperties errors, ErrorThesaurusProperties errors,
MessageSource messageSource, QueryFactory queryFactory, TenantScope tenantScope, CurrentPrincipalResolver currentPrincipalResolver, ClaimExtractorProperties claimExtractorProperties, UserRemovalConsistencyHandler userRemovalConsistencyHandler, UserService userService, AuditService auditService, TenantEntityManager tenantEntityManager MessageSource messageSource, TenantScope tenantScope, CurrentPrincipalResolver currentPrincipalResolver, ClaimExtractorProperties claimExtractorProperties, UserRemovalConsistencyHandler userRemovalConsistencyHandler, UserService userService, AuditService auditService, TenantEntityManager tenantEntityManager
) { ) {
this.jsonHandlingService = jsonHandlingService; this.jsonHandlingService = jsonHandlingService;
this.errors = errors; this.errors = errors;
this.messageSource = messageSource; this.messageSource = messageSource;
this.queryFactory = queryFactory;
this.tenantScope = tenantScope; this.tenantScope = tenantScope;
this.currentPrincipalResolver = currentPrincipalResolver; this.currentPrincipalResolver = currentPrincipalResolver;
this.claimExtractorProperties = claimExtractorProperties; this.claimExtractorProperties = claimExtractorProperties;
@ -81,21 +79,10 @@ public class UserRemovalIntegrationEventHandlerImpl implements UserRemovalIntegr
EventProcessingStatus status = EventProcessingStatus.Success; EventProcessingStatus status = EventProcessingStatus.Success;
try { try {
if (this.tenantScope.isMultitenant() && properties.getTenantId() != null) {
TenantEntity tenant = queryFactory.query(TenantQuery.class).disableTracking().ids(properties.getTenantId()).firstAs(new BaseFieldSet().ensure(Tenant._id).ensure(Tenant._code));
if (tenant == null) {
logger.error("missing tenant from event message");
return EventProcessingStatus.Error;
}
this.tenantScope.setTempTenant(tenantEntityManager, properties.getTenantId(), tenant.getCode());
} else if (this.tenantScope.isMultitenant()) {
// logger.error("missing tenant from event message");
// return EventProcessingStatus.Error;
this.tenantScope.setTempTenant(tenantEntityManager, null, this.tenantScope.getDefaultTenantCode());
}
currentPrincipalResolver.push(InboxPrincipal.build(properties, claimExtractorProperties)); currentPrincipalResolver.push(InboxPrincipal.build(properties, claimExtractorProperties));
try{
this.tenantEntityManager.disableTenantFilters();
if (!(userRemovalConsistencyHandler.isConsistent(new UserRemovalConsistencyPredicates(event.getUserId())))) { if (!(userRemovalConsistencyHandler.isConsistent(new UserRemovalConsistencyPredicates(event.getUserId())))) {
status = EventProcessingStatus.Postponed; status = EventProcessingStatus.Postponed;
currentPrincipalResolver.pop(); currentPrincipalResolver.pop();
@ -104,6 +91,9 @@ public class UserRemovalIntegrationEventHandlerImpl implements UserRemovalIntegr
} }
userService.deleteAndSave(event.getUserId()); userService.deleteAndSave(event.getUserId());
} finally {
this.tenantEntityManager.reloadTenantFilters();
}
auditService.track(AuditableAction.User_Delete, Map.ofEntries( auditService.track(AuditableAction.User_Delete, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("id", event.getUserId()) new AbstractMap.SimpleEntry<String, Object>("id", event.getUserId())
@ -114,11 +104,6 @@ public class UserRemovalIntegrationEventHandlerImpl implements UserRemovalIntegr
logger.error("Problem getting list of queue outbox. Skipping: {}", ex.getMessage(), ex); logger.error("Problem getting list of queue outbox. Skipping: {}", ex.getMessage(), ex);
} finally { } finally {
currentPrincipalResolver.pop(); currentPrincipalResolver.pop();
try {
tenantScope.removeTempTenant(this.tenantEntityManager);
} catch (InvalidApplicationException e) {
logger.error( e.getMessage(), e);
}
} }
return status; return status;

View File

@ -71,23 +71,14 @@ public class UserTouchedIntegrationEventHandlerImpl implements UserTouchedIntegr
EventProcessingStatus status = EventProcessingStatus.Success; EventProcessingStatus status = EventProcessingStatus.Success;
try { try {
if (this.tenantScope.isMultitenant() && properties.getTenantId() != null) {
TenantEntity tenant = queryFactory.query(TenantQuery.class).disableTracking().ids(properties.getTenantId()).firstAs(new BaseFieldSet().ensure(Tenant._id).ensure(Tenant._code));
if (tenant == null) {
logger.error("missing tenant from event message");
return EventProcessingStatus.Error;
}
this.tenantScope.setTempTenant(tenantEntityManager, properties.getTenantId(), tenant.getCode());
} else if (this.tenantScope.isMultitenant()) {
// logger.error("missing tenant from event message");
// return EventProcessingStatus.Error;
this.tenantScope.setTempTenant(tenantEntityManager, null, this.tenantScope.getDefaultTenantCode());
}
currentPrincipalResolver.push(InboxPrincipal.build(properties, claimExtractorProperties)); currentPrincipalResolver.push(InboxPrincipal.build(properties, claimExtractorProperties));
try {
this.tenantEntityManager.disableTenantFilters();
userService.persist(event, null); userService.persist(event, null);
} finally {
this.tenantEntityManager.reloadTenantFilters();
}
auditService.track(AuditableAction.User_Persist, Map.ofEntries( auditService.track(AuditableAction.User_Persist, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("model", event) new AbstractMap.SimpleEntry<String, Object>("model", event)
)); ));
@ -97,11 +88,6 @@ public class UserTouchedIntegrationEventHandlerImpl implements UserTouchedIntegr
logger.error("Problem getting list of queue outbox. Skipping: {}", ex.getMessage(), ex); logger.error("Problem getting list of queue outbox. Skipping: {}", ex.getMessage(), ex);
} finally { } finally {
currentPrincipalResolver.pop(); currentPrincipalResolver.pop();
try {
tenantScope.removeTempTenant(this.tenantEntityManager);
} catch (InvalidApplicationException e) {
logger.error( e.getMessage(), e);
}
} }
return status; return status;

View File

@ -88,7 +88,7 @@ public class UserServiceImpl implements UserService {
public User persist(UserTouchedIntegrationEvent model, FieldSet fields) throws MyForbiddenException, MyValidationException, MyApplicationException, MyNotFoundException, InvalidApplicationException, JsonProcessingException { public User persist(UserTouchedIntegrationEvent model, FieldSet fields) throws MyForbiddenException, MyValidationException, MyApplicationException, MyNotFoundException, InvalidApplicationException, JsonProcessingException {
logger.debug(new MapLogEntry("persisting user").And("model", model).And("fields", fields)); logger.debug(new MapLogEntry("persisting user").And("model", model).And("fields", fields));
// this.authorizationService.authorizeAtLeastOneForce(model.getId() != null ? List.of(new OwnedResource(model.getId())) : null, Permission.EditUser); this.authorizationService.authorize(Permission.EditUser);
Boolean isValid = this.conventionService.isValidGuid(model.getId()); Boolean isValid = this.conventionService.isValidGuid(model.getId());