feat(账号到期): 普通账号不再带自己的到期时间,入口与接口一起收窄

口径(2026-10-05 用户定):到期时间只需要设在管理员/公司超管身上,普通账号的停用
由上级级联决定(组长 + 创建人 + 本公司公司超管,见 AccountExpiryService)。
此前这条只存在于约定里,任何调用方都能给普通账号写 expires_at。

- 后端 AdminUserService:建号/编辑两道 400(绕过界面直接打接口传值就拒),
  把管理员改成普通账号时同批把 expires_at 置 null;判据 mayCarryOwnExpiry
  = admin|company_super_admin。
- 前端:user-create-model 导出同名判据(一支跨端用例逐字钉两端集合相等),
  toCreateUserRequest 对 normal 不下发;toEditUserUpdateRequest 按"改完之后
  的角色"决定,不能带就发空串=清空,公司超管目标因请求不带 role 而保留原值;
  两个对话框的「到期时间」项加 v-if="expiryEditable"。
- 用例:后端新增 4 支(含一条「普通账号仍可传空串清除」控制组,14 例绿);
  前端新增 9 支;旧 company-user-create 那条编码旧口径的断言改 role=admin。
This commit is contained in:
2026-10-05 17:00:18 +08:00
parent 4c7d21eb52
commit d04bf6c3d1
8 changed files with 294 additions and 13 deletions
@@ -9,6 +9,7 @@ import com.nanri.aiimage.modules.admin.model.vo.AdminBriefVo;
import com.nanri.aiimage.modules.admin.model.vo.AdminUserItemVo;
import com.nanri.aiimage.modules.admin.model.vo.AdminUserListVo;
import com.nanri.aiimage.common.security.AdminAuthSupport;
import com.nanri.aiimage.common.security.DeviceSessionPolicy;
import com.nanri.aiimage.modules.admin.util.PinyinAbbrUtil;
import com.nanri.aiimage.modules.auth.util.WerkzeugPasswordEncoder;
import com.nanri.aiimage.common.mapper.AdminUserMapper;
@@ -40,6 +41,10 @@ public class AdminUserService {
private static final DateTimeFormatter CREATED_AT_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
/** 普通账号不该带自己的到期时间(2026-10-05 定案):它的停用一律由上级账号级联决定。 */
private static final String NORMAL_ACCOUNT_EXPIRY_MESSAGE =
"普通账号不需要设置到期时间,其停用跟随上级账号(组长/公司超管)级联";
private final AdminUserMapper adminUserMapper;
private final WerkzeugPasswordEncoder passwordEncoder;
private final AdminAuthSupport adminAuthSupport;
@@ -156,6 +161,9 @@ public class AdminUserService {
// 公司超管是「某一家公司的最高账号」,只能由站点超管开设,谁都不能自己造一级
throw new BusinessException(403, "只有超级管理员可以创建公司超级管理员");
}
if (!mayCarryOwnExpiry(wantRole) && hasExpiryValue(request.getExpiresAt())) {
throw new BusinessException(400, NORMAL_ACCOUNT_EXPIRY_MESSAGE);
}
if (username.isEmpty() || password.isEmpty()) {
throw new BusinessException("用户名和密码不能为空");
}
@@ -216,6 +224,16 @@ public class AdminUserService {
return wantRole;
}
/** 哪些角色需要自己带 expires_at:管理员与公司超管;normal 与 super_admin 都不带(超管永不适用)。 */
private static boolean mayCarryOwnExpiry(String resolvedRole) {
return "admin".equals(resolvedRole) || "company_super_admin".equals(resolvedRole);
}
/** 请求里"给了到期时间":非 null 且非空白。空串是清除指令(恢复永不过期),不算给值。 */
private static boolean hasExpiryValue(String raw) {
return raw != null && !raw.trim().isEmpty();
}
/**
* 解析请求里的到期时间:null/空白 = null(永不过期);支持 yyyy-MM-dd、
* yyyy-MM-dd HH:mm、yyyy-MM-dd HH:mm:ss 与 ISO(yyyy-MM-ddTHH:mm)。
@@ -362,6 +380,15 @@ public class AdminUserService {
}
}
// 到期时间只能设在管理员/公司超管身上(普通账号跟随上级级联)。wantRole 为空表示本次不改角色,
// 所以判"改完之后"的角色:非空 storedRole 时 resolveRole 不看 is_admin,推断链不会被带偏。
String roleAfter = DeviceSessionPolicy.resolveRole(
wantRole == null || wantRole.isEmpty() ? target.getRole() : wantRole,
target.getIsAdmin(), target.getCreatedById());
if (!mayCarryOwnExpiry(roleAfter) && hasExpiryValue(request.getExpiresAt())) {
throw new BusinessException(400, NORMAL_ACCOUNT_EXPIRY_MESSAGE);
}
// 移人先于写库:先确认目标公司可用,再确认 created_by_id 不变式没被破坏
Long moveCompanyId = resolveCompanyMove(role, target, request.getCompanyId());
if (moveCompanyId != null) {
@@ -395,6 +422,15 @@ public class AdminUserService {
request.getExpiresAt().trim().isEmpty() ? null : parseExpiresAt(request.getExpiresAt()));
dirty = true;
}
String roleBefore = DeviceSessionPolicy.resolveRole(target.getRole(), target.getIsAdmin(),
target.getCreatedById());
if (request.getExpiresAt() == null && mayCarryOwnExpiry(roleBefore) && !mayCarryOwnExpiry(roleAfter)
&& target.getExpiresAt() != null) {
// 角色被降级成普通账号:同一次更新里摘掉它自己的到期时间,之后由上级级联决定它何时停。
// 只在"真的发生降级"时清,改密码之类的无关编辑不许顺手抹掉历史值。
update.set(AdminUserEntity::getExpiresAt, null);
dirty = true;
}
if (dirty) {
adminUserMapper.update(null, update);
}