增加用户注册功能

This commit is contained in:
scholar 2024-09-10 11:53:28 +08:00
parent 0e1c7b574f
commit 258250e3c4
4 changed files with 18 additions and 105 deletions

View File

@ -164,8 +164,7 @@ public class AuthController {
@PostMapping("/register") @PostMapping("/register")
@PermitAll @PermitAll
@Operation(summary = "注册用户") @Operation(summary = "注册用户")
@OperateLog(enable = false) // 避免 Post 请求被记录操作日志 public CommonResult<AuthLoginRespVO> register(@RequestBody @Valid AuthRegisterReqVO registerReqVO) {
public CommonResult<Long> register(@RequestBody @Valid AuthRegisterReqVO reqVO) { return success(authService.register(registerReqVO));
return success(authService.register(reqVO));
} }
} }

View File

@ -1,22 +1,16 @@
package cn.iocoder.yudao.module.system.controller.admin.auth.vo; package cn.iocoder.yudao.module.system.controller.admin.auth.vo;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.validation.Mobile;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data; import lombok.Data;
import org.hibernate.validator.constraints.Length; import org.hibernate.validator.constraints.Length;
import jakarta.validation.constraints.*; import jakarta.validation.constraints.*;
import java.util.Set;
@Schema(description = "管理后台 - Register Request VO") @Schema(description = "管理后台 - Register Request VO")
@Data @Data
public class AuthRegisterReqVO { public class AuthRegisterReqVO {
@Schema(description = "用户编号", example = "1024")
private Long id;
@Schema(description = "用户账号", requiredMode = Schema.RequiredMode.REQUIRED, example = "yudao") @Schema(description = "用户账号", requiredMode = Schema.RequiredMode.REQUIRED, example = "yudao")
@NotBlank(message = "用户账号不能为空") @NotBlank(message = "用户账号不能为空")
@Pattern(regexp = "^[a-zA-Z0-9]{4,30}$", message = "用户账号由 数字、字母 组成") @Pattern(regexp = "^[a-zA-Z0-9]{4,30}$", message = "用户账号由 数字、字母 组成")
@ -27,41 +21,13 @@ public class AuthRegisterReqVO {
@Size(max = 30, message = "用户昵称长度不能超过30个字符") @Size(max = 30, message = "用户昵称长度不能超过30个字符")
private String nickname; private String nickname;
@Schema(description = "备注", example = "我是一个用户")
private String remark;
@Schema(description = "部门ID", example = "我是一个用户")
private Long deptId;
@Schema(description = "岗位编号数组", example = "1")
private Set<Long> postIds;
@Schema(description = "用户邮箱", example = "yudao@iocoder.cn")
@Email(message = "邮箱格式不正确")
@Size(max = 50, message = "邮箱长度不能超过 50 个字符")
private String email;
@Schema(description = "手机号码", example = "15601691300")
@Mobile
private String mobile;
@Schema(description = "用户性别,参见 SexEnum 枚举类", example = "1")
private Integer sex;
@Schema(description = "用户头像", example = "https://www.iocoder.cn/xxx.png")
private String avatar;
// ========== 创建需要传递的字段 ==========
@Schema(description = "密码", requiredMode = Schema.RequiredMode.REQUIRED, example = "123456") @Schema(description = "密码", requiredMode = Schema.RequiredMode.REQUIRED, example = "123456")
@Length(min = 4, max = 16, message = "密码长度为 4-16 位") @Length(min = 4, max = 16, message = "密码长度为 4-16 位")
private String password; private String password;
@AssertTrue(message = "密码不能为空") // ========== 图片验证码相关 ==========
@JsonIgnore @Schema(description = "验证码,验证码开启时,需要传递", requiredMode = Schema.RequiredMode.REQUIRED,
public boolean isPasswordValid() { example = "PfcH6mgr8tpXuMWFjvW6YVaqrswIuwmWI5dsVZSg7sGpWtDCUbHuDEXl3cFB1+VvCC/rAkSwK8Fad52FSuncVg==")
return id != null // 修改时不需要传递 @NotEmpty(message = "验证码不能为空", groups = AuthLoginReqVO.CodeEnableGroup.class)
|| (ObjectUtil.isAllNotEmpty(password)); // 新增时必须都传递 password private String captchaVerification;
}
} }

View File

@ -70,13 +70,11 @@ public interface AdminAuthService {
*/ */
AuthLoginRespVO refreshToken(String refreshToken); AuthLoginRespVO refreshToken(String refreshToken);
/** /**
* 用户注册 * 用户注册
* *
* @param createReqVO 注册用户 * @param createReqVO 注册用户
* @return 注册结果 * @return 注册结果
*/ */
Long register(AuthRegisterReqVO createReqVO); AuthLoginRespVO register(AuthRegisterReqVO createReqVO);
} }

View File

@ -1,25 +1,20 @@
package cn.iocoder.yudao.module.system.service.auth; package cn.iocoder.yudao.module.system.service.auth;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils; import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils; import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils; import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils;
import cn.iocoder.yudao.framework.common.util.validation.ValidationUtils; import cn.iocoder.yudao.framework.common.util.validation.ValidationUtils;
import cn.iocoder.yudao.framework.datapermission.core.util.DataPermissionUtils;
import cn.iocoder.yudao.module.system.api.logger.dto.LoginLogCreateReqDTO; 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.SmsCodeApi;
import cn.iocoder.yudao.module.system.api.social.dto.SocialUserBindReqDTO; 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.api.social.dto.SocialUserRespDTO;
import cn.iocoder.yudao.module.system.controller.admin.auth.vo.*; import cn.iocoder.yudao.module.system.controller.admin.auth.vo.*;
import cn.iocoder.yudao.module.system.convert.auth.AuthConvert; import cn.iocoder.yudao.module.system.convert.auth.AuthConvert;
import cn.iocoder.yudao.module.system.dal.dataobject.dept.UserPostDO;
import cn.iocoder.yudao.module.system.dal.dataobject.oauth2.OAuth2AccessTokenDO; import cn.iocoder.yudao.module.system.dal.dataobject.oauth2.OAuth2AccessTokenDO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO; import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.dal.mysql.dept.UserPostMapper;
import cn.iocoder.yudao.module.system.dal.mysql.user.AdminUserMapper; import cn.iocoder.yudao.module.system.dal.mysql.user.AdminUserMapper;
import cn.iocoder.yudao.module.system.enums.logger.LoginLogTypeEnum; import cn.iocoder.yudao.module.system.enums.logger.LoginLogTypeEnum;
import cn.iocoder.yudao.module.system.enums.logger.LoginResultEnum; import cn.iocoder.yudao.module.system.enums.logger.LoginResultEnum;
@ -44,10 +39,8 @@ import org.springframework.stereotype.Service;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import jakarta.validation.Validator; import jakarta.validation.Validator;
import java.util.Objects; import java.util.Objects;
import java.util.Set;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.servlet.ServletUtils.getClientIP; 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.*;
@ -88,9 +81,6 @@ public class AdminAuthServiceImpl implements AdminAuthService {
*/ */
@Value("${yudao.captcha.enable:true}") @Value("${yudao.captcha.enable:true}")
private Boolean captchaEnable; private Boolean captchaEnable;
@Resource
private UserPostMapper userPostMapper;
@Override @Override
public AdminUserDO authenticate(String username, String password) { public AdminUserDO authenticate(String username, String password) {
@ -269,7 +259,10 @@ public class AdminAuthServiceImpl implements AdminAuthService {
} }
public Long register(AuthRegisterReqVO registerReqVO) { public AuthLoginRespVO register(AuthRegisterReqVO registerReqVO) {
// 校验验证码
AuthLoginReqVO loginReqVO = BeanUtils.toBean(registerReqVO, AuthLoginReqVO.class);
validateCaptcha(loginReqVO);
// 校验账户配合 // 校验账户配合
tenantService.handleTenantInfo(tenant -> { tenantService.handleTenantInfo(tenant -> {
long count = userMapper.selectCount(); long count = userMapper.selectCount();
@ -277,60 +270,17 @@ public class AdminAuthServiceImpl implements AdminAuthService {
throw exception(USER_COUNT_MAX, tenant.getAccountCount()); throw exception(USER_COUNT_MAX, tenant.getAccountCount());
} }
}); });
// 校验正确性 // 校验用户名是否已存在
validateUserForRegister(null, registerReqVO.getUsername(), if (userMapper.selectByUsername(registerReqVO.getUsername()) != null) {
registerReqVO.getMobile(), registerReqVO.getEmail(), registerReqVO.getDeptId(), registerReqVO.getPostIds()); throw exception(USER_USERNAME_EXISTS);
}
// 插入用户 // 插入用户
AdminUserDO user = BeanUtils.toBean(registerReqVO, AdminUserDO.class); AdminUserDO user = BeanUtils.toBean(registerReqVO, AdminUserDO.class);
user.setStatus(CommonStatusEnum.ENABLE.getStatus()); // 默认开启 user.setStatus(CommonStatusEnum.ENABLE.getStatus()); // 默认开启
user.setPassword(encodePassword(registerReqVO.getPassword())); // 加密密码 user.setPassword(encodePassword(registerReqVO.getPassword())); // 加密密码
userMapper.insert(user); userMapper.insert(user);
// 插入关联岗位
if (CollectionUtil.isNotEmpty(user.getPostIds())) {
userPostMapper.insertBatch(convertList(user.getPostIds(),
postId -> new UserPostDO().setUserId(user.getId()).setPostId(postId)));
}
return user.getId();
}
private void validateUserForRegister(Long id, String username, String mobile, String email, return createTokenAfterLoginSuccess(user.getId(), registerReqVO.getUsername(), LoginLogTypeEnum.LOGIN_USERNAME);
Long deptId, Set<Long> postIds) {
// 关闭数据权限避免因为没有数据权限查询不到数据进而导致唯一校验不正确
DataPermissionUtils.executeIgnore(() -> {
// 校验用户存在
validateUserExists(id);
// 校验用户名唯一
validateUsernameUnique(id, username);
});
}
@VisibleForTesting
void validateUserExists(Long id) {
if (id == null) {
return;
}
AdminUserDO user = userMapper.selectById(id);
if (user == null) {
throw exception(USER_NOT_EXISTS);
}
}
@VisibleForTesting
void validateUsernameUnique(Long id, String username) {
if (StrUtil.isBlank(username)) {
return;
}
AdminUserDO user = userMapper.selectByUsername(username);
if (user == null) {
return;
}
// 如果 id 为空说明不用比较是否为相同 id 的用户
if (id == null) {
throw exception(USER_USERNAME_EXISTS);
}
if (!user.getId().equals(id)) {
throw exception(USER_USERNAME_EXISTS);
}
} }
/** /**