ÎļþÃû´Ó ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/SysRegisterService.java ÐÞ¸Ä |
| | |
| | | package com.ruoyi.system.service; |
| | | package com.ruoyi.web.service; |
| | | |
| | | import cn.dev33.satoken.secure.BCrypt; |
| | | import com.ruoyi.common.core.constant.CacheConstants; |
| | | import com.ruoyi.common.core.constant.Constants; |
| | | import com.ruoyi.common.core.constant.GlobalConstants; |
| | | import com.ruoyi.common.core.constant.UserConstants; |
| | | import com.ruoyi.common.core.domain.model.RegisterBody; |
| | | import com.ruoyi.common.core.enums.UserType; |
| | |
| | | import com.ruoyi.common.core.utils.StringUtils; |
| | | import com.ruoyi.common.log.event.LogininforEvent; |
| | | import com.ruoyi.common.redis.utils.RedisUtils; |
| | | import com.ruoyi.common.web.config.properties.CaptchaProperties; |
| | | import com.ruoyi.system.domain.bo.SysUserBo; |
| | | import com.ruoyi.system.service.ISysUserService; |
| | | import jakarta.servlet.http.HttpServletRequest; |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.springframework.stereotype.Service; |
| | |
| | | public class SysRegisterService { |
| | | |
| | | private final ISysUserService userService; |
| | | private final ISysConfigService configService; |
| | | private final CaptchaProperties captchaProperties; |
| | | |
| | | /** |
| | | * 注å |
| | | */ |
| | | public void register(RegisterBody registerBody) { |
| | | HttpServletRequest request = ServletUtils.getRequest(); |
| | | String tenantId = registerBody.getTenantId(); |
| | | String username = registerBody.getUsername(); |
| | | String password = registerBody.getPassword(); |
| | | // æ ¡éªç¨æ·ç±»åæ¯å¦åå¨ |
| | | String userType = UserType.getUserType(registerBody.getUserType()).getUserType(); |
| | | |
| | | boolean captchaEnabled = configService.selectCaptchaEnabled(); |
| | | boolean captchaEnabled = captchaProperties.getEnable(); |
| | | // éªè¯ç å¼å
³ |
| | | if (captchaEnabled) { |
| | | validateCaptcha(username, registerBody.getCode(), registerBody.getUuid(), request); |
| | | validateCaptcha(tenantId, username, registerBody.getCode(), registerBody.getUuid(), request); |
| | | } |
| | | SysUserBo sysUser = new SysUserBo(); |
| | | sysUser.setUserName(username); |
| | |
| | | if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(sysUser))) { |
| | | throw new UserException("user.register.save.error", username); |
| | | } |
| | | boolean regFlag = userService.registerUser(sysUser); |
| | | boolean regFlag = userService.registerUser(sysUser, tenantId); |
| | | if (!regFlag) { |
| | | throw new UserException("user.register.error"); |
| | | } |
| | | recordLogininfor(username, Constants.REGISTER, MessageUtils.message("user.register.success")); |
| | | recordLogininfor(tenantId, username, Constants.REGISTER, MessageUtils.message("user.register.success")); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @param uuid å¯ä¸æ è¯ |
| | | * @return ç»æ |
| | | */ |
| | | public void validateCaptcha(String username, String code, String uuid, HttpServletRequest request) { |
| | | String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + StringUtils.defaultString(uuid, ""); |
| | | public void validateCaptcha(String tenantId, String username, String code, String uuid, HttpServletRequest request) { |
| | | String verifyKey = GlobalConstants.CAPTCHA_CODE_KEY + StringUtils.defaultString(uuid, ""); |
| | | String captcha = RedisUtils.getCacheObject(verifyKey); |
| | | RedisUtils.deleteObject(verifyKey); |
| | | if (captcha == null) { |
| | | recordLogininfor(username, Constants.REGISTER, MessageUtils.message("user.jcaptcha.expire")); |
| | | recordLogininfor(tenantId, username, Constants.REGISTER, MessageUtils.message("user.jcaptcha.expire")); |
| | | throw new CaptchaExpireException(); |
| | | } |
| | | if (!code.equalsIgnoreCase(captcha)) { |
| | | recordLogininfor(username, Constants.REGISTER, MessageUtils.message("user.jcaptcha.error")); |
| | | recordLogininfor(tenantId, username, Constants.REGISTER, MessageUtils.message("user.jcaptcha.error")); |
| | | throw new CaptchaException(); |
| | | } |
| | | } |
| | |
| | | /** |
| | | * è®°å½ç»å½ä¿¡æ¯ |
| | | * |
| | | * @param tenantId ç§æ·ID |
| | | * @param username ç¨æ·å |
| | | * @param status ç¶æ |
| | | * @param message æ¶æ¯å
容 |
| | | * @return |
| | | */ |
| | | private void recordLogininfor(String username, String status, String message) { |
| | | private void recordLogininfor(String tenantId, String username, String status, String message) { |
| | | LogininforEvent logininforEvent = new LogininforEvent(); |
| | | logininforEvent.setTenantId(tenantId); |
| | | logininforEvent.setUsername(username); |
| | | logininforEvent.setStatus(status); |
| | | logininforEvent.setMessage(message); |