fix(mobile-api): stop admins widening their own access
- A non-super admin cannot change the roles on their own account (403 OWN_ROLES).
- ROLE_ADMIN and ROLE_MEMBER cannot be assigned (400 UNKNOWN_ROLE, as if absent).
- A non-super admin cannot change the grants of a role they hold (403 OWN_ROLE);
this replaces the BO_ROLES lock-out check, which it makes unreachable.
- POST /api/admin/users/{id}/changePassword is removed; the emailed reset stays.
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
816c8bcb5e
commit
4c48ebea2e
9 files changed
+114
-70
No files matched your search
-1
@@ -15,7 +15,6 @@ public class RestApiSystemServiceConstants {
|
||||
public static final String API_REST_SYSTEM_MODULE_ADD_MODULE_ROLE = "/module/addModuleRole/{moduleId}/{roleId}";
|
||||
public static final String API_REST_SYSTEM_MODULE_DELETE_MODULE_ROLE = "/module/deleteModuleRole/{moduleId}/{roleId}";
|
||||
public static final String API_REST_SYSTEM_USER_SAVE = "/user/save";
|
||||
public static final String API_REST_SYSTEM_USER_UPDATE_USER_PASSWORD = "/user/updateUserPassword";
|
||||
public static final String API_REST_SYSTEM_USER_FIND_EXIST_BY_USERNAME = "/user/findExistByUsername/{username}";
|
||||
public static final String API_REST_SYSTEM_USER_FIND_BY_USERNAME_WITH_PASSWORD = "/user/findByUsernameWithPassword/{username}";
|
||||
public static final String API_REST_SYSTEM_USER_UPDATE_MEMBER_PHONE_VERIFIED = "/user/updatePhoneVerified";
|
||||
|
||||
-6
@@ -49,12 +49,6 @@ public class AdminUserController {
|
||||
return ResponseEntity.ok(adminUserService.update(id, request));
|
||||
}
|
||||
|
||||
@AppAuth
|
||||
@PostMapping("/users/{id}/changePassword")
|
||||
public ResponseEntity<Map<String, Object>> changePassword(@PathVariable Long id, @RequestBody Map<String, String> body) {
|
||||
return ResponseEntity.ok(Map.of("updated", adminUserService.changePassword(id, body.get("password"))));
|
||||
}
|
||||
|
||||
@AppAuth
|
||||
@PostMapping("/users/{id}/resetPassword")
|
||||
public ResponseEntity<Map<String, Object>> resetPassword(@PathVariable Long id) {
|
||||
|
||||
+5
-14
@@ -22,7 +22,6 @@ import java.util.Set;
|
||||
public class AdminAclService {
|
||||
private static final ParameterizedTypeReference<List<Map<String, Object>>> MAP_LIST = new ParameterizedTypeReference<>() {};
|
||||
private static final String AREA = "BO";
|
||||
private static final String ROLES_MODULE = "BO_ROLES";
|
||||
private static final long SUPER_ADMIN_ROLE_ID = 1L;
|
||||
private static final long ROLE_ADMIN_ID = 2L;
|
||||
|
||||
@@ -45,6 +44,11 @@ public class AdminAclService {
|
||||
User caller = adminAuthenticationService.loadUserByToken(HttpReqRespUtils.getJwtToken());
|
||||
if (caller == null) throw error(HttpStatus.UNAUTHORIZED, "Not signed in", "UNAUTHORIZED");
|
||||
|
||||
boolean isSuper = caller.getRoles() != null && caller.getRoles().stream().anyMatch(r -> r.getId() != null && r.getId() == SUPER_ADMIN_ROLE_ID);
|
||||
if (!isSuper && caller.getRoles() != null && caller.getRoles().stream().anyMatch(r -> r.getId() != null && r.getId() != ROLE_ADMIN_ID && r.getId().equals(roleId))) {
|
||||
throw error(HttpStatus.FORBIDDEN, "You cannot change the access of a role you hold", "OWN_ROLE");
|
||||
}
|
||||
|
||||
List<Map<String, Object>> modules = modules();
|
||||
Set<Long> known = new HashSet<>();
|
||||
Set<Long> current = new HashSet<>();
|
||||
@@ -54,7 +58,6 @@ public class AdminAclService {
|
||||
}
|
||||
Set<Long> wanted = new HashSet<>(moduleIds);
|
||||
if (!known.containsAll(wanted)) throw error(HttpStatus.BAD_REQUEST, "Unknown back-office module", "UNKNOWN_MODULE");
|
||||
requireNoLockout(caller, roleId, wanted, modules);
|
||||
|
||||
// ponytail: not atomic, adds run before deletes and a failure midway leaves a partial grant; a repeated PUT converges. Upgrade: one services-system endpoint that replaces grants in a transaction.
|
||||
for (Long id : wanted) if (!current.contains(id)) call(RestApiSystemServiceConstants.API_REST_SYSTEM_MODULE_ADD_MODULE_ROLE, id, roleId);
|
||||
@@ -62,18 +65,6 @@ public class AdminAclService {
|
||||
return getGrantTree(roleId);
|
||||
}
|
||||
|
||||
/** A non-super caller must still reach BO_ROLES through one of their roles (role 2 is on every admin and grants nothing). */
|
||||
private static void requireNoLockout(User caller, Long roleId, Set<Long> wanted, List<Map<String, Object>> modules) {
|
||||
if (caller.getRoles() == null || caller.getRoles().stream().anyMatch(r -> r.getId() != null && r.getId() == SUPER_ADMIN_ROLE_ID)) return;
|
||||
Set<Long> held = new HashSet<>();
|
||||
caller.getRoles().stream().map(r -> r.getId()).filter(i -> i != null && i != ROLE_ADMIN_ID).forEach(held::add);
|
||||
if (!held.contains(roleId)) return;
|
||||
Map<String, Object> rolesModule = modules.stream().filter(m -> ROLES_MODULE.equals(m.get("name"))).findFirst().orElse(null);
|
||||
if (rolesModule == null) return;
|
||||
boolean stillGranted = held.stream().anyMatch(r -> r.equals(roleId) ? wanted.contains(AdminRoleService.idOf(rolesModule)) : grantedTo(rolesModule, r));
|
||||
if (!stillGranted) throw error(HttpStatus.BAD_REQUEST, "This change would remove your own access to role management", "ACL_LOCKOUT");
|
||||
}
|
||||
|
||||
/** Hidden roles (ROLE_MEMBER, ROLE_ADMIN) look absent, as in the role list. */
|
||||
private void requireVisibleRole(Long roleId) {
|
||||
if (AdminRoleService.isHidden(adminRoleService.find(roleId))) {
|
||||
|
||||
+18
-11
@@ -17,8 +17,10 @@ import org.springframework.util.StringUtils;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Admin users for the portal. Users travel as maps so fields the core {@link User} lacks (status) survive
|
||||
@@ -117,6 +119,7 @@ public class AdminUserService {
|
||||
User caller = caller();
|
||||
Map<String, Object> user = loadManaged(id, caller);
|
||||
requireSuperToGrant(caller, r.roles());
|
||||
requireOwnRolesUnchanged(caller, id, user, r.roles());
|
||||
if (r.enabled() != null && !r.enabled() && caller.getId() != null && caller.getId().equals(id)) {
|
||||
throw error(HttpStatus.BAD_REQUEST, "You cannot deactivate your own account", "SELF_DEACTIVATION");
|
||||
}
|
||||
@@ -143,16 +146,6 @@ public class AdminUserService {
|
||||
return adminAuthenticationService.resetAdminPassword((String) user.get("username"), (String) user.get("email"));
|
||||
}
|
||||
|
||||
public int changePassword(Long id, String password) {
|
||||
loadManaged(id, caller());
|
||||
ResponseEntity<Integer> ent = restTemplateUtil.postForEntity(
|
||||
url(RestApiSystemServiceConstants.API_REST_SYSTEM_USER_UPDATE_USER_PASSWORD), Integer.class,
|
||||
Map.of("userId", id, "password", password == null ? "" : password));
|
||||
int updated = ent.getBody() == null ? 0 : ent.getBody();
|
||||
if (updated == 0) throw error(HttpStatus.BAD_REQUEST, "Password does not meet the password rules", "PASSWORD_RULES");
|
||||
return updated;
|
||||
}
|
||||
|
||||
private Map<String, Object> load(Long id) {
|
||||
Map<String, Object> user = get(RestApiSystemServiceConstants.API_REST_SYSTEM_USER_FIND_BY_ID, id);
|
||||
if (user == null || !"A".equals(user.get("type"))) throw error(HttpStatus.NOT_FOUND, "User not found", "USER_NOT_FOUND");
|
||||
@@ -179,6 +172,20 @@ public class AdminUserService {
|
||||
}
|
||||
}
|
||||
|
||||
/** A non-super admin may not change their own roles; hidden roles are not part of what they could change. */
|
||||
private static void requireOwnRolesUnchanged(User caller, Long id, Map<String, Object> stored, List<AdminRoleRef> requested) {
|
||||
if (requested == null || isSuper(caller) || caller.getId() == null || !caller.getId().equals(id)) return;
|
||||
Set<Long> current = new HashSet<>();
|
||||
if (stored.get("roles") instanceof List<?> l) {
|
||||
for (Object o : l) {
|
||||
if (o instanceof Map<?, ?> m && m.get("id") instanceof Number n && !AdminRoleService.isHidden((Map<String, Object>) m)) current.add(n.longValue());
|
||||
}
|
||||
}
|
||||
Set<Long> wanted = new HashSet<>();
|
||||
requested.forEach(x -> wanted.add(x.id()));
|
||||
if (!wanted.equals(current)) throw error(HttpStatus.FORBIDDEN, "You cannot change your own roles", "OWN_ROLES");
|
||||
}
|
||||
|
||||
private static CodedResponseStatusException superRequired(String reason) {
|
||||
return error(HttpStatus.FORBIDDEN, reason, "SUPER_ADMIN_REQUIRED");
|
||||
}
|
||||
@@ -189,7 +196,7 @@ public class AdminUserService {
|
||||
List<Map<String, Object>> out = new ArrayList<>();
|
||||
for (AdminRoleRef ref : refs) {
|
||||
out.add((stored == null ? List.<Map<String, Object>>of() : stored).stream()
|
||||
.filter(m -> m.get("id") instanceof Number n && ref.id() != null && n.longValue() == ref.id())
|
||||
.filter(m -> m.get("id") instanceof Number n && ref.id() != null && n.longValue() == ref.id() && !AdminRoleService.isHidden(m))
|
||||
.findFirst().orElseThrow(() -> error(HttpStatus.BAD_REQUEST, "Unknown role " + ref.id(), "UNKNOWN_ROLE")));
|
||||
}
|
||||
return out;
|
||||
|
||||
Reference in new issue
Block a user