refactor(系统设置): 补充项目操作日志

This commit is contained in:
wxg0103 2023-07-24 18:04:30 +08:00 committed by fit2-zhao
parent 4ebc36d5a9
commit d2842126a3
3 changed files with 61 additions and 7 deletions

View File

@ -405,4 +405,11 @@ http_result_validate=参数校验失败
http_result_unauthorized=用户认证失败
http_result_forbidden=权限认证失败
enum_value_valid_message=枚举值不合法,必须为
enum_value_valid_message=枚举值不合法,必须为
project_admin=添加项目管理员
project_member=添加项目成员
project=项目
add=添加
delete=删除
update=更新

View File

@ -3,6 +3,7 @@ package io.metersphere.system.controller;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.metersphere.constants.HttpMethodConstants;
import io.metersphere.project.domain.Project;
import io.metersphere.sdk.constants.PermissionConstants;
import io.metersphere.sdk.dto.AddProjectRequest;
@ -13,6 +14,7 @@ import io.metersphere.sdk.log.constants.OperationLogType;
import io.metersphere.sdk.util.PageUtils;
import io.metersphere.sdk.util.Pager;
import io.metersphere.sdk.util.SessionUtils;
import io.metersphere.sdk.util.Translator;
import io.metersphere.system.dto.UserExtend;
import io.metersphere.system.request.ProjectAddMemberRequest;
import io.metersphere.system.request.ProjectMemberRequest;
@ -105,7 +107,8 @@ public class SystemProjectController {
@RequiresPermissions(PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ_UPDATE)
@Operation(summary = "添加项目成员")
public void addProjectMember(@Validated @RequestBody ProjectAddMemberRequest request) {
systemProjectService.addProjectMember(request, SessionUtils.getUserId(), false);
systemProjectService.addProjectMember(request, SessionUtils.getUserId(), false, "/system/project/add-member",
OperationLogType.ADD.name(), HttpMethodConstants.POST.name(), Translator.get("add"));
}
@GetMapping("/remove-member/{projectId}/{userId}")

View File

@ -1,20 +1,26 @@
package io.metersphere.system.service;
import io.metersphere.constants.HttpMethodConstants;
import io.metersphere.project.domain.Project;
import io.metersphere.project.domain.ProjectExample;
import io.metersphere.project.mapper.ProjectMapper;
import io.metersphere.sdk.constants.InternalUserRole;
import io.metersphere.sdk.dto.AddProjectRequest;
import io.metersphere.sdk.dto.LogDTO;
import io.metersphere.sdk.dto.ProjectDTO;
import io.metersphere.sdk.dto.UpdateProjectRequest;
import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.invoker.ProjectServiceInvoker;
import io.metersphere.sdk.log.constants.OperationLogModule;
import io.metersphere.sdk.log.constants.OperationLogType;
import io.metersphere.sdk.log.service.OperationLogService;
import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.util.Translator;
import io.metersphere.system.domain.User;
import io.metersphere.system.domain.UserRoleRelation;
import io.metersphere.system.domain.UserRoleRelationExample;
import io.metersphere.system.dto.OrganizationProjectOptionsDto;
import io.metersphere.system.dto.UserExtend;
import io.metersphere.sdk.invoker.ProjectServiceInvoker;
import io.metersphere.system.mapper.ExtSystemProjectMapper;
import io.metersphere.system.mapper.UserMapper;
import io.metersphere.system.mapper.UserRoleRelationMapper;
@ -30,6 +36,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
@ -46,9 +53,16 @@ public class SystemProjectService {
private UserRoleRelationMapper userRoleRelationMapper;
@Resource
private ExtSystemProjectMapper extSystemProjectMapper;
@Resource
private OperationLogService operationLogService;
private final ProjectServiceInvoker serviceInvoker;
private final static String prefix = "/system/project";
private final static String addProject = prefix + "/add";
private final static String updateProject = prefix + "/update";
private final static String removeProjectMember = prefix + "/remove-member/";
@Autowired
public SystemProjectService(ProjectServiceInvoker serviceInvoker) {
this.serviceInvoker = serviceInvoker;
@ -82,7 +96,7 @@ public class SystemProjectService {
ProjectAddMemberRequest memberRequest = new ProjectAddMemberRequest();
memberRequest.setProjectId(project.getId());
memberRequest.setUserIds(addProjectDTO.getUserIds());
this.addProjectMember(memberRequest, createUser, true);
this.addProjectMember(memberRequest, createUser, true, addProject, OperationLogType.ADD.name(), HttpMethodConstants.POST.name(), Translator.get("add"));
return project;
}
@ -143,7 +157,8 @@ public class SystemProjectService {
ProjectAddMemberRequest memberRequest = new ProjectAddMemberRequest();
memberRequest.setProjectId(project.getId());
memberRequest.setUserIds(insertIds);
this.addProjectMember(memberRequest, updateUser, true);
this.addProjectMember(memberRequest, updateUser, true, updateProject, OperationLogType.UPDATE.name(),
HttpMethodConstants.POST.name(), Translator.get("update"));
}
projectMapper.updateByPrimaryKeySelective(project);
@ -165,7 +180,9 @@ public class SystemProjectService {
return projectMemberList;
}
public void addProjectMember(ProjectAddMemberRequest request, String createUser, boolean isAdmin) {
public void addProjectMember(ProjectAddMemberRequest request, String createUser, boolean isAdmin, String path, String type,
String method, String content) {
List<LogDTO> logDTOList = new ArrayList<>();
//TODO 添加项目成员需要检查配额 这个需要等后续定下来补全逻辑
request.getUserIds().forEach(userId -> {
User user = userMapper.selectByPrimaryKey(userId);
@ -181,6 +198,7 @@ public class SystemProjectService {
System.currentTimeMillis(),
createUser);
userRoleRelationMapper.insertSelective(adminRole);
setLog(request.getProjectId(), path, content + Translator.get("project_admin") +": "+ user.getName(), createUser, "", type, method, logDTOList);
}
UserRoleRelationExample userRoleRelationExample = new UserRoleRelationExample();
userRoleRelationExample.createCriteria().andUserIdEqualTo(userId)
@ -194,8 +212,10 @@ public class SystemProjectService {
System.currentTimeMillis(),
createUser);
userRoleRelationMapper.insertSelective(memberRole);
setLog(request.getProjectId(), path, content + Translator.get("project_member")+": "+ user.getName(), createUser, "", type, method, logDTOList);
}
});
operationLogService.batchAdd(logDTOList);
}
@ -208,7 +228,11 @@ public class SystemProjectService {
user.setLastProjectId(StringUtils.EMPTY);
userMapper.updateByPrimaryKeySelective(user);
}
//写入操作日志
List<LogDTO> logDTOList = new ArrayList<>();
setLog(projectId, removeProjectMember + "/" + projectId + "/" +userId,
Translator.get("delete") + Translator.get("project_member") +": "+ user.getName(),
userId, "", OperationLogType.DELETE.name() , HttpMethodConstants.GET.name(), logDTOList);
operationLogService.batchAdd(logDTOList);
return userRoleRelationMapper.deleteByExample(userRoleRelationExample);
}
@ -223,6 +247,7 @@ public class SystemProjectService {
public void deleteProject(List<Project> projects) {
// 删除项目
List<LogDTO> logDTOList = new ArrayList<>();
projects.forEach(project -> {
serviceInvoker.invokeServices(project.getId());
LoggerUtil.info("send delete_project message, project id: " + project.getId());
@ -230,7 +255,10 @@ public class SystemProjectService {
deleteProjectUserGroup(project.getId());
// delete project
projectMapper.deleteByPrimaryKey(project.getId());
setLog(project.getId(), "null", Translator.get("delete") + Translator.get("project") + ": " + project.getName(),
"system", "", OperationLogType.DELETE.name(), "", logDTOList);
});
operationLogService.batchAdd(logDTOList);
}
private void deleteProjectUserGroup(String projectId) {
@ -242,4 +270,20 @@ public class SystemProjectService {
public List<OrganizationProjectOptionsDto> getProjectOptions() {
return extSystemProjectMapper.selectProjectOptions();
}
private static void setLog( String projectId, String path, String content, String userId, Object originalValue,
String type, String method, List<LogDTO> logDTOList) {
LogDTO dto = new LogDTO(
"system",
"system",
projectId,
userId,
type,
OperationLogModule.SYSTEM_PROJECT,
content);
dto.setPath(path);
dto.setMethod(method);
dto.setOriginalValue(JSON.toJSONBytes(originalValue));
logDTOList.add(dto);
}
}