From 5754dd178386c54a7e31cf6c997f33771feca748 Mon Sep 17 00:00:00 2001 From: jianxing Date: Thu, 24 Aug 2023 16:14:45 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E7=B3=BB=E7=BB=9F=E8=AE=BE=E7=BD=AE):=20?= =?UTF-8?q?=E6=8F=92=E4=BB=B6=E5=88=97=E8=A1=A8=E5=88=9B=E5=BB=BA=E4=BA=BA?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=E7=9A=84=E6=98=AFID?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --task=1012390 --user=陈建星 系统设置-系统-插件管理-后台 https://www.tapd.cn/55049933/s/1408086 --- .../sdk/mapper/BaseUserMapper.java | 1 + .../metersphere/sdk/mapper/BaseUserMapper.xml | 9 ++++++ .../sdk/service/BaseUserService.java | 22 +++++++++++++- .../system/service/PluginService.java | 10 +++++++ .../controller/PluginControllerTests.java | 29 ++++++++++++++----- 5 files changed, 63 insertions(+), 8 deletions(-) diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/BaseUserMapper.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/BaseUserMapper.java index 86fdb27107..5c8c3128b3 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/BaseUserMapper.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/BaseUserMapper.java @@ -33,4 +33,5 @@ public interface BaseUserMapper { List getExcludeSelectOption(); + List getSelectOptionByIds(List ids); } diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/BaseUserMapper.xml b/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/BaseUserMapper.xml index 090e02ad14..2ec9b2f71b 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/BaseUserMapper.xml +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/BaseUserMapper.xml @@ -96,4 +96,13 @@ select id, name from user + \ No newline at end of file diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/BaseUserService.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/BaseUserService.java index 05d5537960..d3d2249e53 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/BaseUserService.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/BaseUserService.java @@ -107,8 +107,9 @@ public class BaseUserService { throw new UnauthorizedException(Translator.get("not_authorized") + e.getMessage()); } } + //保存日志 - public void saveLog(String userId, String method, String path, String content, String type){ + public void saveLog(String userId, String method, String path, String content, String type) { User user = userMapper.selectByPrimaryKey(userId); LogDTO dto = new LogDTO( OperationLogConstants.SYSTEM, @@ -428,10 +429,29 @@ public class BaseUserService { return baseUserMapper.getExcludeSelectOption(); } + public List getSelectOptionByIds(List ids) { + return baseUserMapper.getSelectOptionByIds(ids); + } + public Map getUserNameMap() { List excludeSelectOption = getExcludeSelectOption(); Map nameMap = new HashMap<>(); excludeSelectOption.forEach(option -> nameMap.put(option.getId(), option.getName())); return nameMap; } + + /** + * 根据用户ID列表,获取用户 + * + * @param userIds + * @return + */ + public Map getUserNameMap(List userIds) { + if (CollectionUtils.isEmpty(userIds)) { + return Collections.emptyMap(); + } + return getSelectOptionByIds(userIds) + .stream() + .collect(Collectors.toMap(OptionDTO::getId, OptionDTO::getName)); + } } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/PluginService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/PluginService.java index f9f35f3907..2dc927a163 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/PluginService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/PluginService.java @@ -8,6 +8,7 @@ import io.metersphere.sdk.constants.PluginScenarioType; import io.metersphere.sdk.controller.handler.result.CommonResultCode; import io.metersphere.sdk.dto.OptionDTO; import io.metersphere.sdk.exception.MSException; +import io.metersphere.sdk.service.BaseUserService; import io.metersphere.sdk.service.JdbcDriverPluginService; import io.metersphere.sdk.service.PluginLoadService; import io.metersphere.sdk.util.BeanUtils; @@ -58,18 +59,27 @@ public class PluginService { JdbcDriverPluginService jdbcDriverPluginService; @Resource private KafkaTemplate kafkaTemplate; + @Resource + private BaseUserService baseUserService; public List list() { List plugins = extPluginMapper.getPlugins(); List pluginIds = plugins.stream().map(Plugin::getId).toList(); Map> scripteMap = pluginScriptService.getScripteMap(pluginIds); Map> orgMap = pluginOrganizationService.getOrgMap(pluginIds); + + // 获取用户ID和名称的映射 + List userIds = plugins.stream().map(PluginDTO::getCreateUser).toList(); + Map userNameMap = baseUserService.getUserNameMap(userIds); + plugins.forEach(plugin -> { List pluginForms = scripteMap.get(plugin.getId()); List organizations = orgMap.get(plugin.getId()); plugin.setPluginForms(pluginForms == null ? new ArrayList<>(0) : pluginForms); plugin.setOrganizations(organizations == null ? new ArrayList<>(0) : organizations); + plugin.setCreateUser(userNameMap.get(plugin.getCreateUser())); }); + return plugins; } diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/PluginControllerTests.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/PluginControllerTests.java index 8dcadd0b3a..2a8eb25495 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/PluginControllerTests.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/PluginControllerTests.java @@ -5,12 +5,14 @@ import io.metersphere.sdk.constants.PermissionConstants; import io.metersphere.sdk.constants.PluginScenarioType; import io.metersphere.sdk.dto.OptionDTO; import io.metersphere.sdk.log.constants.OperationLogType; +import io.metersphere.sdk.service.BaseUserService; import io.metersphere.sdk.service.JdbcDriverPluginService; import io.metersphere.sdk.util.JSON; import io.metersphere.system.controller.param.PluginUpdateRequestDefinition; import io.metersphere.system.domain.*; import io.metersphere.system.dto.OrganizationDTO; import io.metersphere.system.dto.PluginDTO; +import io.metersphere.system.mapper.ExtPluginMapper; import io.metersphere.system.mapper.PluginMapper; import io.metersphere.system.mapper.PluginOrganizationMapper; import io.metersphere.system.mapper.PluginScriptMapper; @@ -25,10 +27,8 @@ import org.springframework.test.web.servlet.MvcResult; import org.springframework.util.MultiValueMap; import java.io.File; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Optional; +import java.util.*; +import java.util.stream.Collectors; import static io.metersphere.sdk.controller.handler.result.CommonResultCode.FILE_NAME_ILLEGAL; import static io.metersphere.system.controller.result.SystemResultCode.*; @@ -48,6 +48,10 @@ public class PluginControllerTests extends BaseTest { @Resource private PluginMapper pluginMapper; @Resource + private ExtPluginMapper extPluginMapper; + @Resource + private BaseUserService baseUserService; + @Resource private PluginOrganizationMapper pluginOrganizationMapper; @Resource private PluginScriptMapper pluginScriptMapper; @@ -231,7 +235,7 @@ public class PluginControllerTests extends BaseTest { // @@重名校验异常 request.setName(anotherAddPlugin.getName()); - assertErrorCode( this.requestPost(DEFAULT_UPDATE, request), PLUGIN_EXIST); + assertErrorCode(this.requestPost(DEFAULT_UPDATE, request), PLUGIN_EXIST); // @@校验日志 checkLog(request.getId(), OperationLogType.UPDATE); @@ -260,10 +264,21 @@ public class PluginControllerTests extends BaseTest { // 校验数据是否正确 List pluginList = getResultDataArray(mvcResult, PluginDTO.class); Assertions.assertEquals(3, pluginList.size()); + + // 获取数据库数据 + List pluginIds = pluginList.stream().map(PluginDTO::getId).toList(); + PluginExample example = new PluginExample(); + example.createCriteria().andPluginIdNotIn(pluginIds); + List dbPlugins = pluginMapper.selectByExample(example); + List userIds = dbPlugins.stream().map(Plugin::getCreateUser).toList(); + Map userNameMap = baseUserService.getUserNameMap(userIds); + Map dbPluginMap = dbPlugins.stream().collect(Collectors.toMap(Plugin::getId, i -> i)); + for (PluginDTO pluginDTO : pluginList) { - Plugin comparePlugin = pluginMapper.selectByPrimaryKey(pluginDTO.getId()); + Plugin comparePlugin = dbPluginMap.get(pluginDTO.getId()); Plugin plugin = JSON.parseObject(JSON.toJSONString(pluginDTO), Plugin.class); List scriptIds = pluginDTO.getPluginForms().stream().map(OptionDTO::getId).toList(); + comparePlugin.setCreateUser(userNameMap.get(comparePlugin.getCreateUser())); Assertions.assertEquals(plugin, comparePlugin); Assertions.assertEquals(scriptIds, getScriptIdsByPlugId(plugin.getId())); List orgList = Optional.ofNullable(pluginDTO.getOrganizations()).orElse(new ArrayList<>(0)); @@ -281,7 +296,7 @@ public class PluginControllerTests extends BaseTest { mockMvc.perform(getRequestBuilder(PLUGIN_IMAGE, anotherAddPlugin.getId(), "/static/jira.jpg")) .andExpect(status().isOk()); - assertErrorCode( this.requestGet(PLUGIN_IMAGE, anotherAddPlugin.getId(), "/static/jira.doc"), FILE_NAME_ILLEGAL); + assertErrorCode(this.requestGet(PLUGIN_IMAGE, anotherAddPlugin.getId(), "/static/jira.doc"), FILE_NAME_ILLEGAL); } @Test