feat(公司范围): DataReadScope 唯一读范围出口 + G2 扫 company_id 端点

- ReadScope/DataReadScope 落在 common/service:超管不选公司=不筛、选公司=成员集替换、
  选不存在公司=空页(不 403 不全量);非超管永远锁本公司且与分组可见集求交(只能收窄),
  传别家得空结果,没公司归属则炸
- G2 护栏逐文件扫"含 company_id 的 controller 必须走 readScope( 且显式 name=",
  带取样非空自检;白名单点名只放先于本计划存在的 AdminUserController,
  且只豁免 readScope 那一条、不豁免取样与显式 name=
This commit is contained in:
2026-10-05 03:03:06 +08:00
parent fb407bdaee
commit a5a9df650b
4 changed files with 273 additions and 0 deletions
@@ -0,0 +1,42 @@
package com.nanri.aiimage.common.service;
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
import com.nanri.aiimage.common.security.AdminAuthSupport;
import com.nanri.aiimage.modules.permission.support.UserDataScopeSupport;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
/**
* "操作者 + 请求里的 company_id → 该查哪批账号"的**唯一**出口(设计文档 §7)。
*
* <p>各页 service 一律走这里,不许自己写 if:一条越权旁路的成本是"这一页的过滤是假的",
* 而它不会报错。护栏 {@code CompanyIdParamGoesThroughReadScopeTest} 逐文件扫这条纪律。</p>
*/
@Service
@RequiredArgsConstructor
public class DataReadScope {
private static final String SUPER_ADMIN = "super_admin";
private final CompanyScopeSupport companyScope;
private final AdminAuthSupport adminAuthSupport;
private final UserDataScopeSupport userDataScopeSupport;
public ReadScope readScope(AdminUserEntity operator, Long requestedCompanyId) {
Long companyId = requestedCompanyId != null && requestedCompanyId > 0 ? requestedCompanyId : null;
if (SUPER_ADMIN.equals(adminAuthSupport.currentRole(operator))) {
if (companyId == null) {
return ReadScope.unfiltered();
}
return ReadScope.users(companyScope.memberUserIds(companyId));
}
// 非超管:一律按会话裁剪,前端传什么都不算(spec §4——tab 不是安全边界,这条才是)。
Long own = companyScope.requireCompanyId(operator);
if (companyId != null && !companyId.equals(own)) {
return ReadScope.users(List.of());
}
List<Long> base = userDataScopeSupport.resolveVisibleUserIds(operator.getId());
return ReadScope.users(ReadScope.users(companyScope.memberUserIds(own)).userIdsOr(base));
}
}
@@ -0,0 +1,48 @@
package com.nanri.aiimage.common.service;
import java.util.ArrayList;
import java.util.List;
/**
* 一次读请求的租户范围(设计文档 §7)。两种状态:不筛(超管没选公司 = 今天的行为)
* 与"限定到这批账号"。
*
* <p>{@link #userIdsOr(List)} 是它与各页**既有** `scopedUserIds` 唯一的接合点:
* base 传 null 表示"这页原本不筛",此时返回公司范围;两边都有值时求交,
* 因为公司 tab 只能**收窄**、不能放大组长本来看不到的行。</p>
*/
public final class ReadScope {
private final boolean unfiltered;
private final List<Long> userIds;
private ReadScope(boolean unfiltered, List<Long> userIds) {
this.unfiltered = unfiltered;
this.userIds = userIds;
}
public static ReadScope unfiltered() {
return new ReadScope(true, List.of());
}
public static ReadScope users(List<Long> ids) {
return new ReadScope(false, ids == null ? List.of() : List.copyOf(ids));
}
public boolean isUnfiltered() {
return unfiltered;
}
public List<Long> userIds() {
return userIds;
}
/** 空集 = 调用方应当直接返回空页(各页服务本来就有这条分支,照它写)。 */
public List<Long> userIdsOr(List<Long> base) {
if (unfiltered) return base;
if (base == null) return userIds;
List<Long> out = new ArrayList<>(base);
out.retainAll(userIds);
return out;
}
}