Merge branch 'dmp-refactoring' of https://code-repo.d4science.org/MaDgiK-CITE/argos into dmp-refactoring
This commit is contained in:
commit
d57fed927a
|
@ -1,24 +1,19 @@
|
||||||
package org.opencdmp.data;
|
package org.opencdmp.data;
|
||||||
|
|
||||||
import org.opencdmp.authorization.Permission;
|
import gr.cite.tools.exception.MyForbiddenException;
|
||||||
|
import jakarta.persistence.EntityManager;
|
||||||
|
import jakarta.persistence.FlushModeType;
|
||||||
|
import jakarta.persistence.PersistenceContext;
|
||||||
|
import org.hibernate.Session;
|
||||||
import org.opencdmp.commons.scope.tenant.TenantScope;
|
import org.opencdmp.commons.scope.tenant.TenantScope;
|
||||||
import org.opencdmp.commons.scope.tenant.TenantScoped;
|
import org.opencdmp.commons.scope.tenant.TenantScoped;
|
||||||
import org.opencdmp.data.tenant.TenantScopedBaseEntity;
|
import org.opencdmp.data.tenant.TenantScopedBaseEntity;
|
||||||
import org.opencdmp.errorcode.ErrorThesaurusProperties;
|
import org.opencdmp.errorcode.ErrorThesaurusProperties;
|
||||||
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.*;
|
|
||||||
import org.hibernate.Session;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.context.annotation.RequestScope;
|
import org.springframework.web.context.annotation.RequestScope;
|
||||||
|
|
||||||
import javax.management.InvalidApplicationException;
|
import javax.management.InvalidApplicationException;
|
||||||
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@RequestScope
|
@RequestScope
|
||||||
public class TenantEntityManager {
|
public class TenantEntityManager {
|
||||||
|
@ -38,9 +33,9 @@ public class TenantEntityManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T> T merge(T entity) throws InvalidApplicationException {
|
public <T> T merge(T entity) throws InvalidApplicationException {
|
||||||
if (tenantScope.isMultitenant() && (entity instanceof TenantScoped tenantScopedEntity)) {
|
if (this.tenantScope.isMultitenant() && (entity instanceof TenantScoped tenantScopedEntity)) {
|
||||||
if (!tenantScope.isDefaultTenant()) {
|
if (!this.tenantScope.isDefaultTenant()) {
|
||||||
if (tenantScopedEntity.getTenantId() == null || !tenantScopedEntity.getTenantId().equals(tenantScope.getTenant())) throw new MyForbiddenException(this.errors.getTenantTampering().getCode(), this.errors.getTenantTampering().getMessage());
|
if (tenantScopedEntity.getTenantId() == null || !tenantScopedEntity.getTenantId().equals(this.tenantScope.getTenant())) throw new MyForbiddenException(this.errors.getTenantTampering().getCode(), this.errors.getTenantTampering().getMessage());
|
||||||
} else if (tenantScopedEntity.getTenantId() != null) {
|
} else if (tenantScopedEntity.getTenantId() != null) {
|
||||||
throw new MyForbiddenException(this.errors.getTenantTampering().getCode(), this.errors.getTenantTampering().getMessage());
|
throw new MyForbiddenException(this.errors.getTenantTampering().getCode(), this.errors.getTenantTampering().getMessage());
|
||||||
}
|
}
|
||||||
|
@ -49,9 +44,9 @@ public class TenantEntityManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void remove(Object entity) throws InvalidApplicationException {
|
public void remove(Object entity) throws InvalidApplicationException {
|
||||||
if (tenantScope.isMultitenant() && (entity instanceof TenantScoped tenantScopedEntity)) {
|
if (this.tenantScope.isMultitenant() && (entity instanceof TenantScoped tenantScopedEntity)) {
|
||||||
if (!tenantScope.isDefaultTenant()) {
|
if (!this.tenantScope.isDefaultTenant()) {
|
||||||
if (tenantScopedEntity.getTenantId() == null || !tenantScopedEntity.getTenantId().equals(tenantScope.getTenant())) throw new MyForbiddenException(this.errors.getTenantTampering().getCode(), this.errors.getTenantTampering().getMessage());
|
if (tenantScopedEntity.getTenantId() == null || !tenantScopedEntity.getTenantId().equals(this.tenantScope.getTenant())) throw new MyForbiddenException(this.errors.getTenantTampering().getCode(), this.errors.getTenantTampering().getMessage());
|
||||||
} else if (tenantScopedEntity.getTenantId() != null) {
|
} else if (tenantScopedEntity.getTenantId() != null) {
|
||||||
throw new MyForbiddenException(this.errors.getTenantTampering().getCode(), this.errors.getTenantTampering().getMessage());
|
throw new MyForbiddenException(this.errors.getTenantTampering().getCode(), this.errors.getTenantTampering().getMessage());
|
||||||
}
|
}
|
||||||
|
@ -62,12 +57,22 @@ public class TenantEntityManager {
|
||||||
public <T> T find(Class<T> entityClass, Object primaryKey) throws InvalidApplicationException {
|
public <T> T find(Class<T> entityClass, Object primaryKey) throws InvalidApplicationException {
|
||||||
T entity = this.entityManager.find(entityClass, primaryKey);
|
T entity = this.entityManager.find(entityClass, primaryKey);
|
||||||
|
|
||||||
if (tenantScope.isMultitenant() && (entity instanceof TenantScoped tenantScopedEntity)) {
|
if (this.tenantScope.isMultitenant() && (entity instanceof TenantScoped tenantScopedEntity)) {
|
||||||
if (tenantScopedEntity.getTenantId() != null && !tenantScopedEntity.getTenantId().equals(tenantScope.getTenant())) return null;
|
if (tenantScopedEntity.getTenantId() != null && !tenantScopedEntity.getTenantId().equals(this.tenantScope.getTenant())) return null;
|
||||||
}
|
}
|
||||||
return entity;
|
return entity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public <T> T find(Class<T> entityClass, Object primaryKey, boolean disableTracking) throws InvalidApplicationException {
|
||||||
|
T entity = this.entityManager.find(entityClass, primaryKey);
|
||||||
|
|
||||||
|
if (this.tenantScope.isMultitenant() && (entity instanceof TenantScoped tenantScopedEntity)) {
|
||||||
|
if (tenantScopedEntity.getTenantId() != null && !tenantScopedEntity.getTenantId().equals(this.tenantScope.getTenant())) return null;
|
||||||
|
}
|
||||||
|
if (disableTracking) this.entityManager.detach(entity);
|
||||||
|
return entity;
|
||||||
|
}
|
||||||
|
|
||||||
public void flush() {
|
public void flush() {
|
||||||
this.entityManager.flush();
|
this.entityManager.flush();
|
||||||
}
|
}
|
||||||
|
@ -87,12 +92,12 @@ public class TenantEntityManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void enableTenantFilters() throws InvalidApplicationException {
|
public void enableTenantFilters() throws InvalidApplicationException {
|
||||||
if (!tenantScope.isSet()) return;
|
if (!this.tenantScope.isSet()) return;
|
||||||
if(!tenantScope.isDefaultTenant()) {
|
if(!this.tenantScope.isDefaultTenant()) {
|
||||||
this.entityManager
|
this.entityManager
|
||||||
.unwrap(Session.class)
|
.unwrap(Session.class)
|
||||||
.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 {
|
||||||
this.entityManager
|
this.entityManager
|
||||||
.unwrap(Session.class)
|
.unwrap(Session.class)
|
||||||
|
@ -111,7 +116,7 @@ public class TenantEntityManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
public EntityManager getEntityManager() {
|
public EntityManager getEntityManager() {
|
||||||
return entityManager;
|
return this.entityManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setEntityManager(EntityManager entityManager) {
|
public void setEntityManager(EntityManager entityManager) {
|
||||||
|
|
Loading…
Reference in New Issue