fix(客户10-08): 分组名收到同公司唯一、去重按分组写不再 403、登录态失效前端会真退出

付费客户「先锋-团队」一次报的四件事,根因各自独立,这一版一起收:

1) 去重数据汇总「新增导入」选自己公司的分组提交必 403。DedupeTotalDataService
   .resolveAccessScope 给 company_super_admin 时只开了 wholeCompany 那一支、groupIds
   留空集,而写侧闸门 resolveWritableGroup / ensureImportTaskAccess 只认 allUsers 与
   groupIds——读侧宽、写侧窄,表现就是「下拉选得到、提交就 403」,且 403 是 HTTP 状态
   不是中文文案,前端只能显示 Request failed with status code 403。改为同一口径把本公司的
   分组 id 一起带上(src/main/.../DedupeTotalDataService.java:86,104,112 + 注释)。

2) 「暂无分组」与「分组名称已存在」是同一行的两面:老「先锋」的组长 uid=1188 已被删,
   分组 id=33 成了孤儿行,仍占着全平台唯一的组名;而 list() 只按 created_by_id 筛,
   这一行被每一个公司的 tab 静默排除(生产 13 个分组里 2 个组长只落在旧列 user_id 上)。
   V153 给 biz_shop_manage_group 加 company_id(建组按组长定死、改名按组长当前公司纠回,
   顺手治「移人」造成的落值漂移),唯一键换成 uk_company_group_name(company_id, group_name),
   先 ADD 后 DROP。校验收窄到本公司,文案也从「分组名称已存在」改成「本公司内已存在同名分组」。
   MySQL 唯一索引里 NULL 互不相异 ⇒ 迁移上线后 (NULL,'先锋') 不挡 (3,'先锋'),
   客户当场就能建组,不需要先清那两行孤儿数据。

3) 删号/凭证过期后「软件还能用、没有强制退出」:前端 http 拦截器只认业务码 4011(互踢),
   JWT 过期或被拒返回的 401 一路静默,页面停在原处。新增 handleSessionExpired(),
   401 与「code/status=401」都清本地登录态并跳 /login?expired=1;匿名 401 不收口
   (那是常态噪声,且没登录态可清)。403 仍不触发下线——那是权限缺口,不能说成登录坏了。

4) 互踢(客户要「支持同时登录」)走配置不改码:AIIMAGE_AUTH_SINGLE_DEVICE_ENABLED=false,
   开关只有 3 个消费点、OFF 支已有用例钉着。这里只把公司超管的角色串收成
   DeviceSessionPolicy.COMPANY_SUPER_ADMIN,并记下它 src/main 仍有 22 处手抄比较
   (SuperAdminPredicateSingleOutletTest 那支绊线只管 super_admin,别以为它也在守这一串)。

⚠ 本提交带着并行会话 10-07 那批分组公司归属校验一起上线(ShopManageGroupService 的
ensureLeaderSharesOperatorCompany / resolveMembers 公司校验等,约 58 行):HEAD 里这个类
连 companyScope 字段都没有,我的写侧代码压在同一处,逐 hunk 拆不出「纯本批」的一版,
拆出来的也编不过。他那支 ShopManageGroupCompanyScopeTest(5 例)一并收了,否则这 58 行进仓
却没有用例。他的 ShopKey*/AdminUserService/admin-frontend-vue 账号页那批一个字没动,仍在
工作树里等他自己发。

验证:mvn -o clean test 全量在 detached worktree(HEAD + 本批白名单)跑;定向 8 支类 61 例 0 红;
shopkey+dedupe 整包 130 例 0 红;本批判据变异自证 6/6 被杀(每轮还原后与注入前哈希逐字节一致);
前端 node --test 21 例 0 红、变异 3/3。回滚:jar 单独回退即可,V153 的手术式回滚见
backend-java/docs/rollback-V153-shop-manage-group-company-id.sql(不要 DROP COLUMN,它是资产)。
This commit is contained in:
2026-10-08 19:04:51 +08:00
parent de526f51c6
commit a24d7b9bac
15 changed files with 1091 additions and 38 deletions
@@ -15,6 +15,9 @@ public class ShopManageGroupEntity {
@TableId(type = IdType.AUTO)
private Long id;
private String groupName;
/** 归属公司(V153):写入时由组长账号推导;NULL = 组长账号已不存在、归属无从追溯。 */
@TableField("company_id")
private Long companyId;
@TableField("user_id")
private Long userId;
@TableField("created_by_id")
@@ -25,16 +25,24 @@ public final class DeviceSessionPolicy {
/** {@code super_admin} 的字面量在 src/main 只允许出现在这一处;比较口径见 {@link #isSuper(String)}。 */
public static final String SUPER_ADMIN = "super_admin";
/**
* 公司超管的角色串。与 {@link #SUPER_ADMIN} 不同,它在 src/main 里还有 <b>22 处手抄比较</b>
* (6 个文件:AdminUserService 14、PermissionMenuService 4、去重/分组/到期停任务/看板各 1;
* {@code SuperAdminPredicateSingleOutletTest} 那支绊线只管 super_admin)。
* 要收成一处,得连「业务数据面同权、管理动作不同」那条定案一起落,别只搬字面量。
*/
public static final String COMPANY_SUPER_ADMIN = "company_super_admin";
/** 计算用户管理角色:super_admin / company_super_admin / admin / null(含老数据 role 为空时按 created_by_id 推断)。 */
public static String resolveRole(String role, Integer isAdmin, Long createdById) {
String storedRole = role == null ? "" : role.trim().toLowerCase();
if (SUPER_ADMIN.equals(storedRole)) {
return SUPER_ADMIN;
}
if ("company_super_admin".equals(storedRole)) {
if (COMPANY_SUPER_ADMIN.equals(storedRole)) {
// 公司超级管理员(2026-10-05):单家公司内的最高账号,数据只看本公司,由站点
// super_admin 创建。必须先于 is_admin 兼容推断返回,否则会被顶成站点超管。
return "company_super_admin";
return COMPANY_SUPER_ADMIN;
}
if ("admin".equals(storedRole)) {
return "admin";
@@ -21,6 +21,7 @@ import com.nanri.aiimage.common.model.entity.AdminUserEntity;
import com.nanri.aiimage.common.mapper.ShopManageGroupMapper;
import com.nanri.aiimage.common.model.entity.ShopManageGroupEntity;
import com.nanri.aiimage.common.service.CompanyScopeSupport;
import com.nanri.aiimage.modules.shopkey.service.ShopManageGroupService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
@@ -82,6 +83,7 @@ public class DedupeTotalDataService {
private final DedupeTotalDataMapper dedupeTotalDataMapper;
private final AdminUserMapper adminUserMapper;
private final ShopManageGroupMapper shopManageGroupMapper;
private final ShopManageGroupService shopManageGroupService;
private final CompanyScopeSupport companyScopeSupport;
private final PlatformTransactionManager transactionManager;
@@ -99,6 +101,7 @@ public class DedupeTotalDataService {
public DedupeTotalDataService(DedupeTotalDataMapper dedupeTotalDataMapper,
AdminUserMapper adminUserMapper,
ShopManageGroupMapper shopManageGroupMapper,
ShopManageGroupService shopManageGroupService,
CompanyScopeSupport companyScopeSupport,
PlatformTransactionManager transactionManager,
org.springframework.data.redis.core.StringRedisTemplate stringRedisTemplate,
@@ -106,6 +109,7 @@ public class DedupeTotalDataService {
this.dedupeTotalDataMapper = dedupeTotalDataMapper;
this.adminUserMapper = adminUserMapper;
this.shopManageGroupMapper = shopManageGroupMapper;
this.shopManageGroupService = shopManageGroupService;
this.companyScopeSupport = companyScopeSupport;
this.transactionManager = transactionManager;
this.importProgressMap = progressStore("aiimage:dedupe:import:progress", stringRedisTemplate, objectMapper);
@@ -1233,7 +1237,11 @@ public class DedupeTotalDataService {
companyUserIds.add(memberId);
}
}
return new AccessScope(false, Set.of(), Set.copyOf(companyUserIds), true);
// groupIds 必须一起给:写侧闸门(resolveAccessibleGroup / ensureImportTaskAccess)只认
// allUsers 与 groupIds 两样,读侧才走 wholeCompany 那一支。这里留空集的表现是
// 「下拉选得到自己公司的分组、提交必 403」,而 403 是 HTTP 状态不是中文文案。
Set<Long> companyGroupIds = shopManageGroupService.listCompanyGroupIds(List.copyOf(companyUserIds));
return new AccessScope(false, companyGroupIds, Set.copyOf(companyUserIds), true);
}
List<Long> accessibleGroupIds = shopManageGroupMapper.selectAccessibleGroupIds(operator.getId());
LinkedHashSet<Long> visibleUserIds = new LinkedHashSet<>();
@@ -7,6 +7,8 @@ import com.nanri.aiimage.common.model.entity.AdminUserEntity;
import com.nanri.aiimage.modules.shopkey.mapper.QueryAsinMapper;
import com.nanri.aiimage.modules.shopkey.spi.GroupDeletionGuardPort;
import com.nanri.aiimage.common.mapper.ShopManageGroupMapper;
import com.nanri.aiimage.common.security.DeviceSessionPolicy;
import com.nanri.aiimage.common.service.CompanyScopeSupport;
import com.nanri.aiimage.modules.shopkey.mapper.ShopManageGroupMemberMapper;
import com.nanri.aiimage.modules.shopkey.mapper.ShopManageMapper;
import com.nanri.aiimage.modules.shopkey.mapper.SkipPriceAsinMapper;
@@ -43,6 +45,7 @@ public class ShopManageGroupService {
private final QueryAsinMapper queryAsinMapper;
private final SkipPriceAsinMapper skipPriceAsinMapper;
private final AdminUserMapper adminUserMapper;
private final CompanyScopeSupport companyScope;
public List<ShopManageGroupItemVo> list() {
return list(null, null);
@@ -61,8 +64,13 @@ public class ShopManageGroupService {
if (companyMemberIds.isEmpty()) {
qw.eq(ShopManageGroupEntity::getId, -1L);
} else {
// 同样自成一组:上面那支 and() 里已经混着 OR,这条若平铺下去,AND 优先只会作用在 OR 的右半边。
qw.and(w -> w.in(ShopManageGroupEntity::getCreatedById, companyMemberIds));
// 两列一起认,与 listCompanyGroupIds 同一口径:生产 13 个分组里 2 个(组长只落在
// 旧列 user_id 上)只按 created_by_id 筛会被每一个公司 tab 静默删掉——不报错、不 403,
// 表现是"切到本公司,分组凭空少一半,而它们下面挂着几万行数据"。
// 仍要自成一组:上面那支 and() 里已经混着 OR,这条若平铺下去,AND 优先只会作用在
// OR 的右半边。
qw.and(w -> w.in(ShopManageGroupEntity::getCreatedById, companyMemberIds)
.or(x -> x.in(ShopManageGroupEntity::getUserId, companyMemberIds)));
}
}
return toVoList(groupMapper.selectList(qw));
@@ -159,12 +167,18 @@ public class ShopManageGroupService {
public ShopManageGroupItemVo create(ShopManageGroupCreateRequest request, Long operatorId, boolean superAdmin) {
Long createdById = normalizePositiveId(request.getCreatedById(), "创建人不能为空");
validateLeaderOperator(createdById, operatorId, superAdmin);
ensureLeaderSharesOperatorCompany(createdById, operatorId);
String groupName = normalizeRequired(request.getGroupName(), "分组名称不能为空");
ensureGroupNameUnique(groupName, null);
// 归属在写侧一次定死:分组的公司就是组长的公司(同 listCompanyGroupIds 的推导口径),
// 而 V153 之后"名字在哪家范围内唯一"就看这一列。取不到公司必须抛而不是留 NULL——
// NULL 会让这一行落进"孤儿"那一档,从此谁的公司 tab 都排不到它。
Long companyId = companyScope.requireCompanyId(adminUserMapper.selectById(createdById));
ensureGroupNameUnique(groupName, null, companyId);
ShopManageGroupEntity entity = new ShopManageGroupEntity();
entity.setGroupName(groupName);
entity.setCreatedById(createdById);
entity.setCompanyId(companyId);
groupMapper.insert(entity);
replaceMembers(entity.getId(), request.getMemberUserIds(), createdById, operatorId, superAdmin);
return toVo(getById(entity.getId()));
@@ -174,11 +188,20 @@ public class ShopManageGroupService {
public ShopManageGroupItemVo update(Long id, Long operatorId, boolean superAdmin, ShopManageGroupUpdateRequest request) {
ShopManageGroupEntity entity = getManageableById(id, operatorId, superAdmin);
String groupName = normalizeRequired(request.getGroupName(), "分组名称不能为空");
ensureGroupNameUnique(groupName, id);
// 每次改名都按组长当前公司重算并回写:账号可以被「移人」挪到别家公司,而移人不会来更新
// 这张表 ⇒ 落值会漂。在这里顺手纠回来,比另立一个漂移哨兵便宜。
// 组长推不出账号时 requireCompanyId 直接抛:孤儿分组本来就过不了上面的管理门。
Long companyId = companyScope.requireCompanyId(
adminUserMapper.selectById(resolveGroupLeaderUserId(entity)));
ensureGroupNameUnique(groupName, id, companyId);
entity.setGroupName(groupName);
entity.setCompanyId(companyId);
groupMapper.updateById(entity);
replaceMembers(entity.getId(), request.getMemberUserIds(), entity.getCreatedById(), operatorId, superAdmin);
// 组长取 resolveGroupLeaderUserId 而不是 created_by_id:生产有 2 个分组只有 user_id
// (见 listCompanyGroupIds 的注释),只认 created_by_id 会让那 2 个分组绕过组员的公司校验。
replaceMembers(entity.getId(), request.getMemberUserIds(), resolveGroupLeaderUserId(entity),
operatorId, superAdmin);
return toVo(getById(id));
}
@@ -229,15 +252,32 @@ public class ShopManageGroupService {
return entity;
}
private void ensureGroupNameUnique(String groupName, Long excludeId) {
/**
* 分组名<b>同一家公司内</b>唯一(V153 之前是全平台唯一,2026-10-08 定档改窄)。
*
* <p>依据是 {@code company_id} 这一列,而库级唯一键 {@code uk_company_group_name
* (company_id, group_name)} 与这里必须同口径:Java 少筛一次的表现是"报错了但库里其实允许",
* 多筛一次则是"这里放行、INSERT 撞 1062 变成 500"。</p>
*
* <p>报错只说"本公司内已存在",不报出别的公司有没有同名——那既是跨租户信息,
* 也不是调用方能处理的事。</p>
*/
private void ensureGroupNameUnique(String groupName, Long excludeId, Long companyId) {
LambdaQueryWrapper<ShopManageGroupEntity> query = new LambdaQueryWrapper<ShopManageGroupEntity>()
.eq(ShopManageGroupEntity::getGroupName, groupName);
// NULL 归属(组长账号已删的孤儿行)只能按 IS NULL 比:eq(col, null) 渲染成
// company_id = NULL,MySQL 里恒不成立 ⇒ 校验永远查不到占用者,等于把这条校验整支删掉。
if (companyId == null) {
query.isNull(ShopManageGroupEntity::getCompanyId);
} else {
query.eq(ShopManageGroupEntity::getCompanyId, companyId);
}
if (excludeId != null) {
query.ne(ShopManageGroupEntity::getId, excludeId);
}
Long count = groupMapper.selectCount(query);
if (count != null && count > 0) {
throw new BusinessException("分组名称已存在");
throw new BusinessException("本公司内已存在同名分组,请换一个名称");
}
}
@@ -252,10 +292,12 @@ public class ShopManageGroupService {
}
private void validateManageAccess(ShopManageGroupEntity entity, Long operatorId, boolean superAdmin) {
Long normalizedOperatorId = normalizePositiveId(operatorId, "操作人不能为空");
if (superAdmin) {
// 站点超管也按组长公司收窄:读侧已经按公司 tab 收窄了,写侧不收窄等于「列表里藏起来的分组仍然能改删」。
ensureLeaderSharesOperatorCompany(resolveGroupLeaderUserId(entity), normalizedOperatorId);
return;
}
Long normalizedOperatorId = normalizePositiveId(operatorId, "操作人不能为空");
boolean leaderBySelf = entity.getCreatedById() != null && entity.getCreatedById().equals(normalizedOperatorId);
boolean legacyLeaderBySelf = entity.getUserId() != null && entity.getUserId().equals(normalizedOperatorId);
if (!leaderBySelf && !legacyLeaderBySelf) {
@@ -263,6 +305,23 @@ public class ShopManageGroupService {
}
}
/**
* 组长必须与操作者同一家公司——站点超管因此不能替别家公司开分组(2026-10-07,与建号那条同口径)。
*
* <p>分组表没有 {@code company_id} 列,归属只能从组长账号推(同 {@link #listCompanyGroupIds});
* 推不出来时必须报错而不是放行:把"没有组长"读成"不限公司",等于给所有无主分组开了一个越权口子。</p>
*/
private void ensureLeaderSharesOperatorCompany(Long leaderId, Long operatorId) {
if (leaderId == null || leaderId <= 0) {
throw new BusinessException("该分组没有组长账号,无法确认所属公司");
}
Long leaderCompanyId = companyScope.requireCompanyId(adminUserMapper.selectById(leaderId));
Long operatorCompanyId = companyScope.requireCompanyId(adminUserMapper.selectById(operatorId));
if (!leaderCompanyId.equals(operatorCompanyId)) {
throw new BusinessException("只能维护本公司的分组:组长必须与操作者同一家公司");
}
}
private void validateLeaderOperator(Long leaderId, Long operatorId, boolean superAdmin) {
if (superAdmin) {
return;
@@ -304,6 +363,10 @@ public class ShopManageGroupService {
AdminUserEntity operatorUser = operatorId == null ? null : adminUserMapper.selectById(operatorId);
boolean operatorCanBindAllNormals = superAdmin
|| (operatorUser != null && "admin".equalsIgnoreCase(normalizeBlank(operatorUser.getUsername())));
// 组长的公司 = 这个分组的公司(分组表没有 company_id 列,见 listCompanyGroupIds 的注释)
Long leaderCompanyId = leaderId == null || leaderId <= 0
? null
: companyScope.requireCompanyId(adminUserMapper.selectById(leaderId));
LinkedHashSet<Long> uniqueIds = new LinkedHashSet<>();
for (Long memberUserId : memberUserIds) {
uniqueIds.add(normalizePositiveId(memberUserId, "组员用户不能为空"));
@@ -330,6 +393,18 @@ public class ShopManageGroupService {
if (leaderId != null && leaderId.equals(user.getId())) {
throw new BusinessException("组长不能作为组员重复加入分组");
}
// 超管(站点与公司两档)永不入组:它是那家公司的最高账号,进了组就等于把自己
// 关进某个组长的可见范围里。此前超管操作者能绕过这条,把别家公司的人(含别家超管)拉进分组。
boolean siteSuper = DeviceSessionPolicy.isSuperAdmin(
user.getRole(), user.getIsAdmin(), user.getCreatedById());
String memberRole = DeviceSessionPolicy.resolveRole(
user.getRole(), user.getIsAdmin(), user.getCreatedById());
if (siteSuper || "company_super_admin".equals(memberRole)) {
throw new BusinessException("超级管理员不属于任何分组");
}
if (leaderCompanyId != null && !leaderCompanyId.equals(user.getCompanyId())) {
throw new BusinessException("只能添加组长本公司的账号");
}
if (!superAdmin && !"normal".equals(normalizeBlank(user.getRole()))) {
throw new BusinessException("只能绑定普通员工账号");
}