Skip to content

Commit

Permalink
#948: support or fallbacks to find tenant in url
Browse files Browse the repository at this point in the history
  • Loading branch information
clean-coder committed Jul 4, 2024
1 parent f0853d7 commit b750e4d
Showing 1 changed file with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import ch.puzzle.okr.dto.ClientConfigDto;
import ch.puzzle.okr.multitenancy.TenantConfigProvider;
import jakarta.persistence.EntityNotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

Expand All @@ -12,6 +14,7 @@
@Service
public class ClientConfigService {

private static final Logger logger = LoggerFactory.getLogger(ClientConfigService.class);
private final ClientCustomizationProperties clientCustomizationProperties;
private final TenantConfigProvider tenantConfigProvider;

Expand All @@ -26,8 +29,11 @@ public ClientConfigService(final ClientCustomizationProperties clientCustomizati

public ClientConfigDto getConfigBasedOnActiveEnv(String hostName) {
String subdomain = hostName.split("\\.")[0];
String domainPrefixByHyphen = hostName.split("-")[0];

Optional<TenantConfigProvider.TenantConfig> tenantConfig = getTenantConfig(hostName, subdomain,
domainPrefixByHyphen);

Optional<TenantConfigProvider.TenantConfig> tenantConfig = tenantConfigProvider.getTenantConfigById(subdomain);
if (tenantConfig.isEmpty()) {
throw new EntityNotFoundException(
MessageFormat.format("Could not find tenant for subdomain:{0}", subdomain));
Expand All @@ -37,4 +43,17 @@ public ClientConfigDto getConfigBasedOnActiveEnv(String hostName) {
clientCustomizationProperties.getFavicon(), clientCustomizationProperties.getLogo(),
clientCustomizationProperties.getTitle(), clientCustomizationProperties.getCustomStyles());
}

private Optional<TenantConfigProvider.TenantConfig> getTenantConfig(String hostname, String... tenantsFromUrl) {
for (String tenant : tenantsFromUrl) {
Optional<TenantConfigProvider.TenantConfig> tenantConfig = tenantConfigProvider.getTenantConfigById(tenant);
if (tenantConfig.isPresent()) {
logger.info("get config for " + tenant + ": OK");
return tenantConfig;
}
logger.info("get config found for " + tenant + ": failed");
}
logger.info("no config found for " + hostname + ": failed");
return Optional.empty();
}
}

0 comments on commit b750e4d

Please sign in to comment.