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

@@ -46,7 +46,7 @@ class AdminAuditLogServiceTest {
service = new AdminAuditLogService(rest, auth, new AdminCompanyScope());
http = mockStatic(HttpReqRespUtils.class);
http.when(HttpReqRespUtils::getJwtToken).thenReturn("tok");
when(auth.loadUserByToken("tok")).thenReturn(new User());
when(auth.loadUserByToken("tok")).thenReturn(superAdmin()); // all companies: these tests are about filter forwarding
when(rest.getURLforApiService(any())).thenAnswer(i -> i.getArgument(0));
}
@@ -102,4 +102,12 @@ class AdminAuditLogServiceTest {
assertThat(e.getReason()).isEqualTo("Audit event not found");
});
}
private static User superAdmin() {
User u = new User();
com.solvway.core.model.Role r = new com.solvway.core.model.Role();
r.setId(1L);
u.setRoles(java.util.List.of(r));
return u;
}
}
@@ -67,12 +67,14 @@ class AdminEmailServiceTest {
Role r = new Role();
r.setId(roleId);
caller.setRoles(List.of(r));
if (roleId != 1L) caller.setCompanyId(5L); // staff always belong to a company; only a super admin has none
when(auth.loadUserByToken("tok")).thenReturn(caller);
}
private static Map<String, Object> emailRow(long id, String to) {
Map<String, Object> m = new HashMap<>();
m.put("id", id);
m.put("companyId", 5L); // the staff caller's company
m.put("sender", "noreply@example.com");
m.put("subject", "Reset Admin Password");
m.put("body", BODY);
@@ -100,6 +102,7 @@ class AdminEmailServiceTest {
@Test
void list_sendsOnlyGivenFiltersNewestFirstAndConvertsDates() {
callerWithRole(1L); // a super admin with no company picked: no company filter
when(rest.postForEntity(eq(PAGE), any(ParameterizedTypeReference.class), any()))
.thenReturn(ResponseEntity.ok(Map.of("content", List.of(), "totalElements", 0)));
@@ -161,6 +164,23 @@ class AdminEmailServiceTest {
});
}
@Test
void detail_companyCallerHoldingRole1_isNotSuper() {
when(auth.loadUserByToken("tok")).thenAnswer(i -> {
User u = new User();
u.setId(1L);
Role r = new Role();
r.setId(1L);
u.setRoles(List.of(r));
u.setCompanyId(5L);
return u;
});
stubFind(7, emailRow(7, "root@example.com"));
stubUserByEmail("root@example.com", adminUser(1));
assertThatThrownBy(() -> service.detail(7L)).isInstanceOfSatisfying(CodedResponseStatusException.class,
e -> assertThat(e.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN));
}
@Test
void detail_superAdminRoleSentAsBareId_isStillProtected() {
stubFind(7, emailRow(7, "root@example.com"));
@@ -66,4 +66,20 @@ class AdminCompanyScopeTest {
assertStatus(() -> scope.requireSuper(user(5L, 1)), HttpStatus.FORBIDDEN);
assertStatus(() -> scope.requireSuper(user(null, 4)), HttpStatus.FORBIDDEN);
}
@Test
void companyLessNonSuper_isRefusedEverywhere() {
User orphan = user(null, 4);
for (Runnable call : List.<Runnable>of(
() -> scope.scope(orphan, null),
() -> scope.scope(orphan, 9L),
() -> scope.requireCompanyForCreate(orphan, 9L))) {
assertThatThrownBy(call::run).isInstanceOf(ResponseStatusException.class)
.satisfies(ex -> {
ResponseStatusException e = (ResponseStatusException) ex;
assertThat(e.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN);
assertThat(e.getBody().getProperties()).containsEntry("code", "COMPANY_REQUIRED");
});
}
}
}
@@ -278,6 +278,18 @@ class AppAccessAspectAdminTest {
}
}
@Test
void companyUserHoldingRole1_getsOnlyTheirOtherRolesModules() throws Throwable {
target(Target.class, "methodWins");
User staff = admin(1L, 5L); // role 5 holds BO_LEADS, not BO_USERS
staff.setCompanyId(5L);
when(guard.check(TOK, false)).thenReturn(staff);
try (MockedStatic<HttpReqRespUtils> h = mockStatic(HttpReqRespUtils.class)) {
http(h, TOK, "A");
expect(HttpStatus.FORBIDDEN);
}
}
@Test
void undeclaredHandler_isForbiddenForNonSuperAdmin() throws Throwable {
target(Undeclared.class, "handler");
@@ -140,6 +140,11 @@ class AppAccessAspectAuditTest {
u.getRoles().add(r);
}
when(guard.check(eq(TOK), any(Boolean.class))).thenReturn(u);
// A company user is never a super admin, so role 1 here is an ordinary role granted both test modules.
when(rest.getForEntity(any(), any(org.springframework.core.ParameterizedTypeReference.class), eq("BO")))
.thenReturn(org.springframework.http.ResponseEntity.ok(List.of(
Map.of("name", "BO_USERS", "roles", List.of(Map.of("id", 1))),
Map.of("name", "BO_ROLES", "roles", List.of(Map.of("id", 1))))));
return u;
}