feat(公司tab): 店铺管理与数据权限分组按成员集筛, 凭据按 id 补读闸门

Task 11: ShopManageController.page / listGroups 接 DataReadScope.readScope,
把 company_id 算出的成员集当参数传给 service(controller 层收口, service 手上
只有 operatorId+布尔); ShopManageGroupService.list 增加 companyMemberIds 腿。

凭据 getCredentialById 的闸门用与 page() 同形的尺子(created_by_id IN creators
OR group_id IN groups),不是账号集合 —— 后者表达不了分组那一腿, 会把列表看得见
的行读凭据时 403 掉, 那是把正当访问静默删掉。

新增 testutil/SourceScan 供源码扫描式护栏取文(找不到方法必须抛, 不许返回空串恒绿)。

前端两页接 tab: companyId 取 companyTab.requestCompanyId, 并 watch activeId 重载;
G1 登记表给 admin_shop_manage 钉两条发送点(白名单 builder 故意不收 companyId)。
写侧入口一律不收 company_id, 由 ShopManageServiceTest 逐方法反向钉住。
This commit is contained in:
2026-10-05 16:25:24 +08:00
parent 1391a8aab1
commit 41dbcad1ac
12 changed files with 362 additions and 36 deletions
@@ -3,6 +3,7 @@ package com.nanri.aiimage.modules.shopkey.controller;
import com.nanri.aiimage.common.api.ApiResponse;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
import com.nanri.aiimage.common.service.DataReadScope;
import com.nanri.aiimage.common.security.AdminAuthSupport;
import com.nanri.aiimage.modules.shopkey.model.vo.ShopManageCredentialVo;
import com.nanri.aiimage.modules.shopkey.service.ShopManageGroupService;
@@ -30,10 +31,11 @@ class ShopManageCredentialAuthTest {
private final ShopManageService shopManageService = mock(ShopManageService.class);
private final ShopManageGroupService shopManageGroupService = mock(ShopManageGroupService.class);
private final AdminAuthSupport adminAuthSupport = mock(AdminAuthSupport.class);
private final DataReadScope dataReadScope = mock(DataReadScope.class);
private final HttpServletRequest request = mock(HttpServletRequest.class);
private ShopManageController newController() {
return new ShopManageController(shopManageService, shopManageGroupService, adminAuthSupport);
return new ShopManageController(shopManageService, shopManageGroupService, adminAuthSupport, dataReadScope);
}
@Test
@@ -1,19 +1,35 @@
package com.nanri.aiimage.modules.shopkey.service;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.common.model.entity.ShopManageGroupEntity;
import com.nanri.aiimage.common.security.ShopCredentialCryptoService;
import com.nanri.aiimage.modules.shopkey.mapper.ShopManageMapper;
import com.nanri.aiimage.modules.shopkey.model.entity.ShopManageEntity;
import com.nanri.aiimage.testutil.SourceScan;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.List;
import java.util.Set;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
@@ -29,6 +45,15 @@ class ShopManageServiceTest {
@InjectMocks
private ShopManageService service;
/** getSqlSegment() 解析 lambda 靠 TableInfo 缓存,单测没有 Spring 容器去建它,不初始化就抛 "can not find lambda cache"。 */
@BeforeEach
void initTableInfo() {
TableInfoHelper.initTableInfo(
new MapperBuilderAssistant(new MybatisConfiguration(), ""), ShopManageEntity.class);
TableInfoHelper.initTableInfo(
new MapperBuilderAssistant(new MybatisConfiguration(), ""), ShopManageGroupEntity.class);
}
@Test
void requireShopByNameReturnsManagedShop() {
ShopManageEntity entity = new ShopManageEntity();
@@ -47,4 +72,73 @@ class ShopManageServiceTest {
assertEquals("后台店铺管理中未找到店铺:缺失店铺,请先添加店铺信息", exception.getMessage());
}
@Test
@DisplayName("读:超管选了公司 ⇒ created_by_id 落进成员集;非超管不叠这一条")
void superAdminPageNarrowsToCompanyMembers() {
when(shopManageMapper.selectCount(any())).thenReturn(1L);
when(shopManageMapper.selectList(any())).thenReturn(List.of());
service.page(1L, 15L, null, null, 1L, true, List.of(11L, 12L));
ArgumentCaptor<LambdaQueryWrapper<ShopManageEntity>> captor =
ArgumentCaptor.forClass(LambdaQueryWrapper.class);
verify(shopManageMapper).selectCount(captor.capture());
// 值在 paramNameValuePairs 里,segment 里只有 MPGENVAL 占位 ⇒ 结构从 segment 读、值从 pairs 读。
assertThat(captor.getValue().getSqlSegment()).contains("created_by_id").contains("IN (");
assertThat(captor.getValue().getParamNameValuePairs().values()).contains(11L, 12L);
}
@Test
@DisplayName("凭据:非超管读「创建人不认识、分组也不认识」的行 ⇒ 403(§6.1 列表筛了按 id 照读)")
void credentialByIdRejectsRowOutsideBothVisibleSets() {
when(shopManageMapper.selectById(77L)).thenReturn(shop(77L, 999L, 7L));
when(shopManageGroupService.listAccessibleCreatorUserIds(2L, false)).thenReturn(Set.of(11L, 12L));
when(shopManageGroupService.listAccessibleGroupIds(2L, false)).thenReturn(Set.of());
assertThatThrownBy(() -> service.getCredentialById(77L, 2L, false))
.isInstanceOf(BusinessException.class)
.hasFieldOrPropertyWithValue("code", 403);
}
@Test
@DisplayName("凭据:店铺建在可访问分组里、创建人却不在我的账号集 ⇒ 必须 200(闸门用列表那把尺子,不是账号集合)")
void credentialByIdKeepsGroupVisibleRowsReadable() {
when(shopManageMapper.selectById(78L)).thenReturn(shop(78L, 999L, 5L));
when(shopManageGroupService.listAccessibleCreatorUserIds(2L, false)).thenReturn(Set.of(11L));
when(shopManageGroupService.listAccessibleGroupIds(2L, false)).thenReturn(Set.of(5L));
// 这一支是防误伤的正样本:把它删掉,下一轮"改成按账号集合卡"就没有任何尺子看得见。
assertThatCode(() -> service.getCredentialById(78L, 2L, false)).doesNotThrowAnyException();
}
@Test
@DisplayName("凭据:超管不查可见集(今天不筛、tab 也不是安全边界;短路写了就红在 NPE 上)")
void credentialByIdStaysUnfilteredForSuperAdmin() {
when(shopManageMapper.selectById(79L)).thenReturn(shop(79L, 999L, 7L));
assertThatCode(() -> service.getCredentialById(79L, 1L, true)).doesNotThrowAnyException();
}
private static ShopManageEntity shop(long id, Long createdById, Long groupId) {
ShopManageEntity entity = new ShopManageEntity();
entity.setId(id);
entity.setCreatedById(createdById);
entity.setGroupId(groupId);
return entity;
}
@Test
@DisplayName("写:店铺与分组的写入口一律不收 company_id——归属不从界面注入(§6.2,未批的界面暴露)")
void writeEntryPointsCarryNoCompanyId() {
String text = SourceScan.sourceText("ShopManageController.java");
// 必须逐方法体判:page 这个读入口这轮就要加 company_id,全文 doesNotContain 会被自己弄红。
for (String writer : List.of("create", "update", "delete", "createGroup", "updateGroup", "deleteGroup")) {
assertThat(SourceScan.methodBody(text, writer))
.as("写入口「" + writer + "」一旦出现 company_id,等于把公司维度端回界面(未批)")
.doesNotContain("company_id")
.doesNotContain("companyId");
}
assertThat(SourceScan.methodBody(text, "page")).contains("company_id");
}
}