From 0cbb59364599ff08459efc1b6cf815350cb14fc0 Mon Sep 17 00:00:00 2001 From: zhangtao Date: Wed, 25 Dec 2024 18:58:27 +0800 Subject: [PATCH] =?UTF-8?q?=E7=99=BB=E5=BD=95=E8=AF=B7=E6=B1=82=EF=BC=8C?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=94=A8=E6=88=B7=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../module/system/dal/dataobject/user/AdminUserDO.java | 5 ++++- .../module/system/service/auth/AdminAuthService.java | 2 +- .../module/system/service/auth/AdminAuthServiceImpl.java | 6 +++--- .../system/service/oauth2/OAuth2GrantServiceImpl.java | 2 +- .../system/service/auth/AdminAuthServiceImplTest.java | 8 ++++---- .../system/service/oauth2/OAuth2GrantServiceImplTest.java | 2 +- 6 files changed, 14 insertions(+), 11 deletions(-) diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/user/AdminUserDO.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/user/AdminUserDO.java index 2f07a3015..3fb91fe46 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/user/AdminUserDO.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/user/AdminUserDO.java @@ -92,5 +92,8 @@ public class AdminUserDO extends TenantBaseDO { * 最后登录时间 */ private LocalDateTime loginDate; - + /** + * 用户类型:0 - 管理员、1-老师、2-学生 + */ + private Integer userType; } diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthService.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthService.java index 280a8304b..11dda0011 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthService.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthService.java @@ -21,7 +21,7 @@ public interface AdminAuthService { * @param password 密码 * @return 用户 */ - AdminUserDO authenticate(String username, String password); + AdminUserDO authenticate(String username, String password, Integer userType); /** * 账号登录 diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthServiceImpl.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthServiceImpl.java index 2a189d98e..5dd47023d 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthServiceImpl.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthServiceImpl.java @@ -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) { diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/oauth2/OAuth2GrantServiceImpl.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/oauth2/OAuth2GrantServiceImpl.java index adb07f9b5..dba8c1469 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/oauth2/OAuth2GrantServiceImpl.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/oauth2/OAuth2GrantServiceImpl.java @@ -72,7 +72,7 @@ public class OAuth2GrantServiceImpl implements OAuth2GrantService { @Override public OAuth2AccessTokenDO grantPassword(String username, String password, String clientId, List scopes) { // 使用账号 + 密码进行登录 - AdminUserDO user = adminAuthService.authenticate(username, password); + AdminUserDO user = adminAuthService.authenticate(username, password, null); Assert.notNull(user, "用户不能为空!"); // 防御性编程 // 创建访问令牌 diff --git a/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthServiceImplTest.java b/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthServiceImplTest.java index ba751ebe4..db715c6bb 100644 --- a/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthServiceImplTest.java +++ b/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthServiceImplTest.java @@ -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()) diff --git a/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/oauth2/OAuth2GrantServiceImplTest.java b/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/oauth2/OAuth2GrantServiceImplTest.java index 52c722831..9ecc1b0e5 100644 --- a/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/oauth2/OAuth2GrantServiceImplTest.java +++ b/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/oauth2/OAuth2GrantServiceImplTest.java @@ -109,7 +109,7 @@ public class OAuth2GrantServiceImplTest extends BaseMockitoUnitTest { List 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()),