fix(jd): 分类可见沿管理链上溯,修普通管理员看不到同组分类
线上账号链:阿华112336(normal,1366) → 阿华226655(admin,1365) → 阿华66 (company_super_admin,1353)。旧实现只沿 created_by_id 走一跳,中间 1365 不带组 时可见集为空,分类页报「本组暂无分类」——实际 5 个分类都挂在 1353 建的组 49 下。 - JdCategoryScopeService 新增 collectAncestorGroups:沿 created_by_id 递归上溯, 带已访问集合防成环、深度上限 10、链断即停; - 只继承同公司的组(company_id 相等):平台超管是整片下游的根,不加这道闸会把 别家公司建的组顺链漏进来;HEAD 原有「一跳上级」路径补同一道过滤; - 继承放在可写集计算之后,只影响可见(只读),writable 不因上溯放宽; - 判据 +4:两跳链、继承只读+同级隔离、成环不死循环、跨公司不继承。 反向验证:去掉同公司过滤→跨公司用例红;链收成一跳→两跳用例红。 验证:本提交内容(HEAD+本改动)在干净树 JdCategoryScopeServiceTest 23/23 绿。
This commit is contained in:
+52
-4
@@ -32,9 +32,10 @@ import java.util.Set;
|
|||||||
* <ul>
|
* <ul>
|
||||||
* <li><b>平台超管</b>:全部公司全部<b>京东档</b>组可读可写;</li>
|
* <li><b>平台超管</b>:全部公司全部<b>京东档</b>组可读可写;</li>
|
||||||
* <li><b>公司超管</b>:本公司全部<b>京东档</b>组可读可写(与「公司超管看本公司全部成员」同源);</li>
|
* <li><b>公司超管</b>:本公司全部<b>京东档</b>组可读可写(与「公司超管看本公司全部成员」同源);</li>
|
||||||
* <li><b>管理员</b>:可见=自己带的组 + 上级带的组 + 自己所属组的组长带的组(过平台闸后);
|
* <li><b>管理员</b>:可见=自己带的组 + 上级带的组 + 自己所属组的组长带的组 + 管理链上级
|
||||||
* 可写=全部可见组(2026-11-23 客户口径「管理人员只有分类权限」,不再要求组长身份);</li>
|
* 各账号带的组(沿 created_by_id 上溯、只读;2026-11-24 客户「普通管理员看不到同组
|
||||||
* <li><b>普通号</b>:可见同上,可写空(能开弹窗看,不能增删改分类、不能归类)。</li>
|
* 管理员的分类」);可写=上溯之前的可见组(继承来的组只读,不因上溯放宽写权限);</li>
|
||||||
|
* <li><b>普通号</b>:可见同上(含管理链上溯),可写空(能开弹窗看,不能增删改分类、不能归类)。</li>
|
||||||
* </ul>
|
* </ul>
|
||||||
*
|
*
|
||||||
* <p>平台超管判定走 {@link DataReadScope#isPlatformSuperAdmin}({@code isSuper} 的唯一包装),
|
* <p>平台超管判定走 {@link DataReadScope#isPlatformSuperAdmin}({@code isSuper} 的唯一包装),
|
||||||
@@ -52,6 +53,9 @@ import java.util.Set;
|
|||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class JdCategoryScopeService {
|
public class JdCategoryScopeService {
|
||||||
|
|
||||||
|
/** 管理链上溯深度上限:防脏数据成环(created_by_id 自指/互指),正常公司链远小于此。 */
|
||||||
|
private static final int MAX_ANCESTOR_DEPTH = 10;
|
||||||
|
|
||||||
private final AdminAuthSupport adminAuthSupport;
|
private final AdminAuthSupport adminAuthSupport;
|
||||||
private final DataReadScope dataReadScope;
|
private final DataReadScope dataReadScope;
|
||||||
private final AdminUserMapper adminUserMapper;
|
private final AdminUserMapper adminUserMapper;
|
||||||
@@ -162,7 +166,15 @@ public class JdCategoryScopeService {
|
|||||||
// 可见集先收全:自己带的组 + 上级(created_by_id)带的组 + 作为成员所属组的组长带的组。
|
// 可见集先收全:自己带的组 + 上级(created_by_id)带的组 + 作为成员所属组的组长带的组。
|
||||||
collect(visible, ledGroups(user.getId()));
|
collect(visible, ledGroups(user.getId()));
|
||||||
if (user.getCreatedById() != null && user.getCreatedById() > 0) {
|
if (user.getCreatedById() != null && user.getCreatedById() > 0) {
|
||||||
collect(visible, ledGroups(user.getCreatedById()));
|
// 上级的组只收同公司(2026-11-24):原实现不看公司,平台超管在别家公司建的组
|
||||||
|
// 会顺着 created_by_id 漏进本账号可见集。
|
||||||
|
Long parentCompanyId = user.getCompanyId();
|
||||||
|
for (ShopManageGroupEntity group : ledGroups(user.getCreatedById())) {
|
||||||
|
if (group != null && parentCompanyId != null
|
||||||
|
&& parentCompanyId.equals(group.getCompanyId())) {
|
||||||
|
collect(visible, List.of(group));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
List<Long> memberOfLeaders = shopManageGroupMapper.selectLeaderIdsByMemberUserId(user.getId());
|
List<Long> memberOfLeaders = shopManageGroupMapper.selectLeaderIdsByMemberUserId(user.getId());
|
||||||
if (memberOfLeaders != null) {
|
if (memberOfLeaders != null) {
|
||||||
@@ -178,6 +190,9 @@ public class JdCategoryScopeService {
|
|||||||
if ("admin".equals(role)) {
|
if ("admin".equals(role)) {
|
||||||
writable.addAll(visible.keySet());
|
writable.addAll(visible.keySet());
|
||||||
}
|
}
|
||||||
|
// 管理链继承放在可写集计算之后:只让上级链上的组变得可读,绝不把继承来的组塞进
|
||||||
|
// writable(那会让下级能改上级的分类,把「管理员之间不能互看」从读侧破到写侧)。
|
||||||
|
collectAncestorGroups(visible, user);
|
||||||
}
|
}
|
||||||
// 2026-11-17 用户口径「同理京东那边也是」:京东侧作用域只认京东档(platform='jd')的
|
// 2026-11-17 用户口径「同理京东那边也是」:京东侧作用域只认京东档(platform='jd')的
|
||||||
// 数据权限分组——亚马逊档与 NULL 一律不进下拉。V206 存量默认 amazon、京东侧数据由编辑
|
// 数据权限分组——亚马逊档与 NULL 一律不进下拉。V206 存量默认 amazon、京东侧数据由编辑
|
||||||
@@ -208,6 +223,39 @@ public class JdCategoryScopeService {
|
|||||||
return rows == null ? List.of() : rows;
|
return rows == null ? List.of() : rows;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 把「管理链上各账号带的组」并入可见集(2026-11-24 客户口径「普通管理员看不到同组管理员的
|
||||||
|
* 分类」:管理员/员工要能看到自己上级链上超管、管理员建的分类)。
|
||||||
|
*
|
||||||
|
* <p>只沿 {@code users.created_by_id} 向上走,且带已访问集合:脏数据成环(自指/互指)与
|
||||||
|
* 超深链都在这里停下,绝不进入死循环;上级账号已被删除({@code selectById} 回 null)时
|
||||||
|
* 同样停止——链断了就不往上猜。命中只进可见集(只读),可写集在调用前已算定,所以平行
|
||||||
|
* 管理员之间互相看不到、也改不了对方自己建的组。</p>
|
||||||
|
*
|
||||||
|
* <p><b>只继承同公司的组</b>:平台超管的 created_by_id 是整片下游的根,若不加这道闸,
|
||||||
|
* 他在 A 公司建的组会顺着链漏给 B 公司的管理员——公司隔离(V203 公司→组两级)就破了。
|
||||||
|
* 组行 company_id 为 NULL 的存量无法归属,一并不继承(宁可少看,不越权)。</p>
|
||||||
|
*/
|
||||||
|
private void collectAncestorGroups(Map<Long, ShopManageGroupEntity> visible, AdminUserEntity user) {
|
||||||
|
Long companyId = user.getCompanyId();
|
||||||
|
if (companyId == null || companyId <= 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Set<Long> visited = new LinkedHashSet<>();
|
||||||
|
visited.add(user.getId());
|
||||||
|
Long parentId = user.getCreatedById();
|
||||||
|
for (int depth = 0; depth < MAX_ANCESTOR_DEPTH
|
||||||
|
&& parentId != null && parentId > 0 && visited.add(parentId); depth++) {
|
||||||
|
for (ShopManageGroupEntity group : ledGroups(parentId)) {
|
||||||
|
if (group != null && companyId.equals(group.getCompanyId())) {
|
||||||
|
collect(visible, List.of(group));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
AdminUserEntity parent = adminUserMapper.selectById(parentId);
|
||||||
|
parentId = parent == null ? null : parent.getCreatedById();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static void collect(Map<Long, ShopManageGroupEntity> target, List<ShopManageGroupEntity> rows) {
|
private static void collect(Map<Long, ShopManageGroupEntity> target, List<ShopManageGroupEntity> rows) {
|
||||||
if (rows == null) {
|
if (rows == null) {
|
||||||
return;
|
return;
|
||||||
|
|||||||
+96
@@ -21,6 +21,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
import static org.mockito.ArgumentMatchers.anyLong;
|
import static org.mockito.ArgumentMatchers.anyLong;
|
||||||
|
import static org.mockito.Mockito.atLeastOnce;
|
||||||
import static org.mockito.Mockito.never;
|
import static org.mockito.Mockito.never;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
@@ -236,6 +237,101 @@ class JdCategoryScopeServiceTest {
|
|||||||
assertThat(scope.groups()).isEmpty();
|
assertThat(scope.groups()).isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 2026-11-24 客户口径「普通管理员看不到同组管理员的分类」(线上账号 阿华112336 的形状:
|
||||||
|
* 员工 1366 → 管理员 1365(不带组)→ 公司超管 1353(带组 49)):可见组沿 created_by_id
|
||||||
|
* <b>两跳</b>上溯补齐,员工只读。
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void 普通号_沿管理链两跳看到超管建的组_只读() {
|
||||||
|
AdminUserEntity operator = user(USER_ID, COMPANY_ID);
|
||||||
|
operator.setCreatedById(5L);
|
||||||
|
when(adminAuthSupport.currentRole(operator)).thenReturn("normal");
|
||||||
|
when(dataReadScope.isPlatformSuperAdmin(operator)).thenReturn(false);
|
||||||
|
when(shopManageGroupMapper.selectLedGroups(USER_ID)).thenReturn(List.of());
|
||||||
|
when(shopManageGroupMapper.selectLeaderIdsByMemberUserId(USER_ID)).thenReturn(List.of());
|
||||||
|
AdminUserEntity admin = user(5L, COMPANY_ID);
|
||||||
|
admin.setCreatedById(7L);
|
||||||
|
when(adminUserMapper.selectById(5L)).thenReturn(admin);
|
||||||
|
when(adminUserMapper.selectById(7L)).thenReturn(user(7L, COMPANY_ID));
|
||||||
|
when(shopManageGroupMapper.selectLedGroups(5L)).thenReturn(List.of());
|
||||||
|
when(shopManageGroupMapper.selectLedGroups(7L)).thenReturn(List.of(group(49L, COMPANY_ID, "测试1111")));
|
||||||
|
|
||||||
|
JdCategoryScopeService.JdCategoryScope scope = service.resolve(operator);
|
||||||
|
|
||||||
|
assertThat(scope.visibleGroupIds()).containsExactly(49L);
|
||||||
|
assertThat(scope.writableGroupIds()).isEmpty();
|
||||||
|
assertThat(scope.defaultGroupId()).isEqualTo(49L);
|
||||||
|
assertThat(scope.groups()).singleElement()
|
||||||
|
.satisfies(option -> assertThat(option.isEditable()).isFalse());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 「不同组不能看」仍成立:继承只沿自己的上级链。与上级共用同一上游的同级账号(另一条链)
|
||||||
|
* 自己建的组不进可见集;继承来的组也不因它变得可写。
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void 沿管理链继承上级组_同级账号自己建的组仍不可见() {
|
||||||
|
AdminUserEntity operator = user(USER_ID, COMPANY_ID);
|
||||||
|
operator.setCreatedById(3L);
|
||||||
|
when(adminAuthSupport.currentRole(operator)).thenReturn("normal");
|
||||||
|
when(dataReadScope.isPlatformSuperAdmin(operator)).thenReturn(false);
|
||||||
|
when(shopManageGroupMapper.selectLedGroups(USER_ID)).thenReturn(List.of(group(10L, COMPANY_ID, "自己的组")));
|
||||||
|
when(shopManageGroupMapper.selectLeaderIdsByMemberUserId(USER_ID)).thenReturn(List.of());
|
||||||
|
when(adminUserMapper.selectById(3L)).thenReturn(user(3L, COMPANY_ID));
|
||||||
|
when(shopManageGroupMapper.selectLedGroups(3L)).thenReturn(List.of(group(20L, COMPANY_ID, "上级的组")));
|
||||||
|
|
||||||
|
JdCategoryScopeService.JdCategoryScope scope = service.resolve(operator);
|
||||||
|
|
||||||
|
assertThat(scope.visibleGroupIds()).containsExactly(10L, 20L);
|
||||||
|
assertThat(scope.writableGroupIds()).isEmpty();
|
||||||
|
assertThat(scope.canRead(20L)).isTrue();
|
||||||
|
assertThat(scope.canWrite(20L)).isFalse();
|
||||||
|
// 同级账号(另一条 created_by_id 链)自己建的组 30 不在本账号的链上 ⇒ 不可见。
|
||||||
|
assertThat(scope.canRead(30L)).isFalse();
|
||||||
|
assertThat(scope.canWrite(30L)).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 管理链成环(脏数据自指/互指)必须停下,不得死循环。 */
|
||||||
|
@Test
|
||||||
|
void 管理链成环不死循环() {
|
||||||
|
AdminUserEntity operator = user(USER_ID, COMPANY_ID);
|
||||||
|
operator.setCreatedById(4L);
|
||||||
|
when(adminAuthSupport.currentRole(operator)).thenReturn("normal");
|
||||||
|
when(dataReadScope.isPlatformSuperAdmin(operator)).thenReturn(false);
|
||||||
|
when(shopManageGroupMapper.selectLedGroups(USER_ID)).thenReturn(List.of());
|
||||||
|
when(shopManageGroupMapper.selectLeaderIdsByMemberUserId(USER_ID)).thenReturn(List.of());
|
||||||
|
AdminUserEntity parent = user(4L, COMPANY_ID);
|
||||||
|
parent.setCreatedById(USER_ID);
|
||||||
|
when(adminUserMapper.selectById(4L)).thenReturn(parent);
|
||||||
|
when(shopManageGroupMapper.selectLedGroups(4L)).thenReturn(List.of(group(11L, COMPANY_ID, "A组")));
|
||||||
|
|
||||||
|
JdCategoryScopeService.JdCategoryScope scope = service.resolve(operator);
|
||||||
|
|
||||||
|
assertThat(scope.visibleGroupIds()).containsExactly(11L);
|
||||||
|
verify(shopManageGroupMapper, atLeastOnce()).selectLedGroups(4L);
|
||||||
|
assertThat(scope.writableGroupIds()).isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 管理链上溯只继承<b>同公司</b>的组:平台超管根下别家公司建的组不得顺链漏进来。 */
|
||||||
|
@Test
|
||||||
|
void 管理链上溯_别家公司的组不继承() {
|
||||||
|
AdminUserEntity operator = user(USER_ID, COMPANY_ID);
|
||||||
|
operator.setCreatedById(3L);
|
||||||
|
when(adminAuthSupport.currentRole(operator)).thenReturn("normal");
|
||||||
|
when(dataReadScope.isPlatformSuperAdmin(operator)).thenReturn(false);
|
||||||
|
when(shopManageGroupMapper.selectLedGroups(USER_ID)).thenReturn(List.of());
|
||||||
|
when(shopManageGroupMapper.selectLeaderIdsByMemberUserId(USER_ID)).thenReturn(List.of());
|
||||||
|
when(adminUserMapper.selectById(3L)).thenReturn(user(3L, COMPANY_ID));
|
||||||
|
when(shopManageGroupMapper.selectLedGroups(3L)).thenReturn(List.of(
|
||||||
|
group(21L, COMPANY_ID, "同公司组"), group(22L, 99L, "别家公司组")));
|
||||||
|
|
||||||
|
JdCategoryScopeService.JdCategoryScope scope = service.resolve(operator);
|
||||||
|
|
||||||
|
assertThat(scope.visibleGroupIds()).containsExactly(21L);
|
||||||
|
assertThat(scope.canRead(22L)).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
// ---------------------------------------------------------------- 入参收敛
|
// ---------------------------------------------------------------- 入参收敛
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Reference in New Issue
Block a user