fix(mobile-api): read roles services-system sends as bare ids

services-system writes a role already seen in a response as its bare id. The
module check missed it, so a role granted two modules was refused the second;
the super-admin target guard now reads it too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
This commit is contained in:
ngqileandClaude Opus 5.5 committed 2026-11-26 22:37:49 +08:00
1 parent 4c48ebea2e
commit 69e2b4eb7b
4 files changed
+22 -3

No files matched your search

@@ -155,8 +155,9 @@ public class AdminUserService {
/** The target admin; a non-super caller may not manage a super admin. */
private Map<String, Object> loadManaged(Long id, User caller) {
Map<String, Object> user = load(id);
// idOf also reads a role services-system sent as a bare id
if (!isSuper(caller) && user.get("roles") instanceof List<?> l && l.stream().anyMatch(
x -> x instanceof Map<?, ?> m && m.get("id") instanceof Number n && n.longValue() == SUPER_ADMIN_ROLE_ID)) {
x -> Long.valueOf(SUPER_ADMIN_ROLE_ID).equals(AdminRoleService.idOf(x)))) {
throw superRequired("Only a super admin can manage a super admin");
}
return user;
@@ -27,8 +27,9 @@ public final class AdminModuleAccess {
List<Map<String, Object>> modules = ent.getBody() != null ? ent.getBody() : List.of();
return modules.stream()
.filter(m -> m.get("name") instanceof String name && wanted.contains(name))
// services-system writes a role already seen in this response as its bare id
.anyMatch(m -> m.get("roles") instanceof List<?> roles && roles.stream()
.anyMatch(r -> r instanceof Map<?, ?> rm && rm.get("id") instanceof Number n
&& roleIds.contains(n.longValue())));
.map(r -> r instanceof Map<?, ?> rm ? rm.get("id") : r)
.anyMatch(id -> id instanceof Number n && roleIds.contains(n.longValue())));
}
}
@@ -500,6 +500,13 @@ class AdminUserServiceTest {
verify(auth, never()).resetAdminPassword(anyString(), anyString());
}
@Test
void nonSuperCaller_cannotManageSuperAdminTargetEvenWhenTheRoleArrivesAsABareId() {
stubFind(6, userWithRoles(6, "Root", 1));
assertCoded(() -> service.resetPassword(6L), HttpStatus.FORBIDDEN, "SUPER_ADMIN_REQUIRED");
verify(auth, never()).resetAdminPassword(anyString(), anyString());
}
@Test
void superCaller_canManageSuperAdminTarget() {
callerIsSuper();
@@ -32,6 +32,16 @@ class AdminModuleAccessTest {
Map.of("name", "BO_EMPTY"))));
}
@Test
void roleSentAsBareIdBecauseItAppearedEarlier_isGranted() {
// services-system's @JsonIdentityInfo writes a role already seen in the response as its bare id.
when(rest.getForEntity(any(String.class), any(ParameterizedTypeReference.class), eq("BO")))
.thenReturn(ResponseEntity.ok(List.of(
Map.of("name", "BO_USERS", "roles", List.of(Map.of("id", 5))),
Map.of("name", "BO_ROLES", "roles", List.of(5)))));
assertThat(AdminModuleAccess.grantedAny(rest, Set.of(5L), "BO_ROLES")).isTrue();
}
@Test
void roleGrantedModule_isGranted() {
assertThat(AdminModuleAccess.grantedAny(rest, Set.of(6L), "BO_LEADS")).isTrue();