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
@@ -45,31 +45,25 @@ public class CompanyService {
}
/**
* 公司目录(设计文档 §2.3):超管看全部,公司内管理员只看自己那一家。
* 公司目录(设计文档 §2.3):<b>仅超管读写</b>,非超管一律 403(spec §12 需求 2——
* 除了超管都不能操作公司配置,读写都不行)。「用户管理」页的公司下拉也<b>只超管可见</b>:
* Task 2 起前端按角色剪枝、非超管根本不发这个请求,这里再兜一道服务端闸门。
*
* <p>读口径必须和写口径一起收窄——只把写拦成超管、列表仍返回全部公司,等于把别家公司的
* 名字泄露给本公司管理员。管理员没有公司归属时是拒绝,不是"那就不过滤"。</p>
*
* <p>这里<b>不查菜单权限</b>:{@code admin_companies} 那道闸门管的是「公司管理」这个页面
* 出不出现在侧边栏,而本方法同时是「用户管理」页公司下拉的数据源,两者不是一回事。
* 收窄靠 role + companyId 已经到位,本公司管理员能读到的那一行就是他在登录响应和
* /current-user 里本来就拿着的那个自己的公司名,菜单闸门在这里多守不住一个字节。</p>
* <p>闸门判<b>角色</b>、不判菜单授权:{@code admin_companies} 那道菜单闸门挂在
* {@code user_column_permission} 上,2026-10-04 12:3x 就是因为它一笔授权都没发,
* 让 15 个公司内管理员一开「用户管理」就吃 403、被前端拦成"登录闪退"。
* 角色不会被"忘了授权"打穿,所以这里用 {@link #ensureSuperAdmin} 与写入口共用同一个出口。</p>
*/
public List<CompanyEntity> listVisible(AdminUserEntity operator) {
String role = adminAuthSupport.currentRole(operator);
if (role == null) {
throw new BusinessException(403, "需要管理员权限");
}
if ("super_admin".equals(role)) {
return companyMapper.selectList(new LambdaQueryWrapper<CompanyEntity>()
.orderByAsc(CompanyEntity::getId));
}
Long ownCompanyId = operator.getCompanyId();
if (ownCompanyId == null) {
throw new BusinessException(403, "当前账号没有所属公司,无法查看公司目录");
}
// 读写都只超管(spec §2 的 A 类):判角色、不判菜单授权——
// 12:3x 已经证明"把可见性挂在 user_column_permission 上"会因一笔授权没发而全员 403。
ensureSuperAdmin(operator);
return companyMapper.selectList(new LambdaQueryWrapper<CompanyEntity>()
.eq(CompanyEntity::getId, ownCompanyId));
.orderByAsc(CompanyEntity::getId));
}
/**
@@ -77,7 +71,7 @@ public class CompanyService {
*
* <p><b>这个方法不做权限判断</b>:它只是把调用方已经拿在手里的那些 id 换成名字,
* 谁能看到哪些行由调用方的查询范围决定(同 {@code AdminUserService.loadCreatorMap} 的口径)。
* 要给前端列公司目录请走 {@link #listVisible},它才按角色和公司收窄。</p>
* 要给前端列公司目录请走 {@link #listVisible},它才做超管闸门(非超管一律 403)。</p>
*/
public Map<Long, String> namesByIds(Collection<Long> companyIds) {
if (companyIds == null || companyIds.isEmpty()) {
@@ -197,7 +191,7 @@ public class CompanyService {
private void ensureSuperAdmin(AdminUserEntity operator) {
if (!"super_admin".equals(adminAuthSupport.currentRole(operator))) {
throw new BusinessException(403, "只有超级管理员可以新增或修改公司");
throw new BusinessException(403, "只有超级管理员可以查看或修改公司配置");
}
}
}
@@ -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)