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:
1 parent
7d29d89b3f
commit
998e04b844
12 files changed
+122
-10
No files matched your search
+9
-1
@@ -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;
|
||||
}
|
||||
}
|
||||
+20
@@ -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"));
|
||||
|
||||
Reference in new issue
Block a user