fix(商品类目): 非超管读侧钉本公司 + 写侧补行归属闸门

10-06 逐节点复验实测:公司 2 的 admin 读 /api/admin/product-categories 仍得 412 行,
其中 394 行是公司 1 的自建类目。根因是这一页走的是第二道门 readCompanyId ——
它「非超管返回 null(不筛)」是为品牌库「靠分组可见的行」刻意设计的,
而 biz_product_category 连账号列都没有,null 在这里就是把整棵树交给每一家。

- DataReadScope 新增第三道门 readCompanyIdOrOwn:超管按 tab,非超管(含
  company_super_admin)一律 requireCompanyId(operator),带别家 tab 也不越权、也不返回空
  (类目树没有「空树」这个可用视图,返回空会连自建根都挂不上)。
- 写侧同批补闸门:读收窄而写不收窄=藏起来的行照样能改名/删除,那是静默删除不是隔离。
  update/delete 收 writeCompanyId(operator),与行自己的 company_id 比(不与 tab 比:
  内置行归哨兵 0,任何公司账号都不该改得动它);判定复用 CompanyScopeSupport
  .assertSameCompany,不再各处比一次。
- 品牌库那扇门与其三条「非超管=null」用例原样保留 —— 那是另一张表的正确口径。
- 护栏:G2 把第三道门接进 SCOPE_DOORS/SCOPE_CALLEE_NAMES,并自带能红/能绿两向探针
  (只认 readScope 的自检等于第二、三道门没人看守);新增 14 例,后端全量 3841 例 0 红。
This commit is contained in:
2026-10-06 11:44:20 +08:00
parent 581a596836
commit 4596e7d840
7 changed files with 355 additions and 27 deletions
@@ -50,7 +50,7 @@ public class ProductCategoryController {
@RequestParam(value = "keyword", required = false) String keyword,
@Parameter(description = "公司 ID") @RequestParam(name = "company_id", required = false) Long companyId) {
AdminUserEntity operator = adminAuthSupport.requireAdminOrInternal(request);
Long scopedCompanyId = dataReadScope.readCompanyId(operator, companyId);
Long scopedCompanyId = dataReadScope.readCompanyIdOrOwn(operator, companyId);
return ApiResponse.success(productCategoryService.list(keyword, scopedCompanyId));
}
@@ -63,7 +63,7 @@ public class ProductCategoryController {
@RequestParam(value = "page_size", defaultValue = "20") Long pageSize,
@Parameter(description = "公司 ID") @RequestParam(name = "company_id", required = false) Long companyId) {
AdminUserEntity operator = adminAuthSupport.requireAdminOrInternal(request);
Long scopedCompanyId = dataReadScope.readCompanyId(operator, companyId);
Long scopedCompanyId = dataReadScope.readCompanyIdOrOwn(operator, companyId);
return ApiResponse.success(productCategoryService.children(parentId, page, pageSize, scopedCompanyId));
}
@@ -76,7 +76,7 @@ public class ProductCategoryController {
@RequestParam(value = "page_size", defaultValue = "20") Long pageSize,
@Parameter(description = "公司 ID") @RequestParam(name = "company_id", required = false) Long companyId) {
AdminUserEntity operator = adminAuthSupport.requireAdminOrInternal(request);
Long scopedCompanyId = dataReadScope.readCompanyId(operator, companyId);
Long scopedCompanyId = dataReadScope.readCompanyIdOrOwn(operator, companyId);
return ApiResponse.success(productCategoryService.search(keyword, page, pageSize, scopedCompanyId));
}
@@ -87,7 +87,7 @@ public class ProductCategoryController {
@RequestParam(value = "keyword", required = false) String keyword,
@Parameter(description = "公司 ID") @RequestParam(name = "company_id", required = false) Long companyId) {
AdminUserEntity operator = adminAuthSupport.requireAdminOrInternal(request);
Long scopedCompanyId = dataReadScope.readCompanyId(operator, companyId);
Long scopedCompanyId = dataReadScope.readCompanyIdOrOwn(operator, companyId);
byte[] bytes = productCategoryService.export(keyword, scopedCompanyId);
String filename = "product-categories-" + LocalDateTime.now().format(EXPORT_FILENAME_FORMATTER) + ".xlsx";
return ResponseEntity.ok()
@@ -114,8 +114,10 @@ public class ProductCategoryController {
HttpServletRequest request,
@PathVariable Long id,
@Valid @RequestBody ProductCategorySaveRequest body) {
adminAuthSupport.requireAdminOrInternal(request);
return ApiResponse.success("保存成功", productCategoryService.update(id, body));
// 写侧闸门与读侧同源:读已经收窄到本公司,写不收窄就是"藏起来的行照样能删"。
AdminUserEntity operator = adminAuthSupport.requireAdminOrInternal(request);
return ApiResponse.success("保存成功",
productCategoryService.update(id, body, dataReadScope.writeCompanyId(operator)));
}
@DeleteMapping("/product-category/{id}")
@@ -123,8 +125,8 @@ public class ProductCategoryController {
public ApiResponse<Void> delete(
HttpServletRequest request,
@PathVariable Long id) {
adminAuthSupport.requireAdminOrInternal(request);
productCategoryService.delete(id);
AdminUserEntity operator = adminAuthSupport.requireAdminOrInternal(request);
productCategoryService.delete(id, dataReadScope.writeCompanyId(operator));
return ApiResponse.success("删除成功", null);
}
}
@@ -148,12 +148,18 @@ public class ProductCategoryService {
return findItem(entity.getId());
}
/**
* @param writableCompanyId 行归属闸门({@code DataReadScope.writeCompanyId} 给):
* {@code null} = 平台超管不限,否则只能动这一家自己的行。
* 读侧收窄而写侧不收窄="藏起来的行仍然能删",那是静默删除不是隔离。
*/
@Transactional
public ProductCategoryItemVo update(Long id, ProductCategorySaveRequest request) {
public ProductCategoryItemVo update(Long id, ProductCategorySaveRequest request, Long writableCompanyId) {
ProductCategoryEntity entity = getById(id);
assertRowWritable(entity, writableCompanyId);
String name = normalizeRequired(request.getName(), "类目名称不能为空");
Long parentId = normalizeParentId(request.getParentId());
// 改行时" Acting 公司"取行自己的归属,不取 tab:内置行归 0、自建行归那一家,
// 改行时"生效公司"取行自己的归属,不取 tab:内置行归 0、自建行归那一家,
// 换父级时不能把行挪到可见集外的节点下面(那会让它从原本看得见它的 tab 里消失)。
Long companyId = entity.getCompanyId();
ensureParentVisible(parentId, id, companyId);
@@ -168,8 +174,9 @@ public class ProductCategoryService {
}
@Transactional
public void delete(Long id) {
public void delete(Long id, Long writableCompanyId) {
ProductCategoryEntity entity = getById(id);
assertRowWritable(entity, writableCompanyId);
Long childCount = productCategoryMapper.selectCount(new LambdaQueryWrapper<ProductCategoryEntity>()
.eq(ProductCategoryEntity::getParentId, entity.getId()));
if (childCount != null && childCount > 0) {
@@ -178,6 +185,22 @@ public class ProductCategoryService {
productCategoryMapper.deleteById(entity.getId());
}
/**
* 行归属闸门。走 {@code assertSameCompany} 而不是在这里比一次:它对本仓所有"按公司写"的闸门
* 是唯一一份判定,两处各写就会有一处先漂。
*
* <p>内置节点(哨兵 0)在这一支里必然被拒——{@code 0 != 任何真实公司 id},
* 而"公司账号能改/删平台内置类目"正是这一页最容易出事的一刀:改了它,
* 所有公司的类目树与货源归类的候选表同一天跟着变。</p>
*/
private void assertRowWritable(ProductCategoryEntity row, Long writableCompanyId) {
if (writableCompanyId == null) {
return;
}
companyScopeSupport.assertSameCompany(writableCompanyId, row.getCompanyId(),
"无权修改该类目:内置类目或别家公司的类目不在本公司的写范围内");
}
private ProductCategoryItemVo findItem(Long id) {
ProductCategoryEntity entity = getById(id);
Long childCount = productCategoryMapper.selectCount(new LambdaQueryWrapper<ProductCategoryEntity>()