Skip to content

Commit

Permalink
Support for multi tenancy root mode #35424
Browse files Browse the repository at this point in the history
  • Loading branch information
seepine committed Aug 23, 2023
1 parent f29fa19 commit 36f4a64
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ public boolean validateExistingCurrentSessions() {
return false;
}

@Override
public boolean isRoot(String tenantId) {
// Make sure that we're in a request
if (!Arc.container().requestContext().isActive()) {
return false;
}
TenantResolver resolver = tenantResolver(persistenceUnitName);
if (resolver == null) {
return false;
}
return resolver.isRoot(tenantId);
}

private static TenantResolver tenantResolver(String persistenceUnitName) {
InjectableInstance<TenantResolver> instance = PersistenceUnitUtil.legacySingleExtensionInstanceForPersistenceUnit(
TenantResolver.class, persistenceUnitName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,14 @@ public interface TenantResolver {
*/
String resolveTenantId();

/**
* Does the given tenant id represent a "root" tenant with access to all partitions?
*
* @param tenantId a tenant id produced by {@link #resolveTenantId()}
*
* @return true is this is root tenant
*/
default boolean isRoot(String tenantId) {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,17 @@ public String resolveTenantId() {
final String tenantId;
if (path.startsWith("/mycompany")) {
tenantId = "mycompany";
} else if (path.startsWith("/global")) {
tenantId = "global";
} else {
tenantId = getDefaultTenantId();
}
LOG.debugv("TenantId = {0}", tenantId);
return tenantId;
}

@Override
public boolean isRoot(String tenantId) {
return "global".equals(tenantId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.arrayContaining;
import static org.hamcrest.Matchers.arrayWithSize;
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;

import java.util.Arrays;

import jakarta.ws.rs.core.Response.Status;

import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -147,6 +150,18 @@ public void testUpdateFruitDefaultTenant() throws Exception {

}

@Test
public void testGetFruitsTenantRoot() throws Exception {

// Get all tenant fruits
Fruit[] fruits = given().when().get("/global/fruits").then().assertThat()
.statusCode(is(Status.OK.getStatusCode())).extract()
.as(Fruit[].class);
assertThat(fruits, arrayWithSize(6));
assertThat(Arrays.asList(fruits), hasItems(new Fruit(4, "Avocado"), new Fruit(1, "Cherry")));

}

private Fruit findByName(String tenantPath, String name) {
Response response = given().when().get(tenantPath + "/fruitsFindBy?type=name&value={name}", name);
if (response.getStatusCode() == Status.OK.getStatusCode()) {
Expand Down

0 comments on commit 36f4a64

Please sign in to comment.