feat(user-secret): 后台密钥管理/用量统计两菜单扩京东侧+列表按平台隔离(2026-11-22 用户口径「后台京东tab下也要加密钥管理和密钥统计量 数据不要弄过来 这边是京东新的」)

- AdminMenuPlatformScope.BOTH_MENU_KEYS 加 admin_user_secrets/admin_user_secret_usage(双侧清单与前端 platform-scope.ts 逐字同源,三个下发出口自动跟随;零迁移,菜单行 V115/V117 已在 columns 表)
- 两后台端点加 platform 参数:AdminPlatformScope.normalizeOrNull 归一(空白=不过滤、非法 400,与 listUsers 同形)
- 两 service 行后过滤:selectList 拿 rows 后按去重 userId 一次查 users.platform 再滤(不在 allowedUserIds 塞几千 id;platform 缺省旧行为逐字不变);groupOptions 平台筛(NULL/空白归一 amazon 再 equals,与铁律 33 分组闸同形)
- 判据:AdminMenuPlatformScopeTest 双侧清单逐字+AdminConsoleCurrentUserMenusPlatformTest jd 树含两页+PermissionMenuServiceConsolePageTest 京东公司超管过两页平台门+UserApiSecretServiceTest/UserSecretUsageServiceTest 各两支平台过滤新用例(六支点名 141 例全绿)

随下一 jar 窗上线;菜单归属前后端两份清单必须同窗发布
This commit is contained in:
2026-11-22 21:51:15 +08:00
parent 79b6f76654
commit 4819b243d6
12 changed files with 330 additions and 24 deletions
@@ -33,9 +33,15 @@ public final class AdminMenuPlatformScope {
/** 桌面端(app 树)京东菜单 key(与前端 JD_APP_MENU_KEYS 逐字一致,V209:组行 + 两工具子行)。 */
public static final Set<String> JD_APP_MENU_KEYS = Set.of("jd_tools", "jd-collect", "jd-publish");
/** 双侧共用账号管理页面;密钥管理与用量统计按 2026-11-18 截图口径收回亚马逊侧。 */
/**
* 双侧共用账号管理页面。密钥管理与用量统计(admin_user_secrets/admin_user_secret_usage)
* 按 2026-11-22 用户拍板扩回双侧(「后台京东 tab 下也要加密钥管理和密钥统计量」),
* 页面数据按 {@code users.platform} 过滤(AdminUserSecretQuery.platform),菜单归属与
* 数据隔离是两层,互不替代。
*/
public static final Set<String> BOTH_MENU_KEYS = Set.of(
"admin_users", "admin_group_manage", "admin_companies", "admin_columns");
"admin_users", "admin_group_manage", "admin_companies", "admin_columns",
"admin_user_secrets", "admin_user_secret_usage");
private AdminMenuPlatformScope() {
}
@@ -4,6 +4,7 @@ import com.nanri.aiimage.common.api.ApiResponse;
import com.nanri.aiimage.common.api.PageDefaults;
import com.nanri.aiimage.config.UserSecretProperties;
import com.nanri.aiimage.common.security.AdminAuthSupport;
import com.nanri.aiimage.common.service.AdminPlatformScope;
import com.nanri.aiimage.common.service.DataReadScope;
import com.nanri.aiimage.common.service.ReadScope;
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
@@ -53,13 +54,14 @@ public class AdminUserApiSecretController {
private final DataReadScope dataReadScope;
@GetMapping
@Operation(summary = "分页查询用户密钥", description = "一行一用户;keyword 匹配用户名,checkStatus 按行级状态筛选。主管只能看自己带的分组成员,超管看全量并可按分组筛选。company_id 只收窄可见集、不放大,跨公司查看仅超管。")
@Operation(summary = "分页查询用户密钥", description = "一行一用户;keyword 匹配用户名,checkStatus 按行级状态筛选。主管只能看自己带的分组成员,超管看全量并可按分组筛选。company_id 只收窄可见集、不放大,跨公司查看仅超管。platform 按后管当前平台 tab 过滤用户侧(2026-11-22 京东侧新增)。")
public ApiResponse<AdminUserSecretPageVo> page(
HttpServletRequest request,
@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 = "平台过滤:amazon/jd;空则不过滤(兼容旧调用)") @RequestParam(name = "platform", required = false) String platform,
@Parameter(description = "页码") @RequestParam(defaultValue = PageDefaults.FIRST_PAGE) Long page,
@Parameter(description = "每页数量") @RequestParam(defaultValue = PageDefaults.USER_LIST_PAGE_SIZE) Long pageSize) {
AdminUserEntity operator = adminAuthSupport.requireAdmin(request);
@@ -67,6 +69,8 @@ public class AdminUserApiSecretController {
query.setKeyword(keyword);
query.setCheckStatus(checkStatus);
query.setGroupId(groupId);
// 平台归一与用户管理(AdminUserService.listUsers)同形:空白=不过滤,非法值 400。
query.setPlatform(AdminPlatformScope.normalizeOrNull(platform));
query.setPage(page);
query.setPageSize(pageSize);
// 读范围只在 controller 这唯一出口算一次;service 拿到的是算好的范围,自己不判公司。
@@ -3,6 +3,7 @@ package com.nanri.aiimage.modules.usersecret.controller;
import com.nanri.aiimage.common.api.ApiResponse;
import com.nanri.aiimage.common.api.PageDefaults;
import com.nanri.aiimage.common.security.AdminAuthSupport;
import com.nanri.aiimage.common.service.AdminPlatformScope;
import com.nanri.aiimage.common.service.DataReadScope;
import com.nanri.aiimage.common.service.ReadScope;
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
@@ -48,6 +49,7 @@ public class AdminUserSecretUsageController {
@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 = "平台过滤:amazon/jd;空则不过滤(兼容旧调用)") @RequestParam(name = "platform", required = false) String platform,
@Parameter(description = "页码") @RequestParam(defaultValue = PageDefaults.FIRST_PAGE) Long page,
@Parameter(description = "每页数量") @RequestParam(defaultValue = PageDefaults.USER_LIST_PAGE_SIZE) Long pageSize) {
AdminUserEntity operator = adminAuthSupport.requireAdmin(request);
@@ -56,6 +58,8 @@ public class AdminUserSecretUsageController {
query.setEndDate(endDate);
query.setKeyword(keyword);
query.setGroupId(groupId);
// 平台归一与用户管理(AdminUserService.listUsers)同形:空白=不过滤,非法值 400。
query.setPlatform(AdminPlatformScope.normalizeOrNull(platform));
query.setPage(page);
query.setPageSize(pageSize);
// 读范围只在 controller 这唯一出口算一次;service 拿到的是算好的范围,自己不判公司。
@@ -16,6 +16,9 @@ public class AdminUserSecretQuery {
@Schema(description = "按数据权限分组筛选(超管可选;主管强制为自己带的分组)")
private Long groupId;
@Schema(description = "平台过滤:amazon/jd;空则不过滤(兼容旧调用)。后管按当前平台 tab 传。")
private String platform;
@Schema(description = "页码,从 1 开始")
private Long page = 1L;
@@ -18,6 +18,9 @@ public class AdminUserSecretUsageQuery {
private Long groupId;
/** 平台过滤:amazon/jd;空则不过滤(兼容旧调用)。 */
private String platform;
private Long page;
private Long pageSize;
@@ -12,6 +12,7 @@ import com.nanri.aiimage.config.AppearancePatentProperties;
import com.nanri.aiimage.config.SimilarAsinProperties;
import com.nanri.aiimage.common.security.AdminAuthSupport;
import com.nanri.aiimage.common.security.DeviceSessionPolicy;
import com.nanri.aiimage.common.service.AdminPlatformScope;
import com.nanri.aiimage.common.service.ReadScope;
import com.nanri.aiimage.modules.notification.service.NotificationDispatchService;
import com.nanri.aiimage.modules.notification.service.NotificationService;
@@ -55,8 +56,10 @@ import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
/**
* 用户 API 密钥服务端存储:按登录用户绑定、AES 加密落库,
@@ -359,6 +362,25 @@ public class UserApiSecretService implements UserSecretCleanupPort {
return module.get().channelOrDefault(stored, null);
}
/**
* 按模块顺序找「渠道登记为龙抬头且已存明文」的用户 LLM 密钥;没有返回空串。
*
* <p>代调型 AI 特性(京东上架尺码表补值、上架必填参数补值)密钥不出服务端,
* 都从这一处取钥:外观专利 → 货源查询的模块顺序,渠道不匹配或明文为空继续看
* 下一把。两个调用方各写一份这份顺序就是两份正本(B79-12 同形教训),渠道口径
* 一动必漏改一处。</p>
*/
public String findLongtaitouPlainValue(Long userId) {
for (UserSecretModule module : List.of(UserSecretModule.APPEARANCE_PATENT, UserSecretModule.SIMILAR_ASIN)) {
String key = findPlainValue(userId, module.key());
if (LlmChannel.LONGTAITOU.key().equals(findChannel(userId, module.key()))
&& key != null && !key.isBlank()) {
return key;
}
}
return "";
}
/**
* 按用户密钥所属渠道解析 LLM 主机:任务执行与检测共用同一口径。
* 用户没配密钥时返回模块全局配置主机,保证后续报错来自「没有密钥」而不是「主机为空」。
@@ -449,6 +471,8 @@ public class UserApiSecretService implements UserSecretCleanupPort {
boolean superAdmin = DeviceSessionPolicy.isSuper(adminAuthSupport.currentRole(operator));
Long requestedGroupId = safeQuery.getGroupId();
// 平台 tab(2026-11-22 京东侧新增):controller 已归一过,这里幂等再归一一次,service 不信任调用方。
String platformFilter = AdminPlatformScope.normalizeOrNull(safeQuery.getPlatform());
// 组隔离(主管锁定自己带的分组、超管按 group_id 筛)叠上公司 tab:两条维度取**交集**,
// 公司 tab 只能收窄,绝不放大分组看不到的行;范围本身由 controller 经唯一出口算好交下来。
List<Long> scopedUserIds = scopedUserIds(operator, requestedGroupId, companyScope);
@@ -466,7 +490,7 @@ public class UserApiSecretService implements UserSecretCleanupPort {
log.info("[user-secret] 后台密钥列表:主管可见集与公司范围交集为空(多为越界 company_id),返回空 operatorId={} companyScope={}",
operator.getId(), companyScopeSize);
}
return emptyPageWithGroups(page, pageSize, groupOptions(operator, superAdmin));
return emptyPageWithGroups(page, pageSize, groupOptions(operator, superAdmin, platformFilter));
}
List<Long> ledGroupIds = superAdmin ? List.of() : userDataScopeSupport.listLedGroupIds(operator.getId());
@@ -488,11 +512,18 @@ public class UserApiSecretService implements UserSecretCleanupPort {
if (allowedUserIds == null || allowedUserIds.isEmpty()) {
log.info("[user-secret] 后台密钥列表无匹配用户 keyword={} groupId={} operatorId={} role={}",
keyword, requestedGroupId, operator.getId(), superAdmin ? "super_admin" : "admin");
return emptyPageWithGroups(page, pageSize, groupOptions(operator, superAdmin));
return emptyPageWithGroups(page, pageSize, groupOptions(operator, superAdmin, platformFilter));
}
wrapper.in(UserApiSecretEntity::getUserId, allowedUserIds);
}
List<UserApiSecretEntity> rows = userApiSecretMapper.selectList(wrapper);
// 平台过滤走行后收窄(不在 allowedUserIds 里塞几千个 id);筛空后自然走到空页装配,日志只留观测。
int beforePlatformFilter = rows.size();
rows = filterRowsByUserPlatform(rows, platformFilter);
if (platformFilter != null && rows.size() != beforePlatformFilter) {
log.info("[user-secret] 后台密钥列表平台筛收窄 platform={} 行数 {}→{} operatorId={}",
platformFilter, beforePlatformFilter, rows.size(), operator.getId());
}
Map<Long, Map<String, UserApiSecretEntity>> grouped = new LinkedHashMap<>();
for (UserApiSecretEntity row : rows) {
@@ -525,9 +556,9 @@ public class UserApiSecretService implements UserSecretCleanupPort {
vo.setTotal(total);
vo.setPage(page);
vo.setPageSize(pageSize);
vo.setGroupOptions(groupOptions(operator, superAdmin));
log.info("[user-secret] 后台密钥列表 keyword={} statusFilter={} groupId={} role={} 聚合用户数={} 本页返回={}",
keyword, statusFilter, requestedGroupId, superAdmin ? "super_admin" : "admin", total, pageItems.size());
vo.setGroupOptions(groupOptions(operator, superAdmin, platformFilter));
log.info("[user-secret] 后台密钥列表 keyword={} statusFilter={} groupId={} platform={} role={} 聚合用户数={} 本页返回={}",
keyword, statusFilter, requestedGroupId, platformFilter, superAdmin ? "super_admin" : "admin", total, pageItems.size());
return vo;
}
@@ -552,17 +583,55 @@ public class UserApiSecretService implements UserSecretCleanupPort {
return companyScope.userIdsOr(base);
}
/** 分组筛选项:超管=全部;主管=自己带的分组。 */
private List<AdminUserSecretPageVo.GroupOptionVo> groupOptions(AdminUserEntity operator, boolean superAdmin) {
/**
* 分组筛选项:超管=全部;主管=自己带的分组。平台 tab 打开时按分组平台收窄
* (与京东侧 {@code retainJdPlatformGroups} 同族口径:京东档只进京东侧,NULL/空白存量归亚马逊——
* 与 users.platform 列 DEFAULT 'amazon' 同形)。
*/
private List<AdminUserSecretPageVo.GroupOptionVo> groupOptions(AdminUserEntity operator, boolean superAdmin,
String platformFilter) {
List<ShopManageGroupEntity> groups = superAdmin
? adminGroupMapper.selectAllGroups()
: adminGroupMapper.selectLedGroups(operator.getId());
return groups.stream()
.filter(group -> group.getId() != null)
.filter(group -> groupMatchesPlatform(group, platformFilter))
.map(group -> new AdminUserSecretPageVo.GroupOptionVo(group.getId(), group.getGroupName()))
.toList();
}
/** 平台过滤(2026-11-22 后管按平台 tab 隔离):行后收窄,不在 allowedUserIds 里塞几千个 id——密钥行只属于配过密钥的用户,先取行再判平台。platform 为 null(旧调用/缺省)原样返回,旧行为逐字不变。 */
private List<UserApiSecretEntity> filterRowsByUserPlatform(List<UserApiSecretEntity> rows, String platform) {
if (platform == null || rows.isEmpty()) {
return rows;
}
List<Long> rowUserIds = rows.stream()
.map(UserApiSecretEntity::getUserId)
.filter(Objects::nonNull)
.distinct()
.toList();
if (rowUserIds.isEmpty()) {
return List.of();
}
Set<Long> platformUserIds = adminUserMapper.selectList(new LambdaQueryWrapper<AdminUserEntity>()
.in(AdminUserEntity::getId, rowUserIds)
.eq(AdminUserEntity::getPlatform, platform))
.stream().map(AdminUserEntity::getId).filter(Objects::nonNull).collect(Collectors.toSet());
return rows.stream()
.filter(row -> row.getUserId() != null && platformUserIds.contains(row.getUserId()))
.toList();
}
/** NULL/空白分组平台归一亚马逊(列 DEFAULT 同口径)后与目标平台比对;未开平台筛一律放行。 */
private static boolean groupMatchesPlatform(ShopManageGroupEntity group, String platformFilter) {
if (platformFilter == null) {
return true;
}
String groupPlatform = group.getPlatform() == null || group.getPlatform().isBlank()
? AdminPlatformScope.AMAZON : group.getPlatform();
return groupPlatform.equals(platformFilter);
}
/** 本页行补「分组」:一次批量查询;无分组记录的用户回退展示所属主管用户名。 */
private void fillRowGroups(List<AdminUserSecretRowVo> pageItems) {
List<Long> userIds = pageItems.stream()
@@ -3,6 +3,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.security.DeviceSessionPolicy;
import com.nanri.aiimage.common.service.AdminPlatformScope;
import com.nanri.aiimage.common.service.ReadScope;
import com.nanri.aiimage.common.mapper.AdminUserMapper;
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
@@ -28,6 +29,9 @@ import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
/**
* 用户密钥调用计次:按用户 × 模块 × 天原子累加,后台按日期范围聚合查询。
@@ -172,6 +176,8 @@ public class UserSecretUsageService {
boolean superAdmin = DeviceSessionPolicy.isSuper(adminAuthSupport.currentRole(operator));
Long requestedGroupId = safeQuery.getGroupId();
// 平台 tab(2026-11-22 京东侧新增):controller 已归一过,这里幂等再归一一次,service 不信任调用方。
String platformFilter = AdminPlatformScope.normalizeOrNull(safeQuery.getPlatform());
// 组隔离(主管锁定自己带的分组、超管按 group_id 筛)叠上公司 tab:取**交集**,公司 tab 只收窄。
List<Long> scopedUserIds = scopedUserIds(operator, requestedGroupId, companyScope);
// 空集必须早退:交给下面的 in(...) 会渲染成 IN () → SQL 语法错 → 500。
@@ -188,7 +194,7 @@ public class UserSecretUsageService {
log.info("[user-secret-usage] 用量统计:主管可见集与公司范围交集为空(多为越界 company_id),返回空 operatorId={} companyScope={}",
operator.getId(), companyScopeSize);
}
return emptyPage(page, pageSize, groupOptions(operator, superAdmin));
return emptyPage(page, pageSize, groupOptions(operator, superAdmin, platformFilter));
}
LambdaQueryWrapper<UserSecretUsageEntity> wrapper = new LambdaQueryWrapper<>();
@@ -215,12 +221,19 @@ public class UserSecretUsageService {
if (allowedUserIds == null || allowedUserIds.isEmpty()) {
log.info("[user-secret-usage] 用量统计无匹配用户 keyword={} groupId={} operatorId={} role={}",
keyword, requestedGroupId, operator.getId(), superAdmin ? "super_admin" : "admin");
return emptyPage(page, pageSize, groupOptions(operator, superAdmin));
return emptyPage(page, pageSize, groupOptions(operator, superAdmin, platformFilter));
}
wrapper.in(UserSecretUsageEntity::getUserId, allowedUserIds);
}
List<UserSecretUsageEntity> rows = usageMapper.selectList(wrapper);
// 平台过滤走行后收窄(不在 allowedUserIds 里塞几千个 id);必须在聚合前滤——汇总卡与行都按平台算。
int beforePlatformFilter = rows.size();
rows = filterRowsByUserPlatform(rows, platformFilter);
if (platformFilter != null && rows.size() != beforePlatformFilter) {
log.info("[user-secret-usage] 用量统计平台筛收窄 platform={} 行数 {}→{} operatorId={}",
platformFilter, beforePlatformFilter, rows.size(), operator.getId());
}
Map<Long, AdminUserSecretUsageRowVo> byUser = new LinkedHashMap<>();
long similarAsinCalls = 0;
@@ -280,9 +293,9 @@ public class UserSecretUsageService {
vo.setAppearancePatentCalls((int) appearancePatentCalls);
vo.setProxyCalls((int) proxyCalls);
vo.setTotalCalls((int) (similarAsinCalls + appearancePatentCalls + proxyCalls));
vo.setGroupOptions(groupOptions(operator, superAdmin));
log.info("[user-secret-usage] 用量统计查询 start={} end={} keyword={} groupId={} role={} 用户数={} 本页={} 总次数={}",
safeQuery.getStartDate(), safeQuery.getEndDate(), keyword, requestedGroupId,
vo.setGroupOptions(groupOptions(operator, superAdmin, platformFilter));
log.info("[user-secret-usage] 用量统计查询 start={} end={} keyword={} groupId={} platform={} role={} 用户数={} 本页={} 总次数={}",
safeQuery.getStartDate(), safeQuery.getEndDate(), keyword, requestedGroupId, platformFilter,
superAdmin ? "super_admin" : "admin", total, pageItems.size(), vo.getTotalCalls());
return vo;
}
@@ -340,17 +353,55 @@ public class UserSecretUsageService {
}
}
/** 分组筛选项:超管=全部;主管=自己带的分组。 */
private List<AdminUserSecretPageVo.GroupOptionVo> groupOptions(AdminUserEntity operator, boolean superAdmin) {
/**
* 分组筛选项:超管=全部;主管=自己带的分组。平台 tab 打开时按分组平台收窄
* (与京东侧 {@code retainJdPlatformGroups} 同族口径:京东档只进京东侧,NULL/空白存量归亚马逊——
* 与 users.platform 列 DEFAULT 'amazon' 同形)。
*/
private List<AdminUserSecretPageVo.GroupOptionVo> groupOptions(AdminUserEntity operator, boolean superAdmin,
String platformFilter) {
List<ShopManageGroupEntity> groups = superAdmin
? adminGroupMapper.selectAllGroups()
: adminGroupMapper.selectLedGroups(operator.getId());
return groups.stream()
.filter(group -> group.getId() != null)
.filter(group -> groupMatchesPlatform(group, platformFilter))
.map(group -> new AdminUserSecretPageVo.GroupOptionVo(group.getId(), group.getGroupName()))
.toList();
}
/** 平台过滤(2026-11-22 后管按平台 tab 隔离):行后收窄,不在 allowedUserIds 里塞几千个 id——用量行只属于有调用的用户,先取行再判平台。platform 为 null(旧调用/缺省)原样返回,旧行为逐字不变。 */
private List<UserSecretUsageEntity> filterRowsByUserPlatform(List<UserSecretUsageEntity> rows, String platform) {
if (platform == null || rows.isEmpty()) {
return rows;
}
List<Long> rowUserIds = rows.stream()
.map(UserSecretUsageEntity::getUserId)
.filter(Objects::nonNull)
.distinct()
.toList();
if (rowUserIds.isEmpty()) {
return List.of();
}
Set<Long> platformUserIds = adminUserMapper.selectList(new LambdaQueryWrapper<AdminUserEntity>()
.in(AdminUserEntity::getId, rowUserIds)
.eq(AdminUserEntity::getPlatform, platform))
.stream().map(AdminUserEntity::getId).filter(Objects::nonNull).collect(Collectors.toSet());
return rows.stream()
.filter(row -> row.getUserId() != null && platformUserIds.contains(row.getUserId()))
.toList();
}
/** NULL/空白分组平台归一亚马逊(列 DEFAULT 同口径)后与目标平台比对;未开平台筛一律放行。 */
private static boolean groupMatchesPlatform(ShopManageGroupEntity group, String platformFilter) {
if (platformFilter == null) {
return true;
}
String groupPlatform = group.getPlatform() == null || group.getPlatform().isBlank()
? AdminPlatformScope.AMAZON : group.getPlatform();
return groupPlatform.equals(platformFilter);
}
private AdminUserSecretUsagePageVo emptyPage(long page, long pageSize,
List<AdminUserSecretPageVo.GroupOptionVo> groupOptions) {
AdminUserSecretUsagePageVo vo = new AdminUserSecretUsagePageVo();
@@ -34,13 +34,15 @@ class AdminMenuPlatformScopeTest {
}
@Test
@DisplayName("双侧清单逐字:用户、分组、公司与菜单权限配置;密钥相关页面仅亚马逊")
@DisplayName("双侧清单逐字:用户、分组、公司与菜单权限配置 + 密钥两页(2026-11-22 扩双侧)")
void bothPlatformMenuKeysArePinnedVerbatim() {
assertThat(AdminMenuPlatformScope.BOTH_MENU_KEYS).containsExactlyInAnyOrder(
"admin_users", "admin_group_manage", "admin_companies",
"admin_columns");
assertThat(AdminMenuPlatformScope.platformsOfMenuKey("admin_user_secrets")).containsExactly("amazon");
assertThat(AdminMenuPlatformScope.platformsOfMenuKey("admin_user_secret_usage")).containsExactly("amazon");
"admin_columns", "admin_user_secrets", "admin_user_secret_usage");
assertThat(AdminMenuPlatformScope.platformsOfMenuKey("admin_user_secrets"))
.containsExactlyInAnyOrder("amazon", "jd");
assertThat(AdminMenuPlatformScope.platformsOfMenuKey("admin_user_secret_usage"))
.containsExactlyInAnyOrder("amazon", "jd");
}
// ------------------------------------------------------------ key 归属判定
@@ -77,9 +79,13 @@ class AdminMenuPlatformScopeTest {
List.of("admin_shop_manage", "admin_query_asin"), "jd")).isTrue();
assertThat(AdminMenuPlatformScope.hasCrossPlatformKey(
List.of("admin_shop_manage"), "amazon")).isFalse();
// 双侧 key 与京东 key 对 jd 用户都放行
// 双侧 key 与京东 key 对 jd 用户都放行(密钥两页 2026-11-22 扩双侧,jd 被授权后同样放行)
assertThat(AdminMenuPlatformScope.hasCrossPlatformKey(
List.of("admin_jd_database", "admin_users"), "jd")).isFalse();
assertThat(AdminMenuPlatformScope.hasCrossPlatformKey(
List.of("admin_user_secrets", "admin_user_secret_usage"), "jd")).isFalse();
assertThat(AdminMenuPlatformScope.hasCrossPlatformKey(
List.of("admin_user_secrets"), "amazon")).isFalse();
// null 平台按 amazon:亚马逊 key 不越侧
assertThat(AdminMenuPlatformScope.hasCrossPlatformKey(
List.of("admin_shop_manage"), (String) null)).isFalse();
@@ -82,9 +82,10 @@ class AdminConsoleCurrentUserMenusPlatformTest {
List<String> keys = new java.util.ArrayList<>();
collectKeys(items(), keys);
assertThat(keys).contains("admin_jd_database", "admin_users", "admin_group_manage", "admin_columns");
assertThat(keys).doesNotContain("admin_brand_gating", "admin_query_asin",
// 密钥两页 2026-11-22 扩双侧:jd 用户树里照发(页面数据按 users.platform 过滤,另一层)。
assertThat(keys).contains("admin_jd_database", "admin_users", "admin_group_manage", "admin_columns",
"admin_user_secrets", "admin_user_secret_usage");
assertThat(keys).doesNotContain("admin_brand_gating", "admin_query_asin");
assertThat(keys).contains("group-account");
}
@@ -185,6 +185,18 @@ class PermissionMenuServiceConsolePageTest {
operator, "无权访问京东数据库", "admin_jd_database")).doesNotThrowAnyException();
}
/** 密钥两页 2026-11-22 扩双侧:jd 用户过平台门(数据按 users.platform 过滤是另一层,门只管归属)。 */
@Test
void jdCompanySuperAdminPassesSecretPagesDoor() {
AdminUserEntity operator = user(9L, "company_super_admin");
operator.setPlatform("jd");
assertThatCode(() -> service.requireConsolePageAccess(
operator, "无权访问密钥管理", "admin_user_secrets")).doesNotThrowAnyException();
assertThatCode(() -> service.requireConsolePageAccess(
operator, "无权访问密钥用量统计", "admin_user_secret_usage")).doesNotThrowAnyException();
}
/** 普通账号同理:授权解决「有没有这一页」,平台门解决「这一页归不归我的平台」。 */
@Test
void jdNormalAccountHoldingAmazonGrantIsStillDenied() {
@@ -58,8 +58,10 @@ import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
@@ -328,6 +330,51 @@ class UserApiSecretServiceTest {
verify(mapper, never()).selectOne(any());
}
/**
* 代调型 AI 特性(尺码表补值/上架必填参数补值)取钥的单一出口:按 外观专利 → 货源查询
* 的模块顺序,渠道登记为龙抬头且明文非空才认;都没有返回空串(调用方按无密钥降级)。
* 循环本体只许这一处——两个调用方各写一份就是两份正本(B79-12 同形教训)。
*/
@Test
void findLongtaitouPlainValueTakesMatchingChannelModuleInOrder() {
UserApiSecretService service = spy(newService());
doReturn("longtaitou").when(service).findChannel(7L, "appearance-patent");
doReturn("sk-ltt-1").when(service).findPlainValue(7L, "appearance-patent");
assertThat(service.findLongtaitouPlainValue(7L)).isEqualTo("sk-ltt-1");
}
@Test
void findLongtaitouPlainValueFallsBackToSimilarAsinWhenAppearanceChannelDiffers() {
UserApiSecretService service = spy(newService());
doReturn("t8star").when(service).findChannel(7L, "appearance-patent");
doReturn("sk-t8").when(service).findPlainValue(7L, "appearance-patent");
doReturn("longtaitou").when(service).findChannel(7L, "similar-asin");
doReturn("sk-ltt-2").when(service).findPlainValue(7L, "similar-asin");
assertThat(service.findLongtaitouPlainValue(7L)).isEqualTo("sk-ltt-2");
}
@Test
void findLongtaitouPlainValueFallsBackToEmptyWhenNoChannelMatches() {
UserApiSecretService service = spy(newService());
doReturn("t8star").when(service).findChannel(7L, "appearance-patent");
doReturn("sk-t8").when(service).findPlainValue(7L, "appearance-patent");
doReturn("t8star").when(service).findChannel(7L, "similar-asin");
doReturn("sk-t8b").when(service).findPlainValue(7L, "similar-asin");
assertThat(service.findLongtaitouPlainValue(7L)).isEmpty();
}
@Test
void findLongtaitouPlainValueIgnoresBlankPlainValue() {
UserApiSecretService service = spy(newService());
doReturn("longtaitou").when(service).findChannel(7L, "appearance-patent");
doReturn(" ").when(service).findPlainValue(7L, "appearance-patent");
assertThat(service.findLongtaitouPlainValue(7L)).isEmpty();
}
/**
* 建单闸门:这一单要拿谁的密钥跑 LLM。两侧都空必须当场拒——放过去就是
* 「保留原始行」的静默降级:表照出、行数照扣,只是整列检测结论是空的。
@@ -679,6 +726,74 @@ class UserApiSecretServiceTest {
assertThat(page.getGroupOptions()).isEmpty();
}
@Test
@DisplayName("平台过滤(2026-11-22):platform=jd 只留京东用户的行;缺省=旧行为逐字不筛")
void adminPageFiltersRowsByPlatformTab() {
UserApiSecretService service = newService();
TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""),
AdminUserEntity.class);
when(mapper.selectList(any())).thenReturn(List.of(
row(1L, "similar-asin", "enc:sk-jd", "passed"),
row(2L, "similar-asin", "enc:sk-amazon", "failed")));
// 平台收窄查询:users.platform='jd' 只命中用户 1(行后过滤,不在 allowedUserIds 里塞几千个 id)
AdminUserEntity jdUser = new AdminUserEntity();
jdUser.setId(1L);
when(adminUserMapper.selectList(any())).thenReturn(List.of(jdUser));
AdminUserEntity operator = new AdminUserEntity();
operator.setId(88L);
when(adminAuthSupport.currentRole(operator)).thenReturn("super_admin");
AdminUserSecretQuery query = new AdminUserSecretQuery();
query.setPlatform("jd");
var page = service.adminPage(operator, query, ReadScope.unfiltered());
assertThat(page.getItems()).hasSize(1);
assertThat(page.getItems().get(0).getUserId()).isEqualTo(1L);
// 缺省(platform 不传)= 旧行为:两行都在,平台查询一次都没发
var legacy = service.adminPage(operator, new AdminUserSecretQuery(), ReadScope.unfiltered());
assertThat(legacy.getItems()).hasSize(2);
verify(adminUserMapper, times(1)).selectList(any());
}
@Test
@DisplayName("platform=amazon 只留亚马逊用户;分组下拉按分组平台收窄(NULL/空白存量归亚马逊)")
void adminPageAmazonTabKeepsAmazonUsersAndFiltersGroupOptions() {
UserApiSecretService service = newService();
TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""),
AdminUserEntity.class);
when(mapper.selectList(any())).thenReturn(List.of(
row(1L, "similar-asin", "enc:sk-jd", "passed"),
row(2L, "similar-asin", "enc:sk-amazon", "failed")));
AdminUserEntity amazonUser = new AdminUserEntity();
amazonUser.setId(2L);
when(adminUserMapper.selectList(any())).thenReturn(List.of(amazonUser));
ShopManageGroupEntity jdGroup = new ShopManageGroupEntity();
jdGroup.setId(5L);
jdGroup.setGroupName("京东组");
jdGroup.setPlatform("jd");
ShopManageGroupEntity amazonGroup = new ShopManageGroupEntity();
amazonGroup.setId(6L);
amazonGroup.setGroupName("亚马逊组");
amazonGroup.setPlatform("amazon");
ShopManageGroupEntity legacyGroup = new ShopManageGroupEntity();
legacyGroup.setId(7L);
legacyGroup.setGroupName("存量组");
when(adminGroupMapper.selectAllGroups()).thenReturn(List.of(jdGroup, amazonGroup, legacyGroup));
AdminUserEntity operator = new AdminUserEntity();
operator.setId(88L);
when(adminAuthSupport.currentRole(operator)).thenReturn("super_admin");
AdminUserSecretQuery query = new AdminUserSecretQuery();
query.setPlatform("amazon");
var page = service.adminPage(operator, query, ReadScope.unfiltered());
assertThat(page.getItems()).hasSize(1);
assertThat(page.getItems().get(0).getUserId()).isEqualTo(2L);
// 亚马逊侧:显式 amazon 与 NULL 存量组都在,京东档组不进下拉
assertThat(page.getGroupOptions()).extracting("id").containsExactly(6L, 7L);
}
@Test
void adminPageScopesToLedGroupMembers() {
UserApiSecretService service = newService();
@@ -236,6 +236,38 @@ class UserSecretUsageServiceTest {
verify(adminGroupMapper).selectUserIdsByGroupId(eq(5L));
}
@Test
@DisplayName("平台过滤(2026-11-22):platform=jd 只聚合京东用户的行,汇总卡同步收窄;缺省=旧行为逐字不筛")
void adminPageFiltersUsageRowsByPlatformTab() {
UserSecretUsageService service = newService();
TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""),
AdminUserEntity.class);
when(adminAuthSupport.currentRole(any())).thenReturn("super_admin");
when(usageMapper.selectList(any())).thenReturn(List.of(
usageRow(1L, "similar-asin", 10),
usageRow(2L, "proxy", 3)));
// 平台收窄查询:users.platform='jd' 只命中用户 1(行后过滤,聚合前滤——汇总卡与行都按平台算)
AdminUserEntity jdUser = new AdminUserEntity();
jdUser.setId(1L);
when(adminUserMapper.selectList(any())).thenReturn(List.of(jdUser));
when(adminGroupMapper.selectGroupNamesByUserIds(any())).thenReturn(List.of());
AdminUserSecretUsageQuery query = new AdminUserSecretUsageQuery();
query.setPlatform("jd");
var page = service.adminPage(new AdminUserEntity(), query, ReadScope.unfiltered());
assertThat(page.getItems()).hasSize(1);
assertThat(page.getItems().get(0).getUserId()).isEqualTo(1L);
assertThat(page.getSimilarAsinCalls()).isEqualTo(10);
assertThat(page.getTotalCalls()).isEqualTo(10);
// 缺省(platform 不传)= 旧行为:两行都在,平台查询一次都没发
var legacy = service.adminPage(new AdminUserEntity(), new AdminUserSecretUsageQuery(), ReadScope.unfiltered());
assertThat(legacy.getItems()).hasSize(2);
assertThat(legacy.getTotalCalls()).isEqualTo(13);
verify(adminUserMapper, times(1)).selectList(any());
}
@Test
void adminPageReturnsEmptyWhenKeywordMatchesNobody() {
UserSecretUsageService service = newService();