argos/dmp-backend/core/src/main/java/eu/eudat/data/TenantEntityManager.java

122 lines
4.1 KiB
Java
Raw Normal View History

2023-12-06 12:08:49 +01:00
package eu.eudat.data;
import eu.eudat.authorization.Permission;
import eu.eudat.commons.scope.tenant.TenantScope;
import eu.eudat.commons.scope.tenant.TenantScoped;
2024-04-03 11:22:43 +02:00
import eu.eudat.data.tenant.TenantScopedBaseEntity;
2024-04-03 12:22:22 +02:00
import eu.eudat.errorcode.ErrorThesaurusProperties;
2023-12-06 12:08:49 +01:00
import gr.cite.commons.web.authz.service.AuthorizationService;
import gr.cite.commons.web.oidc.principal.CurrentPrincipalResolver;
import gr.cite.commons.web.oidc.principal.extractor.ClaimExtractor;
import gr.cite.tools.exception.MyForbiddenException;
import jakarta.persistence.*;
2024-04-03 11:22:43 +02:00
import org.hibernate.Session;
2023-12-06 12:08:49 +01:00
import org.springframework.stereotype.Service;
import org.springframework.web.context.annotation.RequestScope;
import javax.management.InvalidApplicationException;
import java.util.UUID;
@Service
@RequestScope
public class TenantEntityManager {
@PersistenceContext
private EntityManager entityManager;
private final TenantScope tenantScope;
2024-04-03 12:22:22 +02:00
private final ErrorThesaurusProperties errors;
2023-12-06 12:08:49 +01:00
2024-04-03 12:22:22 +02:00
public TenantEntityManager(TenantScope tenantScope, ErrorThesaurusProperties errors) {
2023-12-06 12:08:49 +01:00
this.tenantScope = tenantScope;
2024-04-03 12:22:22 +02:00
this.errors = errors;
2023-12-06 12:08:49 +01:00
}
public void persist(Object entity) {
this.entityManager.persist(entity);
}
public <T> T merge(T entity) throws InvalidApplicationException {
2024-04-02 16:18:07 +02:00
if (tenantScope.isMultitenant() && (entity instanceof TenantScoped tenantScopedEntity)) {
if (!tenantScope.isDefaultTenant()) {
2024-04-03 12:22:22 +02:00
if (tenantScopedEntity.getTenantId() == null || !tenantScopedEntity.getTenantId().equals(tenantScope.getTenant())) throw new MyForbiddenException(this.errors.getTenantTampering().getCode(), this.errors.getTenantTampering().getMessage());
2024-04-02 16:18:07 +02:00
} else if (tenantScopedEntity.getTenantId() != null) {
2024-04-03 12:22:22 +02:00
throw new MyForbiddenException(this.errors.getTenantTampering().getCode(), this.errors.getTenantTampering().getMessage());
2024-04-02 16:18:07 +02:00
}
2023-12-06 12:08:49 +01:00
}
return this.entityManager.merge(entity);
}
public void remove(Object entity) throws InvalidApplicationException {
2024-04-02 16:18:07 +02:00
if (tenantScope.isMultitenant() && (entity instanceof TenantScoped tenantScopedEntity)) {
if (!tenantScope.isDefaultTenant()) {
2024-04-03 12:22:22 +02:00
if (tenantScopedEntity.getTenantId() == null || !tenantScopedEntity.getTenantId().equals(tenantScope.getTenant())) throw new MyForbiddenException(this.errors.getTenantTampering().getCode(), this.errors.getTenantTampering().getMessage());
2024-04-02 16:18:07 +02:00
} else if (tenantScopedEntity.getTenantId() != null) {
2024-04-03 12:22:22 +02:00
throw new MyForbiddenException(this.errors.getTenantTampering().getCode(), this.errors.getTenantTampering().getMessage());
2024-04-02 16:18:07 +02:00
}
2023-12-06 12:08:49 +01:00
}
this.entityManager.remove(entity);
}
public <T> T find(Class<T> entityClass, Object primaryKey) throws InvalidApplicationException {
T entity = this.entityManager.find(entityClass, primaryKey);
2024-04-02 16:18:07 +02:00
if (tenantScope.isMultitenant() && (entity instanceof TenantScoped tenantScopedEntity)) {
if (tenantScopedEntity.getTenantId() != null && !tenantScopedEntity.getTenantId().equals(tenantScope.getTenant())) return null;
2023-12-06 12:08:49 +01:00
}
return entity;
}
public void flush() {
this.entityManager.flush();
}
public void setFlushMode(FlushModeType flushMode) {
this.entityManager.setFlushMode(flushMode);
}
public FlushModeType getFlushMode() {
return this.entityManager.getFlushMode();
}
public void clear() {
this.entityManager.clear();
}
2024-04-03 11:22:43 +02:00
public void enableTenantFilters() throws InvalidApplicationException {
2024-04-04 11:21:12 +02:00
if (!tenantScope.isSet()) return;
2024-04-03 11:22:43 +02:00
if(!tenantScope.isDefaultTenant()) {
this.entityManager
.unwrap(Session.class)
.enableFilter(TenantScopedBaseEntity.TENANT_FILTER)
.setParameter(TenantScopedBaseEntity.TENANT_FILTER_TENANT_PARAM, tenantScope.getTenant().toString());
} else {
this.entityManager
.unwrap(Session.class)
.enableFilter(TenantScopedBaseEntity.DEFAULT_TENANT_FILTER);
}
}
public void disableTenantFilters(){
this.entityManager
.unwrap(Session.class)
.disableFilter(TenantScopedBaseEntity.TENANT_FILTER);
this.entityManager
.unwrap(Session.class)
.disableFilter(TenantScopedBaseEntity.DEFAULT_TENANT_FILTER);
}
2024-04-03 17:35:37 +02:00
public EntityManager getEntityManager() {
return entityManager;
}
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
2023-12-06 12:08:49 +01:00
}