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:
+7
-1
@@ -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")
|
||||
|
||||
+7
-1
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
+34
-12
@@ -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
|
||||
|
||||
+31
-10
@@ -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()
|
||||
|
||||
+61
-5
@@ -8,6 +8,7 @@ import org.apache.ibatis.builder.MapperBuilderAssistant;
|
||||
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.common.mapper.AdminUserMapper;
|
||||
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
|
||||
@@ -22,6 +23,7 @@ import com.nanri.aiimage.modules.usersecret.model.entity.UserApiSecretEntity;
|
||||
import com.nanri.aiimage.modules.usersecret.model.vo.AdminUserSecretModuleVo;
|
||||
import com.nanri.aiimage.modules.usersecret.model.vo.UserApiSecretBundleVo;
|
||||
import com.nanri.aiimage.modules.usersecret.support.UserSecretModule;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
@@ -477,7 +479,7 @@ class UserApiSecretServiceTest {
|
||||
AdminUserEntity operator = new AdminUserEntity();
|
||||
operator.setId(88L);
|
||||
when(adminAuthSupport.currentRole(operator)).thenReturn("super_admin");
|
||||
var page = service.adminPage(operator, new AdminUserSecretQuery());
|
||||
var page = service.adminPage(operator, new AdminUserSecretQuery(), ReadScope.unfiltered());
|
||||
|
||||
assertThat(page.getItems()).hasSize(1);
|
||||
var rowVo = page.getItems().get(0);
|
||||
@@ -500,7 +502,7 @@ class UserApiSecretServiceTest {
|
||||
when(adminGroupMapper.selectLedGroups(88L)).thenReturn(List.of());
|
||||
when(adminUserMapper.selectList(any())).thenReturn(List.of());
|
||||
|
||||
var page = service.adminPage(operator, new AdminUserSecretQuery());
|
||||
var page = service.adminPage(operator, new AdminUserSecretQuery(), ReadScope.unfiltered());
|
||||
|
||||
assertThat(page.getItems()).isEmpty();
|
||||
assertThat(page.getGroupOptions()).isEmpty();
|
||||
@@ -526,7 +528,7 @@ class UserApiSecretServiceTest {
|
||||
ref.setGroupName("一组");
|
||||
when(adminGroupMapper.selectGroupNamesByUserIds(any())).thenReturn(List.of(ref));
|
||||
|
||||
var page = service.adminPage(operator, new AdminUserSecretQuery());
|
||||
var page = service.adminPage(operator, new AdminUserSecretQuery(), ReadScope.unfiltered());
|
||||
|
||||
assertThat(page.getItems()).hasSize(1);
|
||||
assertThat(page.getItems().get(0).getGroups()).containsExactly("一组");
|
||||
@@ -589,7 +591,7 @@ class UserApiSecretServiceTest {
|
||||
when(adminUserMapper.selectBatchIds(any())).thenReturn(List.of(user, leader));
|
||||
when(adminGroupMapper.selectAllGroups()).thenReturn(List.of());
|
||||
|
||||
var page = service.adminPage(operator, new AdminUserSecretQuery());
|
||||
var page = service.adminPage(operator, new AdminUserSecretQuery(), ReadScope.unfiltered());
|
||||
|
||||
assertThat(page.getItems()).hasSize(1);
|
||||
assertThat(page.getItems().get(0).getGroups()).isEmpty();
|
||||
@@ -607,12 +609,66 @@ class UserApiSecretServiceTest {
|
||||
|
||||
AdminUserSecretQuery query = new AdminUserSecretQuery();
|
||||
query.setGroupId(5L);
|
||||
var page = service.adminPage(operator, query);
|
||||
var page = service.adminPage(operator, query, ReadScope.unfiltered());
|
||||
|
||||
assertThat(page.getItems()).isEmpty();
|
||||
verify(adminGroupMapper).selectUserIdsByGroupId(5L);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("超管 group_id + company_id 同时给:取交集,不是后一个覆盖前一个")
|
||||
void groupAndCompanyIntersect() {
|
||||
UserApiSecretService service = newService();
|
||||
AdminUserEntity operator = new AdminUserEntity();
|
||||
operator.setId(88L);
|
||||
when(adminAuthSupport.currentRole(operator)).thenReturn("super_admin");
|
||||
when(adminGroupMapper.selectUserIdsByGroupId(7L)).thenReturn(List.of(12L, 13L, 99L));
|
||||
|
||||
List<Long> scoped = service.scopedUserIds(operator, 7L, ReadScope.users(List.of(11L, 12L, 13L)));
|
||||
|
||||
assertThat(scoped).containsExactlyInAnyOrder(12L, 13L);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("公司选了但交集为空:返回空页,不许退化成「不筛」(那是越权)")
|
||||
void emptyIntersectionReturnsEmptyPage() {
|
||||
UserApiSecretService service = newService();
|
||||
AdminUserEntity operator = new AdminUserEntity();
|
||||
operator.setId(88L);
|
||||
when(adminAuthSupport.currentRole(operator)).thenReturn("super_admin");
|
||||
when(adminGroupMapper.selectUserIdsByGroupId(7L)).thenReturn(List.of(99L));
|
||||
assertThat(service.scopedUserIds(operator, 7L, ReadScope.users(List.of(11L)))).isEmpty();
|
||||
|
||||
// 空集必须由 adminPage 早退:交给 in(...) 会渲染成 IN () → SQL 语法错 → 500。
|
||||
AdminUserSecretQuery query = new AdminUserSecretQuery();
|
||||
query.setGroupId(7L);
|
||||
var page = service.adminPage(operator, query, ReadScope.users(List.of(11L)));
|
||||
assertThat(page.getItems()).isEmpty();
|
||||
verify(mapper, never()).selectList(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("超管不传公司:保持今天的不筛(全局约束 2——tab 不是安全边界,不许把空公司 id 当成筛)")
|
||||
void superAdminWithoutCompanyStaysUnfiltered() {
|
||||
UserApiSecretService service = newService();
|
||||
AdminUserEntity operator = new AdminUserEntity();
|
||||
operator.setId(88L);
|
||||
when(adminAuthSupport.currentRole(operator)).thenReturn("super_admin");
|
||||
assertThat(service.scopedUserIds(operator, null, ReadScope.unfiltered())).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("主管:组长锁定 ∩ 公司成员集;越界公司的成员集是空集 ⇒ 空页,不是全量")
|
||||
void leaderVisibleSetIsNarrowedByCompany() {
|
||||
UserApiSecretService service = newService();
|
||||
AdminUserEntity operator = new AdminUserEntity();
|
||||
operator.setId(88L);
|
||||
when(userDataScopeSupport.resolveVisibleUserIds(88L)).thenReturn(List.of(1L, 2L, 88L));
|
||||
assertThat(service.scopedUserIds(operator, null, ReadScope.users(List.of(2L, 88L))))
|
||||
.containsExactlyInAnyOrder(2L, 88L);
|
||||
assertThat(service.scopedUserIds(operator, null, ReadScope.users(List.of()))).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void adminClearByUserDeletesAllRowsOfUser() {
|
||||
UserApiSecretService service = newService();
|
||||
|
||||
+40
-4
@@ -1,6 +1,7 @@
|
||||
package com.nanri.aiimage.modules.usersecret.service;
|
||||
|
||||
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;
|
||||
@@ -9,6 +10,7 @@ import com.nanri.aiimage.modules.shopkey.model.dto.UserGroupRef;
|
||||
import com.nanri.aiimage.modules.usersecret.mapper.UserSecretUsageMapper;
|
||||
import com.nanri.aiimage.modules.usersecret.model.dto.AdminUserSecretUsageQuery;
|
||||
import com.nanri.aiimage.modules.usersecret.model.entity.UserSecretUsageEntity;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.LocalDate;
|
||||
@@ -114,7 +116,7 @@ class UserSecretUsageServiceTest {
|
||||
ref.setGroupName("一组");
|
||||
when(adminGroupMapper.selectGroupNamesByUserIds(any())).thenReturn(List.of(ref));
|
||||
|
||||
var page = service.adminPage(new AdminUserEntity(), new AdminUserSecretUsageQuery());
|
||||
var page = service.adminPage(new AdminUserEntity(), new AdminUserSecretUsageQuery(), ReadScope.unfiltered());
|
||||
|
||||
assertThat(page.getTotal()).isEqualTo(2);
|
||||
assertThat(page.getItems()).hasSize(2);
|
||||
@@ -144,7 +146,7 @@ class UserSecretUsageServiceTest {
|
||||
when(userDataScopeSupport.resolveVisibleUserIds(88L)).thenReturn(List.of());
|
||||
when(adminGroupMapper.selectLedGroups(88L)).thenReturn(List.of());
|
||||
|
||||
var page = service.adminPage(operator, new AdminUserSecretUsageQuery());
|
||||
var page = service.adminPage(operator, new AdminUserSecretUsageQuery(), ReadScope.unfiltered());
|
||||
|
||||
assertThat(page.getItems()).isEmpty();
|
||||
assertThat(page.getTotal()).isZero();
|
||||
@@ -167,7 +169,7 @@ class UserSecretUsageServiceTest {
|
||||
|
||||
AdminUserSecretUsageQuery query = new AdminUserSecretUsageQuery();
|
||||
query.setGroupId(5L);
|
||||
var page = service.adminPage(new AdminUserEntity(), query);
|
||||
var page = service.adminPage(new AdminUserEntity(), query, ReadScope.unfiltered());
|
||||
|
||||
assertThat(page.getItems()).hasSize(1);
|
||||
assertThat(page.getItems().get(0).getProxyCount()).isEqualTo(3);
|
||||
@@ -186,9 +188,43 @@ class UserSecretUsageServiceTest {
|
||||
|
||||
AdminUserSecretUsageQuery query = new AdminUserSecretUsageQuery();
|
||||
query.setKeyword("不存在的人");
|
||||
var page = service.adminPage(new AdminUserEntity(), query);
|
||||
var page = service.adminPage(new AdminUserEntity(), query, ReadScope.unfiltered());
|
||||
|
||||
assertThat(page.getItems()).isEmpty();
|
||||
verify(usageMapper, never()).selectList(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("用量统计:超管 group_id ∩ 公司成员集;交集为空时早退且不查库")
|
||||
void groupAndCompanyIntersectThenEarlyReturn() {
|
||||
UserSecretUsageService service = newService();
|
||||
AdminUserEntity operator = new AdminUserEntity();
|
||||
operator.setId(88L);
|
||||
when(adminAuthSupport.currentRole(any())).thenReturn("super_admin");
|
||||
when(adminGroupMapper.selectUserIdsByGroupId(7L)).thenReturn(List.of(12L, 13L, 99L));
|
||||
assertThat(service.scopedUserIds(operator, 7L, ReadScope.users(List.of(11L, 12L, 13L))))
|
||||
.containsExactlyInAnyOrder(12L, 13L);
|
||||
|
||||
when(adminGroupMapper.selectUserIdsByGroupId(7L)).thenReturn(List.of(99L));
|
||||
AdminUserSecretUsageQuery query = new AdminUserSecretUsageQuery();
|
||||
query.setGroupId(7L);
|
||||
var page = service.adminPage(operator, query, ReadScope.users(List.of(11L)));
|
||||
|
||||
assertThat(page.getItems()).isEmpty();
|
||||
verify(usageMapper, never()).selectList(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("用量统计:主管只看自己带的分组成员,公司 tab 只能再收窄不能放大")
|
||||
void leaderVisibleSetIsNarrowedByCompany() {
|
||||
UserSecretUsageService service = newService();
|
||||
AdminUserEntity operator = new AdminUserEntity();
|
||||
operator.setId(88L);
|
||||
when(userDataScopeSupport.resolveVisibleUserIds(88L)).thenReturn(List.of(1L, 2L, 88L));
|
||||
assertThat(service.scopedUserIds(operator, null, ReadScope.users(List.of(2L))))
|
||||
.containsExactly(2L);
|
||||
assertThat(service.scopedUserIds(operator, null, ReadScope.unfiltered()))
|
||||
.containsExactlyInAnyOrder(1L, 2L, 88L);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user