2023-12-06 12:08:49 +01:00
|
|
|
package eu.eudat.commons.scope.tenant;
|
|
|
|
|
|
|
|
import eu.eudat.data.tenant.TenantScopedBaseEntity;
|
|
|
|
import jakarta.persistence.EntityManager;
|
|
|
|
import org.hibernate.Session;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import org.springframework.web.context.annotation.RequestScope;
|
|
|
|
|
|
|
|
import javax.management.InvalidApplicationException;
|
|
|
|
|
|
|
|
import java.util.UUID;
|
|
|
|
import java.util.concurrent.atomic.AtomicReference;
|
|
|
|
|
|
|
|
@Component
|
2024-04-01 17:36:03 +02:00
|
|
|
@RequestScope
|
2023-12-06 12:08:49 +01:00
|
|
|
public class TenantScope {
|
|
|
|
public static final String TenantReplaceParameter = "::TenantCode::";
|
|
|
|
private final MultitenancyProperties multitenancy;
|
|
|
|
private final AtomicReference<UUID> tenant = new AtomicReference<>();
|
|
|
|
private final AtomicReference<String> tenantCode = new AtomicReference<>();
|
|
|
|
private final AtomicReference<UUID> initialTenant = new AtomicReference<>();
|
|
|
|
private final AtomicReference<String> initialTenantCode = new AtomicReference<>();
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
public TenantScope(MultitenancyProperties multitenancy) {
|
|
|
|
this.multitenancy = multitenancy;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Boolean isMultitenant() {
|
|
|
|
return multitenancy.isMultitenant();
|
|
|
|
}
|
|
|
|
|
2024-04-02 12:07:00 +02:00
|
|
|
public String getDefaultTenantCode() {
|
|
|
|
return multitenancy.getDefaultTenantCode();
|
|
|
|
}
|
|
|
|
|
2023-12-06 12:08:49 +01:00
|
|
|
public Boolean isSet() {
|
|
|
|
if (!this.isMultitenant())
|
|
|
|
return Boolean.TRUE;
|
2024-04-02 12:07:00 +02:00
|
|
|
return this.tenant.get() != null || this.isDefaultTenant();
|
|
|
|
}
|
|
|
|
|
|
|
|
public Boolean isDefaultTenant() {
|
|
|
|
if (!this.isMultitenant())
|
|
|
|
return Boolean.TRUE;
|
|
|
|
return this.multitenancy.getDefaultTenantCode().equalsIgnoreCase(this.tenantCode.get());
|
2023-12-06 12:08:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public UUID getTenant() throws InvalidApplicationException {
|
|
|
|
if (!this.isMultitenant())
|
|
|
|
return null;
|
2024-04-02 12:07:00 +02:00
|
|
|
if (this.tenant.get() == null && !this.isDefaultTenant())
|
2023-12-06 12:08:49 +01:00
|
|
|
throw new InvalidApplicationException("tenant not set");
|
2024-04-02 12:07:00 +02:00
|
|
|
return this.isDefaultTenant() ? null : this.tenant.get();
|
2023-12-06 12:08:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getTenantCode() throws InvalidApplicationException {
|
|
|
|
if (!this.isMultitenant())
|
|
|
|
return null;
|
2024-04-02 12:07:00 +02:00
|
|
|
if (this.tenantCode.get() == null)
|
2023-12-06 12:08:49 +01:00
|
|
|
throw new InvalidApplicationException("tenant not set");
|
|
|
|
return this.tenantCode.get();
|
|
|
|
}
|
|
|
|
|
2024-04-02 12:07:00 +02:00
|
|
|
public void setTempTenant(EntityManager entityManager, UUID tenant, String tenantCode) {
|
2023-12-06 12:08:49 +01:00
|
|
|
this.tenant.set(tenant);
|
2024-04-02 12:07:00 +02:00
|
|
|
this.tenantCode.set(tenantCode);
|
2023-12-06 12:08:49 +01:00
|
|
|
|
2024-04-16 12:17:06 +02:00
|
|
|
entityManager
|
|
|
|
.unwrap(Session.class)
|
|
|
|
.disableFilter(TenantScopedBaseEntity.TENANT_FILTER);
|
|
|
|
|
|
|
|
entityManager
|
|
|
|
.unwrap(Session.class)
|
|
|
|
.disableFilter(TenantScopedBaseEntity.DEFAULT_TENANT_FILTER);
|
2024-04-16 10:02:17 +02:00
|
|
|
if (this.tenant.get() != null || this.isDefaultTenant()) {
|
2024-04-02 12:07:00 +02:00
|
|
|
if(!this.isDefaultTenant()) {
|
|
|
|
entityManager
|
|
|
|
.unwrap(Session.class)
|
|
|
|
.enableFilter(TenantScopedBaseEntity.TENANT_FILTER)
|
|
|
|
.setParameter(TenantScopedBaseEntity.TENANT_FILTER_TENANT_PARAM, this.tenant.get().toString());
|
|
|
|
} else {
|
|
|
|
entityManager
|
|
|
|
.unwrap(Session.class)
|
|
|
|
.enableFilter(TenantScopedBaseEntity.DEFAULT_TENANT_FILTER);
|
|
|
|
}
|
2023-12-06 12:08:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void removeTempTenant(EntityManager entityManager) {
|
|
|
|
this.tenant.set(this.initialTenant.get());
|
|
|
|
this.tenantCode.set(this.initialTenantCode.get());
|
2024-04-16 12:17:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
entityManager
|
|
|
|
.unwrap(Session.class)
|
|
|
|
.disableFilter(TenantScopedBaseEntity.TENANT_FILTER);
|
|
|
|
|
|
|
|
entityManager
|
|
|
|
.unwrap(Session.class)
|
|
|
|
.disableFilter(TenantScopedBaseEntity.DEFAULT_TENANT_FILTER);
|
2024-04-16 10:02:17 +02:00
|
|
|
if (this.initialTenant.get() != null || this.isDefaultTenant()) {
|
2024-04-02 12:07:00 +02:00
|
|
|
if(!this.isDefaultTenant()) {
|
|
|
|
entityManager
|
|
|
|
.unwrap(Session.class)
|
|
|
|
.enableFilter(TenantScopedBaseEntity.TENANT_FILTER)
|
|
|
|
.setParameter(TenantScopedBaseEntity.TENANT_FILTER_TENANT_PARAM, this.tenant.get().toString());
|
|
|
|
} else {
|
|
|
|
entityManager
|
|
|
|
.unwrap(Session.class)
|
|
|
|
.enableFilter(TenantScopedBaseEntity.DEFAULT_TENANT_FILTER);
|
|
|
|
}
|
2023-12-06 12:08:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setTenant(UUID tenant, String tenantCode) {
|
|
|
|
if (this.isMultitenant()) {
|
|
|
|
this.tenant.set(tenant);
|
|
|
|
this.initialTenant.set(tenant);
|
|
|
|
this.tenantCode.set(tenantCode);
|
|
|
|
this.initialTenantCode.set(tenantCode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|