feat(公司tab): 接线 密钥管理与用量统计——分组∩公司取交集,空集不退化

两页都从 controller 这一个唯一出口算范围:收了 company_id 的 handler 自己调
dataReadScope.readScope(operator, companyId),把算好的 ReadScope 交给 service;
service 里 scopedUserIds() 只做 companyScope.userIdsOr(base),不再出现第二个判公司的 if。
readScope 落在 controller 而非计划原文写的 service,是因为 Task 8 补的 G2 尺子
CompanyIdParamGoesThroughReadScopeTest 逐 controller 断言"收参数就得自己调出口并把结果当实参交出去"。

空集一律早退(交给 in(...) 会渲染成 IN () → 500),且两条"筛空"日志按超管/主管分开,
排障时看得出是筛空还是查空;非超管空页仍返回带分组下拉的空页,下拉不缩水。

新增 6 条行为用例(超管 group_id∩公司成员集、交集空早退且不查库、不传公司保持不筛、
主管锁定集只被收窄不放大)。分母 3716 → 3722,Failures/Errors 均 0。
FE 侧两页带 company_id 并把 company-tab-wiring 缺口清单从 6 页收到 4 页。
This commit is contained in:
2026-10-05 12:18:18 +08:00
parent df03dce9c1
commit baf8baef0b
11 changed files with 216 additions and 37 deletions
@@ -3,6 +3,8 @@ package com.nanri.aiimage.modules.usersecret.controller;
import com.nanri.aiimage.common.api.ApiResponse;
import com.nanri.aiimage.config.UserSecretProperties;
import com.nanri.aiimage.common.security.AdminAuthSupport;
import com.nanri.aiimage.common.service.DataReadScope;
import com.nanri.aiimage.common.service.ReadScope;
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
import com.nanri.aiimage.modules.usersecret.model.dto.AdminUserSecretQuery;
import com.nanri.aiimage.modules.usersecret.model.dto.UserApiSecretSaveRequest;
@@ -43,6 +45,7 @@ public class AdminUserApiSecretController {
private final UserApiSecretService userApiSecretService;
private final UserSecretProperties userSecretProperties;
private final AdminAuthSupport adminAuthSupport;
private final DataReadScope dataReadScope;
@GetMapping
@Operation(summary = "分页查询用户密钥", description = "一行一用户;keyword 匹配用户名,checkStatus 按行级状态筛选。主管只能看自己带的分组成员,超管看全量并可按分组筛选。")
@@ -51,6 +54,7 @@ public class AdminUserApiSecretController {
@Parameter(description = "关键字:用户名") @RequestParam(required = false) String keyword,
@Parameter(description = "行级状态筛选:passed/failed/incomplete/error/unknown") @RequestParam(required = false) String checkStatus,
@Parameter(description = "按数据权限分组筛选(仅超管生效)") @RequestParam(name = "group_id", required = false) Long groupId,
@Parameter(description = "按公司筛选(仅超管生效;非超管一律按会话公司裁剪,越界公司为空页)") @RequestParam(name = "company_id", required = false) Long companyId,
@Parameter(description = "页码") @RequestParam(defaultValue = "1") Long page,
@Parameter(description = "每页数量") @RequestParam(defaultValue = "15") Long pageSize) {
AdminUserEntity operator = adminAuthSupport.requireAdmin(request);
@@ -60,7 +64,9 @@ public class AdminUserApiSecretController {
query.setGroupId(groupId);
query.setPage(page);
query.setPageSize(pageSize);
return ApiResponse.success(userApiSecretService.adminPage(operator, query));
// 读范围只在 controller 这唯一出口算一次;service 拿到的是算好的范围,自己不判公司。
ReadScope companyScope = dataReadScope.readScope(operator, companyId);
return ApiResponse.success(userApiSecretService.adminPage(operator, query, companyScope));
}
@PostMapping("/{userId}/check")
@@ -2,6 +2,8 @@ package com.nanri.aiimage.modules.usersecret.controller;
import com.nanri.aiimage.common.api.ApiResponse;
import com.nanri.aiimage.common.security.AdminAuthSupport;
import com.nanri.aiimage.common.service.DataReadScope;
import com.nanri.aiimage.common.service.ReadScope;
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
import com.nanri.aiimage.modules.usersecret.model.dto.AdminUserSecretUsageQuery;
import com.nanri.aiimage.modules.usersecret.model.vo.AdminUserSecretUsagePageVo;
@@ -31,6 +33,7 @@ public class AdminUserSecretUsageController {
private final UserSecretUsageService userSecretUsageService;
private final AdminAuthSupport adminAuthSupport;
private final DataReadScope dataReadScope;
@GetMapping
@Operation(summary = "分页查询密钥用量",
@@ -43,6 +46,7 @@ public class AdminUserSecretUsageController {
@RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate,
@Parameter(description = "关键字:用户名") @RequestParam(required = false) String keyword,
@Parameter(description = "按数据权限分组筛选(仅超管生效)") @RequestParam(name = "group_id", required = false) Long groupId,
@Parameter(description = "按公司筛选(仅超管生效;非超管一律按会话公司裁剪,越界公司为空页)") @RequestParam(name = "company_id", required = false) Long companyId,
@Parameter(description = "页码") @RequestParam(defaultValue = "1") Long page,
@Parameter(description = "每页数量") @RequestParam(defaultValue = "15") Long pageSize) {
AdminUserEntity operator = adminAuthSupport.requireAdmin(request);
@@ -53,6 +57,8 @@ public class AdminUserSecretUsageController {
query.setGroupId(groupId);
query.setPage(page);
query.setPageSize(pageSize);
return ApiResponse.success(userSecretUsageService.adminPage(operator, query));
// 读范围只在 controller 这唯一出口算一次;service 拿到的是算好的范围,自己不判公司。
ReadScope companyScope = dataReadScope.readScope(operator, companyId);
return ApiResponse.success(userSecretUsageService.adminPage(operator, query, companyScope));
}
}
@@ -7,6 +7,7 @@ import com.nanri.aiimage.modules.admin.spi.UserSecretCleanupPort;
import com.nanri.aiimage.common.security.ShopCredentialCryptoService;
import com.nanri.aiimage.config.UserSecretProperties;
import com.nanri.aiimage.common.security.AdminAuthSupport;
import com.nanri.aiimage.common.service.ReadScope;
import com.nanri.aiimage.modules.notification.service.NotificationDispatchService;
import com.nanri.aiimage.modules.notification.service.NotificationService;
import com.nanri.aiimage.common.mapper.AdminUserMapper;
@@ -314,8 +315,9 @@ public class UserApiSecretService implements UserSecretCleanupPort {
* 先按关键字圈定用户,再全量聚合、行级状态筛选、按最近更新时间倒序后内存分页
* (当前规模为用户数×3,内存聚合成本可控;量级上来后可改为 GROUP BY 下推)。
*/
/** 后台分页查询:主管(admin)只看自己带的「数据权限分组」成员;超管看全量、可按分组筛选。 */
public AdminUserSecretPageVo adminPage(AdminUserEntity operator, AdminUserSecretQuery query) {
/** 后台分页查询:主管(admin)只看自己带的「数据权限分组」成员;超管看全量、可按分组与公司筛选。 */
public AdminUserSecretPageVo adminPage(AdminUserEntity operator, AdminUserSecretQuery query,
ReadScope companyScope) {
AdminUserSecretQuery safeQuery = query == null ? new AdminUserSecretQuery() : query;
long page = safeQuery.getPage() == null || safeQuery.getPage() < 1 ? 1L : safeQuery.getPage();
long pageSize = safeQuery.getPageSize() == null || safeQuery.getPageSize() < 1
@@ -323,18 +325,19 @@ public class UserApiSecretService implements UserSecretCleanupPort {
boolean superAdmin = "super_admin".equals(adminAuthSupport.currentRole(operator));
Long requestedGroupId = safeQuery.getGroupId();
// 组隔离:主管强制锁定自己带的分组(组长本人 + 名下子账户);超管按 group_id 筛选(空 = 全部)。
List<Long> scopedUserIds = null;
if (superAdmin) {
if (requestedGroupId != null && requestedGroupId > 0) {
scopedUserIds = adminGroupMapper.selectUserIdsByGroupId(requestedGroupId);
}
} else {
scopedUserIds = userDataScopeSupport.resolveVisibleUserIds(operator.getId());
if (scopedUserIds.isEmpty()) {
// 组隔离(主管锁定自己带的分组、超管按 group_id 筛)叠上公司 tab:两条维度取**交集**,
// 公司 tab 只能收窄,绝不放大分组看不到的行;范围本身由 controller 经唯一出口算好交下来。
List<Long> scopedUserIds = scopedUserIds(operator, requestedGroupId, companyScope);
// 空集必须早退:交给下面的 in(...) 会渲染成 IN () → SQL 语法错 → 500。
// 两条日志分开,排障时看得出是「筛空」(范围交集为空)还是「查空」(库里没记录)。
if (scopedUserIds != null && scopedUserIds.isEmpty()) {
if (superAdmin) {
log.info("[user-secret] 后台密钥列表:公司或分组筛后无匹配用户,返回空 operatorId={} groupId={}",
operator.getId(), requestedGroupId);
} else {
log.info("[user-secret] 后台密钥列表:主管名下无子账户,返回空 operatorId={}", operator.getId());
return emptyPageWithGroups(page, pageSize, groupOptions(operator, false));
}
return emptyPageWithGroups(page, pageSize, groupOptions(operator, superAdmin));
}
List<Long> ledGroupIds = superAdmin ? List.of() : userDataScopeSupport.listLedGroupIds(operator.getId());
@@ -399,6 +402,25 @@ public class UserApiSecretService implements UserSecretCleanupPort {
return vo;
}
/**
* 后台列表的可见用户集:组长锁定 ∩ 超管 group_id ∩ 公司 tab。
* <p>{@code null} = 不筛(超管既没选公司也没选分组,今天的行为);<b>空集由调用方早退</b>,
* 不许交给 {@code LambdaQueryWrapper.in(...)}。交集只在 {@link ReadScope#userIdsOr(java.util.List)}
* 这一处发生——service 里不许再写第二个判公司的 if。
*/
List<Long> scopedUserIds(AdminUserEntity operator, Long requestedGroupId, ReadScope companyScope) {
boolean superAdmin = "super_admin".equals(adminAuthSupport.currentRole(operator));
List<Long> base = null;
if (superAdmin) {
if (requestedGroupId != null && requestedGroupId > 0) {
base = adminGroupMapper.selectUserIdsByGroupId(requestedGroupId);
}
} else {
base = userDataScopeSupport.resolveVisibleUserIds(operator.getId());
}
return companyScope.userIdsOr(base);
}
/** 分组筛选项:超管=全部;主管=自己带的分组。 */
private List<AdminUserSecretPageVo.GroupOptionVo> groupOptions(AdminUserEntity operator, boolean superAdmin) {
List<ShopManageGroupEntity> groups = superAdmin
@@ -2,6 +2,7 @@ package com.nanri.aiimage.modules.usersecret.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nanri.aiimage.common.security.AdminAuthSupport;
import com.nanri.aiimage.common.service.ReadScope;
import com.nanri.aiimage.common.mapper.AdminUserMapper;
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
import com.nanri.aiimage.modules.permission.support.UserDataScopeSupport;
@@ -90,7 +91,8 @@ public class UserSecretUsageService {
* 隔离规则同密钥列表:主管只看自己带的「数据权限分组」成员,超管看全量并可按分组筛选;
* 先全量聚合、再按合计次数倒序内存分页(当前规模可控,量级上来后可改 GROUP BY 下推)。
*/
public AdminUserSecretUsagePageVo adminPage(AdminUserEntity operator, AdminUserSecretUsageQuery query) {
public AdminUserSecretUsagePageVo adminPage(AdminUserEntity operator, AdminUserSecretUsageQuery query,
ReadScope companyScope) {
AdminUserSecretUsageQuery safeQuery = query == null ? new AdminUserSecretUsageQuery() : query;
long page = safeQuery.getPage() == null || safeQuery.getPage() < 1 ? 1L : safeQuery.getPage();
long pageSize = safeQuery.getPageSize() == null || safeQuery.getPageSize() < 1
@@ -98,17 +100,18 @@ public class UserSecretUsageService {
boolean superAdmin = "super_admin".equals(adminAuthSupport.currentRole(operator));
Long requestedGroupId = safeQuery.getGroupId();
List<Long> scopedUserIds = null;
if (superAdmin) {
if (requestedGroupId != null && requestedGroupId > 0) {
scopedUserIds = adminGroupMapper.selectUserIdsByGroupId(requestedGroupId);
}
} else {
scopedUserIds = userDataScopeSupport.resolveVisibleUserIds(operator.getId());
if (scopedUserIds.isEmpty()) {
// 组隔离(主管锁定自己带的分组、超管按 group_id 筛)叠上公司 tab:取**交集**,公司 tab 只收窄。
List<Long> scopedUserIds = scopedUserIds(operator, requestedGroupId, companyScope);
// 空集必须早退:交给下面的 in(...) 会渲染成 IN () → SQL 语法错 → 500。
// 两条日志分开,排障时看得出是「筛空」(范围交集为空)还是「查空」(库里没记录)。
if (scopedUserIds != null && scopedUserIds.isEmpty()) {
if (superAdmin) {
log.info("[user-secret-usage] 用量统计:公司或分组筛后无匹配用户,返回空 operatorId={} groupId={}",
operator.getId(), requestedGroupId);
} else {
log.info("[user-secret-usage] 用量统计:主管名下无子账户,返回空 operatorId={}", operator.getId());
return emptyPage(page, pageSize, groupOptions(operator, false));
}
return emptyPage(page, pageSize, groupOptions(operator, superAdmin));
}
LambdaQueryWrapper<UserSecretUsageEntity> wrapper = new LambdaQueryWrapper<>();
@@ -207,6 +210,24 @@ public class UserSecretUsageService {
return vo;
}
/**
* 用量统计的可见用户集:组长锁定 ∩ 超管 group_id ∩ 公司 tab(与密钥列表同形)。
* <p>{@code null} = 不筛(超管既没选公司也没选分组);<b>空集由调用方早退</b>,不许交给
* {@code LambdaQueryWrapper.in(...)}。交集只在 {@link ReadScope#userIdsOr(java.util.List)} 这一处发生。
*/
List<Long> scopedUserIds(AdminUserEntity operator, Long requestedGroupId, ReadScope companyScope) {
boolean superAdmin = "super_admin".equals(adminAuthSupport.currentRole(operator));
List<Long> base = null;
if (superAdmin) {
if (requestedGroupId != null && requestedGroupId > 0) {
base = adminGroupMapper.selectUserIdsByGroupId(requestedGroupId);
}
} else {
base = userDataScopeSupport.resolveVisibleUserIds(operator.getId());
}
return companyScope.userIdsOr(base);
}
/** 本页行补用户名与分组名(各一次批量查询)。 */
private void fillRowMeta(List<AdminUserSecretUsageRowVo> pageItems) {
List<Long> userIds = pageItems.stream()