Commit 258250e3c4a9c9378835493d13e84f9c2a1ab316
1 parent
0e1c7b57
增加用户注册功能
Showing
4 changed files
with
18 additions
and
105 deletions
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/auth/AuthController.java
| ... | ... | @@ -164,8 +164,7 @@ public class AuthController { |
| 164 | 164 | @PostMapping("/register") |
| 165 | 165 | @PermitAll |
| 166 | 166 | @Operation(summary = "注册用户") |
| 167 | - @OperateLog(enable = false) // 避免 Post 请求被记录操作日志 | |
| 168 | - public CommonResult<Long> register(@RequestBody @Valid AuthRegisterReqVO reqVO) { | |
| 169 | - return success(authService.register(reqVO)); | |
| 167 | + public CommonResult<AuthLoginRespVO> register(@RequestBody @Valid AuthRegisterReqVO registerReqVO) { | |
| 168 | + return success(authService.register(registerReqVO)); | |
| 170 | 169 | } |
| 171 | 170 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/auth/vo/AuthRegisterReqVO.java
| 1 | 1 | package cn.iocoder.yudao.module.system.controller.admin.auth.vo; |
| 2 | 2 | |
| 3 | -import cn.hutool.core.util.ObjectUtil; | |
| 4 | -import cn.iocoder.yudao.framework.common.validation.Mobile; | |
| 5 | -import com.fasterxml.jackson.annotation.JsonIgnore; | |
| 3 | + | |
| 6 | 4 | import io.swagger.v3.oas.annotations.media.Schema; |
| 7 | 5 | import lombok.Data; |
| 8 | 6 | import org.hibernate.validator.constraints.Length; |
| 9 | 7 | |
| 10 | 8 | import jakarta.validation.constraints.*; |
| 11 | -import java.util.Set; | |
| 12 | 9 | |
| 13 | 10 | @Schema(description = "管理后台 - Register Request VO") |
| 14 | 11 | @Data |
| 15 | 12 | public class AuthRegisterReqVO { |
| 16 | 13 | |
| 17 | - @Schema(description = "用户编号", example = "1024") | |
| 18 | - private Long id; | |
| 19 | - | |
| 20 | 14 | @Schema(description = "用户账号", requiredMode = Schema.RequiredMode.REQUIRED, example = "yudao") |
| 21 | 15 | @NotBlank(message = "用户账号不能为空") |
| 22 | 16 | @Pattern(regexp = "^[a-zA-Z0-9]{4,30}$", message = "用户账号由 数字、字母 组成") |
| ... | ... | @@ -27,41 +21,13 @@ public class AuthRegisterReqVO { |
| 27 | 21 | @Size(max = 30, message = "用户昵称长度不能超过30个字符") |
| 28 | 22 | private String nickname; |
| 29 | 23 | |
| 30 | - @Schema(description = "备注", example = "我是一个用户") | |
| 31 | - private String remark; | |
| 32 | - | |
| 33 | - @Schema(description = "部门ID", example = "我是一个用户") | |
| 34 | - private Long deptId; | |
| 35 | - | |
| 36 | - @Schema(description = "岗位编号数组", example = "1") | |
| 37 | - private Set<Long> postIds; | |
| 38 | - | |
| 39 | - @Schema(description = "用户邮箱", example = "yudao@iocoder.cn") | |
| 40 | - @Email(message = "邮箱格式不正确") | |
| 41 | - @Size(max = 50, message = "邮箱长度不能超过 50 个字符") | |
| 42 | - private String email; | |
| 43 | - | |
| 44 | - @Schema(description = "手机号码", example = "15601691300") | |
| 45 | - @Mobile | |
| 46 | - private String mobile; | |
| 47 | - | |
| 48 | - @Schema(description = "用户性别,参见 SexEnum 枚举类", example = "1") | |
| 49 | - private Integer sex; | |
| 50 | - | |
| 51 | - @Schema(description = "用户头像", example = "https://www.iocoder.cn/xxx.png") | |
| 52 | - private String avatar; | |
| 53 | - | |
| 54 | - // ========== 仅【创建】时,需要传递的字段 ========== | |
| 55 | - | |
| 56 | 24 | @Schema(description = "密码", requiredMode = Schema.RequiredMode.REQUIRED, example = "123456") |
| 57 | 25 | @Length(min = 4, max = 16, message = "密码长度为 4-16 位") |
| 58 | 26 | private String password; |
| 59 | 27 | |
| 60 | - @AssertTrue(message = "密码不能为空") | |
| 61 | - @JsonIgnore | |
| 62 | - public boolean isPasswordValid() { | |
| 63 | - return id != null // 修改时,不需要传递 | |
| 64 | - || (ObjectUtil.isAllNotEmpty(password)); // 新增时,必须都传递 password | |
| 65 | - } | |
| 66 | - | |
| 28 | + // ========== 图片验证码相关 ========== | |
| 29 | + @Schema(description = "验证码,验证码开启时,需要传递", requiredMode = Schema.RequiredMode.REQUIRED, | |
| 30 | + example = "PfcH6mgr8tpXuMWFjvW6YVaqrswIuwmWI5dsVZSg7sGpWtDCUbHuDEXl3cFB1+VvCC/rAkSwK8Fad52FSuncVg==") | |
| 31 | + @NotEmpty(message = "验证码不能为空", groups = AuthLoginReqVO.CodeEnableGroup.class) | |
| 32 | + private String captchaVerification; | |
| 67 | 33 | } |
| 68 | 34 | \ No newline at end of file | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthService.java
| ... | ... | @@ -70,13 +70,11 @@ public interface AdminAuthService { |
| 70 | 70 | */ |
| 71 | 71 | AuthLoginRespVO refreshToken(String refreshToken); |
| 72 | 72 | |
| 73 | - | |
| 74 | 73 | /** |
| 75 | 74 | * 用户注册 |
| 76 | 75 | * |
| 77 | 76 | * @param createReqVO 注册用户 |
| 78 | 77 | * @return 注册结果 |
| 79 | 78 | */ |
| 80 | - Long register(AuthRegisterReqVO createReqVO); | |
| 81 | - | |
| 79 | + AuthLoginRespVO register(AuthRegisterReqVO createReqVO); | |
| 82 | 80 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthServiceImpl.java
| 1 | 1 | package cn.iocoder.yudao.module.system.service.auth; |
| 2 | 2 | |
| 3 | -import cn.hutool.core.collection.CollectionUtil; | |
| 4 | 3 | import cn.hutool.core.util.ObjectUtil; |
| 5 | -import cn.hutool.core.util.StrUtil; | |
| 6 | 4 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 7 | 5 | import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; |
| 8 | 6 | import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils; |
| 9 | 7 | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| 10 | 8 | import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils; |
| 11 | 9 | import cn.iocoder.yudao.framework.common.util.validation.ValidationUtils; |
| 12 | -import cn.iocoder.yudao.framework.datapermission.core.util.DataPermissionUtils; | |
| 13 | 10 | import cn.iocoder.yudao.module.system.api.logger.dto.LoginLogCreateReqDTO; |
| 14 | 11 | import cn.iocoder.yudao.module.system.api.sms.SmsCodeApi; |
| 15 | 12 | import cn.iocoder.yudao.module.system.api.social.dto.SocialUserBindReqDTO; |
| 16 | 13 | import cn.iocoder.yudao.module.system.api.social.dto.SocialUserRespDTO; |
| 17 | 14 | import cn.iocoder.yudao.module.system.controller.admin.auth.vo.*; |
| 18 | 15 | import cn.iocoder.yudao.module.system.convert.auth.AuthConvert; |
| 19 | -import cn.iocoder.yudao.module.system.dal.dataobject.dept.UserPostDO; | |
| 20 | 16 | import cn.iocoder.yudao.module.system.dal.dataobject.oauth2.OAuth2AccessTokenDO; |
| 21 | 17 | import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO; |
| 22 | -import cn.iocoder.yudao.module.system.dal.mysql.dept.UserPostMapper; | |
| 23 | 18 | import cn.iocoder.yudao.module.system.dal.mysql.user.AdminUserMapper; |
| 24 | 19 | import cn.iocoder.yudao.module.system.enums.logger.LoginLogTypeEnum; |
| 25 | 20 | import cn.iocoder.yudao.module.system.enums.logger.LoginResultEnum; |
| ... | ... | @@ -44,10 +39,8 @@ import org.springframework.stereotype.Service; |
| 44 | 39 | import jakarta.annotation.Resource; |
| 45 | 40 | import jakarta.validation.Validator; |
| 46 | 41 | import java.util.Objects; |
| 47 | -import java.util.Set; | |
| 48 | 42 | |
| 49 | 43 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| 50 | -import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; | |
| 51 | 44 | import static cn.iocoder.yudao.framework.common.util.servlet.ServletUtils.getClientIP; |
| 52 | 45 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; |
| 53 | 46 | |
| ... | ... | @@ -88,9 +81,6 @@ public class AdminAuthServiceImpl implements AdminAuthService { |
| 88 | 81 | */ |
| 89 | 82 | @Value("${yudao.captcha.enable:true}") |
| 90 | 83 | private Boolean captchaEnable; |
| 91 | - @Resource | |
| 92 | - private UserPostMapper userPostMapper; | |
| 93 | - | |
| 94 | 84 | |
| 95 | 85 | @Override |
| 96 | 86 | public AdminUserDO authenticate(String username, String password) { |
| ... | ... | @@ -269,7 +259,10 @@ public class AdminAuthServiceImpl implements AdminAuthService { |
| 269 | 259 | } |
| 270 | 260 | |
| 271 | 261 | |
| 272 | - public Long register(AuthRegisterReqVO registerReqVO) { | |
| 262 | + public AuthLoginRespVO register(AuthRegisterReqVO registerReqVO) { | |
| 263 | + // 校验验证码 | |
| 264 | + AuthLoginReqVO loginReqVO = BeanUtils.toBean(registerReqVO, AuthLoginReqVO.class); | |
| 265 | + validateCaptcha(loginReqVO); | |
| 273 | 266 | // 校验账户配合 |
| 274 | 267 | tenantService.handleTenantInfo(tenant -> { |
| 275 | 268 | long count = userMapper.selectCount(); |
| ... | ... | @@ -277,60 +270,17 @@ public class AdminAuthServiceImpl implements AdminAuthService { |
| 277 | 270 | throw exception(USER_COUNT_MAX, tenant.getAccountCount()); |
| 278 | 271 | } |
| 279 | 272 | }); |
| 280 | - // 校验正确性 | |
| 281 | - validateUserForRegister(null, registerReqVO.getUsername(), | |
| 282 | - registerReqVO.getMobile(), registerReqVO.getEmail(), registerReqVO.getDeptId(), registerReqVO.getPostIds()); | |
| 273 | + // 校验用户名是否已存在 | |
| 274 | + if (userMapper.selectByUsername(registerReqVO.getUsername()) != null) { | |
| 275 | + throw exception(USER_USERNAME_EXISTS); | |
| 276 | + } | |
| 283 | 277 | // 插入用户 |
| 284 | 278 | AdminUserDO user = BeanUtils.toBean(registerReqVO, AdminUserDO.class); |
| 285 | 279 | user.setStatus(CommonStatusEnum.ENABLE.getStatus()); // 默认开启 |
| 286 | 280 | user.setPassword(encodePassword(registerReqVO.getPassword())); // 加密密码 |
| 287 | 281 | userMapper.insert(user); |
| 288 | - // 插入关联岗位 | |
| 289 | - if (CollectionUtil.isNotEmpty(user.getPostIds())) { | |
| 290 | - userPostMapper.insertBatch(convertList(user.getPostIds(), | |
| 291 | - postId -> new UserPostDO().setUserId(user.getId()).setPostId(postId))); | |
| 292 | - } | |
| 293 | - return user.getId(); | |
| 294 | - } | |
| 295 | - | |
| 296 | - private void validateUserForRegister(Long id, String username, String mobile, String email, | |
| 297 | - Long deptId, Set<Long> postIds) { | |
| 298 | - // 关闭数据权限,避免因为没有数据权限,查询不到数据,进而导致唯一校验不正确 | |
| 299 | - DataPermissionUtils.executeIgnore(() -> { | |
| 300 | - // 校验用户存在 | |
| 301 | - validateUserExists(id); | |
| 302 | - // 校验用户名唯一 | |
| 303 | - validateUsernameUnique(id, username); | |
| 304 | - }); | |
| 305 | - } | |
| 306 | - | |
| 307 | - @VisibleForTesting | |
| 308 | - void validateUserExists(Long id) { | |
| 309 | - if (id == null) { | |
| 310 | - return; | |
| 311 | - } | |
| 312 | - AdminUserDO user = userMapper.selectById(id); | |
| 313 | - if (user == null) { | |
| 314 | - throw exception(USER_NOT_EXISTS); | |
| 315 | - } | |
| 316 | - } | |
| 317 | 282 | |
| 318 | - @VisibleForTesting | |
| 319 | - void validateUsernameUnique(Long id, String username) { | |
| 320 | - if (StrUtil.isBlank(username)) { | |
| 321 | - return; | |
| 322 | - } | |
| 323 | - AdminUserDO user = userMapper.selectByUsername(username); | |
| 324 | - if (user == null) { | |
| 325 | - return; | |
| 326 | - } | |
| 327 | - // 如果 id 为空,说明不用比较是否为相同 id 的用户 | |
| 328 | - if (id == null) { | |
| 329 | - throw exception(USER_USERNAME_EXISTS); | |
| 330 | - } | |
| 331 | - if (!user.getId().equals(id)) { | |
| 332 | - throw exception(USER_USERNAME_EXISTS); | |
| 333 | - } | |
| 283 | + return createTokenAfterLoginSuccess(user.getId(), registerReqVO.getUsername(), LoginLogTypeEnum.LOGIN_USERNAME); | |
| 334 | 284 | } |
| 335 | 285 | |
| 336 | 286 | /** | ... | ... |