增加用户注册功能

This commit is contained in:
scholar 2024-09-06 15:40:32 +08:00
parent 1c1abae5bb
commit 0e1c7b574f
4 changed files with 179 additions and 1 deletions

View File

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

View File

@ -0,0 +1,67 @@
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 lombok.Data;
import org.hibernate.validator.constraints.Length;
import jakarta.validation.constraints.*;
import java.util.Set;
@Schema(description = "管理后台 - Register Request VO")
@Data
public class AuthRegisterReqVO {
@Schema(description = "用户编号", example = "1024")
private Long id;
@Schema(description = "用户账号", requiredMode = Schema.RequiredMode.REQUIRED, example = "yudao")
@NotBlank(message = "用户账号不能为空")
@Pattern(regexp = "^[a-zA-Z0-9]{4,30}$", message = "用户账号由 数字、字母 组成")
@Size(min = 4, max = 30, message = "用户账号长度为 4-30 个字符")
private String username;
@Schema(description = "用户昵称", requiredMode = Schema.RequiredMode.REQUIRED, example = "芋艿")
@Size(max = 30, message = "用户昵称长度不能超过30个字符")
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")
@Length(min = 4, max = 16, message = "密码长度为 4-16 位")
private String password;
@AssertTrue(message = "密码不能为空")
@JsonIgnore
public boolean isPasswordValid() {
return id != null // 修改时不需要传递
|| (ObjectUtil.isAllNotEmpty(password)); // 新增时必须都传递 password
}
}

View File

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

View File

@ -1,19 +1,26 @@
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.StrUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
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.servlet.ServletUtils;
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.sms.SmsCodeApi;
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.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.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.enums.logger.LoginLogTypeEnum;
import cn.iocoder.yudao.module.system.enums.logger.LoginResultEnum;
import cn.iocoder.yudao.module.system.enums.oauth2.OAuth2ClientConstants;
@ -22,6 +29,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;
@ -29,13 +37,17 @@ import com.xingyuv.captcha.model.vo.CaptchaVO;
import com.xingyuv.captcha.service.CaptchaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import jakarta.validation.Validator;
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.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.servlet.ServletUtils.getClientIP;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
@ -64,12 +76,21 @@ public class AdminAuthServiceImpl implements AdminAuthService {
private CaptchaService captchaService;
@Resource
private SmsCodeApi smsCodeApi;
@Resource
@Lazy // 延迟避免循环依赖报错
private TenantService tenantService;
@Resource
private AdminUserMapper userMapper;
@Resource
private PasswordEncoder passwordEncoder;
/**
* 验证码的开关默认为 true
*/
@Value("${yudao.captcha.enable:true}")
private Boolean captchaEnable;
@Resource
private UserPostMapper userPostMapper;
@Override
public AdminUserDO authenticate(String username, String password) {
@ -247,4 +268,78 @@ public class AdminAuthServiceImpl implements AdminAuthService {
return UserTypeEnum.ADMIN;
}
public Long register(AuthRegisterReqVO registerReqVO) {
// 校验账户配合
tenantService.handleTenantInfo(tenant -> {
long count = userMapper.selectCount();
if (count >= tenant.getAccountCount()) {
throw exception(USER_COUNT_MAX, tenant.getAccountCount());
}
});
// 校验正确性
validateUserForRegister(null, registerReqVO.getUsername(),
registerReqVO.getMobile(), registerReqVO.getEmail(), registerReqVO.getDeptId(), registerReqVO.getPostIds());
// 插入用户
AdminUserDO user = BeanUtils.toBean(registerReqVO, AdminUserDO.class);
user.setStatus(CommonStatusEnum.ENABLE.getStatus()); // 默认开启
user.setPassword(encodePassword(registerReqVO.getPassword())); // 加密密码
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,
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);
}
}
/**
* 对密码进行加密
*
* @param password 密码
* @return 加密后的密码
*/
private String encodePassword(String password) {
return passwordEncoder.encode(password);
}
}