Commit 9f86ef862d4042ad356dd0cba2da6e3810f60589
1 parent
c2de3d5e
【功能新增】INFRA:增加 ConfigApi,支持参数配置的读取
Showing
5 changed files
with
70 additions
and
10 deletions
yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/config/ConfigApi.java
0 → 100644
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/api/config/ConfigApiImpl.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.infra.api.config; | ||
| 2 | + | ||
| 3 | +import cn.iocoder.yudao.module.infra.dal.dataobject.config.ConfigDO; | ||
| 4 | +import cn.iocoder.yudao.module.infra.service.config.ConfigService; | ||
| 5 | +import jakarta.annotation.Resource; | ||
| 6 | +import org.springframework.stereotype.Service; | ||
| 7 | +import org.springframework.validation.annotation.Validated; | ||
| 8 | + | ||
| 9 | +/** | ||
| 10 | + * 参数配置 API 实现类 | ||
| 11 | + * | ||
| 12 | + * @author 芋道源码 | ||
| 13 | + */ | ||
| 14 | +@Service | ||
| 15 | +@Validated | ||
| 16 | +public class ConfigApiImpl implements ConfigApi { | ||
| 17 | + | ||
| 18 | + @Resource | ||
| 19 | + private ConfigService configService; | ||
| 20 | + | ||
| 21 | + @Override | ||
| 22 | + public String getConfigValueByKey(String key) { | ||
| 23 | + ConfigDO config = configService.getConfigByKey(key); | ||
| 24 | + return config != null ? config.getValue() : null; | ||
| 25 | + } | ||
| 26 | + | ||
| 27 | +} |
yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/ErrorCodeConstants.java
| @@ -14,7 +14,6 @@ public interface ErrorCodeConstants { | @@ -14,7 +14,6 @@ public interface ErrorCodeConstants { | ||
| 14 | ErrorCode AUTH_LOGIN_USER_DISABLED = new ErrorCode(1_002_000_001, "登录失败,账号被禁用"); | 14 | ErrorCode AUTH_LOGIN_USER_DISABLED = new ErrorCode(1_002_000_001, "登录失败,账号被禁用"); |
| 15 | ErrorCode AUTH_LOGIN_CAPTCHA_CODE_ERROR = new ErrorCode(1_002_000_004, "验证码不正确,原因:{}"); | 15 | ErrorCode AUTH_LOGIN_CAPTCHA_CODE_ERROR = new ErrorCode(1_002_000_004, "验证码不正确,原因:{}"); |
| 16 | ErrorCode AUTH_THIRD_LOGIN_NOT_BIND = new ErrorCode(1_002_000_005, "未绑定账号,需要进行绑定"); | 16 | ErrorCode AUTH_THIRD_LOGIN_NOT_BIND = new ErrorCode(1_002_000_005, "未绑定账号,需要进行绑定"); |
| 17 | - ErrorCode AUTH_TOKEN_EXPIRED = new ErrorCode(1_002_000_006, "Token 已经过期"); | ||
| 18 | ErrorCode AUTH_MOBILE_NOT_EXISTS = new ErrorCode(1_002_000_007, "手机号不存在"); | 17 | ErrorCode AUTH_MOBILE_NOT_EXISTS = new ErrorCode(1_002_000_007, "手机号不存在"); |
| 19 | 18 | ||
| 20 | // ========== 菜单模块 1-002-001-000 ========== | 19 | // ========== 菜单模块 1-002-001-000 ========== |
| @@ -42,6 +41,7 @@ public interface ErrorCodeConstants { | @@ -42,6 +41,7 @@ public interface ErrorCodeConstants { | ||
| 42 | ErrorCode USER_PASSWORD_FAILED = new ErrorCode(1_002_003_005, "用户密码校验失败"); | 41 | ErrorCode USER_PASSWORD_FAILED = new ErrorCode(1_002_003_005, "用户密码校验失败"); |
| 43 | ErrorCode USER_IS_DISABLE = new ErrorCode(1_002_003_006, "名字为【{}】的用户已被禁用"); | 42 | ErrorCode USER_IS_DISABLE = new ErrorCode(1_002_003_006, "名字为【{}】的用户已被禁用"); |
| 44 | ErrorCode USER_COUNT_MAX = new ErrorCode(1_002_003_008, "创建用户失败,原因:超过租户最大租户配额({})!"); | 43 | ErrorCode USER_COUNT_MAX = new ErrorCode(1_002_003_008, "创建用户失败,原因:超过租户最大租户配额({})!"); |
| 44 | + ErrorCode USER_IMPORT_INIT_PASSWORD = new ErrorCode(1_002_003_009, "初始密码不能为空"); | ||
| 45 | 45 | ||
| 46 | // ========== 部门模块 1-002-004-000 ========== | 46 | // ========== 部门模块 1-002-004-000 ========== |
| 47 | ErrorCode DEPT_NAME_DUPLICATE = new ErrorCode(1_002_004_000, "已经存在该名字的部门"); | 47 | ErrorCode DEPT_NAME_DUPLICATE = new ErrorCode(1_002_004_000, "已经存在该名字的部门"); |
| @@ -49,7 +49,6 @@ public interface ErrorCodeConstants { | @@ -49,7 +49,6 @@ public interface ErrorCodeConstants { | ||
| 49 | ErrorCode DEPT_NOT_FOUND = new ErrorCode(1_002_004_002, "当前部门不存在"); | 49 | ErrorCode DEPT_NOT_FOUND = new ErrorCode(1_002_004_002, "当前部门不存在"); |
| 50 | ErrorCode DEPT_EXITS_CHILDREN = new ErrorCode(1_002_004_003, "存在子部门,无法删除"); | 50 | ErrorCode DEPT_EXITS_CHILDREN = new ErrorCode(1_002_004_003, "存在子部门,无法删除"); |
| 51 | ErrorCode DEPT_PARENT_ERROR = new ErrorCode(1_002_004_004, "不能设置自己为父部门"); | 51 | ErrorCode DEPT_PARENT_ERROR = new ErrorCode(1_002_004_004, "不能设置自己为父部门"); |
| 52 | - ErrorCode DEPT_EXISTS_USER = new ErrorCode(1_002_004_005, "部门中存在员工,无法删除"); | ||
| 53 | ErrorCode DEPT_NOT_ENABLE = new ErrorCode(1_002_004_006, "部门({})不处于开启状态,不允许选择"); | 52 | ErrorCode DEPT_NOT_ENABLE = new ErrorCode(1_002_004_006, "部门({})不处于开启状态,不允许选择"); |
| 54 | ErrorCode DEPT_PARENT_IS_CHILD = new ErrorCode(1_002_004_007, "不能设置自己的子部门为父部门"); | 53 | ErrorCode DEPT_PARENT_IS_CHILD = new ErrorCode(1_002_004_007, "不能设置自己的子部门为父部门"); |
| 55 | 54 | ||
| @@ -96,11 +95,8 @@ public interface ErrorCodeConstants { | @@ -96,11 +95,8 @@ public interface ErrorCodeConstants { | ||
| 96 | ErrorCode SMS_CODE_NOT_FOUND = new ErrorCode(1_002_014_000, "验证码不存在"); | 95 | ErrorCode SMS_CODE_NOT_FOUND = new ErrorCode(1_002_014_000, "验证码不存在"); |
| 97 | ErrorCode SMS_CODE_EXPIRED = new ErrorCode(1_002_014_001, "验证码已过期"); | 96 | ErrorCode SMS_CODE_EXPIRED = new ErrorCode(1_002_014_001, "验证码已过期"); |
| 98 | ErrorCode SMS_CODE_USED = new ErrorCode(1_002_014_002, "验证码已使用"); | 97 | ErrorCode SMS_CODE_USED = new ErrorCode(1_002_014_002, "验证码已使用"); |
| 99 | - ErrorCode SMS_CODE_NOT_CORRECT = new ErrorCode(1_002_014_003, "验证码不正确"); | ||
| 100 | ErrorCode SMS_CODE_EXCEED_SEND_MAXIMUM_QUANTITY_PER_DAY = new ErrorCode(1_002_014_004, "超过每日短信发送数量"); | 98 | ErrorCode SMS_CODE_EXCEED_SEND_MAXIMUM_QUANTITY_PER_DAY = new ErrorCode(1_002_014_004, "超过每日短信发送数量"); |
| 101 | ErrorCode SMS_CODE_SEND_TOO_FAST = new ErrorCode(1_002_014_005, "短信发送过于频繁"); | 99 | ErrorCode SMS_CODE_SEND_TOO_FAST = new ErrorCode(1_002_014_005, "短信发送过于频繁"); |
| 102 | - ErrorCode SMS_CODE_IS_EXISTS = new ErrorCode(1_002_014_006, "手机号已被使用"); | ||
| 103 | - ErrorCode SMS_CODE_IS_UNUSED = new ErrorCode(1_002_014_007, "验证码未被使用"); | ||
| 104 | 100 | ||
| 105 | // ========== 租户信息 1-002-015-000 ========== | 101 | // ========== 租户信息 1-002-015-000 ========== |
| 106 | ErrorCode TENANT_NOT_EXISTS = new ErrorCode(1_002_015_000, "租户不存在"); | 102 | ErrorCode TENANT_NOT_EXISTS = new ErrorCode(1_002_015_000, "租户不存在"); |
| @@ -138,7 +134,6 @@ public interface ErrorCodeConstants { | @@ -138,7 +134,6 @@ public interface ErrorCodeConstants { | ||
| 138 | ErrorCode OAUTH2_GRANT_CLIENT_ID_MISMATCH = new ErrorCode(1_002_021_000, "client_id 不匹配"); | 134 | ErrorCode OAUTH2_GRANT_CLIENT_ID_MISMATCH = new ErrorCode(1_002_021_000, "client_id 不匹配"); |
| 139 | ErrorCode OAUTH2_GRANT_REDIRECT_URI_MISMATCH = new ErrorCode(1_002_021_001, "redirect_uri 不匹配"); | 135 | ErrorCode OAUTH2_GRANT_REDIRECT_URI_MISMATCH = new ErrorCode(1_002_021_001, "redirect_uri 不匹配"); |
| 140 | ErrorCode OAUTH2_GRANT_STATE_MISMATCH = new ErrorCode(1_002_021_002, "state 不匹配"); | 136 | ErrorCode OAUTH2_GRANT_STATE_MISMATCH = new ErrorCode(1_002_021_002, "state 不匹配"); |
| 141 | - ErrorCode OAUTH2_GRANT_CODE_NOT_EXISTS = new ErrorCode(1_002_021_003, "code 不存在"); | ||
| 142 | 137 | ||
| 143 | // ========== OAuth2 授权 1-002-022-000 ========= | 138 | // ========== OAuth2 授权 1-002-022-000 ========= |
| 144 | ErrorCode OAUTH2_CODE_NOT_EXISTS = new ErrorCode(1_002_022_000, "code 不存在"); | 139 | ErrorCode OAUTH2_CODE_NOT_EXISTS = new ErrorCode(1_002_022_000, "code 不存在"); |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImpl.java
| @@ -10,6 +10,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult; | @@ -10,6 +10,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult; | ||
| 10 | import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; | 10 | import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; |
| 11 | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; | 11 | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| 12 | import cn.iocoder.yudao.framework.datapermission.core.util.DataPermissionUtils; | 12 | import cn.iocoder.yudao.framework.datapermission.core.util.DataPermissionUtils; |
| 13 | +import cn.iocoder.yudao.module.infra.api.config.ConfigApi; | ||
| 13 | import cn.iocoder.yudao.module.infra.api.file.FileApi; | 14 | import cn.iocoder.yudao.module.infra.api.file.FileApi; |
| 14 | import cn.iocoder.yudao.module.system.controller.admin.user.vo.profile.UserProfileUpdatePasswordReqVO; | 15 | import cn.iocoder.yudao.module.system.controller.admin.user.vo.profile.UserProfileUpdatePasswordReqVO; |
| 15 | import cn.iocoder.yudao.module.system.controller.admin.user.vo.profile.UserProfileUpdateReqVO; | 16 | import cn.iocoder.yudao.module.system.controller.admin.user.vo.profile.UserProfileUpdateReqVO; |
| @@ -32,7 +33,6 @@ import com.mzt.logapi.service.impl.DiffParseFunction; | @@ -32,7 +33,6 @@ import com.mzt.logapi.service.impl.DiffParseFunction; | ||
| 32 | import com.mzt.logapi.starter.annotation.LogRecord; | 33 | import com.mzt.logapi.starter.annotation.LogRecord; |
| 33 | import jakarta.annotation.Resource; | 34 | import jakarta.annotation.Resource; |
| 34 | import lombok.extern.slf4j.Slf4j; | 35 | import lombok.extern.slf4j.Slf4j; |
| 35 | -import org.springframework.beans.factory.annotation.Value; | ||
| 36 | import org.springframework.context.annotation.Lazy; | 36 | import org.springframework.context.annotation.Lazy; |
| 37 | import org.springframework.security.crypto.password.PasswordEncoder; | 37 | import org.springframework.security.crypto.password.PasswordEncoder; |
| 38 | import org.springframework.stereotype.Service; | 38 | import org.springframework.stereotype.Service; |
| @@ -57,8 +57,7 @@ import static cn.iocoder.yudao.module.system.enums.LogRecordConstants.*; | @@ -57,8 +57,7 @@ import static cn.iocoder.yudao.module.system.enums.LogRecordConstants.*; | ||
| 57 | @Slf4j | 57 | @Slf4j |
| 58 | public class AdminUserServiceImpl implements AdminUserService { | 58 | public class AdminUserServiceImpl implements AdminUserService { |
| 59 | 59 | ||
| 60 | - @Value("${sys.user.init-password:yudaoyuanma}") | ||
| 61 | - private String userInitPassword; | 60 | + static final String USER_INIT_PASSWORD_KEY = "system.user.init-password"; |
| 62 | 61 | ||
| 63 | @Resource | 62 | @Resource |
| 64 | private AdminUserMapper userMapper; | 63 | private AdminUserMapper userMapper; |
| @@ -80,6 +79,8 @@ public class AdminUserServiceImpl implements AdminUserService { | @@ -80,6 +79,8 @@ public class AdminUserServiceImpl implements AdminUserService { | ||
| 80 | 79 | ||
| 81 | @Resource | 80 | @Resource |
| 82 | private FileApi fileApi; | 81 | private FileApi fileApi; |
| 82 | + @Resource | ||
| 83 | + private ConfigApi configApi; | ||
| 83 | 84 | ||
| 84 | @Override | 85 | @Override |
| 85 | @Transactional(rollbackFor = Exception.class) | 86 | @Transactional(rollbackFor = Exception.class) |
| @@ -428,9 +429,17 @@ public class AdminUserServiceImpl implements AdminUserService { | @@ -428,9 +429,17 @@ public class AdminUserServiceImpl implements AdminUserService { | ||
| 428 | @Override | 429 | @Override |
| 429 | @Transactional(rollbackFor = Exception.class) // 添加事务,异常则回滚所有导入 | 430 | @Transactional(rollbackFor = Exception.class) // 添加事务,异常则回滚所有导入 |
| 430 | public UserImportRespVO importUserList(List<UserImportExcelVO> importUsers, boolean isUpdateSupport) { | 431 | public UserImportRespVO importUserList(List<UserImportExcelVO> importUsers, boolean isUpdateSupport) { |
| 432 | + // 1.1 参数校验 | ||
| 431 | if (CollUtil.isEmpty(importUsers)) { | 433 | if (CollUtil.isEmpty(importUsers)) { |
| 432 | throw exception(USER_IMPORT_LIST_IS_EMPTY); | 434 | throw exception(USER_IMPORT_LIST_IS_EMPTY); |
| 433 | } | 435 | } |
| 436 | + // 1.2 初始化密码不能为空 | ||
| 437 | + String initPassword = configApi.getConfigValueByKey(USER_INIT_PASSWORD_KEY); | ||
| 438 | + if (StrUtil.isEmpty(initPassword)) { | ||
| 439 | + throw exception(USER_IMPORT_INIT_PASSWORD); | ||
| 440 | + } | ||
| 441 | + | ||
| 442 | + // 2. 遍历,逐个创建 or 更新 | ||
| 434 | UserImportRespVO respVO = UserImportRespVO.builder().createUsernames(new ArrayList<>()) | 443 | UserImportRespVO respVO = UserImportRespVO.builder().createUsernames(new ArrayList<>()) |
| 435 | .updateUsernames(new ArrayList<>()).failureUsernames(new LinkedHashMap<>()).build(); | 444 | .updateUsernames(new ArrayList<>()).failureUsernames(new LinkedHashMap<>()).build(); |
| 436 | importUsers.forEach(importUser -> { | 445 | importUsers.forEach(importUser -> { |
| @@ -446,7 +455,7 @@ public class AdminUserServiceImpl implements AdminUserService { | @@ -446,7 +455,7 @@ public class AdminUserServiceImpl implements AdminUserService { | ||
| 446 | AdminUserDO existUser = userMapper.selectByUsername(importUser.getUsername()); | 455 | AdminUserDO existUser = userMapper.selectByUsername(importUser.getUsername()); |
| 447 | if (existUser == null) { | 456 | if (existUser == null) { |
| 448 | userMapper.insert(BeanUtils.toBean(importUser, AdminUserDO.class) | 457 | userMapper.insert(BeanUtils.toBean(importUser, AdminUserDO.class) |
| 449 | - .setPassword(encodePassword(userInitPassword)).setPostIds(new HashSet<>())); // 设置默认密码及空岗位编号数组 | 458 | + .setPassword(encodePassword(initPassword)).setPostIds(new HashSet<>())); // 设置默认密码及空岗位编号数组 |
| 450 | respVO.getCreateUsernames().add(importUser.getUsername()); | 459 | respVO.getCreateUsernames().add(importUser.getUsername()); |
| 451 | return; | 460 | return; |
| 452 | } | 461 | } |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImplTest.java
| @@ -7,6 +7,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult; | @@ -7,6 +7,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult; | ||
| 7 | import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils; | 7 | import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils; |
| 8 | import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; | 8 | import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; |
| 9 | import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; | 9 | import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; |
| 10 | +import cn.iocoder.yudao.module.infra.api.config.ConfigApi; | ||
| 10 | import cn.iocoder.yudao.module.infra.api.file.FileApi; | 11 | import cn.iocoder.yudao.module.infra.api.file.FileApi; |
| 11 | import cn.iocoder.yudao.module.system.controller.admin.user.vo.profile.UserProfileUpdatePasswordReqVO; | 12 | import cn.iocoder.yudao.module.system.controller.admin.user.vo.profile.UserProfileUpdatePasswordReqVO; |
| 12 | import cn.iocoder.yudao.module.system.controller.admin.user.vo.profile.UserProfileUpdateReqVO; | 13 | import cn.iocoder.yudao.module.system.controller.admin.user.vo.profile.UserProfileUpdateReqVO; |
| @@ -23,6 +24,7 @@ import cn.iocoder.yudao.module.system.service.dept.DeptService; | @@ -23,6 +24,7 @@ import cn.iocoder.yudao.module.system.service.dept.DeptService; | ||
| 23 | import cn.iocoder.yudao.module.system.service.dept.PostService; | 24 | import cn.iocoder.yudao.module.system.service.dept.PostService; |
| 24 | import cn.iocoder.yudao.module.system.service.permission.PermissionService; | 25 | import cn.iocoder.yudao.module.system.service.permission.PermissionService; |
| 25 | import cn.iocoder.yudao.module.system.service.tenant.TenantService; | 26 | import cn.iocoder.yudao.module.system.service.tenant.TenantService; |
| 27 | +import org.junit.jupiter.api.BeforeEach; | ||
| 26 | import org.junit.jupiter.api.Test; | 28 | import org.junit.jupiter.api.Test; |
| 27 | import org.mockito.stubbing.Answer; | 29 | import org.mockito.stubbing.Answer; |
| 28 | import org.springframework.boot.test.mock.mockito.MockBean; | 30 | import org.springframework.boot.test.mock.mockito.MockBean; |
| @@ -46,6 +48,7 @@ import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEq | @@ -46,6 +48,7 @@ import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEq | ||
| 46 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; | 48 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; |
| 47 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; | 49 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; |
| 48 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; | 50 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; |
| 51 | +import static cn.iocoder.yudao.module.system.service.user.AdminUserServiceImpl.USER_INIT_PASSWORD_KEY; | ||
| 49 | import static java.util.Collections.singleton; | 52 | import static java.util.Collections.singleton; |
| 50 | import static java.util.Collections.singletonList; | 53 | import static java.util.Collections.singletonList; |
| 51 | import static org.assertj.core.util.Lists.newArrayList; | 54 | import static org.assertj.core.util.Lists.newArrayList; |
| @@ -76,6 +79,14 @@ public class AdminUserServiceImplTest extends BaseDbUnitTest { | @@ -76,6 +79,14 @@ public class AdminUserServiceImplTest extends BaseDbUnitTest { | ||
| 76 | private TenantService tenantService; | 79 | private TenantService tenantService; |
| 77 | @MockBean | 80 | @MockBean |
| 78 | private FileApi fileApi; | 81 | private FileApi fileApi; |
| 82 | + @MockBean | ||
| 83 | + private ConfigApi configApi; | ||
| 84 | + | ||
| 85 | + @BeforeEach | ||
| 86 | + public void before() { | ||
| 87 | + // mock 初始化密码 | ||
| 88 | + when(configApi.getConfigValueByKey(USER_INIT_PASSWORD_KEY)).thenReturn("yudaoyuanma"); | ||
| 89 | + } | ||
| 79 | 90 | ||
| 80 | @Test | 91 | @Test |
| 81 | public void testCreatUser_success() { | 92 | public void testCreatUser_success() { |