|
@@ -8,9 +8,17 @@ import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils;
|
|
|
import cn.iocoder.yudao.framework.common.util.validation.ValidationUtils;
|
|
|
import cn.iocoder.yudao.module.system.api.logger.dto.LoginLogCreateReqDTO;
|
|
|
import cn.iocoder.yudao.module.system.api.sms.SmsCodeApi;
|
|
|
+import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeUseReqDTO;
|
|
|
import cn.iocoder.yudao.module.system.api.social.dto.SocialUserBindReqDTO;
|
|
|
import cn.iocoder.yudao.module.system.api.social.dto.SocialUserRespDTO;
|
|
|
-import cn.iocoder.yudao.module.system.controller.admin.auth.vo.*;
|
|
|
+import cn.iocoder.yudao.module.system.controller.admin.auth.vo.AuthLoginReqVO;
|
|
|
+import cn.iocoder.yudao.module.system.controller.admin.auth.vo.AuthLoginRespVO;
|
|
|
+import cn.iocoder.yudao.module.system.controller.admin.auth.vo.AuthRegisterReqVO;
|
|
|
+import cn.iocoder.yudao.module.system.controller.admin.auth.vo.AuthResetPasswordReqVO;
|
|
|
+import cn.iocoder.yudao.module.system.controller.admin.auth.vo.AuthSmsLoginReqVO;
|
|
|
+import cn.iocoder.yudao.module.system.controller.admin.auth.vo.AuthSmsSendReqVO;
|
|
|
+import cn.iocoder.yudao.module.system.controller.admin.auth.vo.AuthSocialLoginReqVO;
|
|
|
+import cn.iocoder.yudao.module.system.controller.admin.auth.vo.CaptchaVerificationReqVO;
|
|
|
import cn.iocoder.yudao.module.system.convert.auth.AuthConvert;
|
|
|
import cn.iocoder.yudao.module.system.dal.dataobject.oauth2.OAuth2AccessTokenDO;
|
|
|
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
|
|
@@ -22,6 +30,7 @@ import cn.iocoder.yudao.module.system.service.logger.LoginLogService;
|
|
|
import cn.iocoder.yudao.module.system.service.member.MemberService;
|
|
|
import cn.iocoder.yudao.module.system.service.oauth2.OAuth2TokenService;
|
|
|
import cn.iocoder.yudao.module.system.service.social.SocialUserService;
|
|
|
+import cn.iocoder.yudao.module.system.service.tenant.TenantService;
|
|
|
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
|
|
|
import com.google.common.annotations.VisibleForTesting;
|
|
|
import com.xingyuv.captcha.model.common.ResponseModel;
|
|
@@ -32,12 +41,20 @@ import jakarta.validation.Validator;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import java.util.Objects;
|
|
|
|
|
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
|
import static cn.iocoder.yudao.framework.common.util.servlet.ServletUtils.getClientIP;
|
|
|
-import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
|
|
|
+import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.AUTH_LOGIN_BAD_CREDENTIALS;
|
|
|
+import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.AUTH_LOGIN_CAPTCHA_CODE_ERROR;
|
|
|
+import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.AUTH_LOGIN_USER_DISABLED;
|
|
|
+import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.AUTH_MOBILE_NOT_EXISTS;
|
|
|
+import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.AUTH_REGISTER_CAPTCHA_CODE_ERROR;
|
|
|
+import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.AUTH_THIRD_LOGIN_NOT_BIND;
|
|
|
+import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.USER_MOBILE_NOT_EXISTS;
|
|
|
+import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.USER_NOT_EXISTS;
|
|
|
|
|
|
/**
|
|
|
* Auth Service 实现类
|
|
@@ -64,6 +81,8 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
|
|
private CaptchaService captchaService;
|
|
|
@Resource
|
|
|
private SmsCodeApi smsCodeApi;
|
|
|
+ @Resource
|
|
|
+ private TenantService tenantService;
|
|
|
|
|
|
/**
|
|
|
* 验证码的开关,默认为 true
|
|
@@ -111,6 +130,13 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
|
|
|
|
|
@Override
|
|
|
public void sendSmsCode(AuthSmsSendReqVO reqVO) {
|
|
|
+ // 如果是重置密码场景,需要校验图形验证码是否正确
|
|
|
+ if (Objects.equals(SmsSceneEnum.ADMIN_MEMBER_RESET_PASSWORD.getScene(), reqVO.getScene())) {
|
|
|
+ ResponseModel response = doValidateCaptcha(reqVO);
|
|
|
+ if (!response.isSuccess()) {
|
|
|
+ throw exception(AUTH_REGISTER_CAPTCHA_CODE_ERROR, response.getRepMsg());
|
|
|
+ }
|
|
|
+ }
|
|
|
// 登录场景,验证是否存在
|
|
|
if (userService.getUserByMobile(reqVO.getMobile()) == null) {
|
|
|
throw exception(AUTH_MOBILE_NOT_EXISTS);
|
|
@@ -174,16 +200,8 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
|
|
|
|
|
@VisibleForTesting
|
|
|
void validateCaptcha(AuthLoginReqVO reqVO) {
|
|
|
- // 如果验证码关闭,则不进行校验
|
|
|
- if (!captchaEnable) {
|
|
|
- return;
|
|
|
- }
|
|
|
+ ResponseModel response = doValidateCaptcha(reqVO);
|
|
|
// 校验验证码
|
|
|
- ValidationUtils.validate(validator, reqVO, AuthLoginReqVO.CodeEnableGroup.class);
|
|
|
- CaptchaVO captchaVO = new CaptchaVO();
|
|
|
- captchaVO.setCaptchaVerification(reqVO.getCaptchaVerification());
|
|
|
- ResponseModel response = captchaService.verification(captchaVO);
|
|
|
- // 验证不通过
|
|
|
if (!response.isSuccess()) {
|
|
|
// 创建登录失败日志(验证码不正确)
|
|
|
createLoginLog(null, reqVO.getUsername(), LoginLogTypeEnum.LOGIN_USERNAME, LoginResultEnum.CAPTCHA_CODE_ERROR);
|
|
@@ -191,6 +209,17 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private ResponseModel doValidateCaptcha(CaptchaVerificationReqVO reqVO) {
|
|
|
+ // 如果验证码关闭,则不进行校验
|
|
|
+ if (!captchaEnable) {
|
|
|
+ return ResponseModel.success();
|
|
|
+ }
|
|
|
+ ValidationUtils.validate(validator, reqVO, CaptchaVerificationReqVO.CodeEnableGroup.class);
|
|
|
+ CaptchaVO captchaVO = new CaptchaVO();
|
|
|
+ captchaVO.setCaptchaVerification(reqVO.getCaptchaVerification());
|
|
|
+ return captchaService.verification(captchaVO);
|
|
|
+ }
|
|
|
+
|
|
|
private AuthLoginRespVO createTokenAfterLoginSuccess(Long userId, String username, LoginLogTypeEnum logType) {
|
|
|
// 插入登陆日志
|
|
|
createLoginLog(userId, username, logType, LoginResultEnum.SUCCESS);
|
|
@@ -261,19 +290,28 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
|
|
|
|
|
@VisibleForTesting
|
|
|
void validateCaptcha(AuthRegisterReqVO reqVO) {
|
|
|
- // 如果验证码关闭,则不进行校验
|
|
|
- if (!captchaEnable) {
|
|
|
- return;
|
|
|
- }
|
|
|
- // 校验验证码
|
|
|
- ValidationUtils.validate(validator, reqVO, AuthLoginReqVO.CodeEnableGroup.class);
|
|
|
- CaptchaVO captchaVO = new CaptchaVO();
|
|
|
- captchaVO.setCaptchaVerification(reqVO.getCaptchaVerification());
|
|
|
- ResponseModel response = captchaService.verification(captchaVO);
|
|
|
+ ResponseModel response = doValidateCaptcha(reqVO);
|
|
|
// 验证不通过
|
|
|
if (!response.isSuccess()) {
|
|
|
throw exception(AUTH_REGISTER_CAPTCHA_CODE_ERROR, response.getRepMsg());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void resetPassword(AuthResetPasswordReqVO reqVO) {
|
|
|
+ AdminUserDO userByMobile = userService.getUserByMobile(reqVO.getMobile());
|
|
|
+ if (userByMobile == null) {
|
|
|
+ throw exception(USER_MOBILE_NOT_EXISTS);
|
|
|
+ }
|
|
|
+
|
|
|
+ smsCodeApi.useSmsCode(new SmsCodeUseReqDTO()
|
|
|
+ .setCode(reqVO.getCode())
|
|
|
+ .setMobile(reqVO.getMobile())
|
|
|
+ .setScene(SmsSceneEnum.ADMIN_MEMBER_RESET_PASSWORD.getScene())
|
|
|
+ .setUsedIp(getClientIP())
|
|
|
+ );
|
|
|
+
|
|
|
+ userService.updateUserPassword(userByMobile.getId(), reqVO.getPassword());
|
|
|
+ }
|
|
|
}
|