Introduce pluggable tenant resolver (#2151)

Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com>
This commit is contained in:
Avgustin Marinov
2024-12-18 15:17:35 +02:00
committed by GitHub
parent 1c211c81c2
commit ed93d3fc7b
8 changed files with 96 additions and 44 deletions

View File

@@ -9,6 +9,9 @@
*/
package org.eclipse.hawkbit.tenancy;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
/**
* Interface for components that are aware of the application's current tenant.
*/
@@ -66,4 +69,29 @@ public interface TenantAware {
*/
T run();
}
/**
* Resolves the tenant from the current context.
*/
interface TenantResolver {
String resolveTenant();
}
class DefaultTenantResolver implements TenantResolver {
@Override
public String resolveTenant() {
final SecurityContext context = SecurityContextHolder.getContext();
if (context.getAuthentication() != null) {
final Object principal = context.getAuthentication().getPrincipal();
if (context.getAuthentication().getDetails() instanceof TenantAwareAuthenticationDetails tenantAwareAuthenticationDetails) {
return tenantAwareAuthenticationDetails.getTenant();
} else if (principal instanceof TenantAwareUser tenantAwareUser) {
return tenantAwareUser.getTenant();
}
}
return null;
}
}
}