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:
@@ -144,6 +144,19 @@ public interface ShopManageGroupMapper extends BaseMapper<ShopManageGroupEntity>
|
||||
""")
|
||||
List<ShopManageGroupEntity> selectLedGroups(@Param("operatorId") Long operatorId);
|
||||
|
||||
/**
|
||||
* 某用户所属分组(成员表)的组长 id 集合(到期级联用,AccountExpiryService)。
|
||||
* 与数据可见范围同源:组长的旧字段 user_id 与新字段 created_by_id 都算(COALESCE 取新优先旧兜底)。
|
||||
*/
|
||||
@Select("""
|
||||
SELECT DISTINCT COALESCE(g.created_by_id, g.user_id) AS leader_id
|
||||
FROM biz_shop_manage_group_member gm
|
||||
INNER JOIN biz_shop_manage_group g ON g.id = gm.group_id
|
||||
WHERE gm.user_id = #{userId}
|
||||
AND COALESCE(g.created_by_id, g.user_id) IS NOT NULL
|
||||
""")
|
||||
List<Long> selectLeaderIdsByMemberUserId(@Param("userId") Long userId);
|
||||
|
||||
/** 全部分组(超管筛选项用)。 */
|
||||
@Select("""
|
||||
SELECT g.id AS id, g.group_name AS groupName
|
||||
|
||||
@@ -26,6 +26,12 @@ public class AdminUserEntity {
|
||||
/** 所属公司(company.id,V143 新增)。列 NOT NULL,建号必须显式写,见 AdminUserService#createUser。 */
|
||||
@TableField("company_id")
|
||||
private Long companyId;
|
||||
/**
|
||||
* 到期时间(V146 新增):NULL=永不过期。管理员到期则组下普通账户跟随停用、公司超级管理员
|
||||
* 到期则全公司跟随停用——级联判定在 AccountExpiryService,库里不改值。super_admin 不适用。
|
||||
*/
|
||||
@TableField("expires_at")
|
||||
private LocalDateTime expiresAt;
|
||||
@TableField("created_at")
|
||||
private LocalDateTime createdAt;
|
||||
/**
|
||||
|
||||
+161
@@ -0,0 +1,161 @@
|
||||
package com.nanri.aiimage.common.security;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
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 lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 账号到期(expires_at,V146)的级联判定与拦截。
|
||||
*
|
||||
* <p><b>判定口径(库里不改值,全部现算):</b>生效到期 = 自身 expires_at 与所有上级约束里最早的那个。</p>
|
||||
* <ul>
|
||||
* <li>super_admin:永不适用(返回 null);</li>
|
||||
* <li>公司级:本公司所有 company_super_admin 的 expires_at 约束全公司(管理员/普通用户跟随);
|
||||
* 公司没有公司超管时无此约束(站点自己的公司通常如此);</li>
|
||||
* <li>组级:普通用户受两条链约束——所属分组组长(成员表)与创建人(管理员),
|
||||
* 与数据可见范围的派生口径一致(成员表基本未用、按创建人派生);</li>
|
||||
* <li>管理员(admin):自身 + 公司级。</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p>拦截点:登录(403,提示续费)与每次请求鉴权 {@code AdminAuthSupport.requireUser}
|
||||
* (401,前端按下线处理踢回登录页)。任务心跳接口刻意匿名、不走 requireUser,所以
|
||||
* {@code ExpiredAccountTaskStopService} 把任务置终态后,客户端心跳仍能拿到 alive=false
|
||||
* 协作停止——这是"过期停任务"不走 401 的原因。</p>
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class AccountExpiryService {
|
||||
|
||||
private final AdminUserMapper adminUserMapper;
|
||||
private final ShopManageGroupMapper shopManageGroupMapper;
|
||||
|
||||
/** 生效到期时间;null=永不过期。入参见 {@link #computeEffective(Long, String, Integer, Long, Long, LocalDateTime)}。 */
|
||||
public LocalDateTime effectiveExpiresAt(AdminUserEntity user) {
|
||||
if (user == null) {
|
||||
return null;
|
||||
}
|
||||
return computeEffective(user.getId(), user.getRole(), user.getIsAdmin(), user.getCompanyId(),
|
||||
user.getCreatedById(), user.getExpiresAt());
|
||||
}
|
||||
|
||||
/** 同上,登录链路的 users 实体(与 {@code AdminUserEntity} 同表同列)。 */
|
||||
public LocalDateTime effectiveExpiresAt(LoginUserEntity user) {
|
||||
if (user == null) {
|
||||
return null;
|
||||
}
|
||||
return computeEffective(user.getId(), user.getRole(), user.getIsAdmin(), user.getCompanyId(),
|
||||
user.getCreatedById(), user.getExpiresAt());
|
||||
}
|
||||
|
||||
/** 登录拦截:已到期(含级联)抛 403,文案给续费提示。 */
|
||||
public void assertLoginAllowed(AdminUserEntity user) {
|
||||
assertNotExpired(effectiveExpiresAt(user), 403);
|
||||
}
|
||||
|
||||
/** 同上,登录链路的 users 实体。 */
|
||||
public void assertLoginAllowed(LoginUserEntity user) {
|
||||
assertNotExpired(effectiveExpiresAt(user), 403);
|
||||
}
|
||||
|
||||
/** 会话拦截:已到期(含级联)抛 401,前端踢回登录页。 */
|
||||
public void assertSessionAllowed(AdminUserEntity user) {
|
||||
assertNotExpired(effectiveExpiresAt(user), 401);
|
||||
}
|
||||
|
||||
/** 同上,登录链路的 users 实体(check_login 恢复会话时走这里)。 */
|
||||
public void assertSessionAllowed(LoginUserEntity user) {
|
||||
assertNotExpired(effectiveExpiresAt(user), 401);
|
||||
}
|
||||
|
||||
/** 弹窗用:自身 expires_at 还剩整天数(floor);无到期返回 null。只反映自身,不级联——续费是账号自己的事。 */
|
||||
public static Long ownDaysToExpire(LocalDateTime ownExpiresAt) {
|
||||
if (ownExpiresAt == null) {
|
||||
return null;
|
||||
}
|
||||
long seconds = java.time.Duration.between(LocalDateTime.now(), ownExpiresAt).getSeconds();
|
||||
return Math.floorDiv(seconds, 86400L);
|
||||
}
|
||||
|
||||
private void assertNotExpired(LocalDateTime effective, int code) {
|
||||
if (effective != null && effective.isBefore(LocalDateTime.now())) {
|
||||
throw new BusinessException(code, "账号已过期,请联系管理员续费");
|
||||
}
|
||||
}
|
||||
|
||||
private LocalDateTime computeEffective(Long userId, String storedRole, Integer isAdmin, Long companyId,
|
||||
Long createdById, LocalDateTime ownExpiresAt) {
|
||||
String role = DeviceSessionPolicy.resolveRole(storedRole, isAdmin, createdById);
|
||||
if ("super_admin".equals(role)) {
|
||||
return null;
|
||||
}
|
||||
LocalDateTime effective = ownExpiresAt;
|
||||
effective = min(effective, companyCascadeExpiresAt(companyId));
|
||||
if (role == null) {
|
||||
// 普通用户:组级约束(组长 + 创建人)。管理员不级联到组长(上级是公司超管/超管,走公司级)。
|
||||
effective = min(effective, leaderCascadeExpiresAt(userId));
|
||||
effective = min(effective, creatorCascadeExpiresAt(createdById));
|
||||
}
|
||||
return effective;
|
||||
}
|
||||
|
||||
/** 公司级:本公司公司超管里最早的 expires_at;没有公司超管(含 companyId 为空)返回 null。 */
|
||||
private LocalDateTime companyCascadeExpiresAt(Long companyId) {
|
||||
if (companyId == null || companyId <= 0) {
|
||||
return null;
|
||||
}
|
||||
List<AdminUserEntity> csas = adminUserMapper.selectList(new LambdaQueryWrapper<AdminUserEntity>()
|
||||
.select(AdminUserEntity::getExpiresAt)
|
||||
.eq(AdminUserEntity::getRole, "company_super_admin")
|
||||
.eq(AdminUserEntity::getCompanyId, companyId));
|
||||
LocalDateTime earliest = null;
|
||||
for (AdminUserEntity csa : csas) {
|
||||
earliest = min(earliest, csa.getExpiresAt());
|
||||
}
|
||||
return earliest;
|
||||
}
|
||||
|
||||
/** 组级:该用户所属分组(成员表)的组长(created_by_id/user_id)若是管理员,取其最早 expires_at。 */
|
||||
private LocalDateTime leaderCascadeExpiresAt(Long userId) {
|
||||
if (userId == null || userId <= 0) {
|
||||
return null;
|
||||
}
|
||||
LocalDateTime earliest = null;
|
||||
for (Long leaderId : shopManageGroupMapper.selectLeaderIdsByMemberUserId(userId)) {
|
||||
AdminUserEntity leader = adminUserMapper.selectById(leaderId);
|
||||
if (leader != null && "admin".equals(DeviceSessionPolicy.resolveRole(
|
||||
leader.getRole(), leader.getIsAdmin(), leader.getCreatedById()))) {
|
||||
earliest = min(earliest, leader.getExpiresAt());
|
||||
}
|
||||
}
|
||||
return earliest;
|
||||
}
|
||||
|
||||
/** 组级:创建人是管理员(普通账户的归属主管)时,取其 expires_at。 */
|
||||
private LocalDateTime creatorCascadeExpiresAt(Long createdById) {
|
||||
if (createdById == null || createdById <= 0) {
|
||||
return null;
|
||||
}
|
||||
AdminUserEntity creator = adminUserMapper.selectById(createdById);
|
||||
if (creator == null || !"admin".equals(DeviceSessionPolicy.resolveRole(
|
||||
creator.getRole(), creator.getIsAdmin(), creator.getCreatedById()))) {
|
||||
return null;
|
||||
}
|
||||
return creator.getExpiresAt();
|
||||
}
|
||||
|
||||
private static LocalDateTime min(LocalDateTime a, LocalDateTime b) {
|
||||
if (a == null) return b;
|
||||
if (b == null) return a;
|
||||
return b.isBefore(a) ? b : a;
|
||||
}
|
||||
}
|
||||
@@ -30,6 +30,7 @@ public class AdminAuthSupport {
|
||||
private final JwtService jwtService;
|
||||
private final AdminUserMapper adminUserMapper;
|
||||
private final AuthProperties authProperties;
|
||||
private final AccountExpiryService accountExpiryService;
|
||||
|
||||
@Value("${aiimage.security.internal-token:}")
|
||||
private String internalToken;
|
||||
@@ -67,6 +68,9 @@ public class AdminAuthSupport {
|
||||
DeviceSessionPolicy.isSuperAdmin(user.getRole(), user.getIsAdmin(), user.getCreatedById()),
|
||||
user.getId(), user.getUsername());
|
||||
}
|
||||
// 到期拦截(含级联):管理员过期→组下账户停、公司超管过期→全公司停。
|
||||
// 任务心跳接口刻意匿名、不走本方法,过期账号的在跑任务仍能靠 alive=false 协作停止。
|
||||
accountExpiryService.assertSessionAllowed(user);
|
||||
return user;
|
||||
}
|
||||
|
||||
|
||||
+6
-1
@@ -22,12 +22,17 @@ public final class DeviceSessionPolicy {
|
||||
private DeviceSessionPolicy() {
|
||||
}
|
||||
|
||||
/** 计算用户管理角色:super_admin / admin / null(含老数据 role 为空时按 created_by_id 推断)。 */
|
||||
/** 计算用户管理角色: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)) {
|
||||
// 公司超级管理员(2026-10-05):单家公司内的最高账号,数据只看本公司,由站点
|
||||
// super_admin 创建。必须先于 is_admin 兼容推断返回,否则会被顶成站点超管。
|
||||
return "company_super_admin";
|
||||
}
|
||||
if ("admin".equals(storedRole)) {
|
||||
return "admin";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user