feat(账号到期+公司超管): 级联过期判定、过期停任务巡检与两端续费提醒弹窗

需求两条:站点超管可开设其他公司的超级管理员(站点超管看全站、公司超管只看本公司、
组内管理员仍只看本组);账号要有过期时间,管理员过期后名下普通账户一并停用、
正在跑的任务也要停掉。落库只有 users.expires_at(V146,NULL=永不过期),级联一律现算,
不写"上级过期就把下级置成过期"那种续费也救不回来的数据。

- AccountExpiryService:生效到期 = min(自身, 本公司公司超管, 成员表组长, 创建人)。
  登录拦 403(文案给续费提示)、每次请求鉴权拦 401(前端按下线踢回登录页),super_admin
  永不适用。级联口径与数据可见范围同源(成员表基本未用、实际按 created_by_id 派生)。
- ExpiredAccountTaskStopService:每分钟把过期账号(含级联成员)名下 biz_file_task
  PENDING/SCHEDULED/RUNNING → FAILED,含未领取的 PENDING(防内部通道继续派发)。原因文案
  刻意不以「客户端异常中断」开头,否则会被 TaskResumeService 自动复活。品牌采集与跟价循环
  不在那张表上,经 ExpiredAccountTaskStopSpi 由各自模块停:品牌 pending/running→cancelled
  且 UPDATE 的 WHERE 二次核对状态(抢到行后被客户端推进成完成的行不得误杀),跟价循环只置
  stop_requested=1、沿用用户点「停止循环」那条收尾路径,绝不动 status。任务心跳接口保持匿名
  不走 requireUser,所以过期账号的客户端仍能拿到 alive=false 协作停止。
- 角色 company_super_admin:由站点超管开设(唯一要显式选公司的建号场景),建号/授菜单/改用户/
  看数据四条链路都按"本公司"收口;公司超管之间、以及公司超管与站点超管互相碰不得。
- 续费提醒:剩余 <5 天在数富后台与用户端弹窗,只能收起不能关闭(无关闭按钮,收起态是右下角
  浮标),续费到 ≥5 天自动消失。后台读 /api/admin/current-user 的 expiresAt/daysToExpire;
  用户端无 Pinia,走 localStorage 的 account_expires_at(登录/check_login 落键、被顶下线清键,
  换账号不残留)。只反映账号自身到期,弹窗不做任何级联推断。
- V146 撞掉了 V145 那批的一条护栏:它按"db/ 里不许出现 V146__ 前缀"编死号位,被一条正常的
  前向迁移占用就红。改成钉动作——剥注释后的可执行 SQL 不得写 flyway_schema_history(回滚
  一律是扫描路径外的人工补丁),并把盘点文档改 V146 时丢掉的 V145 文件名补回去。

验证(本会话亲跑,不是转述):后端定向 14 类 123/0(同一轮 mvn 里连批次③的 4 类 53 例、以及既有 ShopDataCrawl/SimilarAsin 两条回归对照 21 例一起跑,合计 197 例 0 红);admin-frontend-vue 本批定向 44/0(弹窗 7 +
公司建号/列表/入口/日期默认 21 + 角色 task-48/task-251 16),全量 1726 例里那 1 支红是另一条
会话刻意留的「公司 tab 接线缺口」尺子(点名 admin_task_center 等 6 页,与本批无关);
frontend-vue 弹窗 9/0、全量 710/0。变异自证两批共 34 点:弹窗语义 16 点全杀(阈值 5、只能收起
不能关闭、挂载点含"注释掉挂载"、落键/清键链路),本批 SPI+迁移护栏 17 点全杀(含 G2 反向:
前向迁移只在注释里提 flyway_schema_history 必须照样绿),注入文件还原 dirty=0、探针残留 0。
This commit is contained in:
2026-10-05 11:51:33 +08:00
parent 265f31681f
commit 95e2318c42
62 changed files with 2866 additions and 112 deletions
@@ -0,0 +1,210 @@
package com.nanri.aiimage.common.security;
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.mapper.AdminUserMapper;
import com.nanri.aiimage.common.mapper.ShopManageGroupMapper;
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
import com.nanri.aiimage.modules.auth.model.entity.LoginUserEntity;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.time.LocalDateTime;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.catchThrowableOfType;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* 到期级联判定(V146)口径钉:
* super_admin 永不适用;公司超管到期 → 全公司跟随;管理员到期 → 创建人链上的普通用户跟随;
* 普通用户同时受自身/创建人/组长/公司超管四条约束取最早;库里不改值,全部现算。
*/
class AccountExpiryServiceTest {
private AdminUserMapper adminUserMapper;
private ShopManageGroupMapper groupMapper;
private AccountExpiryService service;
@BeforeEach
void setUp() {
TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""),
AdminUserEntity.class);
adminUserMapper = mock(AdminUserMapper.class);
groupMapper = mock(ShopManageGroupMapper.class);
service = new AccountExpiryService(adminUserMapper, groupMapper);
// 默认:无公司超管、无组长、无创建人约束
when(adminUserMapper.selectList(any())).thenReturn(List.of());
when(adminUserMapper.selectById(any(Long.class))).thenReturn(null);
when(groupMapper.selectLeaderIdsByMemberUserId(any())).thenReturn(List.of());
}
// ---------- 基本口径 ----------
@Test
void superAdminNeverExpires() {
assertThat(service.effectiveExpiresAt(entity(1L, "super_admin", null, 1L, past()))).isNull();
}
@Test
void ownExpiryAppliesToAdminDirectly() {
LocalDateTime expiry = LocalDateTime.now().plusDays(3);
assertThat(service.effectiveExpiresAt(entity(2L, "admin", null, 1L, expiry))).isEqualTo(expiry);
}
@Test
void nullExpiryMeansNeverWithoutSuperiorConstraints() {
assertThat(service.effectiveExpiresAt(entity(2L, "admin", null, 1L, null))).isNull();
}
// ---------- 公司级联:公司超管到期 → 全公司 ----------
@Test
void companySuperAdminExpiryCapsEveryoneInTheCompany() {
LocalDateTime companyExpiry = LocalDateTime.now().plusDays(2);
AdminUserEntity csa = new AdminUserEntity();
csa.setId(9L);
csa.setRole("company_super_admin");
csa.setExpiresAt(companyExpiry);
when(adminUserMapper.selectList(any())).thenReturn(List.of(csa));
// 普通用户(自身无到期)被公司超管的到期拉进去
assertThat(service.effectiveExpiresAt(entity(3L, "normal", null, 1L, null))).isEqualTo(companyExpiry);
// 管理员同样被拉进去
assertThat(service.effectiveExpiresAt(entity(2L, "admin", null, 1L, null))).isEqualTo(companyExpiry);
}
@Test
void companySuperAdminWithoutExpiryLeavesCompanyUnaffected() {
AdminUserEntity csa = new AdminUserEntity();
csa.setId(9L);
csa.setRole("company_super_admin");
csa.setExpiresAt(null);
when(adminUserMapper.selectList(any())).thenReturn(List.of(csa));
assertThat(service.effectiveExpiresAt(entity(3L, "normal", null, 1L, null))).isNull();
}
@Test
void earliestConstraintWinsOverOwnExpiry() {
LocalDateTime own = LocalDateTime.now().plusDays(10);
LocalDateTime company = LocalDateTime.now().plusDays(1);
AdminUserEntity csa = new AdminUserEntity();
csa.setId(9L);
csa.setRole("company_super_admin");
csa.setExpiresAt(company);
when(adminUserMapper.selectList(any())).thenReturn(List.of(csa));
assertThat(service.effectiveExpiresAt(entity(3L, "normal", null, 1L, own))).isEqualTo(company);
}
// ---------- 组级联:管理员到期 → 名下/组内普通用户 ----------
@Test
void expiredCreatorAdminCascadesToNormalUser() {
LocalDateTime adminExpiry = LocalDateTime.now().plusDays(4);
AdminUserEntity creator = new AdminUserEntity();
creator.setId(2L);
creator.setRole("admin");
creator.setExpiresAt(adminExpiry);
when(adminUserMapper.selectById(2L)).thenReturn(creator);
assertThat(service.effectiveExpiresAt(entity(3L, "normal", 2L, 1L, null))).isEqualTo(adminExpiry);
}
@Test
void expiredGroupLeaderCascadesToGroupMember() {
LocalDateTime leaderExpiry = LocalDateTime.now().plusDays(6);
AdminUserEntity leader = new AdminUserEntity();
leader.setId(5L);
leader.setRole("admin");
leader.setExpiresAt(leaderExpiry);
when(groupMapper.selectLeaderIdsByMemberUserId(3L)).thenReturn(List.of(5L));
when(adminUserMapper.selectById(5L)).thenReturn(leader);
assertThat(service.effectiveExpiresAt(entity(3L, "normal", 8L, 1L, null))).isEqualTo(leaderExpiry);
}
@Test
void normalUserIsNotCascadedByNormalCreator() {
// 创建人自己是普通用户(历史脏数据):不构成组级约束
AdminUserEntity creator = new AdminUserEntity();
creator.setId(8L);
creator.setRole("normal");
creator.setExpiresAt(past());
when(adminUserMapper.selectById(8L)).thenReturn(creator);
assertThat(service.effectiveExpiresAt(entity(3L, "normal", 8L, 1L, null))).isNull();
}
@Test
void adminIsNotCascadedByItsCreatorAdmin() {
// 管理员的上级是公司超管/超管(走公司级),不按"创建人是管理员"级联——否则两家管理员互相锁
AdminUserEntity creator = new AdminUserEntity();
creator.setId(2L);
creator.setRole("admin");
creator.setExpiresAt(past());
when(adminUserMapper.selectById(2L)).thenReturn(creator);
assertThat(service.effectiveExpiresAt(entity(6L, "admin", 2L, 1L, null))).isNull();
}
// ---------- 拦截行为 ----------
@Test
void expiredLoginIsRejectedWithRenewalMessage() {
AccountExpiryService raw = new AccountExpiryService(adminUserMapper, groupMapper);
AdminUserEntity expired = entity(3L, "admin", null, 1L, past());
BusinessException ex = catchThrowableOfType(() -> raw.assertLoginAllowed(expired), BusinessException.class);
assertThat(ex).isNotNull();
assertThat(ex.getMessage()).contains("已过期").contains("续费");
}
@Test
void liveAccountPassesSessionCheck() {
AdminUserEntity live = entity(3L, "admin", null, 1L, LocalDateTime.now().plusDays(9));
assertThatCode(() -> service.assertSessionAllowed(live)).doesNotThrowAnyException();
}
// ---------- 弹窗口径:只看自身 ----------
@Test
void ownDaysToExpireFloorsToWholeDays() {
assertThat(AccountExpiryService.ownDaysToExpire(null)).isNull();
LocalDateTime inSixDays = LocalDateTime.now().plusDays(6);
Long days = AccountExpiryService.ownDaysToExpire(inSixDays);
assertThat(days).isNotNull();
assertThat(days).isBetween(5L, 6L);
// 已过期是负数(调用方按 <5 判展示,登录侧已被 403 拦住,负数不会出现在弹窗里)
assertThat(AccountExpiryService.ownDaysToExpire(past())).isNegative();
}
// ---------- helpers ----------
private static LocalDateTime past() {
return LocalDateTime.now().minusDays(1);
}
private static AdminUserEntity entity(Long id, String role, Long createdById, Long companyId,
LocalDateTime expiresAt) {
AdminUserEntity user = new AdminUserEntity();
user.setId(id);
user.setUsername("u" + id);
user.setRole(role);
user.setIsAdmin("super_admin".equals(role) || "admin".equals(role)
|| "company_super_admin".equals(role) ? 1 : 0);
user.setCreatedById(createdById);
user.setCompanyId(companyId);
user.setExpiresAt(expiresAt);
return user;
}
}
@@ -29,7 +29,7 @@ class AdminAuthSupportTest {
void explicitLowestIdAdminIsNotPromotedToSuperAdmin() {
AdminUserMapper userMapper = mock(AdminUserMapper.class);
AdminAuthSupport support = new AdminAuthSupport(
mock(JwtService.class), userMapper, mock(AuthProperties.class));
mock(JwtService.class), userMapper, mock(AuthProperties.class), mock(AccountExpiryService.class));
AdminUserEntity user = user(1L, "admin", 1, null);
assertThat(support.currentRole(user)).isEqualTo("admin");
@@ -39,7 +39,8 @@ class AdminAuthSupportTest {
@Test
void legacyBlankRoleRootRemainsSuperAdmin() {
AdminAuthSupport support = new AdminAuthSupport(
mock(JwtService.class), mock(AdminUserMapper.class), mock(AuthProperties.class));
mock(JwtService.class), mock(AdminUserMapper.class), mock(AuthProperties.class),
mock(AccountExpiryService.class));
assertThat(support.currentRole(user(1L, null, 1, null))).isEqualTo("super_admin");
}
@@ -47,7 +48,8 @@ class AdminAuthSupportTest {
@Test
void explicitNormalRoleIsNotPromotedByLegacyAdminFields() {
AdminAuthSupport support = new AdminAuthSupport(
mock(JwtService.class), mock(AdminUserMapper.class), mock(AuthProperties.class));
mock(JwtService.class), mock(AdminUserMapper.class), mock(AuthProperties.class),
mock(AccountExpiryService.class));
assertThat(support.currentRole(user(1L, "normal", 1, null))).isNull();
}
@@ -188,7 +190,8 @@ class AdminAuthSupportTest {
private AdminAuthSupport tokenSupport(String current, String previous, String tokenFile) {
AdminAuthSupport support = new AdminAuthSupport(
mock(JwtService.class), mock(AdminUserMapper.class), mock(AuthProperties.class));
mock(JwtService.class), mock(AdminUserMapper.class), mock(AuthProperties.class),
mock(AccountExpiryService.class));
ReflectionTestUtils.setField(support, "internalToken", current);
ReflectionTestUtils.setField(support, "internalTokenPrevious", previous);
ReflectionTestUtils.setField(support, "internalTokenFile", tokenFile);
@@ -207,7 +210,7 @@ class AdminAuthSupportTest {
when(userMapper.selectById(user.getId())).thenReturn(user);
when(props.isSingleDeviceEnabled()).thenReturn(singleDeviceEnabled);
return new AdminAuthSupport(jwtService, userMapper, props);
return new AdminAuthSupport(jwtService, userMapper, props, mock(AccountExpiryService.class));
}
private HttpServletRequest requestWithBearer(String token) {
@@ -19,6 +19,17 @@ class DeviceSessionPolicyTest {
assertThat(DeviceSessionPolicy.resolveRole("super_admin", 1, 9L)).isEqualTo("super_admin");
}
@Test
void companySuperAdminResolvedWithoutBeingPromotedToSiteAdmin() {
assertThat(DeviceSessionPolicy.resolveRole("company_super_admin", 1, 1L))
.isEqualTo("company_super_admin");
// 大小写/空白容错与既有取值一致
assertThat(DeviceSessionPolicy.resolveRole(" Company_Super_Admin ", 1, 1L))
.isEqualTo("company_super_admin");
// 关键反向钉:公司超管绝不能被顶成站点超管(isSuperAdmin 是单设备豁免+全站判定的入口)
assertThat(DeviceSessionPolicy.isSuperAdmin("company_super_admin", 1, null)).isFalse();
}
@Test
void explicitAdminNotPromotedToSuperAdmin() {
assertThat(DeviceSessionPolicy.resolveRole("admin", 1, null)).isEqualTo("admin");