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 | 14 | ErrorCode AUTH_LOGIN_USER_DISABLED = new ErrorCode(1_002_000_001, "登录失败,账号被禁用"); |
| 15 | 15 | ErrorCode AUTH_LOGIN_CAPTCHA_CODE_ERROR = new ErrorCode(1_002_000_004, "验证码不正确,原因:{}"); |
| 16 | 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 | 17 | ErrorCode AUTH_MOBILE_NOT_EXISTS = new ErrorCode(1_002_000_007, "手机号不存在"); |
| 19 | 18 | |
| 20 | 19 | // ========== 菜单模块 1-002-001-000 ========== |
| ... | ... | @@ -42,6 +41,7 @@ public interface ErrorCodeConstants { |
| 42 | 41 | ErrorCode USER_PASSWORD_FAILED = new ErrorCode(1_002_003_005, "用户密码校验失败"); |
| 43 | 42 | ErrorCode USER_IS_DISABLE = new ErrorCode(1_002_003_006, "名字为【{}】的用户已被禁用"); |
| 44 | 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 | 46 | // ========== 部门模块 1-002-004-000 ========== |
| 47 | 47 | ErrorCode DEPT_NAME_DUPLICATE = new ErrorCode(1_002_004_000, "已经存在该名字的部门"); |
| ... | ... | @@ -49,7 +49,6 @@ public interface ErrorCodeConstants { |
| 49 | 49 | ErrorCode DEPT_NOT_FOUND = new ErrorCode(1_002_004_002, "当前部门不存在"); |
| 50 | 50 | ErrorCode DEPT_EXITS_CHILDREN = new ErrorCode(1_002_004_003, "存在子部门,无法删除"); |
| 51 | 51 | ErrorCode DEPT_PARENT_ERROR = new ErrorCode(1_002_004_004, "不能设置自己为父部门"); |
| 52 | - ErrorCode DEPT_EXISTS_USER = new ErrorCode(1_002_004_005, "部门中存在员工,无法删除"); | |
| 53 | 52 | ErrorCode DEPT_NOT_ENABLE = new ErrorCode(1_002_004_006, "部门({})不处于开启状态,不允许选择"); |
| 54 | 53 | ErrorCode DEPT_PARENT_IS_CHILD = new ErrorCode(1_002_004_007, "不能设置自己的子部门为父部门"); |
| 55 | 54 | |
| ... | ... | @@ -96,11 +95,8 @@ public interface ErrorCodeConstants { |
| 96 | 95 | ErrorCode SMS_CODE_NOT_FOUND = new ErrorCode(1_002_014_000, "验证码不存在"); |
| 97 | 96 | ErrorCode SMS_CODE_EXPIRED = new ErrorCode(1_002_014_001, "验证码已过期"); |
| 98 | 97 | ErrorCode SMS_CODE_USED = new ErrorCode(1_002_014_002, "验证码已使用"); |
| 99 | - ErrorCode SMS_CODE_NOT_CORRECT = new ErrorCode(1_002_014_003, "验证码不正确"); | |
| 100 | 98 | ErrorCode SMS_CODE_EXCEED_SEND_MAXIMUM_QUANTITY_PER_DAY = new ErrorCode(1_002_014_004, "超过每日短信发送数量"); |
| 101 | 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 | 101 | // ========== 租户信息 1-002-015-000 ========== |
| 106 | 102 | ErrorCode TENANT_NOT_EXISTS = new ErrorCode(1_002_015_000, "租户不存在"); |
| ... | ... | @@ -138,7 +134,6 @@ public interface ErrorCodeConstants { |
| 138 | 134 | ErrorCode OAUTH2_GRANT_CLIENT_ID_MISMATCH = new ErrorCode(1_002_021_000, "client_id 不匹配"); |
| 139 | 135 | ErrorCode OAUTH2_GRANT_REDIRECT_URI_MISMATCH = new ErrorCode(1_002_021_001, "redirect_uri 不匹配"); |
| 140 | 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 | 138 | // ========== OAuth2 授权 1-002-022-000 ========= |
| 144 | 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 | 10 | import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; |
| 11 | 11 | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| 12 | 12 | import cn.iocoder.yudao.framework.datapermission.core.util.DataPermissionUtils; |
| 13 | +import cn.iocoder.yudao.module.infra.api.config.ConfigApi; | |
| 13 | 14 | import cn.iocoder.yudao.module.infra.api.file.FileApi; |
| 14 | 15 | import cn.iocoder.yudao.module.system.controller.admin.user.vo.profile.UserProfileUpdatePasswordReqVO; |
| 15 | 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 | 33 | import com.mzt.logapi.starter.annotation.LogRecord; |
| 33 | 34 | import jakarta.annotation.Resource; |
| 34 | 35 | import lombok.extern.slf4j.Slf4j; |
| 35 | -import org.springframework.beans.factory.annotation.Value; | |
| 36 | 36 | import org.springframework.context.annotation.Lazy; |
| 37 | 37 | import org.springframework.security.crypto.password.PasswordEncoder; |
| 38 | 38 | import org.springframework.stereotype.Service; |
| ... | ... | @@ -57,8 +57,7 @@ import static cn.iocoder.yudao.module.system.enums.LogRecordConstants.*; |
| 57 | 57 | @Slf4j |
| 58 | 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 | 62 | @Resource |
| 64 | 63 | private AdminUserMapper userMapper; |
| ... | ... | @@ -80,6 +79,8 @@ public class AdminUserServiceImpl implements AdminUserService { |
| 80 | 79 | |
| 81 | 80 | @Resource |
| 82 | 81 | private FileApi fileApi; |
| 82 | + @Resource | |
| 83 | + private ConfigApi configApi; | |
| 83 | 84 | |
| 84 | 85 | @Override |
| 85 | 86 | @Transactional(rollbackFor = Exception.class) |
| ... | ... | @@ -428,9 +429,17 @@ public class AdminUserServiceImpl implements AdminUserService { |
| 428 | 429 | @Override |
| 429 | 430 | @Transactional(rollbackFor = Exception.class) // 添加事务,异常则回滚所有导入 |
| 430 | 431 | public UserImportRespVO importUserList(List<UserImportExcelVO> importUsers, boolean isUpdateSupport) { |
| 432 | + // 1.1 参数校验 | |
| 431 | 433 | if (CollUtil.isEmpty(importUsers)) { |
| 432 | 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 | 443 | UserImportRespVO respVO = UserImportRespVO.builder().createUsernames(new ArrayList<>()) |
| 435 | 444 | .updateUsernames(new ArrayList<>()).failureUsernames(new LinkedHashMap<>()).build(); |
| 436 | 445 | importUsers.forEach(importUser -> { |
| ... | ... | @@ -446,7 +455,7 @@ public class AdminUserServiceImpl implements AdminUserService { |
| 446 | 455 | AdminUserDO existUser = userMapper.selectByUsername(importUser.getUsername()); |
| 447 | 456 | if (existUser == null) { |
| 448 | 457 | userMapper.insert(BeanUtils.toBean(importUser, AdminUserDO.class) |
| 449 | - .setPassword(encodePassword(userInitPassword)).setPostIds(new HashSet<>())); // 设置默认密码及空岗位编号数组 | |
| 458 | + .setPassword(encodePassword(initPassword)).setPostIds(new HashSet<>())); // 设置默认密码及空岗位编号数组 | |
| 450 | 459 | respVO.getCreateUsernames().add(importUser.getUsername()); |
| 451 | 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 | 7 | import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils; |
| 8 | 8 | import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; |
| 9 | 9 | import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; |
| 10 | +import cn.iocoder.yudao.module.infra.api.config.ConfigApi; | |
| 10 | 11 | import cn.iocoder.yudao.module.infra.api.file.FileApi; |
| 11 | 12 | import cn.iocoder.yudao.module.system.controller.admin.user.vo.profile.UserProfileUpdatePasswordReqVO; |
| 12 | 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 | 24 | import cn.iocoder.yudao.module.system.service.dept.PostService; |
| 24 | 25 | import cn.iocoder.yudao.module.system.service.permission.PermissionService; |
| 25 | 26 | import cn.iocoder.yudao.module.system.service.tenant.TenantService; |
| 27 | +import org.junit.jupiter.api.BeforeEach; | |
| 26 | 28 | import org.junit.jupiter.api.Test; |
| 27 | 29 | import org.mockito.stubbing.Answer; |
| 28 | 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 | 48 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; |
| 47 | 49 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; |
| 48 | 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 | 52 | import static java.util.Collections.singleton; |
| 50 | 53 | import static java.util.Collections.singletonList; |
| 51 | 54 | import static org.assertj.core.util.Lists.newArrayList; |
| ... | ... | @@ -76,6 +79,14 @@ public class AdminUserServiceImplTest extends BaseDbUnitTest { |
| 76 | 79 | private TenantService tenantService; |
| 77 | 80 | @MockBean |
| 78 | 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 | 91 | @Test |
| 81 | 92 | public void testCreatUser_success() { | ... | ... |