diff --git a/framework/sdk-parent/sdk/src/main/java/io/metersphere/log/aspect/MsLogAspect.java b/framework/sdk-parent/sdk/src/main/java/io/metersphere/log/aspect/MsLogAspect.java index 1b152fe30e..9f43ccf9d5 100644 --- a/framework/sdk-parent/sdk/src/main/java/io/metersphere/log/aspect/MsLogAspect.java +++ b/framework/sdk-parent/sdk/src/main/java/io/metersphere/log/aspect/MsLogAspect.java @@ -21,7 +21,7 @@ import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; -import org.springframework.core.LocalVariableTableParameterNameDiscoverer; +import org.springframework.core.StandardReflectionParameterNameDiscoverer; import org.springframework.expression.EvaluationContext; import org.springframework.expression.Expression; import org.springframework.expression.ExpressionParser; @@ -50,7 +50,7 @@ public class MsLogAspect { /** * 将方法参数纳入Spring管理 */ - LocalVariableTableParameterNameDiscoverer discoverer = new LocalVariableTableParameterNameDiscoverer(); + StandardReflectionParameterNameDiscoverer discoverer = new StandardReflectionParameterNameDiscoverer(); @Autowired private ApplicationContext applicationContext; diff --git a/test-track/backend/src/main/java/io/metersphere/controller/AttachmentController.java b/test-track/backend/src/main/java/io/metersphere/controller/AttachmentController.java index d53d981193..8a94333c4c 100644 --- a/test-track/backend/src/main/java/io/metersphere/controller/AttachmentController.java +++ b/test-track/backend/src/main/java/io/metersphere/controller/AttachmentController.java @@ -1,6 +1,9 @@ package io.metersphere.controller; import io.metersphere.base.domain.FileAttachmentMetadata; +import io.metersphere.commons.constants.OperLogConstants; +import io.metersphere.commons.constants.OperLogModule; +import io.metersphere.log.annotation.MsAuditLog; import io.metersphere.metadata.service.FileMetadataService; import io.metersphere.request.attachment.AttachmentDumpRequest; import io.metersphere.xpack.track.dto.AttachmentRequest; @@ -27,6 +30,7 @@ public class AttachmentController { @Resource private FileMetadataService fileMetadataService; + @MsAuditLog(module = OperLogModule.TRACK_TEST_CASE, type = OperLogConstants.UPDATE, title = "#request.belongType", content = "#msClass.getLogDetails(#request.belongId, #request.belongType, #file.getOriginalFilename(), false)", msClass = AttachmentService.class) @PostMapping(value = "/upload", consumes = {"multipart/form-data"}) public void uploadAttachment(@RequestPart("request") AttachmentRequest request, @RequestPart(value = "file", required = false) MultipartFile file) { attachmentService.uploadAttachment(request, file); @@ -57,6 +61,7 @@ public class AttachmentController { } } + @MsAuditLog(module = OperLogModule.TRACK_TEST_CASE, type = OperLogConstants.UPDATE, beforeEvent = "#msClass.getLogDetails(#attachmentId, #attachmentType)", title = "#request.belongType", msClass = AttachmentService.class) @GetMapping("/delete/{attachmentType}/{attachmentId}") public void deleteAttachment(@PathVariable String attachmentId, @PathVariable String attachmentType) { attachmentService.deleteAttachment(attachmentId, attachmentType); diff --git a/test-track/backend/src/main/java/io/metersphere/service/AttachmentService.java b/test-track/backend/src/main/java/io/metersphere/service/AttachmentService.java index 6ec3b86bb9..97d74511be 100644 --- a/test-track/backend/src/main/java/io/metersphere/service/AttachmentService.java +++ b/test-track/backend/src/main/java/io/metersphere/service/AttachmentService.java @@ -7,9 +7,12 @@ import io.metersphere.commons.constants.FileAssociationType; import io.metersphere.commons.exception.MSException; import io.metersphere.commons.utils.BeanUtils; import io.metersphere.commons.utils.FileUtils; +import io.metersphere.commons.utils.JSON; import io.metersphere.commons.utils.SessionUtils; import io.metersphere.constants.AttachmentType; import io.metersphere.i18n.Translator; +import io.metersphere.log.vo.DetailColumn; +import io.metersphere.log.vo.OperatingLogDetails; import io.metersphere.metadata.service.FileMetadataService; import io.metersphere.metadata.utils.MetadataUtils; import io.metersphere.platform.domain.SyncIssuesAttachmentRequest; @@ -578,4 +581,81 @@ public class AttachmentService { return type.toUpperCase(); } + /** + * 上传文件的操作记录 + * + * @param sourceId + * @param type + * @return + */ + public String getLogDetails(String sourceId, String type, String fileName, Boolean isDelete) { + String projectId = null; + String createUser = null; + if (StringUtils.isBlank(sourceId) || StringUtils.isBlank(type) || StringUtils.isBlank(fileName)) { + return null; + } + if (AttachmentType.ISSUE.type().equals(type)) { + IssuesWithBLOBs issues = issuesMapper.selectByPrimaryKey(sourceId); + if (issues == null) { + return null; + } + projectId = issues.getProjectId(); + createUser = issues.getCreator(); + } else if (AttachmentType.TEST_CASE.type().equals(type)) { + TestCaseWithBLOBs testCase = testCaseMapper.selectByPrimaryKey(sourceId); + if (testCase == null) { + return null; + } + projectId = testCase.getProjectId(); + createUser = testCase.getCreateUser(); + } + AttachmentRequest attachmentRequest = new AttachmentRequest(); + attachmentRequest.setBelongId(sourceId); + attachmentRequest.setBelongType(type); + List originFiles = listMetadata(attachmentRequest); + List fileNames = new LinkedList<>(); + if (CollectionUtils.isNotEmpty(originFiles)) { + fileNames = originFiles.stream().map(FileAttachmentMetadata::getName).collect(Collectors.toList()); + } + String after; + String before; + + if (fileNames.contains(fileName)) { + after = String.join(",", fileNames); + fileNames.remove(fileName); + before = String.join(",", fileNames); + } else { + after = String.join(",", fileNames); + fileNames.add(fileName); + before = String.join(",", fileNames); + } + + List columns = new ArrayList<>(); + if (isDelete) { + DetailColumn column = new DetailColumn("附件", "files", after, before); + columns.add(column); + } else { + DetailColumn column = new DetailColumn("附件", "files", before, after); + columns.add(column); + } + OperatingLogDetails details = new OperatingLogDetails(JSON.toJSONString(sourceId), projectId, createUser, columns); + return JSON.toJSONString(details); + } + + public String getLogDetails(String attachmentId, String attachmentType) { + FileAttachmentMetadata fileAttachmentMetadata = fileAttachmentMetadataMapper.selectByPrimaryKey(attachmentId); + if (fileAttachmentMetadata == null) { + return null; + } + String fileName = fileAttachmentMetadata.getName(); + AttachmentModuleRelationExample example = new AttachmentModuleRelationExample(); + example.createCriteria().andAttachmentIdEqualTo(attachmentId).andRelationTypeEqualTo(attachmentType); + List attachmentModuleRelations = attachmentModuleRelationMapper.selectByExample(example); + if (CollectionUtils.isEmpty(attachmentModuleRelations)) { + return null; + } + String relationId = attachmentModuleRelations.get(0).getRelationId(); + return this.getLogDetails(relationId, attachmentType, fileName, true); + } + }