fix(mobile-api): fail closed for admins outside the company model

An admin with no company who is not a super admin now gets 403
COMPANY_REQUIRED on every company-scoped request instead of seeing all
companies. The module check and the email detail treat only a real super
admin (no company and role 1) as super; a company user still holding
role 1 gets only what their other roles grant.

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-12-01 09:31:55 +08:00
1 parent 7d29d89b3f
commit 998e04b844
12 files changed
+122 -10

No files matched your search

@@ -87,7 +87,7 @@ public class AdminEmailService {
}
}
}
if (!isSuper(caller) && recipients.stream().anyMatch(r -> isSuperAdminAddress((String) r.get("recipient")))) {
if (!AdminCompanyScope.isSuper(caller) && recipients.stream().anyMatch(r -> isSuperAdminAddress((String) r.get("recipient")))) {
throw new CodedResponseStatusException(HttpStatus.FORBIDDEN, "Only a super admin can read this email", "SUPER_ADMIN_REQUIRED");
}
Map<String, Object> out = new LinkedHashMap<>();
@@ -125,10 +125,6 @@ public class AdminEmailService {
&& roles.stream().anyMatch(r -> Long.valueOf(SUPER_ADMIN_ROLE_ID).equals(AdminRoleService.idOf(r)));
}
private static boolean isSuper(User caller) {
return caller.getRoles() != null && caller.getRoles().stream().anyMatch(r -> r.getId() != null && r.getId() == SUPER_ADMIN_ROLE_ID);
}
private static String upstreamDate(String isoDate) {
try {
return LocalDate.parse(isoDate.trim()).format(UPSTREAM_DATE);
@@ -16,9 +16,12 @@ public class AdminCompanyScope {
&& caller.getRoles().stream().anyMatch(r -> r.getId() != null && r.getId() == SUPER_ADMIN_ROLE_ID);
}
/** Staff: their own company, whatever was requested. Super: the requested company, null = all companies. */
/** Staff: their own company, whatever was requested. Super: the requested company, null = all companies.
* An admin with no company who is not a super admin breaks the model and sees nothing (403). */
public Long scope(User caller, Long requestedCompanyId) {
return isSuper(caller) ? requestedCompanyId : caller.getCompanyId();
if (isSuper(caller)) return requestedCompanyId;
if (caller.getCompanyId() == null) throw coded(HttpStatus.FORBIDDEN, "No company assigned", "COMPANY_REQUIRED");
return caller.getCompanyId();
}
/** Writes need a concrete company: staff get their own, a super admin must choose one. */
@@ -351,7 +351,6 @@ public class AppAccessAspect {
return jwt;
}
private static final long SUPER_ADMIN_ROLE_ID = 1L;
private static final long INJECTED_ROLE_ADMIN_ID = 2L;
/** Admin paths: admin token, live and enabled session, and a granted BO module. Fails closed. */
@@ -376,7 +375,7 @@ public class AppAccessAspect {
// services-auth findUserByToken adds ROLE_ADMIN to every admin, so it never grants a module.
.filter(id -> id != INJECTED_ROLE_ADMIN_ID)
.collect(Collectors.toSet());
if (roleIds.contains(SUPER_ADMIN_ROLE_ID)) {
if (AdminCompanyScope.isSuper(admin)) {
return;
}
boolean granted = false;