revert tenant filter disable

This commit is contained in:
Efstratios Giannopoulos 2024-05-02 12:38:36 +03:00
parent 2d2750c44c
commit 6eb01fbd1c
2 changed files with 7 additions and 7 deletions

View File

@ -1,13 +1,12 @@
package org.opencdmp.data.tenant; package org.opencdmp.data.tenant;
import org.opencdmp.commons.scope.tenant.TenantScope;
import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManager;
import org.aspectj.lang.JoinPoint; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Aspect;
import org.hibernate.Session; import org.hibernate.Session;
import org.opencdmp.commons.scope.tenant.TenantScope;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.management.InvalidApplicationException; import javax.management.InvalidApplicationException;
@ -15,7 +14,7 @@ import javax.management.InvalidApplicationException;
@Aspect @Aspect
@Component @Component
@ConditionalOnMissingBean(TenantScope.class) //@ConditionalOnMissingBean(TenantScope.class)
public class TenantFilterAspect { public class TenantFilterAspect {
private final TenantScope tenantScope; private final TenantScope tenantScope;
@ -31,12 +30,12 @@ public class TenantFilterAspect {
pointcut = "bean(entityManagerFactory) && execution(* createEntityManager(..))", pointcut = "bean(entityManagerFactory) && execution(* createEntityManager(..))",
returning = "retVal") returning = "retVal")
public void getSessionAfter(JoinPoint joinPoint, Object retVal) throws InvalidApplicationException { public void getSessionAfter(JoinPoint joinPoint, Object retVal) throws InvalidApplicationException {
if (retVal instanceof EntityManager && tenantScope.isSet()) { if (retVal instanceof EntityManager && this.tenantScope.isSet()) {
Session session = ((EntityManager) retVal).unwrap(Session.class); Session session = ((EntityManager) retVal).unwrap(Session.class);
if(!tenantScope.isDefaultTenant()) { if(!this.tenantScope.isDefaultTenant()) {
session session
.enableFilter(TenantScopedBaseEntity.TENANT_FILTER) .enableFilter(TenantScopedBaseEntity.TENANT_FILTER)
.setParameter(TenantScopedBaseEntity.TENANT_FILTER_TENANT_PARAM, tenantScope.getTenant().toString()); .setParameter(TenantScopedBaseEntity.TENANT_FILTER_TENANT_PARAM, this.tenantScope.getTenant().toString());
} else { } else {
session session
.enableFilter(TenantScopedBaseEntity.DEFAULT_TENANT_FILTER); .enableFilter(TenantScopedBaseEntity.DEFAULT_TENANT_FILTER);

View File

@ -1,6 +1,7 @@
package org.opencdmp.data.tenant; package org.opencdmp.data.tenant;
import jakarta.persistence.Column; import jakarta.persistence.Column;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.MappedSuperclass; import jakarta.persistence.MappedSuperclass;
import org.hibernate.annotations.Filter; import org.hibernate.annotations.Filter;
import org.hibernate.annotations.FilterDef; import org.hibernate.annotations.FilterDef;
@ -18,7 +19,7 @@ import java.util.UUID;
@FilterDef(name = TenantScopedBaseEntity.DEFAULT_TENANT_FILTER) @FilterDef(name = TenantScopedBaseEntity.DEFAULT_TENANT_FILTER)
@Filter(name = TenantScopedBaseEntity.DEFAULT_TENANT_FILTER, condition = "(tenant = tenant is null)") @Filter(name = TenantScopedBaseEntity.DEFAULT_TENANT_FILTER, condition = "(tenant = tenant is null)")
@Filter(name = TenantScopedBaseEntity.TENANT_FILTER, condition = "(tenant = (cast(:tenantId as uuid)) or tenant is null)") @Filter(name = TenantScopedBaseEntity.TENANT_FILTER, condition = "(tenant = (cast(:tenantId as uuid)) or tenant is null)")
//@EntityListeners(TenantListener.class) @EntityListeners(TenantListener.class)
public abstract class TenantScopedBaseEntity implements TenantScoped, Serializable { public abstract class TenantScopedBaseEntity implements TenantScoped, Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
public static final String TENANT_FILTER = "tenantFilter"; public static final String TENANT_FILTER = "tenantFilter";