From 684e3cf965a14976ec95e836515d307fa38853b0 Mon Sep 17 00:00:00 2001 From: wxg0103 <727495428@qq.com> Date: Wed, 8 Nov 2023 13:56:52 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E7=B3=BB=E7=BB=9F=E8=AE=BE=E7=BD=AE):?= =?UTF-8?q?=20=E4=BC=98=E5=8C=96=E9=A1=B9=E7=9B=AE=E8=AE=B0=E5=BD=95?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../project/service/ProjectLogService.java | 6 ++--- .../system/service/CommonProjectService.java | 26 +++++++++++++++---- .../OrganizationProjectLogService.java | 21 ++++++++------- .../service/SystemProjectLogService.java | 8 +++--- .../OrganizationProjectControllerTests.java | 10 +++---- .../SystemProjectControllerTests.java | 10 +++---- 6 files changed, 49 insertions(+), 32 deletions(-) diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/service/ProjectLogService.java b/backend/services/project-management/src/main/java/io/metersphere/project/service/ProjectLogService.java index 410378b5ed..246ce1a38d 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/service/ProjectLogService.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/service/ProjectLogService.java @@ -2,11 +2,11 @@ package io.metersphere.project.service; import io.metersphere.project.domain.Project; import io.metersphere.project.mapper.ProjectMapper; -import io.metersphere.system.log.dto.LogDTO; -import io.metersphere.system.dto.UpdateProjectRequest; import io.metersphere.sdk.util.JSON; +import io.metersphere.system.dto.UpdateProjectRequest; import io.metersphere.system.log.constants.OperationLogModule; import io.metersphere.system.log.constants.OperationLogType; +import io.metersphere.system.log.dto.LogDTO; import jakarta.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -32,7 +32,7 @@ public class ProjectLogService { project.getCreateUser(), OperationLogType.UPDATE.name(), OperationLogModule.PROJECT_MANAGEMENT, - project.getName()); + request.getName()); dto.setOriginalValue(JSON.toJSONBytes(project)); return dto; diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/CommonProjectService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/CommonProjectService.java index 800f27464d..d705dd2ea7 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/CommonProjectService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/CommonProjectService.java @@ -313,7 +313,11 @@ public class CommonProjectService { deleteExample.createCriteria().andSourceIdEqualTo(project.getId()).andUserIdIn(deleteIds).andRoleIdEqualTo(InternalUserRole.PROJECT_ADMIN.getValue()); userRoleRelationMapper.selectByExample(deleteExample).forEach(userRoleRelation -> { User user = userMapper.selectByPrimaryKey(userRoleRelation.getUserId()); - LogDTO logDTO = new LogDTO(project.getId(), project.getOrganizationId(), userRoleRelation.getId(), updateUser, OperationLogType.DELETE.name(), module, Translator.get("delete") + Translator.get("project_admin") + ": " + user.getName()); + String logProjectId = OperationLogConstants.SYSTEM; + if (StringUtils.equals(module, OperationLogModule.SETTING_ORGANIZATION_PROJECT)) { + logProjectId = OperationLogConstants.ORGANIZATION; + } + LogDTO logDTO = new LogDTO(logProjectId, project.getOrganizationId(), userRoleRelation.getId(), updateUser, OperationLogType.DELETE.name(), module, Translator.get("delete") + Translator.get("project_admin") + ": " + user.getName()); setLog(logDTO, path, HttpMethodConstants.POST.name(), logDTOList); }); userRoleRelationMapper.deleteByExample(deleteExample); @@ -332,7 +336,7 @@ public class CommonProjectService { deleteExample.createCriteria().andSourceIdEqualTo(project.getId()).andUserIdIn(orgUserIds).andRoleIdEqualTo(InternalUserRole.PROJECT_ADMIN.getValue()); userRoleRelationMapper.selectByExample(deleteExample).forEach(userRoleRelation -> { User user = userMapper.selectByPrimaryKey(userRoleRelation.getUserId()); - LogDTO logDTO = new LogDTO(project.getId(), project.getOrganizationId(), userRoleRelation.getId(), updateUser, OperationLogType.DELETE.name(), module, Translator.get("delete") + Translator.get("project_admin") + ": " + user.getName()); + LogDTO logDTO = new LogDTO(OperationLogConstants.SYSTEM, project.getOrganizationId(), userRoleRelation.getId(), updateUser, OperationLogType.DELETE.name(), module, Translator.get("delete") + Translator.get("project_admin") + ": " + user.getName()); setLog(logDTO, path, HttpMethodConstants.POST.name(), logDTOList); }); userRoleRelationMapper.deleteByExample(deleteExample); @@ -406,7 +410,11 @@ public class CommonProjectService { adminRole.setCreateUser(createUser); adminRole.setOrganizationId(project.getOrganizationId()); userRoleRelations.add(adminRole); - LogDTO logDTO = new LogDTO(projectId, project.getOrganizationId(), adminRole.getId(), createUser, type, module, content + Translator.get("project_admin") + ": " + nameMap.get(userId)); + String logProjectId = OperationLogConstants.SYSTEM; + if (StringUtils.equals(module, OperationLogModule.SETTING_ORGANIZATION_PROJECT)) { + logProjectId = OperationLogConstants.ORGANIZATION; + } + LogDTO logDTO = new LogDTO(logProjectId, project.getOrganizationId(), adminRole.getId(), createUser, type, module, content + Translator.get("project_admin") + ": " + nameMap.get(userId)); setLog(logDTO, path, HttpMethodConstants.POST.name(), logDTOList); } }); @@ -462,7 +470,11 @@ public class CommonProjectService { memberRole.setCreateUser(createUser); memberRole.setOrganizationId(project.getOrganizationId()); userRoleRelations.add(memberRole); - LogDTO logDTO = new LogDTO(OperationLogConstants.SYSTEM, OperationLogConstants.SYSTEM, memberRole.getId(), createUser, type, module, content + Translator.get("project_member") + ": " + userMap.get(userId)); + String logProjectId = OperationLogConstants.SYSTEM; + if (StringUtils.equals(module, OperationLogModule.SETTING_ORGANIZATION_PROJECT)) { + logProjectId = OperationLogConstants.ORGANIZATION; + } + LogDTO logDTO = new LogDTO(logProjectId, OperationLogConstants.SYSTEM, memberRole.getId(), createUser, type, module, content + Translator.get("project_member") + ": " + userMap.get(userId)); setLog(logDTO, path, HttpMethodConstants.POST.name(), logDTOList); } }); @@ -491,7 +503,11 @@ public class CommonProjectService { } List logDTOList = new ArrayList<>(); userRoleRelationMapper.selectByExample(userRoleRelationExample).forEach(userRoleRelation -> { - LogDTO logDTO = new LogDTO(OperationLogConstants.SYSTEM, OperationLogConstants.SYSTEM, userRoleRelation.getId(), createUser, OperationLogType.DELETE.name(), module, Translator.get("delete") + Translator.get("project_member") + ": " + user.getName()); + String logProjectId = OperationLogConstants.SYSTEM; + if (StringUtils.equals(module, OperationLogModule.SETTING_ORGANIZATION_PROJECT)) { + logProjectId = OperationLogConstants.ORGANIZATION; + } + LogDTO logDTO = new LogDTO(logProjectId, OperationLogConstants.SYSTEM, userRoleRelation.getId(), createUser, OperationLogType.DELETE.name(), module, Translator.get("delete") + Translator.get("project_member") + ": " + user.getName()); setLog(logDTO, path, HttpMethodConstants.GET.name(), logDTOList); }); operationLogService.batchAdd(logDTOList); diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/OrganizationProjectLogService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/OrganizationProjectLogService.java index 1ff43ef4a0..d0ff16e0ec 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/OrganizationProjectLogService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/OrganizationProjectLogService.java @@ -3,13 +3,14 @@ package io.metersphere.system.service; import io.metersphere.project.domain.Project; import io.metersphere.project.mapper.ProjectMapper; import io.metersphere.sdk.constants.HttpMethodConstants; +import io.metersphere.sdk.constants.OperationLogConstants; +import io.metersphere.sdk.util.JSON; import io.metersphere.system.dto.AddProjectRequest; -import io.metersphere.system.log.dto.LogDTO; import io.metersphere.system.dto.UpdateProjectNameRequest; import io.metersphere.system.dto.UpdateProjectRequest; import io.metersphere.system.log.constants.OperationLogModule; import io.metersphere.system.log.constants.OperationLogType; -import io.metersphere.sdk.util.JSON; +import io.metersphere.system.log.dto.LogDTO; import jakarta.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -28,7 +29,7 @@ public class OrganizationProjectLogService { */ public LogDTO addLog(AddProjectRequest project) { LogDTO dto = new LogDTO( - project.getId(), + OperationLogConstants.ORGANIZATION, project.getOrganizationId(), null, null, @@ -48,13 +49,13 @@ public class OrganizationProjectLogService { Project project = projectMapper.selectByPrimaryKey(request.getId()); if (project != null) { LogDTO dto = new LogDTO( - project.getId(), + OperationLogConstants.ORGANIZATION, project.getOrganizationId(), project.getId(), project.getCreateUser(), OperationLogType.UPDATE.name(), OperationLogModule.SETTING_ORGANIZATION_PROJECT, - project.getName()); + request.getName()); dto.setOriginalValue(JSON.toJSONBytes(project)); return dto; @@ -66,13 +67,13 @@ public class OrganizationProjectLogService { Project project = projectMapper.selectByPrimaryKey(request.getId()); if (project != null) { LogDTO dto = new LogDTO( - project.getId(), + OperationLogConstants.ORGANIZATION, project.getOrganizationId(), project.getId(), project.getCreateUser(), OperationLogType.UPDATE.name(), OperationLogModule.SETTING_ORGANIZATION_PROJECT, - project.getName()); + request.getName()); dto.setOriginalValue(JSON.toJSONBytes(project)); return dto; @@ -84,7 +85,7 @@ public class OrganizationProjectLogService { Project project = projectMapper.selectByPrimaryKey(id); if (project != null) { LogDTO dto = new LogDTO( - project.getId(), + OperationLogConstants.ORGANIZATION, project.getOrganizationId(), project.getId(), project.getCreateUser(), @@ -109,7 +110,7 @@ public class OrganizationProjectLogService { Project project = projectMapper.selectByPrimaryKey(id); if (project != null) { LogDTO dto = new LogDTO( - project.getId(), + OperationLogConstants.ORGANIZATION, project.getOrganizationId(), id, project.getCreateUser(), @@ -132,7 +133,7 @@ public class OrganizationProjectLogService { Project project = projectMapper.selectByPrimaryKey(id); if (project != null) { LogDTO dto = new LogDTO( - project.getId(), + OperationLogConstants.ORGANIZATION, project.getOrganizationId(), id, null, diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/SystemProjectLogService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/SystemProjectLogService.java index 3bef66437a..34cd32ab14 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/SystemProjectLogService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/SystemProjectLogService.java @@ -4,13 +4,13 @@ import io.metersphere.project.domain.Project; import io.metersphere.project.mapper.ProjectMapper; import io.metersphere.sdk.constants.HttpMethodConstants; import io.metersphere.sdk.constants.OperationLogConstants; +import io.metersphere.sdk.util.JSON; import io.metersphere.system.dto.AddProjectRequest; -import io.metersphere.system.log.dto.LogDTO; import io.metersphere.system.dto.UpdateProjectNameRequest; import io.metersphere.system.dto.UpdateProjectRequest; import io.metersphere.system.log.constants.OperationLogModule; import io.metersphere.system.log.constants.OperationLogType; -import io.metersphere.sdk.util.JSON; +import io.metersphere.system.log.dto.LogDTO; import jakarta.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -55,7 +55,7 @@ public class SystemProjectLogService { project.getCreateUser(), OperationLogType.UPDATE.name(), OperationLogModule.SETTING_SYSTEM_ORGANIZATION, - project.getName()); + request.getName()); dto.setOriginalValue(JSON.toJSONBytes(project)); return dto; @@ -73,7 +73,7 @@ public class SystemProjectLogService { project.getCreateUser(), OperationLogType.UPDATE.name(), OperationLogModule.SETTING_SYSTEM_ORGANIZATION, - project.getName()); + request.getName()); dto.setOriginalValue(JSON.toJSONBytes(project)); return dto; diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/OrganizationProjectControllerTests.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/OrganizationProjectControllerTests.java index 29daad50b5..c86dc7e9a9 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/OrganizationProjectControllerTests.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/OrganizationProjectControllerTests.java @@ -989,22 +989,22 @@ public class OrganizationProjectControllerTests extends BaseTest { project.setId("projectId2"); project.setName("org-updateName"); project.setOrganizationId(DEFAULT_ORGANIZATION_ID); - this.requestPost(updateProject, project, ERROR_REQUEST_MATCHER); + this.requestPost(updateName, project, ERROR_REQUEST_MATCHER); //参数组织Id为空 project.setOrganizationId(null); - this.requestPost(updateProject, project, BAD_REQUEST_MATCHER); + this.requestPost(updateName, project, BAD_REQUEST_MATCHER); //项目Id为空 project.setId(null); project.setOrganizationId(DEFAULT_ORGANIZATION_ID); - this.requestPost(updateProject, project, BAD_REQUEST_MATCHER); + this.requestPost(updateName, project, BAD_REQUEST_MATCHER); //项目名称为空 project.setName(null); - this.requestPost(updateProject, project, BAD_REQUEST_MATCHER); + this.requestPost(updateName, project, BAD_REQUEST_MATCHER); //项目不存在 project.setId("1111"); project.setName("updateName"); project.setOrganizationId(DEFAULT_ORGANIZATION_ID); - this.requestPost(updateProject, project, ERROR_REQUEST_MATCHER); + this.requestPost(updateName, project, ERROR_REQUEST_MATCHER); } } diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/SystemProjectControllerTests.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/SystemProjectControllerTests.java index 866b8ff9e8..a1368a35bb 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/SystemProjectControllerTests.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/SystemProjectControllerTests.java @@ -1086,22 +1086,22 @@ public class SystemProjectControllerTests extends BaseTest { project.setId("projectId2"); project.setName("updateName"); project.setOrganizationId(DEFAULT_ORGANIZATION_ID); - this.requestPost(updateProject, project, ERROR_REQUEST_MATCHER); + this.requestPost(updateName, project, ERROR_REQUEST_MATCHER); //参数组织Id为空 project.setOrganizationId(null); - this.requestPost(updateProject, project, BAD_REQUEST_MATCHER); + this.requestPost(updateName, project, BAD_REQUEST_MATCHER); //项目Id为空 project.setId(null); project.setOrganizationId(DEFAULT_ORGANIZATION_ID); - this.requestPost(updateProject, project, BAD_REQUEST_MATCHER); + this.requestPost(updateName, project, BAD_REQUEST_MATCHER); //项目名称为空 project.setName(null); - this.requestPost(updateProject, project, BAD_REQUEST_MATCHER); + this.requestPost(updateName, project, BAD_REQUEST_MATCHER); //项目不存在 project.setId("1111"); project.setName("updateName"); project.setOrganizationId(DEFAULT_ORGANIZATION_ID); - this.requestPost(updateProject, project, ERROR_REQUEST_MATCHER); + this.requestPost(updateName, project, ERROR_REQUEST_MATCHER); } }