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:
+36
@@ -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);
|
||||
}
|
||||
|
||||
+82
@@ -22,11 +22,13 @@ import org.mockito.ArgumentCaptor;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.catchThrowableOfType;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@@ -236,6 +238,86 @@ class AdminUserServiceCompanySuperAdminTest {
|
||||
.hasMessageContaining("只有超级管理员可以删除公司超级管理员");
|
||||
}
|
||||
|
||||
// ---------- 普通账号不带自身到期(2026-10-05 定案:到期只设在管理员/公司超管身上)----------
|
||||
|
||||
@Test
|
||||
void createRejectsExpiryOnNormalAccount() {
|
||||
AdminUserEntity operator = user(1L, "super_admin", 1L, null);
|
||||
AdminUserCreateRequest request = new AdminUserCreateRequest();
|
||||
request.setUsername("staff-1");
|
||||
request.setPassword("secret1");
|
||||
request.setRole("normal");
|
||||
request.setCreatedById(7L);
|
||||
request.setExpiresAt("2026-12-31 23:59");
|
||||
when(authSupport.currentRole(operator)).thenReturn("super_admin");
|
||||
|
||||
BusinessException ex = catchThrowableOfType(() -> service.createUser(operator, request),
|
||||
BusinessException.class);
|
||||
assertThat(ex).isNotNull();
|
||||
assertThat(ex.getCode()).isEqualTo(400);
|
||||
assertThat(ex.getMessage()).contains("普通账号");
|
||||
verify(userMapper, never()).insert(any(AdminUserEntity.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateRejectsExpiryOnNormalTarget() {
|
||||
AdminUserEntity operator = user(1L, "super_admin", 1L, null);
|
||||
AdminUserEntity target = user(9L, "normal", 1L, 7L);
|
||||
when(authSupport.currentRole(operator)).thenReturn("super_admin");
|
||||
when(userMapper.selectById(9L)).thenReturn(target);
|
||||
|
||||
AdminUserUpdateRequest request = new AdminUserUpdateRequest();
|
||||
request.setExpiresAt("2026-12-31 23:59");
|
||||
|
||||
BusinessException ex = catchThrowableOfType(() -> service.updateUser(operator, 9L, request),
|
||||
BusinessException.class);
|
||||
assertThat(ex).isNotNull();
|
||||
assertThat(ex.getCode()).isEqualTo(400);
|
||||
verify(userMapper, never()).update(any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void demotingAdminToNormalClearsOwnExpiry() {
|
||||
AdminUserEntity operator = user(1L, "super_admin", 1L, null);
|
||||
AdminUserEntity target = user(9L, "admin", 1L, 1L);
|
||||
target.setExpiresAt(LocalDateTime.of(2026, 12, 31, 23, 59));
|
||||
when(authSupport.currentRole(operator)).thenReturn("super_admin");
|
||||
when(userMapper.selectById(9L)).thenReturn(target);
|
||||
|
||||
AdminUserUpdateRequest request = new AdminUserUpdateRequest();
|
||||
request.setRole("normal");
|
||||
|
||||
service.updateUser(operator, 9L, request);
|
||||
|
||||
ArgumentCaptor<LambdaUpdateWrapper<AdminUserEntity>> captor =
|
||||
ArgumentCaptor.forClass(LambdaUpdateWrapper.class);
|
||||
verify(userMapper).update(org.mockito.ArgumentMatchers.isNull(), captor.capture());
|
||||
assertThat(captor.getValue().getSqlSet()).contains("expires_at");
|
||||
// 清的是 null,不是把原时间照抄回去
|
||||
assertThat(captor.getValue().getParamNameValuePairs().values())
|
||||
.noneMatch(value -> value instanceof LocalDateTime);
|
||||
}
|
||||
|
||||
/** 控制用例:收掉入口不许顺手把「清空」也禁掉——空串仍要能把历史值摘成永不过期。 */
|
||||
@Test
|
||||
void normalTargetCanStillClearExpiryWithBlankValue() {
|
||||
AdminUserEntity operator = user(1L, "super_admin", 1L, null);
|
||||
AdminUserEntity target = user(9L, "normal", 1L, 7L);
|
||||
target.setExpiresAt(LocalDateTime.of(2026, 12, 31, 23, 59));
|
||||
when(authSupport.currentRole(operator)).thenReturn("super_admin");
|
||||
when(userMapper.selectById(9L)).thenReturn(target);
|
||||
|
||||
AdminUserUpdateRequest request = new AdminUserUpdateRequest();
|
||||
request.setExpiresAt("");
|
||||
|
||||
service.updateUser(operator, 9L, request);
|
||||
|
||||
ArgumentCaptor<LambdaUpdateWrapper<AdminUserEntity>> captor =
|
||||
ArgumentCaptor.forClass(LambdaUpdateWrapper.class);
|
||||
verify(userMapper).update(org.mockito.ArgumentMatchers.isNull(), captor.capture());
|
||||
assertThat(captor.getValue().getSqlSet()).contains("expires_at");
|
||||
}
|
||||
|
||||
// ---------- helpers ----------
|
||||
|
||||
private static AdminUserEntity user(Long id, String role, Long companyId, Long createdById) {
|
||||
|
||||
Reference in New Issue
Block a user