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:
1 parent
4c48ebea2e
commit
69e2b4eb7b
4 files changed
+22
-3
No files matched your search
+2
-1
@@ -155,8 +155,9 @@ public class AdminUserService {
|
|||||||
/** The target admin; a non-super caller may not manage a super admin. */
|
/** The target admin; a non-super caller may not manage a super admin. */
|
||||||
private Map<String, Object> loadManaged(Long id, User caller) {
|
private Map<String, Object> loadManaged(Long id, User caller) {
|
||||||
Map<String, Object> user = load(id);
|
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(
|
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");
|
throw superRequired("Only a super admin can manage a super admin");
|
||||||
}
|
}
|
||||||
return user;
|
return user;
|
||||||
|
|||||||
+3
-2
@@ -27,8 +27,9 @@ public final class AdminModuleAccess {
|
|||||||
List<Map<String, Object>> modules = ent.getBody() != null ? ent.getBody() : List.of();
|
List<Map<String, Object>> modules = ent.getBody() != null ? ent.getBody() : List.of();
|
||||||
return modules.stream()
|
return modules.stream()
|
||||||
.filter(m -> m.get("name") instanceof String name && wanted.contains(name))
|
.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(m -> m.get("roles") instanceof List<?> roles && roles.stream()
|
||||||
.anyMatch(r -> r instanceof Map<?, ?> rm && rm.get("id") instanceof Number n
|
.map(r -> r instanceof Map<?, ?> rm ? rm.get("id") : r)
|
||||||
&& roleIds.contains(n.longValue())));
|
.anyMatch(id -> id instanceof Number n && roleIds.contains(n.longValue())));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+7
@@ -500,6 +500,13 @@ class AdminUserServiceTest {
|
|||||||
verify(auth, never()).resetAdminPassword(anyString(), anyString());
|
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
|
@Test
|
||||||
void superCaller_canManageSuperAdminTarget() {
|
void superCaller_canManageSuperAdminTarget() {
|
||||||
callerIsSuper();
|
callerIsSuper();
|
||||||
|
|||||||
+10
@@ -32,6 +32,16 @@ class AdminModuleAccessTest {
|
|||||||
Map.of("name", "BO_EMPTY"))));
|
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
|
@Test
|
||||||
void roleGrantedModule_isGranted() {
|
void roleGrantedModule_isGranted() {
|
||||||
assertThat(AdminModuleAccess.grantedAny(rest, Set.of(6L), "BO_LEADS")).isTrue();
|
assertThat(AdminModuleAccess.grantedAny(rest, Set.of(6L), "BO_LEADS")).isTrue();
|
||||||
|
|||||||
Reference in new issue
Block a user