feat: admin auth, users and roles (change 2) #3

Merged
ngqile merged 19 commits from add-admin-auth-users-roles into main 2026-11-26 14:40:32 +00:00
4 changed files with 22 additions and 3 deletions
Showing only changes of commit 69e2b4eb7b - Show all commits

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();