From 56f2b63eb6933c64750c4c2b0fb5cf5d8fa03681 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Mon, 10 Jul 2023 17:28:29 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=B7=BB=E5=8A=A0=E5=8F=98?= =?UTF-8?q?=E6=9B=B4=E5=8E=86=E5=8F=B2=E6=BA=90=E6=95=B0=E6=8D=AE=E6=94=B6?= =?UTF-8?q?=E9=9B=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{system => sdk}/domain/OperationLog.java | 6 +- .../sdk/domain/OperationLogBlob.java | 26 ++ .../sdk/domain/OperationLogBlobExample.java | 270 ++++++++++++++++++ .../domain/OperationLogExample.java | 58 ++-- .../sdk/mapper/OperationLogBlobMapper.java | 35 +++ .../sdk/mapper/OperationLogBlobMapper.xml | 211 ++++++++++++++ .../mapper/OperationLogMapper.java | 6 +- .../mapper/OperationLogMapper.xml | 46 +-- .../migration/3.0.0/ddl/V3.0.0_2__sdk_ddl.sql | 26 ++ backend/framework/plugin/pom.xml | 5 + .../plugin/api/dto/TestElementDTO.java | 49 ++++ .../config/interceptor/SdkInterceptor.java | 23 ++ .../java/io/metersphere/sdk/dto/LogDTO.java | 29 ++ .../metersphere/sdk/log/annotation/Log.java | 47 +-- .../sdk/log/aspect/OperationLogAspect.java | 250 +++++++--------- .../sdk/log/constants/OperationLogType.java | 6 +- .../sdk/log/service/OperationLogService.java | 40 ++- .../sdk/service/SystemParameterService.java | 39 +++ .../sdk/service/TestResourcePoolService.java | 72 ++++- .../java/io/metersphere/sdk/util/JSON.java | 8 + .../sdk/src/test/java/base/BaseTest.java | 4 +- .../controller/ApiDefinitionController.java | 45 +-- .../api/service/ApiDefinitionLogService.java | 148 ++++++++++ .../api/service/ApiDefinitionService.java | 33 --- .../ApiDefinitionControllerTests.java | 113 +++----- .../controller/AuthSourceController.java | 14 +- .../controller/GlobalUserRoleController.java | 13 +- .../GlobalUserRoleRelationController.java | 7 +- .../controller/SystemParameterController.java | 5 +- .../controller/SystemProjectController.java | 16 +- .../TestResourcePoolController.java | 13 +- .../system/controller/UserController.java | 7 +- .../system/service/AuthSourceLogService.java | 115 ++++++++ .../service/GlobalUserRoleLogService.java | 120 ++++++++ .../GlobalUserRoleRelationLogService.java | 73 +++++ .../service/SystemProjectLogService.java | 112 ++++++++ .../system/service/SystemProjectService.java | 76 ----- .../service/UserRoleRelationService.java | 19 +- .../system/service/UserService.java | 75 +++-- 39 files changed, 1717 insertions(+), 543 deletions(-) rename backend/framework/domain/src/main/java/io/metersphere/{system => sdk}/domain/OperationLog.java (95%) create mode 100644 backend/framework/domain/src/main/java/io/metersphere/sdk/domain/OperationLogBlob.java create mode 100644 backend/framework/domain/src/main/java/io/metersphere/sdk/domain/OperationLogBlobExample.java rename backend/framework/domain/src/main/java/io/metersphere/{system => sdk}/domain/OperationLogExample.java (93%) create mode 100644 backend/framework/domain/src/main/java/io/metersphere/sdk/mapper/OperationLogBlobMapper.java create mode 100644 backend/framework/domain/src/main/java/io/metersphere/sdk/mapper/OperationLogBlobMapper.xml rename backend/framework/domain/src/main/java/io/metersphere/{system => sdk}/mapper/OperationLogMapper.java (84%) rename backend/framework/domain/src/main/java/io/metersphere/{system => sdk}/mapper/OperationLogMapper.xml (87%) create mode 100644 backend/framework/sdk/src/main/java/io/metersphere/sdk/config/interceptor/SdkInterceptor.java create mode 100644 backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/LogDTO.java create mode 100644 backend/services/api-test/src/main/java/io/metersphere/api/service/ApiDefinitionLogService.java create mode 100644 backend/services/system-setting/src/main/java/io/metersphere/system/service/AuthSourceLogService.java create mode 100644 backend/services/system-setting/src/main/java/io/metersphere/system/service/GlobalUserRoleLogService.java create mode 100644 backend/services/system-setting/src/main/java/io/metersphere/system/service/GlobalUserRoleRelationLogService.java create mode 100644 backend/services/system-setting/src/main/java/io/metersphere/system/service/SystemProjectLogService.java diff --git a/backend/framework/domain/src/main/java/io/metersphere/system/domain/OperationLog.java b/backend/framework/domain/src/main/java/io/metersphere/sdk/domain/OperationLog.java similarity index 95% rename from backend/framework/domain/src/main/java/io/metersphere/system/domain/OperationLog.java rename to backend/framework/domain/src/main/java/io/metersphere/sdk/domain/OperationLog.java index 1830516b5c..1b01147b14 100644 --- a/backend/framework/domain/src/main/java/io/metersphere/system/domain/OperationLog.java +++ b/backend/framework/domain/src/main/java/io/metersphere/sdk/domain/OperationLog.java @@ -1,4 +1,4 @@ -package io.metersphere.system.domain; +package io.metersphere.sdk.domain; import io.metersphere.validation.groups.Created; import io.metersphere.validation.groups.Updated; @@ -43,8 +43,8 @@ public class OperationLog implements Serializable { @Schema(title = "操作模块/api/case/scenario/ui") private String module; - @Schema(title = "操作详情") - private String details; + @Schema(title = "操作内容") + private String content; @Schema(title = "操作路径") private String path; diff --git a/backend/framework/domain/src/main/java/io/metersphere/sdk/domain/OperationLogBlob.java b/backend/framework/domain/src/main/java/io/metersphere/sdk/domain/OperationLogBlob.java new file mode 100644 index 0000000000..a7e57b2033 --- /dev/null +++ b/backend/framework/domain/src/main/java/io/metersphere/sdk/domain/OperationLogBlob.java @@ -0,0 +1,26 @@ +package io.metersphere.sdk.domain; + +import io.metersphere.validation.groups.Created; +import io.metersphere.validation.groups.Updated; +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.Size; +import lombok.Data; + +import java.io.Serializable; + +@Data +public class OperationLogBlob implements Serializable { + @Schema(title = "主键", requiredMode = Schema.RequiredMode.REQUIRED) + @NotBlank(message = "{operation_log_blob.id.not_blank}", groups = {Updated.class}) + @Size(min = 1, max = 50, message = "{operation_log_blob.id.length_range}", groups = {Created.class, Updated.class}) + private String id; + + @Schema(title = "变更前内容") + private byte[] originalValue; + + @Schema(title = "变更后内容") + private byte[] modifiedValue; + + private static final long serialVersionUID = 1L; +} \ No newline at end of file diff --git a/backend/framework/domain/src/main/java/io/metersphere/sdk/domain/OperationLogBlobExample.java b/backend/framework/domain/src/main/java/io/metersphere/sdk/domain/OperationLogBlobExample.java new file mode 100644 index 0000000000..3cb2284d6e --- /dev/null +++ b/backend/framework/domain/src/main/java/io/metersphere/sdk/domain/OperationLogBlobExample.java @@ -0,0 +1,270 @@ +package io.metersphere.sdk.domain; + +import java.util.ArrayList; +import java.util.List; + +public class OperationLogBlobExample { + protected String orderByClause; + + protected boolean distinct; + + protected List oredCriteria; + + public OperationLogBlobExample() { + oredCriteria = new ArrayList(); + } + + public void setOrderByClause(String orderByClause) { + this.orderByClause = orderByClause; + } + + public String getOrderByClause() { + return orderByClause; + } + + public void setDistinct(boolean distinct) { + this.distinct = distinct; + } + + public boolean isDistinct() { + return distinct; + } + + public List getOredCriteria() { + return oredCriteria; + } + + public void or(Criteria criteria) { + oredCriteria.add(criteria); + } + + public Criteria or() { + Criteria criteria = createCriteriaInternal(); + oredCriteria.add(criteria); + return criteria; + } + + public Criteria createCriteria() { + Criteria criteria = createCriteriaInternal(); + if (oredCriteria.size() == 0) { + oredCriteria.add(criteria); + } + return criteria; + } + + protected Criteria createCriteriaInternal() { + Criteria criteria = new Criteria(); + return criteria; + } + + public void clear() { + oredCriteria.clear(); + orderByClause = null; + distinct = false; + } + + protected abstract static class GeneratedCriteria { + protected List criteria; + + protected GeneratedCriteria() { + super(); + criteria = new ArrayList(); + } + + public boolean isValid() { + return criteria.size() > 0; + } + + public List getAllCriteria() { + return criteria; + } + + public List getCriteria() { + return criteria; + } + + protected void addCriterion(String condition) { + if (condition == null) { + throw new RuntimeException("Value for condition cannot be null"); + } + criteria.add(new Criterion(condition)); + } + + protected void addCriterion(String condition, Object value, String property) { + if (value == null) { + throw new RuntimeException("Value for " + property + " cannot be null"); + } + criteria.add(new Criterion(condition, value)); + } + + protected void addCriterion(String condition, Object value1, Object value2, String property) { + if (value1 == null || value2 == null) { + throw new RuntimeException("Between values for " + property + " cannot be null"); + } + criteria.add(new Criterion(condition, value1, value2)); + } + + public Criteria andIdIsNull() { + addCriterion("id is null"); + return (Criteria) this; + } + + public Criteria andIdIsNotNull() { + addCriterion("id is not null"); + return (Criteria) this; + } + + public Criteria andIdEqualTo(String value) { + addCriterion("id =", value, "id"); + return (Criteria) this; + } + + public Criteria andIdNotEqualTo(String value) { + addCriterion("id <>", value, "id"); + return (Criteria) this; + } + + public Criteria andIdGreaterThan(String value) { + addCriterion("id >", value, "id"); + return (Criteria) this; + } + + public Criteria andIdGreaterThanOrEqualTo(String value) { + addCriterion("id >=", value, "id"); + return (Criteria) this; + } + + public Criteria andIdLessThan(String value) { + addCriterion("id <", value, "id"); + return (Criteria) this; + } + + public Criteria andIdLessThanOrEqualTo(String value) { + addCriterion("id <=", value, "id"); + return (Criteria) this; + } + + public Criteria andIdLike(String value) { + addCriterion("id like", value, "id"); + return (Criteria) this; + } + + public Criteria andIdNotLike(String value) { + addCriterion("id not like", value, "id"); + return (Criteria) this; + } + + public Criteria andIdIn(List values) { + addCriterion("id in", values, "id"); + return (Criteria) this; + } + + public Criteria andIdNotIn(List values) { + addCriterion("id not in", values, "id"); + return (Criteria) this; + } + + public Criteria andIdBetween(String value1, String value2) { + addCriterion("id between", value1, value2, "id"); + return (Criteria) this; + } + + public Criteria andIdNotBetween(String value1, String value2) { + addCriterion("id not between", value1, value2, "id"); + return (Criteria) this; + } + } + + public static class Criteria extends GeneratedCriteria { + + protected Criteria() { + super(); + } + } + + public static class Criterion { + private String condition; + + private Object value; + + private Object secondValue; + + private boolean noValue; + + private boolean singleValue; + + private boolean betweenValue; + + private boolean listValue; + + private String typeHandler; + + public String getCondition() { + return condition; + } + + public Object getValue() { + return value; + } + + public Object getSecondValue() { + return secondValue; + } + + public boolean isNoValue() { + return noValue; + } + + public boolean isSingleValue() { + return singleValue; + } + + public boolean isBetweenValue() { + return betweenValue; + } + + public boolean isListValue() { + return listValue; + } + + public String getTypeHandler() { + return typeHandler; + } + + protected Criterion(String condition) { + super(); + this.condition = condition; + this.typeHandler = null; + this.noValue = true; + } + + protected Criterion(String condition, Object value, String typeHandler) { + super(); + this.condition = condition; + this.value = value; + this.typeHandler = typeHandler; + if (value instanceof List) { + this.listValue = true; + } else { + this.singleValue = true; + } + } + + protected Criterion(String condition, Object value) { + this(condition, value, null); + } + + protected Criterion(String condition, Object value, Object secondValue, String typeHandler) { + super(); + this.condition = condition; + this.value = value; + this.secondValue = secondValue; + this.typeHandler = typeHandler; + this.betweenValue = true; + } + + protected Criterion(String condition, Object value, Object secondValue) { + this(condition, value, secondValue, null); + } + } +} \ No newline at end of file diff --git a/backend/framework/domain/src/main/java/io/metersphere/system/domain/OperationLogExample.java b/backend/framework/domain/src/main/java/io/metersphere/sdk/domain/OperationLogExample.java similarity index 93% rename from backend/framework/domain/src/main/java/io/metersphere/system/domain/OperationLogExample.java rename to backend/framework/domain/src/main/java/io/metersphere/sdk/domain/OperationLogExample.java index a113007a82..52d819cd16 100644 --- a/backend/framework/domain/src/main/java/io/metersphere/system/domain/OperationLogExample.java +++ b/backend/framework/domain/src/main/java/io/metersphere/sdk/domain/OperationLogExample.java @@ -1,4 +1,4 @@ -package io.metersphere.system.domain; +package io.metersphere.sdk.domain; import java.util.ArrayList; import java.util.List; @@ -654,73 +654,73 @@ public class OperationLogExample { return (Criteria) this; } - public Criteria andDetailsIsNull() { - addCriterion("details is null"); + public Criteria andContentIsNull() { + addCriterion("content is null"); return (Criteria) this; } - public Criteria andDetailsIsNotNull() { - addCriterion("details is not null"); + public Criteria andContentIsNotNull() { + addCriterion("content is not null"); return (Criteria) this; } - public Criteria andDetailsEqualTo(String value) { - addCriterion("details =", value, "details"); + public Criteria andContentEqualTo(String value) { + addCriterion("content =", value, "content"); return (Criteria) this; } - public Criteria andDetailsNotEqualTo(String value) { - addCriterion("details <>", value, "details"); + public Criteria andContentNotEqualTo(String value) { + addCriterion("content <>", value, "content"); return (Criteria) this; } - public Criteria andDetailsGreaterThan(String value) { - addCriterion("details >", value, "details"); + public Criteria andContentGreaterThan(String value) { + addCriterion("content >", value, "content"); return (Criteria) this; } - public Criteria andDetailsGreaterThanOrEqualTo(String value) { - addCriterion("details >=", value, "details"); + public Criteria andContentGreaterThanOrEqualTo(String value) { + addCriterion("content >=", value, "content"); return (Criteria) this; } - public Criteria andDetailsLessThan(String value) { - addCriterion("details <", value, "details"); + public Criteria andContentLessThan(String value) { + addCriterion("content <", value, "content"); return (Criteria) this; } - public Criteria andDetailsLessThanOrEqualTo(String value) { - addCriterion("details <=", value, "details"); + public Criteria andContentLessThanOrEqualTo(String value) { + addCriterion("content <=", value, "content"); return (Criteria) this; } - public Criteria andDetailsLike(String value) { - addCriterion("details like", value, "details"); + public Criteria andContentLike(String value) { + addCriterion("content like", value, "content"); return (Criteria) this; } - public Criteria andDetailsNotLike(String value) { - addCriterion("details not like", value, "details"); + public Criteria andContentNotLike(String value) { + addCriterion("content not like", value, "content"); return (Criteria) this; } - public Criteria andDetailsIn(List values) { - addCriterion("details in", values, "details"); + public Criteria andContentIn(List values) { + addCriterion("content in", values, "content"); return (Criteria) this; } - public Criteria andDetailsNotIn(List values) { - addCriterion("details not in", values, "details"); + public Criteria andContentNotIn(List values) { + addCriterion("content not in", values, "content"); return (Criteria) this; } - public Criteria andDetailsBetween(String value1, String value2) { - addCriterion("details between", value1, value2, "details"); + public Criteria andContentBetween(String value1, String value2) { + addCriterion("content between", value1, value2, "content"); return (Criteria) this; } - public Criteria andDetailsNotBetween(String value1, String value2) { - addCriterion("details not between", value1, value2, "details"); + public Criteria andContentNotBetween(String value1, String value2) { + addCriterion("content not between", value1, value2, "content"); return (Criteria) this; } diff --git a/backend/framework/domain/src/main/java/io/metersphere/sdk/mapper/OperationLogBlobMapper.java b/backend/framework/domain/src/main/java/io/metersphere/sdk/mapper/OperationLogBlobMapper.java new file mode 100644 index 0000000000..c25e10d684 --- /dev/null +++ b/backend/framework/domain/src/main/java/io/metersphere/sdk/mapper/OperationLogBlobMapper.java @@ -0,0 +1,35 @@ +package io.metersphere.sdk.mapper; + +import io.metersphere.sdk.domain.OperationLogBlob; +import io.metersphere.sdk.domain.OperationLogBlobExample; +import org.apache.ibatis.annotations.Param; + +import java.util.List; + +public interface OperationLogBlobMapper { + long countByExample(OperationLogBlobExample example); + + int deleteByExample(OperationLogBlobExample example); + + int deleteByPrimaryKey(String id); + + int insert(OperationLogBlob record); + + int insertSelective(OperationLogBlob record); + + List selectByExampleWithBLOBs(OperationLogBlobExample example); + + List selectByExample(OperationLogBlobExample example); + + OperationLogBlob selectByPrimaryKey(String id); + + int updateByExampleSelective(@Param("record") OperationLogBlob record, @Param("example") OperationLogBlobExample example); + + int updateByExampleWithBLOBs(@Param("record") OperationLogBlob record, @Param("example") OperationLogBlobExample example); + + int updateByExample(@Param("record") OperationLogBlob record, @Param("example") OperationLogBlobExample example); + + int updateByPrimaryKeySelective(OperationLogBlob record); + + int updateByPrimaryKeyWithBLOBs(OperationLogBlob record); +} \ No newline at end of file diff --git a/backend/framework/domain/src/main/java/io/metersphere/sdk/mapper/OperationLogBlobMapper.xml b/backend/framework/domain/src/main/java/io/metersphere/sdk/mapper/OperationLogBlobMapper.xml new file mode 100644 index 0000000000..7b077a2373 --- /dev/null +++ b/backend/framework/domain/src/main/java/io/metersphere/sdk/mapper/OperationLogBlobMapper.xml @@ -0,0 +1,211 @@ + + + + + + + + + + + + + + + + + + + and ${criterion.condition} + + + and ${criterion.condition} #{criterion.value} + + + and ${criterion.condition} #{criterion.value} and #{criterion.secondValue} + + + and ${criterion.condition} + + #{listItem} + + + + + + + + + + + + + + + + + + and ${criterion.condition} + + + and ${criterion.condition} #{criterion.value} + + + and ${criterion.condition} #{criterion.value} and #{criterion.secondValue} + + + and ${criterion.condition} + + #{listItem} + + + + + + + + + + + id + + + original_value, modified_value + + + + + + delete from operation_log_blob + where id = #{id,jdbcType=VARCHAR} + + + delete from operation_log_blob + + + + + + insert into operation_log_blob (id, original_value, modified_value + ) + values (#{id,jdbcType=VARCHAR}, #{originalValue,jdbcType=LONGVARBINARY}, #{modifiedValue,jdbcType=LONGVARBINARY} + ) + + + insert into operation_log_blob + + + id, + + + original_value, + + + modified_value, + + + + + #{id,jdbcType=VARCHAR}, + + + #{originalValue,jdbcType=LONGVARBINARY}, + + + #{modifiedValue,jdbcType=LONGVARBINARY}, + + + + + + update operation_log_blob + + + id = #{record.id,jdbcType=VARCHAR}, + + + original_value = #{record.originalValue,jdbcType=LONGVARBINARY}, + + + modified_value = #{record.modifiedValue,jdbcType=LONGVARBINARY}, + + + + + + + + update operation_log_blob + set id = #{record.id,jdbcType=VARCHAR}, + original_value = #{record.originalValue,jdbcType=LONGVARBINARY}, + modified_value = #{record.modifiedValue,jdbcType=LONGVARBINARY} + + + + + + update operation_log_blob + set id = #{record.id,jdbcType=VARCHAR} + + + + + + update operation_log_blob + + + original_value = #{originalValue,jdbcType=LONGVARBINARY}, + + + modified_value = #{modifiedValue,jdbcType=LONGVARBINARY}, + + + where id = #{id,jdbcType=VARCHAR} + + + update operation_log_blob + set original_value = #{originalValue,jdbcType=LONGVARBINARY}, + modified_value = #{modifiedValue,jdbcType=LONGVARBINARY} + where id = #{id,jdbcType=VARCHAR} + + \ No newline at end of file diff --git a/backend/framework/domain/src/main/java/io/metersphere/system/mapper/OperationLogMapper.java b/backend/framework/domain/src/main/java/io/metersphere/sdk/mapper/OperationLogMapper.java similarity index 84% rename from backend/framework/domain/src/main/java/io/metersphere/system/mapper/OperationLogMapper.java rename to backend/framework/domain/src/main/java/io/metersphere/sdk/mapper/OperationLogMapper.java index 29ec3c7738..def5b8b907 100644 --- a/backend/framework/domain/src/main/java/io/metersphere/system/mapper/OperationLogMapper.java +++ b/backend/framework/domain/src/main/java/io/metersphere/sdk/mapper/OperationLogMapper.java @@ -1,7 +1,7 @@ -package io.metersphere.system.mapper; +package io.metersphere.sdk.mapper; -import io.metersphere.system.domain.OperationLog; -import io.metersphere.system.domain.OperationLogExample; +import io.metersphere.sdk.domain.OperationLog; +import io.metersphere.sdk.domain.OperationLogExample; import java.util.List; import org.apache.ibatis.annotations.Param; diff --git a/backend/framework/domain/src/main/java/io/metersphere/system/mapper/OperationLogMapper.xml b/backend/framework/domain/src/main/java/io/metersphere/sdk/mapper/OperationLogMapper.xml similarity index 87% rename from backend/framework/domain/src/main/java/io/metersphere/system/mapper/OperationLogMapper.xml rename to backend/framework/domain/src/main/java/io/metersphere/sdk/mapper/OperationLogMapper.xml index b03b447908..e8b6c4fc4e 100644 --- a/backend/framework/domain/src/main/java/io/metersphere/system/mapper/OperationLogMapper.xml +++ b/backend/framework/domain/src/main/java/io/metersphere/sdk/mapper/OperationLogMapper.xml @@ -1,7 +1,7 @@ - - + + @@ -10,7 +10,7 @@ - + @@ -73,9 +73,9 @@ id, project_id, create_time, create_user, source_id, `method`, `type`, `module`, - details, `path` + content, `path` - select distinct @@ -99,23 +99,23 @@ delete from operation_log where id = #{id,jdbcType=VARCHAR} - + delete from operation_log - + insert into operation_log (id, project_id, create_time, create_user, source_id, `method`, - `type`, `module`, details, + `type`, `module`, content, `path`) values (#{id,jdbcType=VARCHAR}, #{projectId,jdbcType=VARCHAR}, #{createTime,jdbcType=BIGINT}, #{createUser,jdbcType=VARCHAR}, #{sourceId,jdbcType=VARCHAR}, #{method,jdbcType=VARCHAR}, - #{type,jdbcType=VARCHAR}, #{module,jdbcType=VARCHAR}, #{details,jdbcType=VARCHAR}, + #{type,jdbcType=VARCHAR}, #{module,jdbcType=VARCHAR}, #{content,jdbcType=VARCHAR}, #{path,jdbcType=VARCHAR}) - + insert into operation_log @@ -142,8 +142,8 @@ `module`, - - details, + + content, `path`, @@ -174,15 +174,15 @@ #{module,jdbcType=VARCHAR}, - - #{details,jdbcType=VARCHAR}, + + #{content,jdbcType=VARCHAR}, #{path,jdbcType=VARCHAR}, - select count(*) from operation_log @@ -215,8 +215,8 @@ `module` = #{record.module,jdbcType=VARCHAR}, - - details = #{record.details,jdbcType=VARCHAR}, + + content = #{record.content,jdbcType=VARCHAR}, `path` = #{record.path,jdbcType=VARCHAR}, @@ -236,13 +236,13 @@ `method` = #{record.method,jdbcType=VARCHAR}, `type` = #{record.type,jdbcType=VARCHAR}, `module` = #{record.module,jdbcType=VARCHAR}, - details = #{record.details,jdbcType=VARCHAR}, + content = #{record.content,jdbcType=VARCHAR}, `path` = #{record.path,jdbcType=VARCHAR} - + update operation_log @@ -266,8 +266,8 @@ `module` = #{module,jdbcType=VARCHAR}, - - details = #{details,jdbcType=VARCHAR}, + + content = #{content,jdbcType=VARCHAR}, `path` = #{path,jdbcType=VARCHAR}, @@ -275,7 +275,7 @@ where id = #{id,jdbcType=VARCHAR} - + update operation_log set project_id = #{projectId,jdbcType=VARCHAR}, create_time = #{createTime,jdbcType=BIGINT}, @@ -284,7 +284,7 @@ `method` = #{method,jdbcType=VARCHAR}, `type` = #{type,jdbcType=VARCHAR}, `module` = #{module,jdbcType=VARCHAR}, - details = #{details,jdbcType=VARCHAR}, + content = #{content,jdbcType=VARCHAR}, `path` = #{path,jdbcType=VARCHAR} where id = #{id,jdbcType=VARCHAR} diff --git a/backend/framework/domain/src/main/resources/migration/3.0.0/ddl/V3.0.0_2__sdk_ddl.sql b/backend/framework/domain/src/main/resources/migration/3.0.0/ddl/V3.0.0_2__sdk_ddl.sql index bda98b50e6..d02e4fc95d 100644 --- a/backend/framework/domain/src/main/resources/migration/3.0.0/ddl/V3.0.0_2__sdk_ddl.sql +++ b/backend/framework/domain/src/main/resources/migration/3.0.0/ddl/V3.0.0_2__sdk_ddl.sql @@ -1,5 +1,31 @@ -- set innodb lock wait timeout SET SESSION innodb_lock_wait_timeout = 7200; +CREATE TABLE IF NOT EXISTS operation_log +( + `id` VARCHAR(50) NOT NULL COMMENT '主键', + `project_id` VARCHAR(50) NOT NULL DEFAULT 'NONE' COMMENT '项目id', + `create_time` BIGINT NOT NULL COMMENT '操作时间', + `create_user` VARCHAR(50) COMMENT '操作人', + `source_id` VARCHAR(50) COMMENT '资源id', + `method` VARCHAR(255) NOT NULL COMMENT '操作方法', + `type` VARCHAR(20) NOT NULL COMMENT '操作类型/add/update/delete', + `module` VARCHAR(50) COMMENT '操作模块/api/case/scenario/ui', + `content` VARCHAR(500) COMMENT '操作详情', + `path` VARCHAR(255) COMMENT '操作路径', + PRIMARY KEY (id) + ) ENGINE = InnoDB + DEFAULT CHARSET = utf8mb4 + COLLATE = utf8mb4_general_ci COMMENT = '操作日志'; + +DROP TABLE IF EXISTS operation_log_blob; +CREATE TABLE operation_log_blob( + `id` VARCHAR(50) NOT NULL COMMENT '主键' , + `original_value` LONGBLOB COMMENT '变更前内容' , + `modified_value` LONGBLOB COMMENT '变更后内容' , + PRIMARY KEY (id) +) ENGINE = InnoDB + DEFAULT CHARSET = utf8mb4 + COLLATE = utf8mb4_general_ci COMMENT = '操作日志内容详情'; -- set innodb lock wait timeout to default SET SESSION innodb_lock_wait_timeout = DEFAULT; \ No newline at end of file diff --git a/backend/framework/plugin/pom.xml b/backend/framework/plugin/pom.xml index 2a30b7737a..0eaa68dcb2 100644 --- a/backend/framework/plugin/pom.xml +++ b/backend/framework/plugin/pom.xml @@ -27,6 +27,11 @@ ApacheJMeter_core ${jmeter.version} + + org.apache.jmeter + jorphan + ${jmeter.version} + org.projectlombok lombok diff --git a/backend/framework/plugin/src/main/java/io/metersphere/plugin/api/dto/TestElementDTO.java b/backend/framework/plugin/src/main/java/io/metersphere/plugin/api/dto/TestElementDTO.java index d6f68798c4..3262c9c866 100644 --- a/backend/framework/plugin/src/main/java/io/metersphere/plugin/api/dto/TestElementDTO.java +++ b/backend/framework/plugin/src/main/java/io/metersphere/plugin/api/dto/TestElementDTO.java @@ -2,10 +2,15 @@ package io.metersphere.plugin.api.dto; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonTypeInfo; +import io.metersphere.plugin.util.PluginLogUtils; import lombok.Data; +import org.apache.jmeter.save.SaveService; +import org.apache.jorphan.collections.ListedHashTree; +import java.io.ByteArrayOutputStream; import java.io.Serializable; import java.util.LinkedList; +import java.util.List; @Data @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type") @@ -34,6 +39,50 @@ public abstract class TestElementDTO implements Serializable { // 父类 private TestElementDTO parent; + + /** + * 自组件重新这个方法 + * + * @param tree + * @param hashTree + * @param config + */ + public void toHashTree(ListedHashTree tree, List hashTree, BaseConfigDTO config) { + if (hashTree != null && hashTree.size() > 0) { + for (TestElementDTO el : hashTree) { + el.toHashTree(tree, el.hashTree, config); + } + } + } + + /** + * 转换JMX + * + * @param hashTree + * @return + */ + public String getJmx(ListedHashTree hashTree) { + try (ByteArrayOutputStream bas = new ByteArrayOutputStream()) { + SaveService.saveTree(hashTree, bas); + return bas.toString(); + } catch (Exception e) { + PluginLogUtils.error("ListedHashTree error, can't log jmx scenarioDefinition"); + } + return null; + } + + /** + * 生成hashTree + * + * @param config + * @return + */ + public ListedHashTree generateHashTree(BaseConfigDTO config) { + ListedHashTree listedHashTree = new ListedHashTree(); + this.toHashTree(listedHashTree, this.hashTree, config); + return listedHashTree; + } + } diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/config/interceptor/SdkInterceptor.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/config/interceptor/SdkInterceptor.java new file mode 100644 index 0000000000..077516abc1 --- /dev/null +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/config/interceptor/SdkInterceptor.java @@ -0,0 +1,23 @@ +package io.metersphere.sdk.config.interceptor; + +import io.metersphere.sdk.domain.OperationLogBlob; +import io.metersphere.sdk.util.CompressUtils; +import io.metersphere.sdk.util.MybatisInterceptorConfig; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.util.ArrayList; +import java.util.List; + +@Configuration +public class SdkInterceptor { + @Bean + public List sdkCompressConfigs() { + List configList = new ArrayList<>(); + + configList.add(new MybatisInterceptorConfig(OperationLogBlob.class, "originalValue", CompressUtils.class, "zip", "unzip")); + configList.add(new MybatisInterceptorConfig(OperationLogBlob.class, "modifiedValue", CompressUtils.class, "zip", "unzip")); + + return configList; + } +} diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/LogDTO.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/LogDTO.java new file mode 100644 index 0000000000..b8059f6cb5 --- /dev/null +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/LogDTO.java @@ -0,0 +1,29 @@ +package io.metersphere.sdk.dto; + +import io.metersphere.sdk.domain.OperationLog; +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +import java.util.UUID; + +@Data +public class LogDTO extends OperationLog { + @Schema(title = "变更前内容") + private byte[] originalValue; + + @Schema(title = "变更后内容") + private byte[] modifiedValue; + + public LogDTO() { + } + public LogDTO(String projectId, String sourceId, String createUser, String type, String module, String content) { + this.setProjectId(projectId); + this.setSourceId(sourceId); + this.setCreateUser(createUser); + this.setType(type); + this.setModule(module); + this.setContent(content); + this.setId(UUID.randomUUID().toString()); + this.setCreateTime(System.currentTimeMillis()); + } +} \ No newline at end of file diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/log/annotation/Log.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/log/annotation/Log.java index d1c62377eb..17e2d05680 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/log/annotation/Log.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/log/annotation/Log.java @@ -13,19 +13,6 @@ import java.lang.annotation.*; * @author mr.zhao */ public @interface Log { - /** - * 功能模块 - * - * @return - */ - String module() default OperationLogModule.UNKNOWN_MODULE; - - /** - * 项目,系统模块没有项目id默认系统 - * - * @return - */ - String projectId() default "system"; /** * 操作类型 @@ -34,38 +21,10 @@ public @interface Log { */ OperationLogType type() default OperationLogType.SELECT; - /** - * 资源ID + /*** + * 操作函数 */ - String sourceId() default ""; - - /** - * 创建用户 - * - * @return - */ - String createUser() default "admin"; - - /** - * 操作内容 - * - * @return - */ - String details() default ""; - - /** - * 是否是批量删除操作 - * - * @return - */ - boolean isBatch() default false; - - /** - * 是否在业务代码执行前记录内容 - * - * @return - */ - boolean isBefore() default false; + String expression(); /** * 传入执行类 diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/log/aspect/OperationLogAspect.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/log/aspect/OperationLogAspect.java index 6594ea003e..6ae30fa29a 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/log/aspect/OperationLogAspect.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/log/aspect/OperationLogAspect.java @@ -1,13 +1,14 @@ package io.metersphere.sdk.log.aspect; +import io.metersphere.sdk.dto.LogDTO; import io.metersphere.sdk.log.annotation.Log; +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.LogUtils; import io.metersphere.sdk.util.SessionUtils; -import io.metersphere.system.domain.OperationLog; import jakarta.annotation.Resource; -import jakarta.servlet.http.HttpServletRequest; +import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; import org.aspectj.lang.JoinPoint; @@ -24,14 +25,13 @@ import org.springframework.expression.ExpressionParser; import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.expression.spel.support.StandardEvaluationContext; import org.springframework.stereotype.Component; -import org.springframework.web.context.request.RequestContextHolder; -import org.springframework.web.context.request.ServletRequestAttributes; import java.lang.reflect.Method; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Map; -import java.util.UUID; +import java.util.stream.Collectors; /** * 系统日志:切面处理类 @@ -55,16 +55,15 @@ public class OperationLogAspect { @Resource private OperationLogService operationLogService; - - private ThreadLocal> beforeValue = new ThreadLocal<>(); - - private ThreadLocal treadLocalDetails = new ThreadLocal<>(); - - private ThreadLocal treadLocalSourceId = new ThreadLocal<>(); + // 批量变更前后内容 + private ThreadLocal> beforeValues = new ThreadLocal<>(); private ThreadLocal localUser = new ThreadLocal<>(); - private final String[] methodNames = new String[]{"delete", "update", "add"}; + // 此方法随时补充类型,需要在内容变更前执行的类型都可以加入 + private final OperationLogType[] beforeMethodNames = new OperationLogType[]{OperationLogType.UPDATE, OperationLogType.DELETE}; + // 需要后置执行合并内容的 + private final OperationLogType[] postMethodNames = new OperationLogType[]{OperationLogType.ADD, OperationLogType.UPDATE}; /** * 定义切点 @Pointcut 在注解的位置切入代码 @@ -76,12 +75,13 @@ public class OperationLogAspect { @Before("logPointCut()") public void before(JoinPoint joinPoint) { try { + localUser.set(SessionUtils.getUserId()); //从切面织入点处通过反射机制获取织入点处的方法 MethodSignature signature = (MethodSignature) joinPoint.getSignature(); //获取切入点所在的方法 Method method = signature.getMethod(); Log msLog = method.getAnnotation(Log.class); - if (msLog != null && msLog.isBefore()) { + if (msLog != null && isMatch(msLog.type())) { //获取参数对象数组 Object[] args = joinPoint.getArgs(); //获取方法参数名 @@ -94,10 +94,6 @@ public class OperationLogAspect { } boolean isNext = false; for (Class clazz : msLog.msClass()) { - if (clazz.getName().endsWith("SessionUtils")) { - localUser.set(SessionUtils.getUserId()); - continue; - } context.setVariable("msClass", applicationContext.getBean(clazz)); isNext = true; } @@ -105,134 +101,87 @@ public class OperationLogAspect { return; } // 初始化details内容 - initDetails(msLog, context); - // 初始化资源id - initResourceId(msLog, context); + initBeforeDetails(msLog, context); } } catch (Exception e) { LogUtils.error("操作日志写入异常:" + joinPoint.getSignature()); } } - public boolean isMatch(String keyword) { - return Arrays.stream(methodNames) + public boolean isMatch(OperationLogType keyword) { + return Arrays.stream(beforeMethodNames) .anyMatch(input -> input.contains(keyword)); } - private void initDetails(Log msLog, EvaluationContext context) { + private void initBeforeDetails(Log msLog, EvaluationContext context) { try { // 批量内容处理 - if (StringUtils.isNotBlank(msLog.details()) && msLog.details().startsWith("#msClass")) { - if (msLog.isBatch()) { - Expression expression = parser.parseExpression(msLog.details()); - List beforeContent = expression.getValue(context, List.class); - beforeValue.set(beforeContent); - } else { - Expression detailsEx = parser.parseExpression(msLog.details()); - String details = detailsEx.getValue(context, String.class); - this.treadLocalDetails.set(details); - } - } else if (StringUtils.isNotBlank(msLog.details()) && msLog.details().startsWith("#")) { - Expression titleExp = parser.parseExpression(msLog.details()); - String details = titleExp.getValue(context, String.class); - this.treadLocalDetails.set(details); - } else { - this.treadLocalDetails.set(msLog.details()); + Expression expression = parser.parseExpression(msLog.expression()); + Object obj = expression.getValue(context); + if (obj == null) { + return; } + if (obj instanceof List) { + beforeValues.set((List) obj); + } else { + List LogDTOs = new ArrayList<>(); + LogDTOs.add((LogDTO) obj); + beforeValues.set(LogDTOs); + } + } catch (Exception e) { LogUtils.error("未获取到details内容", e); - this.treadLocalDetails.set(msLog.details()); } } - private void initResourceId(Log msLog, EvaluationContext context) { - try { - // 批量内容处理 - if (StringUtils.isNotBlank(msLog.sourceId()) && msLog.sourceId().startsWith("#msClass")) { - Expression detailsEx = parser.parseExpression(msLog.sourceId()); - String sourceId = detailsEx.getValue(context, String.class); - treadLocalSourceId.set(sourceId); - } else if (StringUtils.isNotBlank(msLog.sourceId()) && msLog.sourceId().startsWith("#")) { - Expression titleExp = parser.parseExpression(msLog.sourceId()); - String sourceId = titleExp.getValue(context, String.class); - treadLocalSourceId.set(sourceId); - } else { - treadLocalSourceId.set(msLog.sourceId()); - } - } catch (Exception e) { - LogUtils.error("未获取到资源id", e); - treadLocalSourceId.set(msLog.sourceId()); - } - } - - private String getProjectId(Log msLog, EvaluationContext context) { - try { - if (StringUtils.isNotBlank(msLog.projectId()) && msLog.projectId().startsWith("#")) { - Expression titleExp = parser.parseExpression(msLog.projectId()); - return titleExp.getValue(context, String.class); - } else { - return msLog.projectId(); - } - } catch (Exception e) { - return msLog.projectId(); - } - } - - private void add(OperationLog operationLog, Log msLog, JoinPoint joinPoint, Object result) { - //从切面织入点处通过反射机制获取织入点处的方法 - MethodSignature signature = (MethodSignature) joinPoint.getSignature(); - //获取切入点所在的方法 - Method method = signature.getMethod(); - //获取参数对象数组 - Object[] args = joinPoint.getArgs(); - // 操作类型 - operationLog.setType(msLog.type().name()); - // 创建用户 - if (StringUtils.isNotBlank(msLog.createUser())) { - operationLog.setCreateUser(msLog.createUser()); - } - // 项目ID - operationLog.setProjectId(msLog.projectId()); - operationLog.setModule(msLog.module()); - //获取方法参数名 - String[] params = discoverer.getParameterNames(method); - //将参数纳入Spring管理 - EvaluationContext context = new StandardEvaluationContext(); - for (int len = 0; len < params.length; len++) { - context.setVariable(params[len], args[len]); - } - for (Class clazz : msLog.msClass()) { - context.setVariable("msClass", applicationContext.getBean(clazz)); - } - // 批量编辑操作 - if (msLog.isBatch()) { - operationLogService.batchAdd(beforeValue.get()); + public void mergeLists(List beforeLogs, List postLogs) { + if (CollectionUtils.isEmpty(beforeLogs) && CollectionUtils.isNotEmpty(postLogs)) { + beforeValues.set(postLogs); return; } - // 项目ID表达式 - operationLog.setProjectId(getProjectId(msLog, context)); - - if (!msLog.isBefore()) { - // 初始化资源id - initResourceId(msLog, context); - // 初始化内容详情 - initDetails(msLog, context); + if (CollectionUtils.isEmpty(beforeLogs) && CollectionUtils.isEmpty(postLogs)) { + return; } - // 内容详情 - operationLog.setDetails(treadLocalDetails.get()); - // 资源id - operationLog.setSourceId(treadLocalSourceId.get()); - - if (StringUtils.isBlank(operationLog.getCreateUser())) { - operationLog.setCreateUser(localUser.get()); - } - // 从返回内容中获取资源id - if (StringUtils.isEmpty(operationLog.getSourceId()) && !msLog.isBatch()) { - operationLog.setSourceId(getId(result)); - } - operationLogService.add(operationLog); + Map postDto = postLogs.stream().collect(Collectors.toMap(LogDTO::getSourceId, item -> item)); + beforeLogs.forEach(item -> { + LogDTO post = postDto.get(item.getSourceId()); + if (post != null) { + item.setModifiedValue(post.getOriginalValue()); + } + }); } + private void initPostDetails(Log msLog, EvaluationContext context) { + try { + if (StringUtils.isBlank(msLog.expression())) { + return; + } + // 批量内容处理 + Expression expression = parser.parseExpression(msLog.expression()); + Object obj = expression.getValue(context); + if (obj == null) { + return; + } + + if (obj instanceof List) { + mergeLists(beforeValues.get(), (List) obj); + } else if (obj instanceof LogDTO) { + LogDTO log = (LogDTO) obj; + if (CollectionUtils.isNotEmpty(beforeValues.get())) { + beforeValues.get().get(0).setModifiedValue(log.getOriginalValue()); + } else { + beforeValues.set(new ArrayList<>() {{ + this.add(log); + }}); + } + } + + } catch (Exception e) { + LogUtils.error("未获取到details内容", e); + } + } + + public String getId(Object result) { try { if (result != null) { @@ -251,6 +200,26 @@ public class OperationLogAspect { return null; } + private void save(Object result) { + List logDTOList = beforeValues.get(); + if (CollectionUtils.isEmpty(logDTOList)) { + return; + } + // 单条存储 + if (logDTOList.size() == 1) { + LogDTO logDTO = logDTOList.get(0); + if (StringUtils.isBlank(logDTO.getSourceId())) { + logDTO.setSourceId(getId(result)); + } + if (StringUtils.isBlank(logDTO.getCreateUser())) { + logDTO.setCreateUser(localUser.get()); + } + operationLogService.add(logDTO); + } else { + operationLogService.batchAdd(logDTOList); + } + } + /** * 切面 配置通知 */ @@ -261,30 +230,33 @@ public class OperationLogAspect { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); //获取切入点所在的方法 Method method = signature.getMethod(); - //保存日志 - OperationLog operationLog = new OperationLog(); - //保存获取的操作 - operationLog.setId(UUID.randomUUID().toString()); - operationLog.setMethod(method.getName()); - operationLog.setCreateTime(System.currentTimeMillis()); - operationLog.setCreateUser(SessionUtils.getUserId()); - HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); - operationLog.setPath(request.getServletPath()); // 获取操作 Log msLog = method.getAnnotation(Log.class); if (msLog != null) { - add(operationLog, msLog, joinPoint, result); - } - // 兼容遗漏注解的内容 - else if (isMatch(method.getName())) { - operationLogService.add(operationLog); + //获取参数对象数组 + Object[] args = joinPoint.getArgs(); + //获取方法参数名 + String[] params = discoverer.getParameterNames(method); + //将参数纳入Spring管理 + EvaluationContext context = new StandardEvaluationContext(); + for (int len = 0; len < params.length; len++) { + context.setVariable(params[len], args[len]); + } + for (Class clazz : msLog.msClass()) { + context.setVariable("msClass", applicationContext.getBean(clazz)); + } + // 需要后置再次执行的方法 + if (Arrays.stream(postMethodNames).anyMatch(input -> input.contains(msLog.type()))) { + initPostDetails(msLog, context); + } + // 存储日志结果 + save(result); } } catch (Exception e) { LogUtils.error("操作日志写入异常:", e); } finally { localUser.remove(); - beforeValue.remove(); - treadLocalDetails.remove(); + beforeValues.remove(); } } diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/log/constants/OperationLogType.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/log/constants/OperationLogType.java index 91cb671caf..529dfb3373 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/log/constants/OperationLogType.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/log/constants/OperationLogType.java @@ -14,5 +14,9 @@ public enum OperationLogType { EXPORT, LOGIN, SELECT, - RECOVER + RECOVER; + + public boolean contains(OperationLogType keyword) { + return this.name().contains(keyword.name()); + } } diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/log/service/OperationLogService.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/log/service/OperationLogService.java index 79da01667e..18c2095adc 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/log/service/OperationLogService.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/log/service/OperationLogService.java @@ -1,7 +1,9 @@ package io.metersphere.sdk.log.service; -import io.metersphere.system.domain.OperationLog; -import io.metersphere.system.mapper.OperationLogMapper; +import io.metersphere.sdk.domain.OperationLogBlob; +import io.metersphere.sdk.dto.LogDTO; +import io.metersphere.sdk.mapper.OperationLogBlobMapper; +import io.metersphere.sdk.mapper.OperationLogMapper; import jakarta.annotation.Resource; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; @@ -13,6 +15,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; +import java.util.UUID; @Service @Transactional(rollbackFor = Exception.class) @@ -20,9 +23,11 @@ public class OperationLogService { @Resource private OperationLogMapper operationLogMapper; @Resource + private OperationLogBlobMapper operationLogBlobMapper; + @Resource private SqlSessionFactory sqlSessionFactory; - public void add(OperationLog log) { + public void add(LogDTO log) { if (StringUtils.isBlank(log.getProjectId())) { log.setProjectId("none"); } @@ -30,25 +35,24 @@ public class OperationLogService { log.setCreateUser("admin"); } // 限制长度 - if (StringUtils.isNotBlank(log.getDetails()) && log.getDetails().length() > 500) { - log.setDetails(log.getDetails().substring(0, 499)); - } - operationLogMapper.insert(log); + saveBlob(operationLogMapper, operationLogBlobMapper, log); } - public void batchAdd(List logs) { + public void batchAdd(List logs) { if (CollectionUtils.isEmpty(logs)) { return; } SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); OperationLogMapper logMapper = sqlSession.getMapper(OperationLogMapper.class); + OperationLogBlobMapper logBlobMapper = sqlSession.getMapper(OperationLogBlobMapper.class); + if (CollectionUtils.isNotEmpty(logs)) { logs.forEach(item -> { - // 限制长度 - if (StringUtils.isNotBlank(item.getDetails()) && item.getDetails().length() > 500) { - item.setDetails(item.getDetails().substring(0, 499)); + if (StringUtils.isBlank(item.getId())) { + item.setId(UUID.randomUUID().toString()); } - logMapper.insert(item); + // 限制长度 + saveBlob(logMapper, logBlobMapper, item); }); } sqlSession.flushStatements(); @@ -56,4 +60,16 @@ public class OperationLogService { SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); } } + + private void saveBlob(OperationLogMapper logMapper, OperationLogBlobMapper logBlobMapper, LogDTO item) { + if (StringUtils.isNotBlank(item.getContent()) && item.getContent().length() > 500) { + item.setContent(item.getContent().substring(0, 499)); + } + logMapper.insert(item); + OperationLogBlob blob = new OperationLogBlob(); + blob.setId(item.getId()); + blob.setOriginalValue(item.getOriginalValue()); + blob.setModifiedValue(item.getModifiedValue()); + logBlobMapper.insert(blob); + } } diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/SystemParameterService.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/SystemParameterService.java index 5ef11e3409..7a4d6cfa00 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/SystemParameterService.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/SystemParameterService.java @@ -1,11 +1,16 @@ package io.metersphere.sdk.service; +import io.metersphere.constants.HttpMethodConstants; import io.metersphere.dto.BaseSystemConfigDTO; import io.metersphere.sdk.constants.ParamConstants; import io.metersphere.sdk.dto.EMailInfoDto; +import io.metersphere.sdk.dto.LogDTO; import io.metersphere.sdk.exception.MSException; +import io.metersphere.sdk.log.constants.OperationLogModule; +import io.metersphere.sdk.log.constants.OperationLogType; import io.metersphere.sdk.notice.sender.impl.MailNoticeSender; import io.metersphere.sdk.util.EncryptUtils; +import io.metersphere.sdk.util.JSON; import io.metersphere.sdk.util.LogUtils; import io.metersphere.sdk.util.Translator; import io.metersphere.system.domain.SystemParameter; @@ -204,4 +209,38 @@ public class SystemParameterService { } } + /** + * 添加接口日志 + + * @return + */ + public LogDTO addLog(List systemParameter) { + LogDTO dto = new LogDTO( + "system", + "system-parameter", + null, + OperationLogType.ADD.name(), + OperationLogModule.SYSTEM_USER_ROLE_RELATION, + "系统参数"); + + dto.setPath("/system/parameter/save/base-info"); + dto.setMethod(HttpMethodConstants.POST.name()); + dto.setOriginalValue(JSON.toJSONBytes(systemParameter)); + return dto; + } + + public LogDTO updateLog(List systemParameter) { + LogDTO dto = new LogDTO( + "system", + "system-parameter", + null, + OperationLogType.ADD.name(), + OperationLogModule.SYSTEM_USER_ROLE_RELATION, + "编辑邮件信息"); + + dto.setPath("/system/parameter/edit/email-info"); + dto.setMethod(HttpMethodConstants.POST.name()); + dto.setOriginalValue(JSON.toJSONBytes(systemParameter)); + return dto; + } } diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/TestResourcePoolService.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/TestResourcePoolService.java index 4eabcc2914..e3bcf5b5f8 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/TestResourcePoolService.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/TestResourcePoolService.java @@ -1,12 +1,16 @@ package io.metersphere.sdk.service; import groovy.util.logging.Slf4j; +import io.metersphere.constants.HttpMethodConstants; import io.metersphere.sdk.constants.ResourcePoolTypeEnum; -import io.metersphere.sdk.dto.QueryResourcePoolRequest; -import io.metersphere.sdk.dto.TestResourceDTO; -import io.metersphere.sdk.dto.TestResourcePoolDTO; +import io.metersphere.sdk.dto.*; import io.metersphere.sdk.exception.MSException; -import io.metersphere.sdk.util.*; +import io.metersphere.sdk.log.constants.OperationLogModule; +import io.metersphere.sdk.log.constants.OperationLogType; +import io.metersphere.sdk.util.BeanUtils; +import io.metersphere.sdk.util.CommonBeanFactory; +import io.metersphere.sdk.util.JSON; +import io.metersphere.sdk.util.Translator; import io.metersphere.system.domain.*; import io.metersphere.system.mapper.TestResourcePoolBlobMapper; import io.metersphere.system.mapper.TestResourcePoolMapper; @@ -65,21 +69,21 @@ public class TestResourcePoolService { private void checkAndSaveOrgRelation(TestResourcePool testResourcePool, String id, TestResourceDTO testResourceDTO) { //防止前端传入的应用组织为空 - if ((testResourcePool.getAllOrg() == null || !testResourcePool.getAllOrg())&& CollectionUtils.isEmpty(testResourceDTO.getOrgIds())){ + if ((testResourcePool.getAllOrg() == null || !testResourcePool.getAllOrg()) && CollectionUtils.isEmpty(testResourceDTO.getOrgIds())) { throw new MSException(Translator.get("resource_pool_application_organization_is_empty")); } //前端应用组织选择了全部,但是也传了部分组织,以全部组织为主 - if ((testResourcePool.getAllOrg() != null && testResourcePool.getAllOrg()) && CollectionUtils.isNotEmpty(testResourceDTO.getOrgIds()) ) { + if ((testResourcePool.getAllOrg() != null && testResourcePool.getAllOrg()) && CollectionUtils.isNotEmpty(testResourceDTO.getOrgIds())) { testResourceDTO.setOrgIds(new ArrayList<>()); } //前端选择部分组织保存资源池与组织关系 SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); TestResourcePoolOrganizationMapper poolOrganizationMapper = sqlSession.getMapper(TestResourcePoolOrganizationMapper.class); - if ((testResourcePool.getAllOrg() == null || !testResourcePool.getAllOrg())&& CollectionUtils.isNotEmpty(testResourceDTO.getOrgIds())){ + if ((testResourcePool.getAllOrg() == null || !testResourcePool.getAllOrg()) && CollectionUtils.isNotEmpty(testResourceDTO.getOrgIds())) { testResourcePool.setAllOrg(false); - testResourceDTO.getOrgIds().forEach(orgId->{ + testResourceDTO.getOrgIds().forEach(orgId -> { TestResourcePoolOrganization testResourcePoolOrganization = new TestResourcePoolOrganization(); testResourcePoolOrganization.setId(UUID.randomUUID().toString()); testResourcePoolOrganization.setOrgId(orgId); @@ -110,7 +114,7 @@ public class TestResourcePoolService { } private static boolean checkNodeOrK8s(TestResourceDTO testResourceDTO, String type, Boolean usedApiType) { - if (StringUtils.equalsIgnoreCase(type,ResourcePoolTypeEnum.NODE.name())) { + if (StringUtils.equalsIgnoreCase(type, ResourcePoolTypeEnum.NODE.name())) { NodeResourcePoolService resourcePoolService = CommonBeanFactory.getBean(NodeResourcePoolService.class); if (resourcePoolService != null) { return resourcePoolService.validate(testResourceDTO); @@ -249,10 +253,56 @@ public class TestResourcePoolService { return testResourcePoolDTO; } - public String getLogDetails(String id) { + public LogDTO addLog(TestResourcePoolRequest request) { + LogDTO dto = new LogDTO( + "system", + request.getId(), + null, + OperationLogType.ADD.name(), + OperationLogModule.SYSTEM_TEST_RESOURCE_POOL, + request.getName()); + + dto.setPath("/test/resource/pool/add"); + dto.setMethod(HttpMethodConstants.POST.name()); + dto.setOriginalValue(JSON.toJSONBytes(request)); + return dto; + } + + public LogDTO deleteLog(String id) { TestResourcePool pool = testResourcePoolMapper.selectByPrimaryKey(id); if (pool != null) { - return pool.getName(); + LogDTO dto = new LogDTO( + "system", + id, + pool.getCreateUser(), + OperationLogType.DELETE.name(), + OperationLogModule.SYSTEM_TEST_RESOURCE_POOL, + pool.getName()); + + dto.setPath("/delete"); + dto.setMethod(HttpMethodConstants.POST.name()); + + dto.setOriginalValue(JSON.toJSONBytes(pool)); + return dto; + } + return null; + } + + public LogDTO updateLog(TestResourcePoolRequest request) { + TestResourcePool pool = testResourcePoolMapper.selectByPrimaryKey(request.getId()); + if (pool != null) { + LogDTO dto = new LogDTO( + "system", + pool.getId(), + pool.getCreateUser(), + OperationLogType.UPDATE.name(), + OperationLogModule.SYSTEM_TEST_RESOURCE_POOL, + "编辑全局用户组对应的权限配置"); + + dto.setPath("/update"); + dto.setMethod(HttpMethodConstants.POST.name()); + dto.setOriginalValue(JSON.toJSONBytes(pool)); + return dto; } return null; } diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/util/JSON.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/util/JSON.java index fcfd44f1f4..31c8d72355 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/util/JSON.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/util/JSON.java @@ -37,6 +37,14 @@ public class JSON { } } + public static byte[] toJSONBytes(Object value) { + try { + return objectMapper.writeValueAsBytes(value); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + public static Object parseObject(String content) { return parseObject(content, Object.class); } diff --git a/backend/framework/sdk/src/test/java/base/BaseTest.java b/backend/framework/sdk/src/test/java/base/BaseTest.java index bde50bb328..5429ba0128 100644 --- a/backend/framework/sdk/src/test/java/base/BaseTest.java +++ b/backend/framework/sdk/src/test/java/base/BaseTest.java @@ -8,8 +8,8 @@ import io.metersphere.sdk.controller.handler.result.IResultCode; import io.metersphere.sdk.log.constants.OperationLogType; import io.metersphere.sdk.util.JSON; import io.metersphere.sdk.util.Pager; -import io.metersphere.system.domain.OperationLogExample; -import io.metersphere.system.mapper.OperationLogMapper; +import io.metersphere.sdk.domain.OperationLogExample; +import io.metersphere.sdk.mapper.OperationLogMapper; import io.metersphere.validation.groups.Created; import io.metersphere.validation.groups.Updated; import jakarta.annotation.Resource; diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/controller/ApiDefinitionController.java b/backend/services/api-test/src/main/java/io/metersphere/api/controller/ApiDefinitionController.java index 2590e41f27..c34b2f2961 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/controller/ApiDefinitionController.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/controller/ApiDefinitionController.java @@ -1,20 +1,14 @@ package io.metersphere.api.controller; -import com.github.pagehelper.Page; -import com.github.pagehelper.PageHelper; -import io.metersphere.api.domain.ApiDefinition; import io.metersphere.api.dto.definition.ApiDefinitionDTO; -import io.metersphere.api.dto.definition.ListRequestDTO; +import io.metersphere.api.service.ApiDefinitionLogService; import io.metersphere.api.service.ApiDefinitionService; import io.metersphere.sdk.constants.PermissionConstants; import io.metersphere.sdk.log.annotation.Log; -import io.metersphere.sdk.log.constants.OperationLogModule; import io.metersphere.sdk.log.constants.OperationLogType; -import io.metersphere.sdk.util.PageUtils; -import io.metersphere.sdk.util.Pager; import io.metersphere.validation.groups.Created; +import io.metersphere.validation.groups.Updated; import jakarta.annotation.Resource; -import org.apache.commons.lang3.StringUtils; import org.apache.shiro.authz.annotation.RequiresPermissions; import org.springframework.http.MediaType; import org.springframework.validation.annotation.Validated; @@ -32,25 +26,40 @@ public class ApiDefinitionController { @PostMapping(value = "/add", consumes = MediaType.APPLICATION_JSON_VALUE) @RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_READ_ADD_API) - @Log(type = OperationLogType.ADD, module = OperationLogModule.API_DEFINITION, projectId = "#request.projectId", details = "#request.name") + // 添加接口Log示例 + @Log(type = OperationLogType.ADD, expression = "#msClass.addLog(#request)", msClass = ApiDefinitionLogService.class) public ApiDefinitionDTO add(@Validated({Created.class}) @RequestBody ApiDefinitionDTO request, @RequestParam(value = "files") List bodyFiles) { return apiDefinitionService.create(request, bodyFiles); } + @PostMapping(value = "/update", consumes = MediaType.APPLICATION_JSON_VALUE) + @RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_READ_ADD_API) + // 添加修改Log示例 + @Log(type = OperationLogType.UPDATE, expression = "#msClass.updateLog(#request)", msClass = ApiDefinitionLogService.class) + public ApiDefinitionDTO update(@Validated({Updated.class}) @RequestBody ApiDefinitionDTO request) { + return request; + } + + @PostMapping(value = "/batch-update", consumes = MediaType.APPLICATION_JSON_VALUE) + @RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_READ_ADD_API) + // 添加修改Log示例 + @Log(type = OperationLogType.UPDATE, expression = "#msClass.batchUpdateLog(#ids)", msClass = ApiDefinitionLogService.class) + public ApiDefinitionDTO batchUpdate(@RequestBody List ids) { + return null; + } + + @PostMapping(value = "/delete") + @RequiresPermissions(PermissionConstants.PROJECT_API_REPORT_READ_DELETE) + @Log(type = OperationLogType.DELETE, expression = "#msClass.delLog(#id)", msClass = ApiDefinitionLogService.class) + public void batchDelete(@RequestBody String id) { + + } @PostMapping(value = "/batch-del") @RequiresPermissions(PermissionConstants.PROJECT_API_REPORT_READ_DELETE) - @Log(isBatch = true, isBefore = true, details = "#msClass.getLogs(#ids)", msClass = ApiDefinitionService.class) + @Log(type = OperationLogType.DELETE, expression = "#msClass.batchDelLog(#ids)", msClass = ApiDefinitionLogService.class) public void batchDelete(@RequestBody List ids) { apiDefinitionService.batchDelete(ids); } - @PostMapping(value = "/page") - @RequiresPermissions(PermissionConstants.PROJECT_API_DEFINITION_READ) - public Pager> list(@Validated @RequestBody ListRequestDTO request) { - Page page = PageHelper.startPage(request.getCurrent(), request.getPageSize(), - StringUtils.isNotBlank(request.getOrderColumn()) ? request.getOrderColumn() : "create_time desc"); - return PageUtils.setPageInfo(page, apiDefinitionService.list(request)); - } - } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiDefinitionLogService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiDefinitionLogService.java new file mode 100644 index 0000000000..61379ceae2 --- /dev/null +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiDefinitionLogService.java @@ -0,0 +1,148 @@ +package io.metersphere.api.service; + +import io.metersphere.api.domain.ApiDefinition; +import io.metersphere.api.domain.ApiDefinitionExample; +import io.metersphere.api.dto.definition.ApiDefinitionDTO; +import io.metersphere.api.mapper.ApiDefinitionMapper; +import io.metersphere.constants.HttpMethodConstants; +import io.metersphere.sdk.dto.LogDTO; +import io.metersphere.sdk.log.constants.OperationLogModule; +import io.metersphere.sdk.log.constants.OperationLogType; +import io.metersphere.sdk.util.JSON; +import jakarta.annotation.Resource; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; + +@Service +public class ApiDefinitionLogService { + + @Resource + private ApiDefinitionMapper apiDefinitionMapper; + + /** + * 添加接口日志 + * + * @param request + * @return + */ + public LogDTO addLog(ApiDefinitionDTO request) { + LogDTO dto = new LogDTO( + request.getProjectId(), + request.getId(), + request.getCreateUser(), + OperationLogType.ADD.name(), + OperationLogModule.API_DEFINITION, + request.getName()); + + dto.setPath("/api/definition/add"); + dto.setMethod(HttpMethodConstants.POST.name()); + dto.setOriginalValue(JSON.toJSONBytes(request)); + return dto; + } + + /** + * 修改接口日志 + * + * @param request + * @return + */ + public LogDTO updateLog(ApiDefinitionDTO request) { + ApiDefinition apiDefinition = apiDefinitionMapper.selectByPrimaryKey(request.getId()); + + LogDTO dto = new LogDTO( + request.getProjectId(), + request.getId(), + request.getCreateUser(), + OperationLogType.UPDATE.name(), + OperationLogModule.API_DEFINITION, + request.getName()); + + dto.setPath("/api/definition/update"); + dto.setMethod(HttpMethodConstants.POST.name()); + if (apiDefinition == null) { + return dto; + } + dto.setOriginalValue(JSON.toJSONBytes(apiDefinition)); + return dto; + } + + /** + * 删除接口日志 + * + * @param request + * @return + */ + public LogDTO delLog(ApiDefinitionDTO request) { + ApiDefinition apiDefinition = apiDefinitionMapper.selectByPrimaryKey(request.getId()); + LogDTO dto = new LogDTO( + request.getProjectId(), + request.getId(), + request.getCreateUser(), + OperationLogType.DELETE.name(), + OperationLogModule.API_DEFINITION, + request.getName()); + + dto.setPath("/api/definition/delete"); + dto.setMethod(HttpMethodConstants.POST.name()); + if (apiDefinition == null) { + return dto; + } + dto.setOriginalValue(JSON.toJSONBytes(apiDefinition)); + return dto; + } + + /** + * 批量删除接口日志 + * + * @return + */ + public List batchDelLog(List ids) { + ApiDefinitionExample example = new ApiDefinitionExample(); + example.createCriteria().andIdIn(ids); + List apiDefinitions = apiDefinitionMapper.selectByExample(example); + List dtoList = new ArrayList<>(); + apiDefinitions.forEach(item -> { + LogDTO dto = new LogDTO( + item.getProjectId(), + item.getId(), + item.getCreateUser(), + OperationLogType.DELETE.name(), + OperationLogModule.API_DEFINITION, + item.getName()); + + dto.setPath("/api/definition/batch-delete"); + dto.setMethod(HttpMethodConstants.POST.name()); + dto.setOriginalValue(JSON.toJSONBytes(item)); + dtoList.add(dto); + }); + return dtoList; + } + + /** + * 批量更新接口日志 + * @return + */ + public List batchUpdateLog(List ids) { + ApiDefinitionExample example = new ApiDefinitionExample(); + example.createCriteria().andIdIn(ids); + List apiDefinitions = apiDefinitionMapper.selectByExample(example); + List dtoList = new ArrayList<>(); + apiDefinitions.forEach(item -> { + LogDTO dto = new LogDTO( + item.getProjectId(), + item.getId(), + item.getCreateUser(), + OperationLogType.UPDATE.name(), + OperationLogModule.API_DEFINITION, + item.getName()); + + dto.setPath("/api/definition/batch-update"); + dto.setMethod(HttpMethodConstants.POST.name()); + dto.setOriginalValue(JSON.toJSONBytes(item)); + dtoList.add(dto); + }); + return dtoList; + } +} diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiDefinitionService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiDefinitionService.java index 6442baf614..3801efb323 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiDefinitionService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiDefinitionService.java @@ -5,19 +5,13 @@ import io.metersphere.api.domain.ApiDefinitionExample; import io.metersphere.api.dto.definition.ApiDefinitionDTO; import io.metersphere.api.dto.definition.ListRequestDTO; import io.metersphere.api.mapper.ApiDefinitionMapper; -import io.metersphere.sdk.log.constants.OperationLogModule; -import io.metersphere.sdk.log.constants.OperationLogType; -import io.metersphere.system.domain.OperationLog; import jakarta.annotation.Resource; import jakarta.validation.constraints.NotNull; -import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; -import java.util.ArrayList; import java.util.List; -import java.util.UUID; @Service public class ApiDefinitionService { @@ -53,31 +47,4 @@ public class ApiDefinitionService { public void batchDelete(List ids) { } - - /** - * 批量操作日志,由切面触发 - * - * @param ids - */ - public List getLogs(List ids) { - if (CollectionUtils.isEmpty(ids)) { - return new ArrayList<>(); - } - List logs = new ArrayList<>(); - ApiDefinitionExample example = new ApiDefinitionExample(); - example.createCriteria().andIdIn(ids); - List definitions = apiDefinitionMapper.selectByExample(example); - definitions.forEach(definition -> { - OperationLog log = new OperationLog(); - log.setId(UUID.randomUUID().toString()); - log.setCreateUser(definition.getUpdateUser()); - log.setProjectId(definition.getProjectId()); - log.setType(OperationLogType.DELETE.name()); - log.setModule(OperationLogModule.API_DEFINITION); - log.setSourceId(definition.getId()); - log.setDetails(definition.getName()); - logs.add(log); - }); - return logs; - } } diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDefinitionControllerTests.java b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDefinitionControllerTests.java index c8c6ee8517..26804d9980 100644 --- a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDefinitionControllerTests.java +++ b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDefinitionControllerTests.java @@ -1,24 +1,17 @@ package io.metersphere.api.controller; import com.jayway.jsonpath.JsonPath; -import io.metersphere.api.domain.ApiDefinition; import io.metersphere.api.dto.definition.ApiDefinitionDTO; -import io.metersphere.api.dto.definition.ListRequestDTO; import io.metersphere.sdk.constants.SessionConstants; -import io.metersphere.sdk.controller.handler.ResultHolder; import io.metersphere.sdk.util.JSON; import io.metersphere.sdk.util.LogUtils; -import io.metersphere.sdk.util.Pager; import io.metersphere.utils.JsonUtils; import jakarta.annotation.Resource; import org.junit.jupiter.api.*; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; -import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.mock.web.MockMultipartFile; -import org.springframework.test.context.jdbc.Sql; -import org.springframework.test.context.jdbc.SqlConfig; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; @@ -90,86 +83,69 @@ public class ApiDefinitionControllerTests { .andExpect(jsonPath("$.data.id").value("test-api-id")); } - //正常接口获取 @Test - @Sql(scripts = {"/sql/init_api_definition.sql"}, - config = @SqlConfig(encoding = "utf-8", transactionMode = SqlConfig.TransactionMode.ISOLATED), - executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) @Order(2) - public void testPageSuccess() throws Exception { - int pageSize = 10; - int current = 1; - ListRequestDTO request = new ListRequestDTO(); - request.setCurrent(current); - request.setPageSize(pageSize); + public void testUpdate() throws Exception { + LogUtils.info("delete api test"); + ApiDefinitionDTO request = new ApiDefinitionDTO(); + // 补充属性内容 + request.setId("test-api-id"); + request.setCreateUser("test-user"); + request.setCreateTime(System.currentTimeMillis()); + request.setUpdateUser("test-api-id"); + request.setUpdateTime(System.currentTimeMillis()); request.setProjectId("test-project-id"); - - MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.multipart(prefix + "/page") + request.setName("test-api-name"); + request.setMethod("test-api-method"); + request.setPath("test-api-path"); + request.setProtocol("test-api-protocol"); + request.setPos(1l); + request.setLatest(true); + request.setSyncEnable(true); + request.setStatus("test-api-status"); + request.setVersionId("test-api-version"); + request.setDeleted(false); + mockMvc.perform(MockMvcRequestBuilders.multipart(prefix + "/update") .contentType(MediaType.APPLICATION_JSON_VALUE) - .content(JsonUtils.toJSONString(request)) + .content(JSON.toJSONString(request)) .header(SessionConstants.HEADER_TOKEN, sessionId) .header(SessionConstants.CSRF_TOKEN, csrfToken)) - .andExpect(status().isOk()) - .andReturn(); + .andExpect(status().isOk()); - MockHttpServletResponse mockResponse = mvcResult.getResponse(); - - String returnData = mockResponse.getContentAsString(); - ResultHolder resultHolder = JsonUtils.parseObject(returnData, ResultHolder.class); - - //返回请求正常 - Assertions.assertNotNull(resultHolder); - - Pager returnPager = JSON.parseObject(JSON.toJSONString(resultHolder.getData()), Pager.class); - - //返回值不为空 - Assertions.assertNotNull(returnPager); - //返回值的页码和当前页码相同 - Assertions.assertEquals(returnPager.getCurrent(), current); - //返回的数据量不超过规定要返回的数据量相同 - Assertions.assertTrue(((List) returnPager.getList()).size() <= pageSize); } - //没有传入必填值 @Test - @Order(3) - public void testPageError() throws Exception { - // projectId为空 - ListRequestDTO request = new ListRequestDTO(); - request.setCurrent(1); - request.setPageSize(20); - mockMvc.perform(MockMvcRequestBuilders.multipart(prefix + "/page") - .contentType(MediaType.APPLICATION_JSON_VALUE) - .content(JsonUtils.toJSONString(request)) - .header(SessionConstants.HEADER_TOKEN, sessionId) - .header(SessionConstants.CSRF_TOKEN, csrfToken)) - .andExpect(status().isBadRequest()); + @Order(2) + public void testBatchUpdate() throws Exception { + LogUtils.info("delete api test"); + List tests = new ArrayList<>(); + tests.add("test-api-id"); - //pageSize为空 - request = new ListRequestDTO(); - request.setCurrent(1); - request.setProjectId("test-project-id"); - mockMvc.perform(MockMvcRequestBuilders.multipart(prefix + "/page") + mockMvc.perform(MockMvcRequestBuilders.multipart(prefix + "/batch-update") .contentType(MediaType.APPLICATION_JSON_VALUE) - .content(JsonUtils.toJSONString(request)) + .content(JSON.toJSONString(tests)) .header(SessionConstants.HEADER_TOKEN, sessionId) .header(SessionConstants.CSRF_TOKEN, csrfToken)) - .andExpect(status().isBadRequest()); + .andExpect(status().isOk()); - //current为空 - request = new ListRequestDTO(); - request.setPageSize(20); - request.setProjectId("test-project-id"); - mockMvc.perform(MockMvcRequestBuilders.multipart(prefix + "/page") - .contentType(MediaType.APPLICATION_JSON_VALUE) - .content(JsonUtils.toJSONString(request)) - .header(SessionConstants.HEADER_TOKEN, sessionId) - .header(SessionConstants.CSRF_TOKEN, csrfToken)) - .andExpect(status().isBadRequest()); } @Test @Order(4) + public void testDel() throws Exception { + LogUtils.info("delete api test"); + + mockMvc.perform(MockMvcRequestBuilders.multipart(prefix + "/delete") + .contentType(MediaType.APPLICATION_JSON_VALUE) + .content("test-api-id") + .header(SessionConstants.HEADER_TOKEN, sessionId) + .header(SessionConstants.CSRF_TOKEN, csrfToken)) + .andExpect(status().isOk()); + + } + + @Test + @Order(5) public void testBatchDel() throws Exception { LogUtils.info("delete api test"); List tests = new ArrayList<>(); @@ -183,4 +159,5 @@ public class ApiDefinitionControllerTests { .andExpect(status().isOk()); } + } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/AuthSourceController.java b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/AuthSourceController.java index c4b948fba8..eb9ca749d7 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/AuthSourceController.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/AuthSourceController.java @@ -5,12 +5,12 @@ import com.github.pagehelper.PageHelper; import io.metersphere.sdk.constants.PermissionConstants; import io.metersphere.sdk.dto.BasePageRequest; import io.metersphere.sdk.log.annotation.Log; -import io.metersphere.sdk.log.constants.OperationLogModule; import io.metersphere.sdk.log.constants.OperationLogType; import io.metersphere.sdk.util.PageUtils; import io.metersphere.sdk.util.Pager; import io.metersphere.system.domain.AuthSource; import io.metersphere.system.request.AuthSourceRequest; +import io.metersphere.system.service.AuthSourceLogService; import io.metersphere.system.service.AuthSourceService; import io.swagger.v3.oas.annotations.Operation; import jakarta.annotation.Resource; @@ -39,8 +39,7 @@ public class AuthSourceController { @PostMapping("/add") @Operation(summary = "新增认证设置") @RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ_CREAT) - @Log(type = OperationLogType.ADD, module = OperationLogModule.SYSTEM_PARAMETER_SETTING, - details = "认证设置") + @Log(type = OperationLogType.ADD, expression = "#msClass.addLog(#authSource)", msClass = AuthSourceLogService.class) public void add(@Validated @RequestBody AuthSourceRequest authSource) { authSourceService.addAuthSource(authSource); } @@ -48,8 +47,7 @@ public class AuthSourceController { @PostMapping("/update") @Operation(summary = "更新认证设置") @RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ_UPDATE) - @Log(type = OperationLogType.UPDATE, module = OperationLogModule.SYSTEM_PARAMETER_SETTING, - details = "认证设置", sourceId = "#authSource.id") + @Log(type = OperationLogType.UPDATE, expression = "#msClass.updateLog(#authSource)", msClass = AuthSourceLogService.class) public void update(@Validated @RequestBody AuthSourceRequest authSource) { authSourceService.updateAuthSource(authSource); } @@ -64,8 +62,7 @@ public class AuthSourceController { @GetMapping("/delete/{id}") @Operation(summary = "删除认证设置") @RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ_DELETE) - @Log(type = OperationLogType.DELETE, module = OperationLogModule.SYSTEM_PARAMETER_SETTING, - details = "认证设置", sourceId = "#id") + @Log(type = OperationLogType.DELETE, expression = "#msClass.deleteLog(#id)", msClass = AuthSourceLogService.class) public void delete(@PathVariable(value = "id") String id) { authSourceService.deleteAuthSource(id); } @@ -74,8 +71,7 @@ public class AuthSourceController { @GetMapping("/update/{authId}/status/{status}") @Operation(summary = "更新状态") @RequiresPermissions(PermissionConstants.SYSTEM_SETTING_READ_UPDATE) - @Log(type = OperationLogType.UPDATE, module = OperationLogModule.SYSTEM_PARAMETER_SETTING, - details = "认证设置", sourceId = "#authSource.authId") + @Log(type = OperationLogType.UPDATE, expression = "#msClass.updateLog(#authId)", msClass = AuthSourceLogService.class) public void updateStatus(@PathVariable(value = "authId") String authId, @PathVariable("status") String status) { authSourceService.updateStatus(authId, status); } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/GlobalUserRoleController.java b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/GlobalUserRoleController.java index c905083828..62e565c63b 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/GlobalUserRoleController.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/GlobalUserRoleController.java @@ -5,11 +5,11 @@ import io.metersphere.sdk.dto.PermissionDefinitionItem; import io.metersphere.sdk.dto.request.PermissionSettingUpdateRequest; import io.metersphere.sdk.dto.request.UserRoleUpdateRequest; import io.metersphere.sdk.log.annotation.Log; -import io.metersphere.sdk.log.constants.OperationLogModule; import io.metersphere.sdk.log.constants.OperationLogType; import io.metersphere.sdk.util.BeanUtils; import io.metersphere.sdk.util.SessionUtils; import io.metersphere.system.domain.UserRole; +import io.metersphere.system.service.GlobalUserRoleLogService; import io.metersphere.system.service.GlobalUserRoleService; import io.metersphere.validation.groups.Created; import io.metersphere.validation.groups.Updated; @@ -51,8 +51,7 @@ public class GlobalUserRoleController { @PostMapping("/permission/update") @Operation(summary = "编辑全局用户组对应的权限配置") @RequiresPermissions(PermissionConstants.SYSTEM_USER_ROLE_UPDATE) - @Log(type = OperationLogType.UPDATE, module = OperationLogModule.SYSTEM_USER_ROLE, - details = "#msClass.getLogDetails(#request.userRoleId)", msClass = GlobalUserRoleService.class) + @Log(type = OperationLogType.UPDATE, expression = "#msClass.updateLog(#request)", msClass = GlobalUserRoleLogService.class) public void updatePermissionSetting(@Validated @RequestBody PermissionSettingUpdateRequest request) { globalUserRoleService.updatePermissionSetting(request); } @@ -60,7 +59,7 @@ public class GlobalUserRoleController { @PostMapping("/add") @Operation(summary = "添加自定义全局用户组") @RequiresPermissions(PermissionConstants.SYSTEM_USER_ROLE_ADD) - @Log(type = OperationLogType.ADD, module = OperationLogModule.SYSTEM_USER_ROLE, details = "#request.name") + @Log(type = OperationLogType.ADD, expression = "#msClass.addLog(#request)", msClass = GlobalUserRoleLogService.class) public UserRole add(@Validated({Created.class}) @RequestBody UserRoleUpdateRequest request) { UserRole userRole = new UserRole(); userRole.setCreateUser(SessionUtils.getUserId()); @@ -71,8 +70,7 @@ public class GlobalUserRoleController { @PostMapping("/update") @Operation(summary = "更新自定义全局用户组") @RequiresPermissions(PermissionConstants.SYSTEM_USER_ROLE_UPDATE) - @Log(type = OperationLogType.UPDATE, module = OperationLogModule.SYSTEM_USER_ROLE, - sourceId = "#request.id", details = "#request.name") + @Log(type = OperationLogType.UPDATE, expression = "#msClass.updateLog(#request)", msClass = GlobalUserRoleLogService.class) public UserRole update(@Validated({Updated.class}) @RequestBody UserRoleUpdateRequest request) { UserRole userRole = new UserRole(); BeanUtils.copyBean(userRole, request); @@ -82,8 +80,7 @@ public class GlobalUserRoleController { @GetMapping("/delete/{id}") @Operation(summary = "删除自定义全局用户组") @RequiresPermissions(PermissionConstants.SYSTEM_USER_ROLE_DELETE) - @Log(isBefore = true, type = OperationLogType.DELETE, module = OperationLogModule.SYSTEM_USER_ROLE, - sourceId = "#id", details = "#msClass.getLogDetails(#id)", msClass = GlobalUserRoleService.class) + @Log(type = OperationLogType.DELETE, expression = "#msClass.deleteLog(#id)", msClass = GlobalUserRoleLogService.class) public void delete(@PathVariable String id) { globalUserRoleService.delete(id); } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/GlobalUserRoleRelationController.java b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/GlobalUserRoleRelationController.java index 1d1682ff2f..73bb7710d8 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/GlobalUserRoleRelationController.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/GlobalUserRoleRelationController.java @@ -6,7 +6,6 @@ import io.metersphere.sdk.constants.PermissionConstants; import io.metersphere.sdk.dto.UserRoleRelationUserDTO; import io.metersphere.sdk.dto.request.GlobalUserRoleRelationUpdateRequest; import io.metersphere.sdk.log.annotation.Log; -import io.metersphere.sdk.log.constants.OperationLogModule; import io.metersphere.sdk.log.constants.OperationLogType; import io.metersphere.sdk.util.BeanUtils; import io.metersphere.sdk.util.PageUtils; @@ -14,6 +13,7 @@ import io.metersphere.sdk.util.Pager; import io.metersphere.sdk.util.SessionUtils; import io.metersphere.system.domain.UserRoleRelation; import io.metersphere.system.dto.request.GlobalUserRoleRelationQueryRequest; +import io.metersphere.system.service.GlobalUserRoleRelationLogService; import io.metersphere.system.service.GlobalUserRoleRelationService; import io.metersphere.validation.groups.Created; import io.swagger.v3.oas.annotations.Operation; @@ -48,7 +48,7 @@ public class GlobalUserRoleRelationController { @PostMapping("/add") @Operation(summary = "创建全局用户组和用户的关联关系") @RequiresPermissions(PermissionConstants.SYSTEM_USER_ROLE_RELATION_ADD) - @Log(type = OperationLogType.ADD, module = OperationLogModule.SYSTEM_USER_ROLE_RELATION, details = "#request.name") + @Log(type = OperationLogType.ADD, expression = "#msClass.addLog(#request)", msClass = GlobalUserRoleRelationLogService.class) public UserRoleRelation add(@Validated({Created.class}) @RequestBody GlobalUserRoleRelationUpdateRequest request) { UserRoleRelation userRoleRelation = new UserRoleRelation(); BeanUtils.copyBean(userRoleRelation, request); @@ -59,8 +59,7 @@ public class GlobalUserRoleRelationController { @GetMapping("/delete/{id}") @Operation(summary = "删除全局用户组和用户的关联关系") @RequiresPermissions(PermissionConstants.SYSTEM_USER_ROLE_RELATION_DELETE) - @Log(isBefore = true, type = OperationLogType.DELETE, sourceId = "#id", module = OperationLogModule.SYSTEM_USER_ROLE_RELATION, - details = "#msClass.getLogDetails(#id)", msClass = GlobalUserRoleRelationService.class) + @Log(type = OperationLogType.DELETE, expression = "#msClass.deleteLog(#id)", msClass = GlobalUserRoleRelationLogService.class) public void delete(@PathVariable String id) { globalUserRoleRelationService.delete(id); } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/SystemParameterController.java b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/SystemParameterController.java index 422ef0870c..30fab6dddd 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/SystemParameterController.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/SystemParameterController.java @@ -5,7 +5,6 @@ import io.metersphere.dto.BaseSystemConfigDTO; import io.metersphere.sdk.constants.PermissionConstants; import io.metersphere.sdk.dto.EMailInfoDto; import io.metersphere.sdk.log.annotation.Log; -import io.metersphere.sdk.log.constants.OperationLogModule; import io.metersphere.sdk.log.constants.OperationLogType; import io.metersphere.sdk.service.SystemParameterService; import io.metersphere.system.domain.SystemParameter; @@ -32,7 +31,7 @@ public class SystemParameterController { @PostMapping("/save/base-info") @Operation(summary = "保存基本信息") @RequiresPermissions(value= {PermissionConstants.SYSTEM_SETTING_READ_UPDATE, PermissionConstants.SYSTEM_SETTING_READ_CREAT}, logical = Logical.OR) - @Log(type = OperationLogType.UPDATE, module = OperationLogModule.SYSTEM_PARAMETER_SETTING, details = "基本配置", sourceId = "#systemParameter.get(0).paramKey") + @Log(type = OperationLogType.ADD, expression = "#msClass.addLog(#systemParameter)", msClass = SystemParameterService.class) public void saveBaseParameter(@Validated @RequestBody List systemParameter) { systemParameterService.saveBaseInfo(systemParameter); } @@ -57,7 +56,7 @@ public class SystemParameterController { @PostMapping("/edit/email-info") @Operation(summary = "保存邮件信息") @RequiresPermissions(value= {PermissionConstants.SYSTEM_SETTING_READ_UPDATE, PermissionConstants.SYSTEM_SETTING_READ_CREAT}, logical = Logical.OR) - @Log(type = OperationLogType.UPDATE, module = OperationLogModule.SYSTEM_PARAMETER_SETTING, details = "邮件配置", sourceId = "#systemParameter.get(0).paramKey") + @Log(type = OperationLogType.UPDATE, expression = "#msClass.updateLog(#systemParameter)", msClass = SystemParameterService.class) public void editEMailInfo(@Validated @RequestBody List systemParameter) { systemParameterService.editEMailInfo(systemParameter); } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/SystemProjectController.java b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/SystemProjectController.java index 76819ddcf2..6e8c828229 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/SystemProjectController.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/SystemProjectController.java @@ -9,7 +9,6 @@ import io.metersphere.sdk.dto.AddProjectRequest; import io.metersphere.sdk.dto.ProjectDTO; import io.metersphere.sdk.dto.UpdateProjectRequest; import io.metersphere.sdk.log.annotation.Log; -import io.metersphere.sdk.log.constants.OperationLogModule; import io.metersphere.sdk.log.constants.OperationLogType; import io.metersphere.sdk.util.PageUtils; import io.metersphere.sdk.util.Pager; @@ -18,6 +17,7 @@ import io.metersphere.system.dto.UserExtend; import io.metersphere.system.request.ProjectAddMemberRequest; import io.metersphere.system.request.ProjectMemberRequest; import io.metersphere.system.request.ProjectRequest; +import io.metersphere.system.service.SystemProjectLogService; import io.metersphere.system.service.SystemProjectService; import io.metersphere.validation.groups.Created; import io.metersphere.validation.groups.Updated; @@ -39,7 +39,7 @@ public class SystemProjectController { @PostMapping("/add") @RequiresPermissions(PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ_ADD) - @Log(type = OperationLogType.ADD, module = OperationLogModule.SYSTEM_PROJECT, details = "#project.name") + @Log(type = OperationLogType.ADD, expression = "#msClass.addLog(#project)", msClass = SystemProjectLogService.class) @Operation(summary = "添加项目") public Project addProject(@RequestBody @Validated({Created.class}) AddProjectRequest project) { return systemProjectService.add(project, SessionUtils.getUserId()); @@ -63,8 +63,7 @@ public class SystemProjectController { } @PostMapping("/update") - @Log(type = OperationLogType.UPDATE, module = OperationLogModule.SYSTEM_PROJECT, - sourceId = "#project.id", details = "#project.name") + @Log(type = OperationLogType.UPDATE, expression = "#msClass.updateLog(#project)", msClass = SystemProjectLogService.class) @Operation(summary = "更新项目信息") @RequiresPermissions(PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ_UPDATE) public Project updateProject(@RequestBody @Validated({Updated.class}) UpdateProjectRequest project) { @@ -74,16 +73,14 @@ public class SystemProjectController { @GetMapping("/delete/{id}") @RequiresPermissions(PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ_DELETE) @Operation(summary = "删除项目") - @Log(isBefore = true, type = OperationLogType.DELETE, module = OperationLogModule.SYSTEM_PROJECT, - details = "#msClass.getLogDetails(#id)", msClass = SystemProjectService.class, sourceId = "#id") + @Log(type = OperationLogType.DELETE, expression = "#msClass.deleteLog(#id)", msClass = SystemProjectLogService.class) public int deleteProject(@PathVariable String id) { return systemProjectService.delete(id, SessionUtils.getUserId()); } @GetMapping("/revoke/{id}") @RequiresPermissions(PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ_RECOVER) - @Log(isBefore = true, type = OperationLogType.UPDATE, module = OperationLogModule.SYSTEM_PROJECT, - details = "#msClass.getLogDetails(#id)", msClass = SystemProjectService.class, sourceId = "#id") + @Log(type = OperationLogType.UPDATE, expression = "#msClass.updateLog(#project)", msClass = SystemProjectLogService.class) public int revokeProject(@PathVariable String id) { return systemProjectService.revoke(id); } @@ -107,8 +104,7 @@ public class SystemProjectController { @GetMapping("/remove-member/{projectId}/{userId}") @Operation(summary = "移除项目成员") @RequiresPermissions(PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ_UPDATE) - @Log(isBefore = true, type = OperationLogType.DELETE, module = OperationLogModule.SYSTEM_PROJECT_MEMBER, sourceId = "#projectId", - details = "#msClass.getLogs(#userId)", msClass = SystemProjectService.class) + @Log(type = OperationLogType.DELETE, expression = "#msClass.deleteLog(#projectId)", msClass = SystemProjectLogService.class) public int removeProjectMember(@PathVariable String projectId, @PathVariable String userId) { return systemProjectService.removeProjectMember(projectId, userId); } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/TestResourcePoolController.java b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/TestResourcePoolController.java index ad95e92fbc..d4b0b1c63a 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/TestResourcePoolController.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/TestResourcePoolController.java @@ -3,18 +3,17 @@ package io.metersphere.system.controller; import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; import io.metersphere.sdk.constants.PermissionConstants; +import io.metersphere.sdk.dto.QueryResourcePoolRequest; import io.metersphere.sdk.dto.TestResourcePoolDTO; import io.metersphere.sdk.dto.TestResourcePoolRequest; import io.metersphere.sdk.log.annotation.Log; -import io.metersphere.sdk.log.constants.OperationLogModule; import io.metersphere.sdk.log.constants.OperationLogType; +import io.metersphere.sdk.service.TestResourcePoolService; import io.metersphere.sdk.util.BeanUtils; import io.metersphere.sdk.util.PageUtils; import io.metersphere.sdk.util.Pager; import io.metersphere.sdk.util.SessionUtils; import io.metersphere.system.consul.CacheNode; -import io.metersphere.sdk.dto.QueryResourcePoolRequest; -import io.metersphere.sdk.service.TestResourcePoolService; import io.metersphere.system.domain.TestResourcePool; import jakarta.annotation.Resource; import org.apache.shiro.authz.annotation.RequiresPermissions; @@ -33,7 +32,7 @@ public class TestResourcePoolController { @PostMapping("/add") @RequiresPermissions(PermissionConstants.SYSTEM_TEST_RESOURCE_POOL_READ_ADD) - @Log(type = OperationLogType.ADD, module = OperationLogModule.SYSTEM_TEST_RESOURCE_POOL, details = "#request.name") + @Log(type = OperationLogType.ADD, expression = "#msClass.addLog(#request)", msClass = TestResourcePoolService.class) public TestResourcePool addTestResourcePool(@Validated @RequestBody TestResourcePoolRequest request) { String userId = SessionUtils.getUserId(); TestResourcePoolDTO testResourcePool = new TestResourcePoolDTO(); @@ -46,8 +45,7 @@ public class TestResourcePoolController { @GetMapping("/delete/{poolId}") @CacheNode // 把监控节点缓存起来 @RequiresPermissions(PermissionConstants.SYSTEM_TEST_RESOURCE_POOL_READ_DELETE) - @Log(isBefore = true, type = OperationLogType.DELETE, module = OperationLogModule.SYSTEM_TEST_RESOURCE_POOL, - details = "#msClass.getLogDetails(#testResourcePoolId)", msClass = TestResourcePoolService.class) + @Log(type = OperationLogType.DELETE, expression = "#msClass.deleteLog(#testResourcePoolId)", msClass = TestResourcePoolService.class) public void deleteTestResourcePool(@PathVariable(value = "poolId") String testResourcePoolId) { testResourcePoolService.deleteTestResourcePool(testResourcePoolId); } @@ -55,8 +53,7 @@ public class TestResourcePoolController { @PostMapping("/update") @CacheNode // 把监控节点缓存起来 @RequiresPermissions(PermissionConstants.SYSTEM_TEST_RESOURCE_POOL_READ_UPDATE) - @Log(type = OperationLogType.UPDATE, module = OperationLogModule.SYSTEM_TEST_RESOURCE_POOL, - sourceId = "#request.id", details = "#request.name") + @Log(type = OperationLogType.UPDATE, expression = "#msClass.updateLog(#request)", msClass = TestResourcePoolService.class) public void updateTestResourcePool(@Validated @RequestBody TestResourcePoolRequest request) { TestResourcePoolDTO testResourcePool = new TestResourcePoolDTO(); BeanUtils.copyBean(testResourcePool, request); diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/UserController.java b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/UserController.java index e48537763c..4822c5ec8d 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/UserController.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/UserController.java @@ -8,7 +8,6 @@ import io.metersphere.sdk.constants.UserSourceEnum; import io.metersphere.sdk.dto.BasePageRequest; import io.metersphere.sdk.dto.UserDTO; import io.metersphere.sdk.log.annotation.Log; -import io.metersphere.sdk.log.constants.OperationLogModule; import io.metersphere.sdk.log.constants.OperationLogType; import io.metersphere.sdk.util.PageUtils; import io.metersphere.sdk.util.Pager; @@ -61,8 +60,7 @@ public class UserController { @PostMapping("/update") @RequiresPermissions(PermissionConstants.SYSTEM_USER_READ_UPDATE) - @Log(type = OperationLogType.UPDATE, module = OperationLogModule.SYSTEM_USER, - sourceId = "#request.id", details = "#request.name") + @Log(type = OperationLogType.UPDATE, expression = "#msClass.updateLog(#request)", msClass = UserService.class) public UserEditRequest updateUser(@Validated({Updated.class}) @RequestBody UserEditRequest request) { return userService.updateUser(request, SessionUtils.getUserId()); } @@ -89,8 +87,7 @@ public class UserController { @PostMapping("/delete") - @Log(isBefore = true, type = OperationLogType.DELETE, module = OperationLogModule.SYSTEM_USER, isBatch = true, - details = "#msClass.getLogs(#userBatchProcessRequest)", msClass = UserService.class) + @Log(type = OperationLogType.DELETE, expression = "#msClass.deleteLog(#userBatchProcessRequest)", msClass = UserService.class) @RequiresPermissions(PermissionConstants.SYSTEM_USER_READ_DELETE) public UserBatchProcessResponse deleteUser(@Validated @RequestBody UserChangeEnableRequest userBatchProcessRequest) { return userService.deleteUser(userBatchProcessRequest.getUserIdList()); diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/AuthSourceLogService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/AuthSourceLogService.java new file mode 100644 index 0000000000..063a33c9c9 --- /dev/null +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/AuthSourceLogService.java @@ -0,0 +1,115 @@ +package io.metersphere.system.service; + + +import io.metersphere.constants.HttpMethodConstants; +import io.metersphere.sdk.dto.LogDTO; +import io.metersphere.sdk.log.constants.OperationLogModule; +import io.metersphere.sdk.log.constants.OperationLogType; +import io.metersphere.sdk.util.JSON; +import io.metersphere.system.domain.AuthSource; +import io.metersphere.system.mapper.AuthSourceMapper; +import io.metersphere.system.request.AuthSourceRequest; +import jakarta.annotation.Resource; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@Transactional(rollbackFor = Exception.class) +public class AuthSourceLogService { + @Resource + private AuthSourceMapper authSourceMapper; + + private static final String PRE_URI = "/system/authsource"; + + /** + * 添加接口日志 + * + * @param request + * @return + */ + public LogDTO addLog(AuthSourceRequest request) { + LogDTO dto = new LogDTO( + "system", + request.getId(), + null, + OperationLogType.ADD.name(), + OperationLogModule.SYSTEM_PARAMETER_SETTING, + request.getName()); + + dto.setPath(PRE_URI + "/add"); + dto.setMethod(HttpMethodConstants.POST.name()); + dto.setOriginalValue(JSON.toJSONBytes(request)); + return dto; + } + + /** + * 修改接口日志 + * + * @param request + * @return + */ + public LogDTO updateLog(AuthSourceRequest request) { + AuthSource authSource = authSourceMapper.selectByPrimaryKey(request.getId()); + if (authSource != null) { + LogDTO dto = new LogDTO( + "system", + request.getId(), + null, + OperationLogType.UPDATE.name(), + OperationLogModule.SYSTEM_PARAMETER_SETTING, + request.getName()); + + dto.setPath("/update"); + dto.setMethod(HttpMethodConstants.POST.name()); + dto.setOriginalValue(JSON.toJSONBytes(authSource)); + return dto; + } + return null; + } + + public LogDTO updateLog(String id) { + AuthSource authSource = authSourceMapper.selectByPrimaryKey(id); + if (authSource != null) { + LogDTO dto = new LogDTO( + "system", + id, + null, + OperationLogType.UPDATE.name(), + OperationLogModule.SYSTEM_PARAMETER_SETTING, + authSource.getName()); + + dto.setPath("/update"); + dto.setMethod(HttpMethodConstants.POST.name()); + dto.setOriginalValue(JSON.toJSONBytes(authSource)); + return dto; + } + return null; + } + + /** + * 删除接口日志 + * + * @param id + * @return + */ + public LogDTO deleteLog(String id) { + AuthSource authSource = authSourceMapper.selectByPrimaryKey(id); + if (authSource == null) { + return null; + } + LogDTO dto = new LogDTO( + "system", + authSource.getId(), + null, + OperationLogType.DELETE.name(), + OperationLogModule.SYSTEM_PARAMETER_SETTING, + authSource.getName()); + + dto.setPath("/delete"); + dto.setMethod(HttpMethodConstants.POST.name()); + + dto.setOriginalValue(JSON.toJSONBytes(authSource)); + return dto; + } + +} diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/GlobalUserRoleLogService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/GlobalUserRoleLogService.java new file mode 100644 index 0000000000..1caece5e11 --- /dev/null +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/GlobalUserRoleLogService.java @@ -0,0 +1,120 @@ +package io.metersphere.system.service; + +import io.metersphere.constants.HttpMethodConstants; +import io.metersphere.sdk.dto.LogDTO; +import io.metersphere.sdk.dto.request.PermissionSettingUpdateRequest; +import io.metersphere.sdk.dto.request.UserRoleUpdateRequest; +import io.metersphere.sdk.log.constants.OperationLogModule; +import io.metersphere.sdk.log.constants.OperationLogType; +import io.metersphere.sdk.service.BaseUserRoleService; +import io.metersphere.sdk.util.JSON; +import io.metersphere.system.domain.UserRole; +import jakarta.annotation.Resource; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +/** + * 系统设置的接口增删改查都是针对全局用户组 + * + * @author jianxing + * @date : 2023-6-8 + */ +@Service +@Transactional(rollbackFor = Exception.class) +public class GlobalUserRoleLogService extends BaseUserRoleService { + @Resource + private BaseUserRoleService baseUserRoleService; + + private static final String PRE_URI = "/user/role/global"; + + /** + * 添加接口日志 + * + * @param request + * @return + */ + public LogDTO addLog(UserRoleUpdateRequest request) { + LogDTO dto = new LogDTO( + "system", + request.getId(), + null, + OperationLogType.ADD.name(), + OperationLogModule.SYSTEM_PROJECT, + request.getName()); + + dto.setPath(PRE_URI + "/permission/update"); + dto.setMethod(HttpMethodConstants.POST.name()); + dto.setOriginalValue(JSON.toJSONBytes(request)); + return dto; + } + + /** + * @param request + * @return + */ + public LogDTO updateLog(UserRoleUpdateRequest request) { + UserRole userRole = baseUserRoleService.get(request.getId()); + if (userRole != null) { + LogDTO dto = new LogDTO( + "system", + userRole.getId(), + userRole.getCreateUser(), + OperationLogType.UPDATE.name(), + OperationLogModule.SYSTEM_PROJECT, + userRole.getName()); + + dto.setPath("/update"); + dto.setMethod(HttpMethodConstants.POST.name()); + dto.setOriginalValue(JSON.toJSONBytes(userRole)); + return dto; + } + return null; + } + + public LogDTO updateLog(PermissionSettingUpdateRequest request) { + UserRole userRole = get(request.getUserRoleId()); + if (userRole != null) { + LogDTO dto = new LogDTO( + "system", + request.getUserRoleId(), + userRole.getCreateUser(), + OperationLogType.UPDATE.name(), + OperationLogModule.SYSTEM_PROJECT, + userRole.getName()); + + dto.setPath("/update"); + dto.setMethod(HttpMethodConstants.POST.name()); + dto.setOriginalValue(JSON.toJSONBytes(request)); + return dto; + } + return null; + } + + + /** + * 删除接口日志 + * + * @param id + * @return + */ + public LogDTO deleteLog(String id) { + UserRole userRole = baseUserRoleService.get(id); + if (userRole == null) { + return null; + } + LogDTO dto = new LogDTO( + "system", + userRole.getId(), + null, + OperationLogType.DELETE.name(), + OperationLogModule.SYSTEM_PROJECT, + userRole.getName()); + + dto.setPath("/delete"); + dto.setMethod(HttpMethodConstants.POST.name()); + + dto.setOriginalValue(JSON.toJSONBytes(userRole)); + return dto; + } + +} diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/GlobalUserRoleRelationLogService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/GlobalUserRoleRelationLogService.java new file mode 100644 index 0000000000..5a31d0ba85 --- /dev/null +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/GlobalUserRoleRelationLogService.java @@ -0,0 +1,73 @@ +package io.metersphere.system.service; + +import io.metersphere.constants.HttpMethodConstants; +import io.metersphere.sdk.dto.LogDTO; +import io.metersphere.sdk.dto.request.GlobalUserRoleRelationUpdateRequest; +import io.metersphere.sdk.log.constants.OperationLogModule; +import io.metersphere.sdk.log.constants.OperationLogType; +import io.metersphere.sdk.service.BaseUserRoleRelationService; +import io.metersphere.sdk.util.JSON; +import io.metersphere.system.domain.UserRoleRelation; +import io.metersphere.system.mapper.UserRoleRelationMapper; +import jakarta.annotation.Resource; +import org.springframework.stereotype.Service; + +/** + * @author jianxing + * @date : 2023-6-12 + */ +@Service +public class GlobalUserRoleRelationLogService extends BaseUserRoleRelationService { + + @Resource + private UserRoleRelationMapper userRoleRelationMapper; + + private static final String PRE_URI = "/user/role/relation/global"; + + /** + * 添加接口日志 + * + * @param request + * @return + */ + public LogDTO addLog(GlobalUserRoleRelationUpdateRequest request) { + LogDTO dto = new LogDTO( + "system", + null, + null, + OperationLogType.ADD.name(), + OperationLogModule.SYSTEM_USER_ROLE_RELATION, + request.getUserId()); + + dto.setPath(PRE_URI + "/add"); + dto.setMethod(HttpMethodConstants.POST.name()); + dto.setOriginalValue(JSON.toJSONBytes(request)); + return dto; + } + + /** + * 删除接口日志 + * + * @param id + * @return + */ + public LogDTO deleteLog(String id) { + UserRoleRelation userRoleRelation = userRoleRelationMapper.selectByPrimaryKey(id); + if (userRoleRelation != null) { + LogDTO dto = new LogDTO( + "system", + id, + userRoleRelation.getCreateUser(), + OperationLogType.DELETE.name(), + OperationLogModule.SYSTEM_USER_ROLE_RELATION, + userRoleRelation.getUserId()); + + dto.setPath("/delete"); + dto.setMethod(HttpMethodConstants.POST.name()); + + dto.setOriginalValue(JSON.toJSONBytes(userRoleRelation)); + return dto; + } + return 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 new file mode 100644 index 0000000000..a5f31b92a9 --- /dev/null +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/SystemProjectLogService.java @@ -0,0 +1,112 @@ +package io.metersphere.system.service; + +import io.metersphere.constants.HttpMethodConstants; +import io.metersphere.project.domain.Project; +import io.metersphere.project.mapper.ProjectMapper; +import io.metersphere.sdk.dto.AddProjectRequest; +import io.metersphere.sdk.dto.LogDTO; +import io.metersphere.sdk.dto.UpdateProjectRequest; +import io.metersphere.sdk.log.constants.OperationLogModule; +import io.metersphere.sdk.log.constants.OperationLogType; +import io.metersphere.sdk.util.JSON; +import jakarta.annotation.Resource; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@Transactional(rollbackFor = Exception.class) +public class SystemProjectLogService { + + @Resource + private ProjectMapper projectMapper; + + private static final String PRE_URI = "/system/project"; + + /** + * 添加接口日志 + * + * @return + */ + public LogDTO addLog(AddProjectRequest project) { + LogDTO dto = new LogDTO( + "system", + null, + null, + OperationLogType.ADD.name(), + OperationLogModule.SYSTEM_USER_ROLE, + project.getName()); + + dto.setPath(PRE_URI + "/add"); + dto.setMethod(HttpMethodConstants.POST.name()); + dto.setOriginalValue(JSON.toJSONBytes(project)); + return dto; + } + + /** + * @param request + * @return + */ + public LogDTO updateLog(UpdateProjectRequest request) { + Project project = projectMapper.selectByPrimaryKey(request.getId()); + if (project != null) { + LogDTO dto = new LogDTO( + "system", + project.getId(), + project.getCreateUser(), + OperationLogType.UPDATE.name(), + OperationLogModule.SYSTEM_USER_ROLE, + "编辑全局用户组对应的权限配置"); + dto.setPath("/update"); + dto.setMethod(HttpMethodConstants.POST.name()); + + dto.setOriginalValue(JSON.toJSONBytes(project)); + return dto; + } + return null; + } + + public LogDTO updateLog(String id) { + Project project = projectMapper.selectByPrimaryKey(id); + if (project != null) { + LogDTO dto = new LogDTO( + "system", + project.getId(), + project.getCreateUser(), + OperationLogType.UPDATE.name(), + OperationLogModule.SYSTEM_USER_ROLE, + "编辑全局用户组对应的权限配置"); + dto.setPath("/revoke"); + dto.setMethod(HttpMethodConstants.POST.name()); + + dto.setOriginalValue(JSON.toJSONBytes(project)); + return dto; + } + return null; + } + + + /** + * 删除接口日志 + * + * @param id + * @return + */ + public LogDTO deleteLog(String id) { + Project project = projectMapper.selectByPrimaryKey(id); + if (project != null) { + LogDTO dto = new LogDTO( + "system", + id, + project.getCreateUser(), + OperationLogType.DELETE.name(), + OperationLogModule.SYSTEM_USER_ROLE, + project.getName()); + + dto.setPath("/delete"); + dto.setMethod(HttpMethodConstants.POST.name()); + dto.setOriginalValue(JSON.toJSONBytes(project)); + return dto; + } + return null; + } +} diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/SystemProjectService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/SystemProjectService.java index 1acbd1c8fe..59263f8365 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/SystemProjectService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/SystemProjectService.java @@ -8,11 +8,7 @@ import io.metersphere.sdk.dto.AddProjectRequest; import io.metersphere.sdk.dto.ProjectDTO; import io.metersphere.sdk.dto.UpdateProjectRequest; import io.metersphere.sdk.exception.MSException; -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.Translator; -import io.metersphere.system.domain.OperationLog; import io.metersphere.system.domain.User; import io.metersphere.system.domain.UserRoleRelation; import io.metersphere.system.domain.UserRoleRelationExample; @@ -25,14 +21,12 @@ import io.metersphere.system.request.ProjectMemberRequest; import io.metersphere.system.request.ProjectRequest; import io.metersphere.utils.LoggerUtil; import jakarta.annotation.Resource; -import jakarta.validation.Valid; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; 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; @@ -49,8 +43,6 @@ public class SystemProjectService { private UserRoleRelationMapper userRoleRelationMapper; @Resource private ExtSystemProjectMapper extSystemProjectMapper; - @Resource - private OperationLogService operationLogService; public Project get(String id) { return projectMapper.selectByPrimaryKey(id); @@ -189,8 +181,6 @@ public class SystemProjectService { createUser); userRoleRelationMapper.insertSelective(memberRole); }); - //写入操作日志 - operationLogService.batchAdd(this.getBatchAddLogs(request, createUser, OperationLogType.ADD.name(), "addMember")); } @@ -217,7 +207,6 @@ public class SystemProjectService { } public void deleteProject(List projects) { - List operationLogs = new ArrayList<>(); // 删除项目 projects.forEach(project -> { LoggerUtil.info("send delete_project message, project id: " + project.getId()); @@ -228,11 +217,7 @@ public class SystemProjectService { // delete project projectMapper.deleteByPrimaryKey(project.getId()); - //记录日志 - operationLogs.add(this.getDeleteLogs(project)); }); - operationLogService.batchAdd(operationLogs); - } private void deleteProjectUserGroup(String projectId) { @@ -240,65 +225,4 @@ public class SystemProjectService { userGroupExample.createCriteria().andSourceIdEqualTo(projectId); userRoleRelationMapper.deleteByExample(userGroupExample); } - public OperationLog getDeleteLogs(Project project) { - OperationLog log = new OperationLog(); - log.setId(UUID.randomUUID().toString()); - log.setProjectId(project.getId()); - log.setCreateUser("system"); - log.setType(OperationLogType.DELETE.name()); - log.setModule(OperationLogModule.SYSTEM_PROJECT); - log.setCreateTime(System.currentTimeMillis()); - log.setDetails(project.getName()); - log.setMethod("deleteProject"); - return log; - } - - public List getBatchAddLogs(@Valid ProjectAddMemberRequest request, - String createUser, - String operationType, - String operationMethod) { - long operationTime = System.currentTimeMillis(); - List logs = new ArrayList<>(); - request.getUserIds().forEach(userId -> { - User userInfo = userMapper.selectByPrimaryKey(userId); - OperationLog log = new OperationLog(); - log.setId(UUID.randomUUID().toString()); - log.setCreateUser(createUser); - log.setProjectId(request.getProjectId()); - log.setType(operationType); - log.setModule(OperationLogModule.SYSTEM_PROJECT_MEMBER); - log.setMethod(operationMethod); - log.setCreateTime(operationTime); - log.setSourceId(request.getProjectId()); - log.setDetails(userInfo.getName()); - logs.add(log); - }); - return logs; - } - public String getLogDetails(String id) { - Project project = projectMapper.selectByPrimaryKey(id); - if (ObjectUtils.isNotEmpty(project)) { - return project.getName(); - } - return null; - } - - public String getLogs(String userId) { - User user = userMapper.selectByPrimaryKey(userId); - if (ObjectUtils.isNotEmpty(user)) { - return user.getName(); - } - return null; - } - - /*public String getCreateLogDetail(@Validated AddProjectDTO request) { - List addMemberNames = new ArrayList<>(); - request.getUserIds().forEach(memberId -> { - User user = userMapper.selectByPrimaryKey(memberId); - addMemberNames.add(user.getName()); - }); - return request.getName() + StringUtils.SPACE + "并添加项目管理员" + StringUtils.SPACE - + StringUtils.join(addMemberNames, ","); - - }*/ } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/UserRoleRelationService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/UserRoleRelationService.java index 05e5d34895..e19597133e 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/UserRoleRelationService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/UserRoleRelationService.java @@ -1,9 +1,11 @@ package io.metersphere.system.service; import io.metersphere.sdk.constants.UserRoleEnum; +import io.metersphere.sdk.dto.LogDTO; 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.system.domain.*; import io.metersphere.system.dto.response.UserTableResponse; import io.metersphere.system.mapper.ExtUserRoleRelationMapper; @@ -46,18 +48,18 @@ public class UserRoleRelationService { private OperationLogService operationLogService; //批量添加用户记录日志 - public List getBatchLogs(@Valid @NotEmpty List userRoleId, - @Valid User user, - @Valid @NotEmpty String operationMethod, - @Valid @NotEmpty String operator, - @Valid @NotEmpty String operationType) { + public List getBatchLogs(@Valid @NotEmpty List userRoleId, + @Valid User user, + @Valid @NotEmpty String operationMethod, + @Valid @NotEmpty String operator, + @Valid @NotEmpty String operationType) { long operationTime = System.currentTimeMillis(); - List logs = new ArrayList<>(); + List logs = new ArrayList<>(); UserRoleExample example = new UserRoleExample(); example.createCriteria().andIdIn(userRoleId); List userRoleList = userRoleMapper.selectByExample(example); userRoleList.forEach(userRole -> { - OperationLog log = new OperationLog(); + LogDTO log = new LogDTO(); log.setId(UUID.randomUUID().toString()); log.setProjectId("system"); log.setType(operationType); @@ -66,10 +68,11 @@ public class UserRoleRelationService { log.setMethod(operationMethod); log.setCreateTime(operationTime); log.setSourceId(user.getId()); - log.setDetails(user.getName() + StringUtils.SPACE + log.setContent(user.getName() + StringUtils.SPACE + operationType + StringUtils.SPACE + "UserRole" + StringUtils.SPACE + userRole.getName()); + log.setOriginalValue(JSON.toJSONBytes(userRole)); logs.add(log); }); return logs; diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/UserService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/UserService.java index 4c67f890e3..72c531cc78 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/UserService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/UserService.java @@ -1,8 +1,10 @@ package io.metersphere.system.service; import com.alibaba.excel.EasyExcelFactory; +import io.metersphere.constants.HttpMethodConstants; import io.metersphere.sdk.dto.BasePageRequest; import io.metersphere.sdk.dto.ExcelParseDTO; +import io.metersphere.sdk.dto.LogDTO; import io.metersphere.sdk.dto.UserDTO; import io.metersphere.sdk.exception.MSException; import io.metersphere.sdk.log.constants.OperationLogModule; @@ -10,14 +12,12 @@ import io.metersphere.sdk.log.constants.OperationLogType; import io.metersphere.sdk.log.service.OperationLogService; import io.metersphere.sdk.mapper.BaseUserMapper; import io.metersphere.sdk.util.*; -import io.metersphere.system.domain.OperationLog; import io.metersphere.system.domain.User; import io.metersphere.system.domain.UserExample; import io.metersphere.system.dto.UserBatchCreateDTO; import io.metersphere.system.dto.UserCreateInfo; import io.metersphere.system.dto.excel.UserExcel; import io.metersphere.system.dto.excel.UserExcelRowDTO; -import io.metersphere.system.dto.request.UserBatchProcessRequest; import io.metersphere.system.dto.request.UserChangeEnableRequest; import io.metersphere.system.dto.request.UserEditRequest; import io.metersphere.system.dto.response.UserBatchProcessResponse; @@ -58,10 +58,10 @@ public class UserService { private UserRoleService userRoleService; //批量添加用户记录日志 - public List getBatchAddLogs(@Valid List userList) { - List logs = new ArrayList<>(); + public List getBatchAddLogs(@Valid List userList) { + List logs = new ArrayList<>(); userList.forEach(user -> { - OperationLog log = new OperationLog(); + LogDTO log = new LogDTO(); log.setId(UUID.randomUUID().toString()); log.setCreateUser(user.getCreateUser()); log.setProjectId("system"); @@ -70,7 +70,8 @@ public class UserService { log.setMethod("addUser"); log.setCreateTime(user.getCreateTime()); log.setSourceId(user.getId()); - log.setDetails(user.getName() + "(" + user.getEmail() + ")"); + log.setContent(user.getName() + "(" + user.getEmail() + ")"); + log.setOriginalValue(JSON.toJSONBytes(user)); logs.add(log); }); return logs; @@ -82,26 +83,6 @@ public class UserService { return userMapper.selectByExample(example); } - //切面方法调用:获取接口日志 - public List getLogs(UserBatchProcessRequest request) { - List logs = new ArrayList<>(); - List userList = this.selectByIdList(request.getUserIdList()); - userList.forEach(user -> { - OperationLog log = new OperationLog(); - log.setId(UUID.randomUUID().toString()); - log.setCreateUser(SessionUtils.getUserId()); - log.setProjectId("system"); - log.setType(OperationLogType.DELETE.name()); - log.setModule(OperationLogModule.SYSTEM_USER); - log.setCreateTime(System.currentTimeMillis()); - log.setMethod("deleteUser"); - log.setSourceId(user.getId()); - log.setDetails(user.getName()); - logs.add(log); - }); - return logs; - } - private void validateUserInfo(List userList) { //判断参数内是否含有重复邮箱 List emailList = new ArrayList<>(); @@ -303,4 +284,46 @@ public class UserService { userRoleRelationService.deleteByUserIdList(userIdList); return response; } + + public LogDTO updateLog(UserEditRequest request) { + User user = userMapper.selectByPrimaryKey(request.getId()); + if (user != null) { + LogDTO dto = new LogDTO( + "system", + request.getId(), + null, + OperationLogType.UPDATE.name(), + OperationLogModule.SYSTEM_USER, + request.getName()); + + dto.setPath("/update"); + dto.setMethod(HttpMethodConstants.POST.name()); + dto.setOriginalValue(JSON.toJSONBytes(user)); + return dto; + } + return null; + } + + public List deleteLog(UserChangeEnableRequest request) { + List logDTOList = new ArrayList<>(); + request.getUserIdList().forEach(item -> { + User user = userMapper.selectByPrimaryKey(item); + if (user != null) { + + LogDTO dto = new LogDTO( + "system", + user.getId(), + user.getCreateUser(), + OperationLogType.DELETE.name(), + OperationLogModule.SYSTEM_PROJECT, + user.getName()); + + dto.setPath("/delete"); + dto.setMethod(HttpMethodConstants.POST.name()); + dto.setOriginalValue(JSON.toJSONBytes(user)); + logDTOList.add(dto); + } + }); + return logDTOList; + } }