Merge pull request #132 from bsinno/eclipselink-tenancy
Eclipselink tenancy
This commit is contained in:
@@ -11,8 +11,6 @@ package org.eclipse.hawkbit;
|
|||||||
import javax.persistence.EntityManager;
|
import javax.persistence.EntityManager;
|
||||||
import javax.transaction.Transaction;
|
import javax.transaction.Transaction;
|
||||||
|
|
||||||
import org.eclipse.hawkbit.repository.RolloutManagement;
|
|
||||||
import org.eclipse.hawkbit.repository.SystemManagement;
|
|
||||||
import org.eclipse.hawkbit.repository.exception.TenantNotExistException;
|
import org.eclipse.hawkbit.repository.exception.TenantNotExistException;
|
||||||
import org.eclipse.hawkbit.tenancy.TenantAware;
|
import org.eclipse.hawkbit.tenancy.TenantAware;
|
||||||
import org.eclipse.persistence.config.PersistenceUnitProperties;
|
import org.eclipse.persistence.config.PersistenceUnitProperties;
|
||||||
@@ -42,34 +40,12 @@ public class MultiTenantJpaTransactionManager extends JpaTransactionManager {
|
|||||||
.getResource(getEntityManagerFactory());
|
.getResource(getEntityManagerFactory());
|
||||||
final EntityManager em = emHolder.getEntityManager();
|
final EntityManager em = emHolder.getEntityManager();
|
||||||
|
|
||||||
if (notTenantManagement(definition) && notCurrentTenantKeyGenerator(definition)
|
final String currentTenant = tenantAware.getCurrentTenant();
|
||||||
&& notRolloutScheduler(definition) && notGetOrCreateTenantMetadata(definition)) {
|
if (currentTenant == null) {
|
||||||
|
throw new TenantNotExistException("Tenant Unknown. Canceling transaction.");
|
||||||
final String currentTenant = tenantAware.getCurrentTenant();
|
|
||||||
if (currentTenant == null) {
|
|
||||||
throw new TenantNotExistException("Tenant Unknown. Canceling transaction.");
|
|
||||||
}
|
|
||||||
|
|
||||||
em.setProperty(PersistenceUnitProperties.MULTITENANT_PROPERTY_DEFAULT, currentTenant.toUpperCase());
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private boolean notGetOrCreateTenantMetadata(final TransactionDefinition definition) {
|
em.setProperty(PersistenceUnitProperties.MULTITENANT_PROPERTY_DEFAULT, currentTenant.toUpperCase());
|
||||||
return !definition.getName()
|
|
||||||
.startsWith(SystemManagement.class.getCanonicalName() + ".getOrCreateTenantMetadata");
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean notRolloutScheduler(final TransactionDefinition definition) {
|
|
||||||
return !definition.getName().startsWith(RolloutManagement.class.getCanonicalName() + ".rolloutScheduler");
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean notCurrentTenantKeyGenerator(final TransactionDefinition definition) {
|
|
||||||
return !definition.getName()
|
|
||||||
.startsWith(SystemManagement.class.getCanonicalName() + ".currentTenantKeyGenerator");
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean notTenantManagement(final TransactionDefinition definition) {
|
|
||||||
return !definition.getName().startsWith(SystemManagement.class.getCanonicalName() + ".deleteTenant")
|
|
||||||
&& !definition.getName().startsWith(SystemManagement.class.getCanonicalName() + ".findTenants");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -164,6 +164,7 @@ public class SystemManagement {
|
|||||||
* @return the {@link CurrentTenantKeyGenerator}
|
* @return the {@link CurrentTenantKeyGenerator}
|
||||||
*/
|
*/
|
||||||
@Bean
|
@Bean
|
||||||
|
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||||
public CurrentTenantKeyGenerator currentTenantKeyGenerator() {
|
public CurrentTenantKeyGenerator currentTenantKeyGenerator() {
|
||||||
return new CurrentTenantKeyGenerator();
|
return new CurrentTenantKeyGenerator();
|
||||||
}
|
}
|
||||||
@@ -206,7 +207,6 @@ public class SystemManagement {
|
|||||||
@NotNull
|
@NotNull
|
||||||
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_SYSTEM_ADMIN + SpringEvalExpressions.HAS_AUTH_OR
|
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_SYSTEM_ADMIN + SpringEvalExpressions.HAS_AUTH_OR
|
||||||
+ SpringEvalExpressions.IS_SYSTEM_CODE)
|
+ SpringEvalExpressions.IS_SYSTEM_CODE)
|
||||||
// tenant independent
|
|
||||||
public List<String> findTenants() {
|
public List<String> findTenants() {
|
||||||
return tenantMetaDataRepository.findAll().stream().map(md -> md.getTenant()).collect(Collectors.toList());
|
return tenantMetaDataRepository.findAll().stream().map(md -> md.getTenant()).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
@@ -221,7 +221,6 @@ public class SystemManagement {
|
|||||||
@Transactional
|
@Transactional
|
||||||
@Modifying
|
@Modifying
|
||||||
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_SYSTEM_ADMIN)
|
@PreAuthorize(SpringEvalExpressions.HAS_AUTH_SYSTEM_ADMIN)
|
||||||
// tenant independent
|
|
||||||
public void deleteTenant(@NotNull final String tenant) {
|
public void deleteTenant(@NotNull final String tenant) {
|
||||||
cacheManager.evictCaches(tenant);
|
cacheManager.evictCaches(tenant);
|
||||||
cacheManager.getCache("currentTenant").evict(currentTenantKeyGenerator().generate(null, null));
|
cacheManager.getCache("currentTenant").evict(currentTenantKeyGenerator().generate(null, null));
|
||||||
|
|||||||
@@ -78,6 +78,22 @@ public class MultiTenancyEntityTest extends AbstractIntegrationTest {
|
|||||||
assertThat(findTargetsForTenant).hasSize(1);
|
assertThat(findTargetsForTenant).hasSize(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Description(value = "Ensures that tenant with proper permissions can read and delete other tenants.")
|
||||||
|
@WithUser(tenantId = "mytenant", allSpPermissions = true)
|
||||||
|
public void deleteAnotherTenantPossible() throws Exception {
|
||||||
|
// create target for another tenant
|
||||||
|
final String anotherTenant = "anotherTenant";
|
||||||
|
final String controllerAnotherTenant = "anotherController";
|
||||||
|
createTargetForTenant(controllerAnotherTenant, anotherTenant);
|
||||||
|
|
||||||
|
assertThat(systemManagement.findTenants()).as("Expected number if tenants before deletion is").hasSize(3);
|
||||||
|
|
||||||
|
systemManagement.deleteTenant(anotherTenant);
|
||||||
|
|
||||||
|
assertThat(systemManagement.findTenants()).as("Expected number if tenants after deletion is").hasSize(2);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Description(value = "Ensures that tenant metadata is retrieved for the current tenant.")
|
@Description(value = "Ensures that tenant metadata is retrieved for the current tenant.")
|
||||||
@WithUser(tenantId = "mytenant", autoCreateTenant = false, allSpPermissions = true)
|
@WithUser(tenantId = "mytenant", autoCreateTenant = false, allSpPermissions = true)
|
||||||
|
|||||||
Reference in New Issue
Block a user