feat: rewrite MeController — read from JWT claims, Management API only for cold start

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
hsiegeln
2026-04-05 12:38:39 +02:00
parent 48a5035a2c
commit d4408634a6

View File

@@ -15,12 +15,12 @@ import java.util.Map;
@RestController @RestController
public class MeController { public class MeController {
private final LogtoManagementClient logtoClient;
private final TenantService tenantService; private final TenantService tenantService;
private final LogtoManagementClient logtoClient;
public MeController(LogtoManagementClient logtoClient, TenantService tenantService) { public MeController(TenantService tenantService, LogtoManagementClient logtoClient) {
this.logtoClient = logtoClient;
this.tenantService = tenantService; this.tenantService = tenantService;
this.logtoClient = logtoClient;
} }
@GetMapping("/api/me") @GetMapping("/api/me")
@@ -32,19 +32,35 @@ public class MeController {
Jwt jwt = jwtAuth.getToken(); Jwt jwt = jwtAuth.getToken();
String userId = jwt.getSubject(); String userId = jwt.getSubject();
List<String> globalRoles = logtoClient.getUserRoles(userId); String orgId = jwt.getClaimAsString("organization_id");
boolean isPlatformAdmin = globalRoles.contains("platform-admin");
List<String> globalRoles = jwt.getClaimAsStringList("roles");
boolean isPlatformAdmin = globalRoles != null && globalRoles.contains("platform-admin");
if (orgId != null) {
var tenant = tenantService.getByLogtoOrgId(orgId).orElse(null);
List<Map<String, Object>> tenants = tenant != null
? List.of(Map.<String, Object>of(
"id", tenant.getId().toString(),
"name", tenant.getName(),
"slug", tenant.getSlug(),
"logtoOrgId", tenant.getLogtoOrgId()))
: List.of();
return ResponseEntity.ok(Map.of(
"userId", userId,
"isPlatformAdmin", isPlatformAdmin,
"tenants", tenants));
}
List<Map<String, String>> logtoOrgs = logtoClient.getUserOrganizations(userId); List<Map<String, String>> logtoOrgs = logtoClient.getUserOrganizations(userId);
List<Map<String, Object>> tenants = logtoOrgs.stream() List<Map<String, Object>> tenants = logtoOrgs.stream()
.map(org -> tenantService.getByLogtoOrgId(org.get("id")) .map(org -> tenantService.getByLogtoOrgId(org.get("id"))
.map(t -> Map.<String, Object>of( .map(t -> Map.<String, Object>of(
"id", t.getId().toString(), "id", t.getId().toString(),
"name", t.getName(), "name", t.getName(),
"slug", t.getSlug(), "slug", t.getSlug(),
"logtoOrgId", t.getLogtoOrgId() "logtoOrgId", t.getLogtoOrgId()))
))
.orElse(null)) .orElse(null))
.filter(t -> t != null) .filter(t -> t != null)
.toList(); .toList();
@@ -52,7 +68,6 @@ public class MeController {
return ResponseEntity.ok(Map.of( return ResponseEntity.ok(Map.of(
"userId", userId, "userId", userId,
"isPlatformAdmin", isPlatformAdmin, "isPlatformAdmin", isPlatformAdmin,
"tenants", tenants "tenants", tenants));
));
} }
} }