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:
+25
-17
@@ -3,6 +3,7 @@ package com.nanri.aiimage.modules.shopkey.controller;
|
||||
import com.nanri.aiimage.common.api.ApiResponse;
|
||||
import com.nanri.aiimage.common.security.AdminAuthSupport;
|
||||
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
|
||||
import com.nanri.aiimage.common.service.DataReadScope;
|
||||
import com.nanri.aiimage.modules.shopkey.model.dto.ShopManageCreateRequest;
|
||||
import com.nanri.aiimage.modules.shopkey.model.dto.ShopManageGroupCreateRequest;
|
||||
import com.nanri.aiimage.modules.shopkey.model.dto.ShopManageGroupUpdateRequest;
|
||||
@@ -35,6 +36,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@@ -45,6 +48,7 @@ public class ShopManageController {
|
||||
private final ShopManageService shopManageService;
|
||||
private final ShopManageGroupService shopManageGroupService;
|
||||
private final AdminAuthSupport adminAuthSupport;
|
||||
private final DataReadScope dataReadScope;
|
||||
|
||||
/** 解析当前操作者(JWT 或内部令牌通道),数据范围一律以该身份为准,不信任调用方传入的 operatorId/superAdmin。 */
|
||||
private AdminUserEntity requireOperator(HttpServletRequest request) {
|
||||
@@ -56,7 +60,8 @@ public class ShopManageController {
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
@Operation(summary = "分页查询店铺", description = "支持按分组、店铺名、当前操作者可见范围筛选")
|
||||
@Operation(summary = "分页查询店铺", description = "支持按分组、店铺名、当前操作者可见范围筛选;"
|
||||
+ "company_id 只收窄可见集、不放大,跨公司查看仅超管")
|
||||
@ApiResponses({
|
||||
@io.swagger.v3.oas.annotations.responses.ApiResponse(
|
||||
responseCode = "200",
|
||||
@@ -68,15 +73,16 @@ public class ShopManageController {
|
||||
@Parameter(description = "页码") @RequestParam(defaultValue = "1") Long page,
|
||||
@Parameter(description = "每页数量") @RequestParam(defaultValue = "15") Long pageSize,
|
||||
@Parameter(description = "分组 ID") @RequestParam(required = false) Long groupId,
|
||||
@Parameter(description = "店铺名称") @RequestParam(required = false) String shopName) {
|
||||
@Parameter(description = "店铺名称") @RequestParam(required = false) String shopName,
|
||||
@Parameter(description = "公司 ID") @RequestParam(name = "company_id", required = false) Long companyId) {
|
||||
AdminUserEntity operator = requireOperator(request);
|
||||
return ApiResponse.success(shopManageService.page(
|
||||
page,
|
||||
pageSize,
|
||||
groupId,
|
||||
shopName,
|
||||
operator.getId(),
|
||||
isSuper(operator)));
|
||||
Long operatorId = operator.getId();
|
||||
boolean superAdmin = isSuper(operator);
|
||||
// 公司 tab 只对超管收窄:非超管的可见集里含「靠分组可见」的行,而分组行没有账号列可推公司,
|
||||
// 给它叠 created_by_id IN (成员) 会把那批行从每一个 tab 里静默删掉(不报错、不 403)。
|
||||
List<Long> memberIds = superAdmin ? dataReadScope.readScope(operator, companyId).userIdsOr(null) : null;
|
||||
ShopManagePageVo vo = shopManageService.page(page, pageSize, groupId, shopName, operatorId, superAdmin, memberIds);
|
||||
return ApiResponse.success(vo);
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
@@ -111,15 +117,19 @@ public class ShopManageController {
|
||||
}
|
||||
|
||||
@GetMapping("/groups")
|
||||
@Operation(summary = "查询分组列表", description = "普通管理员返回当前可访问的分组,超级管理员返回全部")
|
||||
@Operation(summary = "查询分组列表", description = "普通管理员返回当前可访问的分组,超级管理员返回全部;"
|
||||
+ "company_id 只收窄可见集、不放大,跨公司查看仅超管")
|
||||
public ApiResponse<java.util.List<ShopManageGroupItemVo>> listGroups(
|
||||
HttpServletRequest request,
|
||||
@Parameter(description = "创建人用户ID") @RequestParam(required = false) Long createdById) {
|
||||
@Parameter(description = "创建人用户ID") @RequestParam(required = false) Long createdById,
|
||||
@Parameter(description = "公司 ID") @RequestParam(name = "company_id", required = false) Long companyId) {
|
||||
AdminUserEntity operator = requireOperator(request);
|
||||
if (isSuper(operator)) {
|
||||
return ApiResponse.success(shopManageGroupService.list(createdById));
|
||||
List<Long> memberIds = dataReadScope.readScope(operator, companyId).userIdsOr(null);
|
||||
return ApiResponse.success(shopManageGroupService.list(createdById, memberIds));
|
||||
}
|
||||
return ApiResponse.success(shopManageGroupService.listAccessible(operator.getId(), false));
|
||||
Long operatorId = operator.getId();
|
||||
return ApiResponse.success(shopManageGroupService.listAccessible(operatorId, false));
|
||||
}
|
||||
|
||||
@PostMapping("/groups")
|
||||
@@ -175,10 +185,8 @@ public class ShopManageController {
|
||||
public ApiResponse<ShopManageCredentialVo> adminCredential(
|
||||
HttpServletRequest request,
|
||||
@Parameter(description = "店铺主键 ID") @PathVariable Long id) {
|
||||
requireOperator(request);
|
||||
// 2026-09 修复:此前忽略路径 id、改用请求参数 shop_name 查询,
|
||||
// 会出现「路径声明的店铺」与「实际读取凭据的店铺」不一致
|
||||
return ApiResponse.success(shopManageService.getCredentialById(id));
|
||||
AdminUserEntity operator = requireOperator(request);
|
||||
return ApiResponse.success(shopManageService.getCredentialById(id, operator.getId(), isSuper(operator)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+22
-5
@@ -45,14 +45,31 @@ public class ShopManageGroupService {
|
||||
private final AdminUserMapper adminUserMapper;
|
||||
|
||||
public List<ShopManageGroupItemVo> list() {
|
||||
return list(null);
|
||||
return list(null, null);
|
||||
}
|
||||
|
||||
public List<ShopManageGroupItemVo> list(Long createdById) {
|
||||
return toVoList(groupMapper.selectList(new LambdaQueryWrapper<ShopManageGroupEntity>()
|
||||
.eq(createdById != null && createdById > 0, ShopManageGroupEntity::getCreatedById, createdById)
|
||||
.or(createdById != null && createdById > 0, wrapper -> wrapper.eq(ShopManageGroupEntity::getUserId, createdById))
|
||||
.orderByAsc(ShopManageGroupEntity::getId)));
|
||||
return list(createdById, null);
|
||||
}
|
||||
|
||||
public List<ShopManageGroupItemVo> list(Long createdById, List<Long> companyMemberIds) {
|
||||
LambdaQueryWrapper<ShopManageGroupEntity> qw = new LambdaQueryWrapper<ShopManageGroupEntity>()
|
||||
.orderByAsc(ShopManageGroupEntity::getId);
|
||||
if (createdById != null && createdById > 0) {
|
||||
// 这两条原本是平铺的 .eq(...).or(...):MP 的 normal 段按顺序拼 AND/OR,MySQL 里 AND 优先,
|
||||
// 外面再叠一条公司条件时 created_by_id = ? 那半边会整块逃出筛选(多放行、不报错)⇒ 必须收进 and()。
|
||||
qw.and(w -> w.eq(ShopManageGroupEntity::getCreatedById, createdById)
|
||||
.or(x -> x.eq(ShopManageGroupEntity::getUserId, createdById)));
|
||||
}
|
||||
if (companyMemberIds != null) {
|
||||
if (companyMemberIds.isEmpty()) {
|
||||
qw.eq(ShopManageGroupEntity::getId, -1L);
|
||||
} else {
|
||||
// 公司条件同样自成一组(G2 护栏钉的是 AND (…) 这个形状;平铺 IN 与它在语义上等价)。
|
||||
qw.and(w -> w.in(ShopManageGroupEntity::getCreatedById, companyMemberIds));
|
||||
}
|
||||
}
|
||||
return toVoList(groupMapper.selectList(qw));
|
||||
}
|
||||
|
||||
public List<ShopManageGroupItemVo> listAccessible(Long operatorId, boolean superAdmin) {
|
||||
|
||||
+25
-2
@@ -28,7 +28,8 @@ public class ShopManageService {
|
||||
private final ShopCredentialCryptoService shopCredentialCryptoService;
|
||||
private final ZiniaoShopIndexRefreshTrigger ziniaoShopIndexRefreshTrigger;
|
||||
|
||||
public ShopManagePageVo page(long page, long pageSize, Long groupId, String shopName, Long operatorId, boolean superAdmin) {
|
||||
public ShopManagePageVo page(long page, long pageSize, Long groupId, String shopName, Long operatorId,
|
||||
boolean superAdmin, List<Long> companyMemberIds) {
|
||||
long safePage = Math.max(page, 1);
|
||||
long safePageSize = Math.min(Math.max(pageSize, 1), 100);
|
||||
String safeShopName = shopName == null ? "" : shopName.trim();
|
||||
@@ -60,6 +61,16 @@ public class ShopManageService {
|
||||
}
|
||||
}
|
||||
|
||||
// companyMemberIds 由 controller 算好传进来(service 手上只有 operatorId + 一个布尔,
|
||||
// 而 readScope 要 AdminUserEntity)。null = 不筛,与"超管没选公司"的今天逐字一致。
|
||||
if (companyMemberIds != null) {
|
||||
if (companyMemberIds.isEmpty()) {
|
||||
query.eq(ShopManageEntity::getId, -1L);
|
||||
} else {
|
||||
query.in(ShopManageEntity::getCreatedById, companyMemberIds);
|
||||
}
|
||||
}
|
||||
|
||||
Long total = shopManageMapper.selectCount(query);
|
||||
List<ShopManageEntity> rows = shopManageMapper
|
||||
.selectList(query.last("LIMIT " + ((safePage - 1) * safePageSize) + ", " + safePageSize));
|
||||
@@ -152,7 +163,7 @@ public class ShopManageService {
|
||||
* <p>管理端 {@code GET /{id}/credential} 此前忽略路径里的 id、改用 shop_name 查询——
|
||||
* 路径声明的对象与实际读取的对象可能不一致(同名的第一条被读写,审计日志却记成另一个 id)。
|
||||
*/
|
||||
public ShopManageCredentialVo getCredentialById(Long id) {
|
||||
public ShopManageCredentialVo getCredentialById(Long id, Long operatorId, boolean superAdmin) {
|
||||
if (id == null || id <= 0) {
|
||||
throw new BusinessException("店铺 ID 不合法");
|
||||
}
|
||||
@@ -160,6 +171,18 @@ public class ShopManageService {
|
||||
if (entity == null) {
|
||||
throw new BusinessException("店铺不存在");
|
||||
}
|
||||
// 尺子必须与 page() 那一条同形:created_by_id IN creators OR group_id IN groups。
|
||||
// 账号集合表达不了分组那一腿(店铺建在我可访问的分组里、创建人却不在我的账号集),
|
||||
// 拿它当闸门会把列表看得见的行读凭据时 403 掉——那是把正当访问静默删掉。
|
||||
if (!superAdmin) {
|
||||
Set<Long> creators = shopManageGroupService.listAccessibleCreatorUserIds(operatorId, false);
|
||||
Set<Long> groups = shopManageGroupService.listAccessibleGroupIds(operatorId, false);
|
||||
boolean visible = (entity.getCreatedById() != null && creators.contains(entity.getCreatedById()))
|
||||
|| (entity.getGroupId() != null && groups.contains(entity.getGroupId()));
|
||||
if (!visible) {
|
||||
throw new BusinessException(403, "无权查看该店铺的凭据");
|
||||
}
|
||||
}
|
||||
return toCredentialVo(entity);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user