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