feat(公司配置): 公司目录读侧收成角色内在闸门,非超管一律403

This commit is contained in:
2026-10-04 19:42:59 +08:00
parent 8750f3dd83
commit 898a8e2258
3 changed files with 35 additions and 50 deletions
@@ -46,8 +46,8 @@ class CompanyReadPathMenuGateTest {
.filter(line -> line.contains("ensureSuperAdmin(operator);"))
.collect(Collectors.toList());
assertThat(guards)
.as("create 与 update 两处写入口的超管闸门 must 保留")
.hasSize(2);
.as("create / update / listVisible 三处的超管闸门 must 保留")
.hasSize(3);
}
/** 取样集非空自检:扫描一旦 0 命中,上面几条就恒绿,等于没护栏。 */
@@ -28,9 +28,10 @@ import static org.mockito.Mockito.when;
/**
* 公司目录(设计文档 §2.3)的可见范围与写入边界。
*
* <p>读口径与写口径必须一起收窄:只把写拦成超管、列表仍返回全部公司,等于把别家公司的
* 名字泄露给本公司管理员。收窄靠 role + companyId,不靠菜单权限(见
* {@link CompanyReadPathMenuGateTest} 钉住的那条)。</p>
* <p>读写共用同一道<b>角色</b>闸门:非超管(含公司内管理员)一律 403 —— spec §12 需求 2 的口径是
* 「除了超管都不能操作公司配置,读写都不行」。收窄只靠 role,不靠 companyId、更不靠菜单权限:
* 挂在 {@code admin_companies} 的菜单授权上会因"一笔授权没发"而全员 403(2026-10-04 12:3x),
* "公司服务不查菜单"这条由 {@link CompanyReadPathMenuGateTest} 实跑钉住。</p>
*/
class CompanyServiceDirectoryTest {
@@ -118,16 +119,12 @@ class CompanyServiceDirectoryTest {
}
@Test
@DisplayName("公司内管理员的目录只查自己公司:读口径必须跟写口径一起收窄")
void companyScopedAdminDirectoryIsFilteredToOwnCompany() {
when(companyMapper.selectList(any())).thenReturn(List.of(company(2, "福州数富", 1)));
List<CompanyEntity> visible = companyService.listVisible(operator("admin", 2L));
assertThat(visible).hasSize(1);
LambdaQueryWrapper<CompanyEntity> query = capturedSelectList();
assertThat(query.getSqlSegment()).contains("id");
assertThat(query.getParamNameValuePairs()).containsValue(2L);
@DisplayName("公司内管理员读不到公司目录:读写都只超管(spec §12 需求 2)")
void companyScopedAdminCannotReadDirectory() {
assertThatThrownBy(() -> companyService.listVisible(operator("admin", 2L)))
.isInstanceOf(BusinessException.class)
.hasMessageContaining("公司");
verify(companyMapper, never()).selectList(any());
}
@Test
@@ -149,27 +146,21 @@ class CompanyServiceDirectoryTest {
}
/**
* 回归用例(2026-10-04 生产事故):「用户管理」页的公司下拉也走这个读端点,
* 而 admin_companies 菜单 V143 建了行却一笔授权都没发,15 个公司内管理员因此全被 403,
* 前端把 403 当未登录整页踢回登录页,症状看起来是"登录闪退"。
*
* <p>菜单权限管的是「公司管理」这个页面出不出现在侧边栏,不是这份数据谁能读。
* 读端点挂菜单闸门等于把"页面可见性"当"数据可见性"用;收窄靠 role + companyId 就够了,
* 何况公司名在登录响应和 /current-user 里本来就回带,这道闸门一个字节都没多守住。</p>
* 回归用例(2026-10-04 12:3x 生产事故)的**新形态**:当年挂 403 的是菜单授权(一笔都没发 ⇒ 全员闪退),
* 现在挂 403 的是角色。区别在于角色不会被"忘了授权"打穿,但**前端必须自己不发这个请求**
* (见 Task 2 与 tests/company-config-entry-super-only.test.ts),否则非超管照样吃 403。
*/
@Test
@DisplayName("公司内管理员读到本公司那一行:读端点不查 admin_companies 菜单")
void companyScopedAdminReadsOwnCompanyWithoutTheMenuGrant() {
when(companyMapper.selectList(any())).thenReturn(List.of(company(2, "福州数富", 1)));
List<CompanyEntity> visible = companyService.listVisible(operator("admin", 2L));
assertThat(visible).hasSize(1);
assertThat(capturedSelectList().getParamNameValuePairs()).containsValue(2L);
@DisplayName("非超管读公司目录只认角色,不认菜单授权(菜单那条已经不可能再伤到人)")
void directoryReadGateIsRoleIntrinsicNotMenuBased() {
assertThatThrownBy(() -> companyService.listVisible(operator("admin", 2L)))
.isInstanceOf(BusinessException.class)
.hasMessageContaining("超级管理员");
verify(companyMapper, never()).selectList(any());
}
@Test
@DisplayName("新增公司仅超管:公司内管理员读得到目录也不能建第二家公司")
@DisplayName("新增公司仅超管:公司内管理员读不到目录也建不了公司")
void companyAdminCannotCreate() {
assertThatThrownBy(() -> companyService.create(operator("admin", 2L), request("福州数富", null, null)))
.isInstanceOf(BusinessException.class)