refactor(系统设置): 优化项目记录相关日志

This commit is contained in:
wxg0103 2023-11-08 13:56:52 +08:00 committed by wxg0103
parent 6513e08b30
commit 684e3cf965
6 changed files with 49 additions and 32 deletions

View File

@ -2,11 +2,11 @@ package io.metersphere.project.service;
import io.metersphere.project.domain.Project; import io.metersphere.project.domain.Project;
import io.metersphere.project.mapper.ProjectMapper; 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.sdk.util.JSON;
import io.metersphere.system.dto.UpdateProjectRequest;
import io.metersphere.system.log.constants.OperationLogModule; import io.metersphere.system.log.constants.OperationLogModule;
import io.metersphere.system.log.constants.OperationLogType; import io.metersphere.system.log.constants.OperationLogType;
import io.metersphere.system.log.dto.LogDTO;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -32,7 +32,7 @@ public class ProjectLogService {
project.getCreateUser(), project.getCreateUser(),
OperationLogType.UPDATE.name(), OperationLogType.UPDATE.name(),
OperationLogModule.PROJECT_MANAGEMENT, OperationLogModule.PROJECT_MANAGEMENT,
project.getName()); request.getName());
dto.setOriginalValue(JSON.toJSONBytes(project)); dto.setOriginalValue(JSON.toJSONBytes(project));
return dto; return dto;

View File

@ -313,7 +313,11 @@ public class CommonProjectService {
deleteExample.createCriteria().andSourceIdEqualTo(project.getId()).andUserIdIn(deleteIds).andRoleIdEqualTo(InternalUserRole.PROJECT_ADMIN.getValue()); deleteExample.createCriteria().andSourceIdEqualTo(project.getId()).andUserIdIn(deleteIds).andRoleIdEqualTo(InternalUserRole.PROJECT_ADMIN.getValue());
userRoleRelationMapper.selectByExample(deleteExample).forEach(userRoleRelation -> { userRoleRelationMapper.selectByExample(deleteExample).forEach(userRoleRelation -> {
User user = userMapper.selectByPrimaryKey(userRoleRelation.getUserId()); 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); setLog(logDTO, path, HttpMethodConstants.POST.name(), logDTOList);
}); });
userRoleRelationMapper.deleteByExample(deleteExample); userRoleRelationMapper.deleteByExample(deleteExample);
@ -332,7 +336,7 @@ public class CommonProjectService {
deleteExample.createCriteria().andSourceIdEqualTo(project.getId()).andUserIdIn(orgUserIds).andRoleIdEqualTo(InternalUserRole.PROJECT_ADMIN.getValue()); deleteExample.createCriteria().andSourceIdEqualTo(project.getId()).andUserIdIn(orgUserIds).andRoleIdEqualTo(InternalUserRole.PROJECT_ADMIN.getValue());
userRoleRelationMapper.selectByExample(deleteExample).forEach(userRoleRelation -> { userRoleRelationMapper.selectByExample(deleteExample).forEach(userRoleRelation -> {
User user = userMapper.selectByPrimaryKey(userRoleRelation.getUserId()); 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); setLog(logDTO, path, HttpMethodConstants.POST.name(), logDTOList);
}); });
userRoleRelationMapper.deleteByExample(deleteExample); userRoleRelationMapper.deleteByExample(deleteExample);
@ -406,7 +410,11 @@ public class CommonProjectService {
adminRole.setCreateUser(createUser); adminRole.setCreateUser(createUser);
adminRole.setOrganizationId(project.getOrganizationId()); adminRole.setOrganizationId(project.getOrganizationId());
userRoleRelations.add(adminRole); 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); setLog(logDTO, path, HttpMethodConstants.POST.name(), logDTOList);
} }
}); });
@ -462,7 +470,11 @@ public class CommonProjectService {
memberRole.setCreateUser(createUser); memberRole.setCreateUser(createUser);
memberRole.setOrganizationId(project.getOrganizationId()); memberRole.setOrganizationId(project.getOrganizationId());
userRoleRelations.add(memberRole); 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); setLog(logDTO, path, HttpMethodConstants.POST.name(), logDTOList);
} }
}); });
@ -491,7 +503,11 @@ public class CommonProjectService {
} }
List<LogDTO> logDTOList = new ArrayList<>(); List<LogDTO> logDTOList = new ArrayList<>();
userRoleRelationMapper.selectByExample(userRoleRelationExample).forEach(userRoleRelation -> { 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); setLog(logDTO, path, HttpMethodConstants.GET.name(), logDTOList);
}); });
operationLogService.batchAdd(logDTOList); operationLogService.batchAdd(logDTOList);

View File

@ -3,13 +3,14 @@ package io.metersphere.system.service;
import io.metersphere.project.domain.Project; import io.metersphere.project.domain.Project;
import io.metersphere.project.mapper.ProjectMapper; import io.metersphere.project.mapper.ProjectMapper;
import io.metersphere.sdk.constants.HttpMethodConstants; 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.dto.AddProjectRequest;
import io.metersphere.system.log.dto.LogDTO;
import io.metersphere.system.dto.UpdateProjectNameRequest; import io.metersphere.system.dto.UpdateProjectNameRequest;
import io.metersphere.system.dto.UpdateProjectRequest; import io.metersphere.system.dto.UpdateProjectRequest;
import io.metersphere.system.log.constants.OperationLogModule; import io.metersphere.system.log.constants.OperationLogModule;
import io.metersphere.system.log.constants.OperationLogType; 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 jakarta.annotation.Resource;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -28,7 +29,7 @@ public class OrganizationProjectLogService {
*/ */
public LogDTO addLog(AddProjectRequest project) { public LogDTO addLog(AddProjectRequest project) {
LogDTO dto = new LogDTO( LogDTO dto = new LogDTO(
project.getId(), OperationLogConstants.ORGANIZATION,
project.getOrganizationId(), project.getOrganizationId(),
null, null,
null, null,
@ -48,13 +49,13 @@ public class OrganizationProjectLogService {
Project project = projectMapper.selectByPrimaryKey(request.getId()); Project project = projectMapper.selectByPrimaryKey(request.getId());
if (project != null) { if (project != null) {
LogDTO dto = new LogDTO( LogDTO dto = new LogDTO(
project.getId(), OperationLogConstants.ORGANIZATION,
project.getOrganizationId(), project.getOrganizationId(),
project.getId(), project.getId(),
project.getCreateUser(), project.getCreateUser(),
OperationLogType.UPDATE.name(), OperationLogType.UPDATE.name(),
OperationLogModule.SETTING_ORGANIZATION_PROJECT, OperationLogModule.SETTING_ORGANIZATION_PROJECT,
project.getName()); request.getName());
dto.setOriginalValue(JSON.toJSONBytes(project)); dto.setOriginalValue(JSON.toJSONBytes(project));
return dto; return dto;
@ -66,13 +67,13 @@ public class OrganizationProjectLogService {
Project project = projectMapper.selectByPrimaryKey(request.getId()); Project project = projectMapper.selectByPrimaryKey(request.getId());
if (project != null) { if (project != null) {
LogDTO dto = new LogDTO( LogDTO dto = new LogDTO(
project.getId(), OperationLogConstants.ORGANIZATION,
project.getOrganizationId(), project.getOrganizationId(),
project.getId(), project.getId(),
project.getCreateUser(), project.getCreateUser(),
OperationLogType.UPDATE.name(), OperationLogType.UPDATE.name(),
OperationLogModule.SETTING_ORGANIZATION_PROJECT, OperationLogModule.SETTING_ORGANIZATION_PROJECT,
project.getName()); request.getName());
dto.setOriginalValue(JSON.toJSONBytes(project)); dto.setOriginalValue(JSON.toJSONBytes(project));
return dto; return dto;
@ -84,7 +85,7 @@ public class OrganizationProjectLogService {
Project project = projectMapper.selectByPrimaryKey(id); Project project = projectMapper.selectByPrimaryKey(id);
if (project != null) { if (project != null) {
LogDTO dto = new LogDTO( LogDTO dto = new LogDTO(
project.getId(), OperationLogConstants.ORGANIZATION,
project.getOrganizationId(), project.getOrganizationId(),
project.getId(), project.getId(),
project.getCreateUser(), project.getCreateUser(),
@ -109,7 +110,7 @@ public class OrganizationProjectLogService {
Project project = projectMapper.selectByPrimaryKey(id); Project project = projectMapper.selectByPrimaryKey(id);
if (project != null) { if (project != null) {
LogDTO dto = new LogDTO( LogDTO dto = new LogDTO(
project.getId(), OperationLogConstants.ORGANIZATION,
project.getOrganizationId(), project.getOrganizationId(),
id, id,
project.getCreateUser(), project.getCreateUser(),
@ -132,7 +133,7 @@ public class OrganizationProjectLogService {
Project project = projectMapper.selectByPrimaryKey(id); Project project = projectMapper.selectByPrimaryKey(id);
if (project != null) { if (project != null) {
LogDTO dto = new LogDTO( LogDTO dto = new LogDTO(
project.getId(), OperationLogConstants.ORGANIZATION,
project.getOrganizationId(), project.getOrganizationId(),
id, id,
null, null,

View File

@ -4,13 +4,13 @@ import io.metersphere.project.domain.Project;
import io.metersphere.project.mapper.ProjectMapper; import io.metersphere.project.mapper.ProjectMapper;
import io.metersphere.sdk.constants.HttpMethodConstants; import io.metersphere.sdk.constants.HttpMethodConstants;
import io.metersphere.sdk.constants.OperationLogConstants; import io.metersphere.sdk.constants.OperationLogConstants;
import io.metersphere.sdk.util.JSON;
import io.metersphere.system.dto.AddProjectRequest; import io.metersphere.system.dto.AddProjectRequest;
import io.metersphere.system.log.dto.LogDTO;
import io.metersphere.system.dto.UpdateProjectNameRequest; import io.metersphere.system.dto.UpdateProjectNameRequest;
import io.metersphere.system.dto.UpdateProjectRequest; import io.metersphere.system.dto.UpdateProjectRequest;
import io.metersphere.system.log.constants.OperationLogModule; import io.metersphere.system.log.constants.OperationLogModule;
import io.metersphere.system.log.constants.OperationLogType; 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 jakarta.annotation.Resource;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -55,7 +55,7 @@ public class SystemProjectLogService {
project.getCreateUser(), project.getCreateUser(),
OperationLogType.UPDATE.name(), OperationLogType.UPDATE.name(),
OperationLogModule.SETTING_SYSTEM_ORGANIZATION, OperationLogModule.SETTING_SYSTEM_ORGANIZATION,
project.getName()); request.getName());
dto.setOriginalValue(JSON.toJSONBytes(project)); dto.setOriginalValue(JSON.toJSONBytes(project));
return dto; return dto;
@ -73,7 +73,7 @@ public class SystemProjectLogService {
project.getCreateUser(), project.getCreateUser(),
OperationLogType.UPDATE.name(), OperationLogType.UPDATE.name(),
OperationLogModule.SETTING_SYSTEM_ORGANIZATION, OperationLogModule.SETTING_SYSTEM_ORGANIZATION,
project.getName()); request.getName());
dto.setOriginalValue(JSON.toJSONBytes(project)); dto.setOriginalValue(JSON.toJSONBytes(project));
return dto; return dto;

View File

@ -989,22 +989,22 @@ public class OrganizationProjectControllerTests extends BaseTest {
project.setId("projectId2"); project.setId("projectId2");
project.setName("org-updateName"); project.setName("org-updateName");
project.setOrganizationId(DEFAULT_ORGANIZATION_ID); project.setOrganizationId(DEFAULT_ORGANIZATION_ID);
this.requestPost(updateProject, project, ERROR_REQUEST_MATCHER); this.requestPost(updateName, project, ERROR_REQUEST_MATCHER);
//参数组织Id为空 //参数组织Id为空
project.setOrganizationId(null); project.setOrganizationId(null);
this.requestPost(updateProject, project, BAD_REQUEST_MATCHER); this.requestPost(updateName, project, BAD_REQUEST_MATCHER);
//项目Id为空 //项目Id为空
project.setId(null); project.setId(null);
project.setOrganizationId(DEFAULT_ORGANIZATION_ID); project.setOrganizationId(DEFAULT_ORGANIZATION_ID);
this.requestPost(updateProject, project, BAD_REQUEST_MATCHER); this.requestPost(updateName, project, BAD_REQUEST_MATCHER);
//项目名称为空 //项目名称为空
project.setName(null); project.setName(null);
this.requestPost(updateProject, project, BAD_REQUEST_MATCHER); this.requestPost(updateName, project, BAD_REQUEST_MATCHER);
//项目不存在 //项目不存在
project.setId("1111"); project.setId("1111");
project.setName("updateName"); project.setName("updateName");
project.setOrganizationId(DEFAULT_ORGANIZATION_ID); project.setOrganizationId(DEFAULT_ORGANIZATION_ID);
this.requestPost(updateProject, project, ERROR_REQUEST_MATCHER); this.requestPost(updateName, project, ERROR_REQUEST_MATCHER);
} }
} }

View File

@ -1086,22 +1086,22 @@ public class SystemProjectControllerTests extends BaseTest {
project.setId("projectId2"); project.setId("projectId2");
project.setName("updateName"); project.setName("updateName");
project.setOrganizationId(DEFAULT_ORGANIZATION_ID); project.setOrganizationId(DEFAULT_ORGANIZATION_ID);
this.requestPost(updateProject, project, ERROR_REQUEST_MATCHER); this.requestPost(updateName, project, ERROR_REQUEST_MATCHER);
//参数组织Id为空 //参数组织Id为空
project.setOrganizationId(null); project.setOrganizationId(null);
this.requestPost(updateProject, project, BAD_REQUEST_MATCHER); this.requestPost(updateName, project, BAD_REQUEST_MATCHER);
//项目Id为空 //项目Id为空
project.setId(null); project.setId(null);
project.setOrganizationId(DEFAULT_ORGANIZATION_ID); project.setOrganizationId(DEFAULT_ORGANIZATION_ID);
this.requestPost(updateProject, project, BAD_REQUEST_MATCHER); this.requestPost(updateName, project, BAD_REQUEST_MATCHER);
//项目名称为空 //项目名称为空
project.setName(null); project.setName(null);
this.requestPost(updateProject, project, BAD_REQUEST_MATCHER); this.requestPost(updateName, project, BAD_REQUEST_MATCHER);
//项目不存在 //项目不存在
project.setId("1111"); project.setId("1111");
project.setName("updateName"); project.setName("updateName");
project.setOrganizationId(DEFAULT_ORGANIZATION_ID); project.setOrganizationId(DEFAULT_ORGANIZATION_ID);
this.requestPost(updateProject, project, ERROR_REQUEST_MATCHER); this.requestPost(updateName, project, ERROR_REQUEST_MATCHER);
} }
} }