登录请求,增加用户类型

This commit is contained in:
zhangtao 2024-12-25 18:58:27 +08:00
parent 9353941ebb
commit 0cbb593645
6 changed files with 14 additions and 11 deletions

View File

@ -92,5 +92,8 @@ public class AdminUserDO extends TenantBaseDO {
* 最后登录时间
*/
private LocalDateTime loginDate;
/**
* 用户类型0 - 管理员1-老师2-学生
*/
private Integer userType;
}

View File

@ -21,7 +21,7 @@ public interface AdminAuthService {
* @param password 密码
* @return 用户
*/
AdminUserDO authenticate(String username, String password);
AdminUserDO authenticate(String username, String password, Integer userType);
/**
* 账号登录

View File

@ -72,11 +72,11 @@ public class AdminAuthServiceImpl implements AdminAuthService {
private Boolean captchaEnable;
@Override
public AdminUserDO authenticate(String username, String password) {
public AdminUserDO authenticate(String username, String password, Integer userType) {
final LoginLogTypeEnum logTypeEnum = LoginLogTypeEnum.LOGIN_USERNAME;
// 校验账号是否存在
AdminUserDO user = userService.getUserByUsername(username);
if (user == null) {
if (user == null || !Objects.equals(user.getUserType(), userType)) {
createLoginLog(null, username, logTypeEnum, LoginResultEnum.BAD_CREDENTIALS);
throw exception(AUTH_LOGIN_BAD_CREDENTIALS);
}
@ -98,7 +98,7 @@ public class AdminAuthServiceImpl implements AdminAuthService {
validateCaptcha(reqVO);
// 使用账号密码进行登录
AdminUserDO user = authenticate(reqVO.getUsername(), reqVO.getPassword());
AdminUserDO user = authenticate(reqVO.getUsername(), reqVO.getPassword(), reqVO.getUserType());
// 如果 socialType 非空说明需要绑定社交用户
if (reqVO.getSocialType() != null) {

View File

@ -72,7 +72,7 @@ public class OAuth2GrantServiceImpl implements OAuth2GrantService {
@Override
public OAuth2AccessTokenDO grantPassword(String username, String password, String clientId, List<String> scopes) {
// 使用账号 + 密码进行登录
AdminUserDO user = adminAuthService.authenticate(username, password);
AdminUserDO user = adminAuthService.authenticate(username, password, null);
Assert.notNull(user, "用户不能为空!"); // 防御性编程
// 创建访问令牌

View File

@ -86,7 +86,7 @@ public class AdminAuthServiceImplTest extends BaseDbUnitTest {
when(userService.isPasswordMatch(eq(password), eq(user.getPassword()))).thenReturn(true);
// 调用
AdminUserDO loginUser = authService.authenticate(username, password);
AdminUserDO loginUser = authService.authenticate(username, password, UserTypeEnum.ADMIN.getValue());
// 校验
assertPojoEquals(user, loginUser);
}
@ -98,7 +98,7 @@ public class AdminAuthServiceImplTest extends BaseDbUnitTest {
String password = randomString();
// 调用, 并断言异常
assertServiceException(() -> authService.authenticate(username, password),
assertServiceException(() -> authService.authenticate(username, password, UserTypeEnum.ADMIN.getValue()),
AUTH_LOGIN_BAD_CREDENTIALS);
verify(loginLogService).createLoginLog(
argThat(o -> o.getLogType().equals(LoginLogTypeEnum.LOGIN_USERNAME.getType())
@ -118,7 +118,7 @@ public class AdminAuthServiceImplTest extends BaseDbUnitTest {
when(userService.getUserByUsername(eq(username))).thenReturn(user);
// 调用, 并断言异常
assertServiceException(() -> authService.authenticate(username, password),
assertServiceException(() -> authService.authenticate(username, password, UserTypeEnum.ADMIN.getValue()),
AUTH_LOGIN_BAD_CREDENTIALS);
verify(loginLogService).createLoginLog(
argThat(o -> o.getLogType().equals(LoginLogTypeEnum.LOGIN_USERNAME.getType())
@ -140,7 +140,7 @@ public class AdminAuthServiceImplTest extends BaseDbUnitTest {
when(userService.isPasswordMatch(eq(password), eq(user.getPassword()))).thenReturn(true);
// 调用, 并断言异常
assertServiceException(() -> authService.authenticate(username, password),
assertServiceException(() -> authService.authenticate(username, password, UserTypeEnum.ADMIN.getValue()),
AUTH_LOGIN_USER_DISABLED);
verify(loginLogService).createLoginLog(
argThat(o -> o.getLogType().equals(LoginLogTypeEnum.LOGIN_USERNAME.getType())

View File

@ -109,7 +109,7 @@ public class OAuth2GrantServiceImplTest extends BaseMockitoUnitTest {
List<String> scopes = Lists.newArrayList("read", "write");
// mock 方法(认证)
AdminUserDO user = randomPojo(AdminUserDO.class);
when(adminAuthService.authenticate(eq(username), eq(password))).thenReturn(user);
when(adminAuthService.authenticate(eq(username), eq(password), eq(UserTypeEnum.ADMIN.getValue()))).thenReturn(user);
// mock 方法访问令牌
OAuth2AccessTokenDO accessTokenDO = randomPojo(OAuth2AccessTokenDO.class);
when(oauth2TokenService.createAccessToken(eq(user.getId()), eq(UserTypeEnum.ADMIN.getValue()),