feat(停用): disabled_at 同挂登录与已发令牌两条闸,各沿用既有状态码
只挡请求不挡登录 = 被停用的人重登一次就把停用冲掉,所以四个入口(两张实体 × 两条闸)都要有。 停用判定排在到期之前:被停用的账号不该先收到续费提示。
This commit is contained in:
+16
@@ -57,24 +57,40 @@ public class AccountExpiryService {
|
||||
|
||||
/** 登录拦截:已到期(含级联)抛 403,文案给续费提示。 */
|
||||
public void assertLoginAllowed(AdminUserEntity user) {
|
||||
assertNotDisabled(user == null ? null : user.getDisabledAt(), 403);
|
||||
assertNotExpired(effectiveExpiresAt(user), 403);
|
||||
}
|
||||
|
||||
/** 同上,登录链路的 users 实体。 */
|
||||
public void assertLoginAllowed(LoginUserEntity user) {
|
||||
assertNotDisabled(user == null ? null : user.getDisabledAt(), 403);
|
||||
assertNotExpired(effectiveExpiresAt(user), 403);
|
||||
}
|
||||
|
||||
/** 会话拦截:已到期(含级联)抛 401,前端踢回登录页。 */
|
||||
public void assertSessionAllowed(AdminUserEntity user) {
|
||||
assertNotDisabled(user == null ? null : user.getDisabledAt(), 401);
|
||||
assertNotExpired(effectiveExpiresAt(user), 401);
|
||||
}
|
||||
|
||||
/** 同上,登录链路的 users 实体(check_login 恢复会话时走这里)。 */
|
||||
public void assertSessionAllowed(LoginUserEntity user) {
|
||||
assertNotDisabled(user == null ? null : user.getDisabledAt(), 401);
|
||||
assertNotExpired(effectiveExpiresAt(user), 401);
|
||||
}
|
||||
|
||||
/**
|
||||
* 停用拦截。必须排在到期判定之前:被停用的账号不该先看到「该续费了」。
|
||||
*
|
||||
* <p>与 expires_at 分列的理由写在 V155 头注释里——复用收费到期字段表达停用,
|
||||
* 取消停用时原到期日就被抹掉了。</p>
|
||||
*/
|
||||
private static void assertNotDisabled(java.time.LocalDateTime disabledAt, int code) {
|
||||
if (disabledAt != null) {
|
||||
throw new BusinessException(code, "账号已停用,请联系管理员");
|
||||
}
|
||||
}
|
||||
|
||||
/** 弹窗用:自身 expires_at 还剩整天数(floor);无到期返回 null。只反映自身,不级联——续费是账号自己的事。 */
|
||||
public static Long ownDaysToExpire(LocalDateTime ownExpiresAt) {
|
||||
if (ownExpiresAt == null) {
|
||||
|
||||
+93
@@ -0,0 +1,93 @@
|
||||
package com.nanri.aiimage.common.security;
|
||||
|
||||
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.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
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.Mockito.mock;
|
||||
|
||||
/**
|
||||
* 停用必须同时挡「重新登录」与「已发令牌」。只挡一边等于没停用:
|
||||
* 被停用的人重新登录一次,登录路径不看停用就会把吊销冲掉。
|
||||
* 状态码沿用各自闸门的既有口径(登录 403、会话 401),不新造第三种。
|
||||
*/
|
||||
class AccountDisabledGateTest {
|
||||
|
||||
private static final String MSG = "账号已停用,请联系管理员";
|
||||
|
||||
private AccountExpiryService service() {
|
||||
return new AccountExpiryService(mock(AdminUserMapper.class), mock(ShopManageGroupMapper.class));
|
||||
}
|
||||
|
||||
private AdminUserEntity admin(LocalDateTime disabledAt) {
|
||||
AdminUserEntity user = new AdminUserEntity();
|
||||
user.setId(9L);
|
||||
user.setUsername("小魏16");
|
||||
user.setCompanyId(2L);
|
||||
user.setRole("user");
|
||||
user.setIsAdmin(0);
|
||||
user.setDisabledAt(disabledAt);
|
||||
return user;
|
||||
}
|
||||
|
||||
private LoginUserEntity login(LocalDateTime disabledAt) {
|
||||
LoginUserEntity user = new LoginUserEntity();
|
||||
user.setId(9L);
|
||||
user.setUsername("小魏16");
|
||||
user.setCompanyId(2L);
|
||||
user.setRole("user");
|
||||
user.setIsAdmin(0);
|
||||
user.setDisabledAt(disabledAt);
|
||||
return user;
|
||||
}
|
||||
|
||||
@Test
|
||||
void disabledAdminAccountCannotLogInAgain() {
|
||||
BusinessException e = catchThrowableOfType(
|
||||
() -> service().assertLoginAllowed(admin(LocalDateTime.now().minusDays(1))),
|
||||
BusinessException.class);
|
||||
assertThat(e).isNotNull();
|
||||
assertThat(e.getCode()).isEqualTo(403);
|
||||
assertThat(e.getMessage()).isEqualTo(MSG);
|
||||
}
|
||||
|
||||
@Test
|
||||
void disabledAdminAccountLosesIssuedTokens() {
|
||||
BusinessException e = catchThrowableOfType(
|
||||
() -> service().assertSessionAllowed(admin(LocalDateTime.now())), BusinessException.class);
|
||||
assertThat(e).isNotNull();
|
||||
assertThat(e.getCode()).isEqualTo(401);
|
||||
assertThat(e.getMessage()).isEqualTo(MSG);
|
||||
}
|
||||
|
||||
/** 登录链路走的是另一张实体(同表同列),漏加就是「退出重登即复活」。 */
|
||||
@Test
|
||||
void disabledAccountOnLoginEntityIsAlsoBlocked() {
|
||||
BusinessException loginGate = catchThrowableOfType(
|
||||
() -> service().assertLoginAllowed(login(LocalDateTime.now())), BusinessException.class);
|
||||
assertThat(loginGate).isNotNull();
|
||||
assertThat(loginGate.getMessage()).isEqualTo(MSG);
|
||||
BusinessException sessionGate = catchThrowableOfType(
|
||||
() -> service().assertSessionAllowed(login(LocalDateTime.now())), BusinessException.class);
|
||||
assertThat(sessionGate).isNotNull();
|
||||
assertThat(sessionGate.getMessage()).isEqualTo(MSG);
|
||||
}
|
||||
|
||||
@Test
|
||||
void enabledAccountIsNotBlockedByDisabledGate() {
|
||||
AccountExpiryService s = service();
|
||||
// 未停用:不许出现停用文案(到期判定另有既有用例负责)
|
||||
assertThatCode(() -> s.assertSessionAllowed(admin(null))).doesNotThrowAnyException();
|
||||
BusinessException e = catchThrowableOfType(
|
||||
() -> s.assertLoginAllowed(admin(null)), BusinessException.class);
|
||||
assertThat(e == null || !MSG.equals(e.getMessage())).isTrue();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user