refactor(租户): 站点超管判定收成唯一静态出口,看板补第四道门

"super_admin 才算平台超管"这句判断此前在 src/main 里被手抄过 19 遍,2026-10-07 定的
「company_super_admin 在业务数据面同权、只在管理动作收窄」要落地就得一次改到每一处,
漏一处的表现是某一页静默 403 或静默多放行,两边都不报错。现在口径只在
DeviceSessionPolicy.isSuper 比一次:

- 出口做成静态谓词而不是 AdminAuthSupport 实例方法——那 18 支测试里 mock 的是
  AdminAuthSupport,新增实例方法不会被桩到,默认 false 会把范围静默改窄;
- 12 个文件的 15 处调用点改为 DeviceSessionPolicy.isSuper(currentRole(...)),
  DataReadScope/AdminAuthSupport 只留一个委托包装;
- 「客户端任务管理」五个 handler 的成员集计算折进 DataReadScope.superAdminScopedUserIds,
  并在 CompanyIdParamGoesThroughReadScopeTest 里补成第四道门(正反探针各一,
  判据不会因"抽进私有方法"而失明);
- 护栏 SuperAdminPredicateSingleOutletTest 双向钉:出口定义==1、包装==1、调用点==16,
  且 banned 掉实例方法形状(自检断言保证它真能红)。

今天口径仍只认 super_admin,这一层是接缝不是开关。Java 全量在本批收口后的那棵树实测 4032 例
0 红 7 skip(7 支是要本地凭据/夹具的回放与实况探测类,历来 skip)。

一并落在这支的文件(同一文件只能进一次提交,故与同批的归属收口混装):ImageVideo/InvalidAsin/
AdminDeviceLog/AdminTaskCenter/DedupeTotalData/AdminShopDataCrawlTasks/AdminShopDataDuplicateCheck/
QueryAsin/ShopManage/SkipPriceAsin 这 10 支控制器的请求身份改走会话出口见 cp-item7 的说明;
PermissionMenuService 还删掉了 create/update/delete 三个"operator 传 null 即跳过归属闸门"的
无操作者写重载(src/main 无人调用),判据在 MenuWriteOutletOperatorTest。
This commit is contained in:
2026-10-07 20:01:30 +08:00
parent 7c7a7f2665
commit de2bf05ab7
21 changed files with 487 additions and 137 deletions
@@ -22,11 +22,14 @@ public final class DeviceSessionPolicy {
private DeviceSessionPolicy() {
}
/** {@code super_admin} 的字面量在 src/main 只允许出现在这一处;比较口径见 {@link #isSuper(String)}。 */
public static final String SUPER_ADMIN = "super_admin";
/** 计算用户管理角色:super_admin / company_super_admin / admin / null(含老数据 role 为空时按 created_by_id 推断)。 */
public static String resolveRole(String role, Integer isAdmin, Long createdById) {
String storedRole = role == null ? "" : role.trim().toLowerCase();
if ("super_admin".equals(storedRole)) {
return "super_admin";
if (SUPER_ADMIN.equals(storedRole)) {
return SUPER_ADMIN;
}
if ("company_super_admin".equals(storedRole)) {
// 公司超级管理员(2026-10-05):单家公司内的最高账号,数据只看本公司,由站点
@@ -38,14 +41,31 @@ public final class DeviceSessionPolicy {
}
boolean isAdminFlag = isAdmin != null && isAdmin == 1;
if (storedRole.isEmpty() && isAdminFlag) {
return createdById == null ? "super_admin" : "admin";
return createdById == null ? SUPER_ADMIN : "admin";
}
return null;
}
/**
* 站点超管判定的<b>唯一</b>出口:角色字符串只在这里比一次。
*
* <p>此前这句判断在 {@code src/main} 里被重写过 19 遍(16 处 {@code "super_admin".equals(currentRole(x))}
* 加 3 处连老数据推断一起手抄的副本)。重写本身不是 bug,它是<b>改不动</b>才是 bug:
* 2026-10-07 定的「{@code company_super_admin} 在业务数据面与 {@code super_admin} 同权、
* 只在管理动作上收窄」要落地,就得一次改到这 19 处里的每一处——漏一处的表现是某一页
* 静默 403 或静默多放行,两边都不报错。</p>
*
* <p>今天的口径仍是<b>只认 super_admin</b>:这一层是接缝,不是已经切换的开关。
* 要把公司超管放进来,改这一个方法(以及 {@code DataReadScope} 里那个同名的包装),别改调用方
* (护栏 {@code SuperAdminPredicateSingleOutletTest} 会逐文件扫回来)。</p>
*/
public static boolean isSuper(String resolvedRole) {
return SUPER_ADMIN.equals(resolvedRole);
}
/** 是否超级管理员(互踢的唯一豁免角色)。 */
public static boolean isSuperAdmin(String role, Integer isAdmin, Long createdById) {
return "super_admin".equals(resolveRole(role, isAdmin, createdById));
return isSuper(resolveRole(role, isAdmin, createdById));
}
/** 从 JWT claims 中提取签名的设备号;缺失返回空串。 */
@@ -49,26 +49,6 @@ public class CompanyScopeSupport {
return companyId;
}
/**
* 后台自动写入路径(货源 chunk 回传、品牌补测调度器)专用的宽容版:账号已经不存在时返回 null,
* 而不是像 {@link #requireCompanyId(Long)} 那样抛。
*
* <p>这两个入口的锚点是 {@code biz_file_task.user_id},而账号是**物理删**的:2026-10-05 生产只读
* 取证,2026-06 以来的 716 条任务里 108 条的 user_id 在 users 里查不到(货源 174 条里 2 条)。
* 在这里抛等于让整单采集提交失败——比归属失真更糟。调用方拿到 null 必须记一条可巡检的日志、
* 并放弃这批写入,不许把 null 传进实体让 DEFAULT 1 静默兜住。</p>
*/
public Long companyIdOrNull(Long userId) {
if (userId == null || userId <= 0) {
return null;
}
AdminUserEntity user = adminUserMapper.selectById(userId);
if (user == null || user.getCompanyId() == null || user.getCompanyId() <= 0) {
return null;
}
return user.getCompanyId();
}
/** 行级归属闸门:跨公司写/删的第一道,消息由调用方给(前端按它显示,别在这里编文案)。 */
public void assertSameCompany(Long operatorCompanyId, Long rowCompanyId, String deniedMessage) {
if (rowCompanyId == null || !rowCompanyId.equals(operatorCompanyId)) {
@@ -2,6 +2,7 @@ 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.common.security.DeviceSessionPolicy;
import com.nanri.aiimage.modules.permission.support.UserDataScopeSupport;
import java.util.List;
import lombok.RequiredArgsConstructor;
@@ -17,15 +18,38 @@ import org.springframework.stereotype.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;
/**
* 业务层用的角色判定包装,口径落在 {@link DeviceSessionPolicy#isSuper(String)};
* 本类内部三处收窄分支都走这个包装,不再各比一次(先改包装体,别改分支)。
*/
public boolean isPlatformSuperAdmin(AdminUserEntity operator) {
return DeviceSessionPolicy.isSuper(adminAuthSupport.currentRole(operator));
}
/**
* 公司 tab 的可见账号集:<b>只对站点超管收窄</b>,其余账号返回 {@code null}(= 不叠这一层)。
*
* <p>{@code null} 不是"没算出来",而是这条纪律的形状:非超管的可见集里含「靠分组可见」的行,
* 而分组行没有账号列可推公司,给它叠 {@code created_by_id IN (成员)} 会把那批行从每一个 tab 里
* <b>静默删掉</b>(不报错、不 403);它的公司维度本来就由会话独家决定(spec §4)。</p>
*
* <p>此前这个三元表达式在 {@code AdminTaskCenterController} 里逐字重复了 5 次、在
* {@code ShopManageController} 重复了 1 次。收敛成一处是因为它是安全谓词的一部分:
* 一处漏改的表现是某个 tab 突然看得到别家的行,而它不会红。</p>
*/
public List<Long> superAdminScopedUserIds(AdminUserEntity operator, Long requestedCompanyId) {
return isPlatformSuperAdmin(operator)
? readScope(operator, requestedCompanyId).userIdsOr(null)
: null;
}
public ReadScope readScope(AdminUserEntity operator, Long requestedCompanyId) {
Long companyId = requestedCompanyId != null && requestedCompanyId > 0 ? requestedCompanyId : null;
if (SUPER_ADMIN.equals(adminAuthSupport.currentRole(operator))) {
if (isPlatformSuperAdmin(operator)) {
if (companyId == null) {
return ReadScope.unfiltered();
}
@@ -59,7 +83,7 @@ public class DataReadScope {
* 请求里带什么都不算,与 {@code readScope} 非超管分支同一口径(tab 不是安全边界)。</p>
*/
public Long readCompanyIdOrOwn(AdminUserEntity operator, Long requestedCompanyId) {
if (SUPER_ADMIN.equals(adminAuthSupport.currentRole(operator))) {
if (isPlatformSuperAdmin(operator)) {
return requestedCompanyId != null && requestedCompanyId > 0 ? requestedCompanyId : null;
}
return companyScope.requireCompanyId(operator);
@@ -76,7 +100,7 @@ public class DataReadScope {
* 内置节点现在对任何管理员可改可删——这是控制方点名的口径,不是漏改。</p>
*/
public Long writeCompanyId(AdminUserEntity operator) {
return SUPER_ADMIN.equals(adminAuthSupport.currentRole(operator))
return isPlatformSuperAdmin(operator)
? null
: companyScope.requireCompanyId(operator);
}