diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/constants/OperationLogConstants.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/constants/OperationLogConstants.java new file mode 100644 index 0000000000..dc93e042aa --- /dev/null +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/constants/OperationLogConstants.java @@ -0,0 +1,5 @@ +package io.metersphere.sdk.constants; + +public class OperationLogConstants { + public static final String SYSTEM = "SYSTEM"; +} diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/OptionDTO.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/OptionDTO.java new file mode 100644 index 0000000000..2b209c64fd --- /dev/null +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/OptionDTO.java @@ -0,0 +1,9 @@ +package io.metersphere.sdk.dto; + +import lombok.Data; + +@Data +public class OptionDTO { + private String id; + private String name; +} diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/request/GlobalUserRoleRelationUpdateRequest.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/request/GlobalUserRoleRelationUpdateRequest.java index d68ccb07da..6fb4a3887e 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/request/GlobalUserRoleRelationUpdateRequest.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/request/GlobalUserRoleRelationUpdateRequest.java @@ -3,10 +3,14 @@ package io.metersphere.sdk.dto.request; import io.metersphere.validation.groups.Created; import io.metersphere.validation.groups.Updated; import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.Valid; import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotEmpty; import jakarta.validation.constraints.Size; import lombok.Data; +import java.util.List; + /** * @author jianxing */ @@ -14,12 +18,19 @@ import lombok.Data; public class GlobalUserRoleRelationUpdateRequest { @Schema(title = "用户ID", requiredMode = Schema.RequiredMode.REQUIRED) - @NotBlank(message = "{user_role_relation.user_id.not_blank}", groups = {Created.class}) - @Size(min = 1, max = 50, message = "{user_role_relation.user_id.length_range}", groups = {Created.class, Updated.class}) - private String userId; + @NotEmpty(message = "{user_role_relation.user_id.not_blank}", groups = {Created.class, Updated.class}) + @Valid + private List< + @NotBlank(message = "{user_role_relation.user_id.not_blank}", groups = {Created.class, Updated.class}) + @Size(min = 1, max = 50, message = "{user_role_relation.user_id.length_range}", groups = {Created.class, Updated.class}) + String + > userIds; @Schema(title = "组ID", requiredMode = Schema.RequiredMode.REQUIRED) @NotBlank(message = "{user_role_relation.role_id.not_blank}", groups = {Created.class}) @Size(min = 1, max = 50, message = "{user_role_relation.role_id.length_range}", groups = {Created.class, Updated.class}) private String roleId; + + @Schema(hidden = true) + private String createUser; } 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 36fb3d716f..5635d876fd 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 @@ -8,6 +8,7 @@ import io.metersphere.sdk.util.JSON; import io.metersphere.sdk.util.LogUtils; import io.metersphere.sdk.util.SessionUtils; 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; @@ -22,12 +23,12 @@ 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.RequestAttributes; +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.*; import java.util.stream.Collectors; /** @@ -223,6 +224,8 @@ public class OperationLogAspect { logDTO.setCreateUser(StringUtils.defaultIfBlank(logDTO.getCreateUser(), localUser.get())); logDTO.setOrganizationId(StringUtils.defaultIfBlank(logDTO.getOrganizationId(), localOrganizationId.get())); logDTO.setProjectId(StringUtils.defaultIfBlank(logDTO.getProjectId(), localProjectId.get())); + logDTO.setMethod(getMethod()); + logDTO.setPath(getPath()); }); // 单条存储 @@ -273,4 +276,23 @@ public class OperationLogAspect { } } + + protected String getPath() { + HttpServletRequest httpRequest = getHttpRequest(); + String path = httpRequest == null ? StringUtils.EMPTY : httpRequest.getRequestURI(); + return path.length() > 255 ? path.substring(0, 255) : path; + } + + protected String getMethod() { + HttpServletRequest httpRequest = getHttpRequest(); + return httpRequest == null ? StringUtils.EMPTY : httpRequest.getMethod(); + } + + private HttpServletRequest getHttpRequest() { + RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); + if (requestAttributes == null) { + return null; + } + return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); + } } 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 e719f62ee0..aab41efff2 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 @@ -3,13 +3,13 @@ package io.metersphere.sdk.log.service; import io.metersphere.project.domain.Project; import io.metersphere.sdk.domain.OperationLogBlob; import io.metersphere.sdk.dto.LogDTO; +import io.metersphere.sdk.dto.OptionDTO; import io.metersphere.sdk.exception.MSException; import io.metersphere.sdk.log.vo.OperationLogRequest; import io.metersphere.sdk.log.vo.OperationLogResponse; import io.metersphere.sdk.mapper.*; import io.metersphere.sdk.util.Translator; import io.metersphere.system.domain.Organization; -import io.metersphere.system.domain.User; import jakarta.annotation.Resource; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; @@ -105,8 +105,8 @@ public class OperationLogService { List userIds = list.stream().map(OperationLogResponse::getCreateUser).collect(Collectors.toList()); List projectIds = list.stream().map(OperationLogResponse::getProjectId).collect(Collectors.toList()); List organizationIds = list.stream().map(OperationLogResponse::getOrganizationId).collect(Collectors.toList()); - List userList = baseUserMapper.selectUserByIdList(userIds); - Map userMap = userList.stream().collect(Collectors.toMap(User::getId, User::getName)); + List userList = baseUserMapper.selectUserOptionByIds(userIds); + Map userMap = userList.stream().collect(Collectors.toMap(OptionDTO::getId, OptionDTO::getName)); List projects = baseProjectMapper.selectProjectByIdList(projectIds); Map projectMap = projects.stream().collect(Collectors.toMap(Project::getId, Project::getName)); List organizations = baseOrganizationMapper.selectOrganizationByIdList(organizationIds); diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/BaseUserMapper.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/BaseUserMapper.java index 5ba183550b..64e482f098 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/BaseUserMapper.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/BaseUserMapper.java @@ -1,5 +1,6 @@ package io.metersphere.sdk.mapper; +import io.metersphere.sdk.dto.OptionDTO; import io.metersphere.sdk.dto.UserDTO; import io.metersphere.system.domain.User; import org.apache.ibatis.annotations.Param; @@ -25,7 +26,6 @@ public interface BaseUserMapper { List selectUnDeletedUserIdByIdList(@Param("idList") List userIdList); - List selectUserByIdList(List userIds); - long deleteUser(String id); + List selectUserOptionByIds(List userIds); } diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/BaseUserMapper.xml b/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/BaseUserMapper.xml index 32920ebb04..43e861a4b2 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/BaseUserMapper.xml +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/BaseUserMapper.xml @@ -71,7 +71,7 @@ - SELECT id, name FROM user WHERE id IN diff --git a/backend/framework/sdk/src/test/java/base/param/NotEmptyParamGenerator.java b/backend/framework/sdk/src/test/java/base/param/NotEmptyParamGenerator.java new file mode 100644 index 0000000000..38b50ac30d --- /dev/null +++ b/backend/framework/sdk/src/test/java/base/param/NotEmptyParamGenerator.java @@ -0,0 +1,19 @@ +package base.param; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Field; +import java.util.ArrayList; + +/** + * @author jianxing + */ +public class NotEmptyParamGenerator extends ParamGenerator { + + /** + * 生成空字符串 + */ + @Override + public Object invalidGenerate(Annotation annotation, Field field) { + return new ArrayList<>(0); + } +} diff --git a/backend/framework/sdk/src/test/java/base/param/ParamGeneratorFactory.java b/backend/framework/sdk/src/test/java/base/param/ParamGeneratorFactory.java index 4eacd33074..79c610e9a8 100644 --- a/backend/framework/sdk/src/test/java/base/param/ParamGeneratorFactory.java +++ b/backend/framework/sdk/src/test/java/base/param/ParamGeneratorFactory.java @@ -24,6 +24,7 @@ public class ParamGeneratorFactory { private static final Map paramGeneratorMap = new HashMap<>() {{ put(Size.class.getName(), new SizeParamGenerator()); put(NotBlank.class.getName(), new NotBlankParamGenerator()); + put(NotEmpty.class.getName(), new NotEmptyParamGenerator()); put(NotNull.class.getName(), new NotNullParamGenerator()); put(EnumValue.class.getName(), new EnumValueParamGenerator()); put(Min.class.getName(), new MinParamGenerator()); 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 60a6e7f0b8..6f299d1dcf 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 @@ -7,11 +7,9 @@ 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.OperationLogType; -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.domain.UserRoleRelation; import io.metersphere.system.dto.request.GlobalUserRoleRelationQueryRequest; import io.metersphere.system.service.GlobalUserRoleRelationLogService; import io.metersphere.system.service.GlobalUserRoleRelationService; @@ -49,11 +47,9 @@ public class GlobalUserRoleRelationController { @Operation(summary = "创建全局用户组和用户的关联关系") @RequiresPermissions(PermissionConstants.SYSTEM_USER_ROLE_UPDATE) @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); - userRoleRelation.setCreateUser(SessionUtils.getUserId()); - return globalUserRoleRelationService.add(userRoleRelation); + public void add(@Validated({Created.class}) @RequestBody GlobalUserRoleRelationUpdateRequest request) { + request.setCreateUser(SessionUtils.getUserId()); + globalUserRoleRelationService.add(request); } @GetMapping("/delete/{id}") 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 index 3de6a82010..0bf0195ca0 100644 --- 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 @@ -1,6 +1,7 @@ package io.metersphere.system.service; -import io.metersphere.sdk.constants.HttpMethodConstants; + +import io.metersphere.sdk.constants.OperationLogConstants; import io.metersphere.sdk.dto.LogDTO; import io.metersphere.sdk.log.constants.OperationLogModule; import io.metersphere.sdk.log.constants.OperationLogType; @@ -18,8 +19,6 @@ public class AuthSourceLogService { @Resource private AuthSourceMapper authSourceMapper; - private static final String PRE_URI = "/system/authsource"; - /** * 添加接口日志 * @@ -28,16 +27,14 @@ public class AuthSourceLogService { */ public LogDTO addLog(AuthSourceRequest request) { LogDTO dto = new LogDTO( - "system", - "", + OperationLogConstants.SYSTEM, + OperationLogConstants.SYSTEM, request.getId(), null, OperationLogType.ADD.name(), OperationLogModule.SYSTEM_PARAMETER_SETTING, request.getName()); - dto.setPath(PRE_URI + "/add"); - dto.setMethod(HttpMethodConstants.POST.name()); dto.setModifiedValue(JSON.toJSONBytes(request)); return dto; } @@ -50,42 +47,38 @@ public class AuthSourceLogService { */ public LogDTO updateLog(AuthSourceRequest request) { AuthSource authSource = authSourceMapper.selectByPrimaryKey(request.getId()); + LogDTO dto = null; if (authSource != null) { - LogDTO dto = new LogDTO( - "system", - "", + dto = new LogDTO( + OperationLogConstants.SYSTEM, + OperationLogConstants.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; + return dto; } public LogDTO updateLog(String id) { AuthSource authSource = authSourceMapper.selectByPrimaryKey(id); + LogDTO dto = null; if (authSource != null) { - LogDTO dto = new LogDTO( - "system", - "", + dto = new LogDTO( + OperationLogConstants.SYSTEM, + OperationLogConstants.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; + return dto; } /** @@ -100,17 +93,14 @@ public class AuthSourceLogService { return null; } LogDTO dto = new LogDTO( - "system", - "", + OperationLogConstants.SYSTEM, + OperationLogConstants.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 index 0748ec2dd2..e9067461e9 100644 --- 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 @@ -1,6 +1,6 @@ package io.metersphere.system.service; -import io.metersphere.sdk.constants.HttpMethodConstants; +import io.metersphere.sdk.constants.OperationLogConstants; import io.metersphere.sdk.dto.LogDTO; import io.metersphere.sdk.dto.request.PermissionSettingUpdateRequest; import io.metersphere.sdk.dto.request.UserRoleUpdateRequest; @@ -21,12 +21,10 @@ import org.springframework.transaction.annotation.Transactional; */ @Service @Transactional(rollbackFor = Exception.class) -public class GlobalUserRoleLogService extends BaseUserRoleService { +public class GlobalUserRoleLogService { @Resource private BaseUserRoleService baseUserRoleService; - private static final String PRE_URI = "/user/role/global"; - /** * 添加接口日志 * @@ -35,16 +33,14 @@ public class GlobalUserRoleLogService extends BaseUserRoleService { */ public LogDTO addLog(UserRoleUpdateRequest request) { LogDTO dto = new LogDTO( - "system", - "", - request.getId(), + OperationLogConstants.SYSTEM, + OperationLogConstants.SYSTEM, + null, 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; } @@ -55,42 +51,38 @@ public class GlobalUserRoleLogService extends BaseUserRoleService { */ public LogDTO updateLog(UserRoleUpdateRequest request) { UserRole userRole = baseUserRoleService.get(request.getId()); + LogDTO dto = null; if (userRole != null) { - LogDTO dto = new LogDTO( - "system", - "", + dto = new LogDTO( + OperationLogConstants.SYSTEM, + OperationLogConstants.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; + return dto; } public LogDTO updateLog(PermissionSettingUpdateRequest request) { - UserRole userRole = get(request.getUserRoleId()); + UserRole userRole = baseUserRoleService.get(request.getUserRoleId()); + LogDTO dto = null; if (userRole != null) { - LogDTO dto = new LogDTO( - "system", - "", + dto = new LogDTO( + OperationLogConstants.SYSTEM, + OperationLogConstants.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; + return dto; } @@ -106,17 +98,14 @@ public class GlobalUserRoleLogService extends BaseUserRoleService { return null; } LogDTO dto = new LogDTO( - "system", - "", + OperationLogConstants.SYSTEM, + OperationLogConstants.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 index 23c997cd6f..64171a341d 100644 --- 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 @@ -1,28 +1,36 @@ package io.metersphere.system.service; -import io.metersphere.sdk.constants.HttpMethodConstants; +import io.metersphere.sdk.constants.OperationLogConstants; import io.metersphere.sdk.dto.LogDTO; +import io.metersphere.sdk.dto.OptionDTO; +import io.metersphere.sdk.dto.UserDTO; 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.mapper.BaseUserMapper; import io.metersphere.sdk.util.JSON; +import io.metersphere.system.domain.UserRole; import io.metersphere.system.domain.UserRoleRelation; +import io.metersphere.system.mapper.UserRoleMapper; import io.metersphere.system.mapper.UserRoleRelationMapper; import jakarta.annotation.Resource; import org.springframework.stereotype.Service; +import java.util.List; + /** * @author jianxing * @date : 2023-6-12 */ @Service -public class GlobalUserRoleRelationLogService extends BaseUserRoleRelationService { +public class GlobalUserRoleRelationLogService { @Resource private UserRoleRelationMapper userRoleRelationMapper; - - private static final String PRE_URI = "/user/role/relation/global"; + @Resource + private BaseUserMapper baseUserMapper; + @Resource + private UserRoleMapper userRoleMapper; /** * 添加接口日志 @@ -31,18 +39,19 @@ public class GlobalUserRoleRelationLogService extends BaseUserRoleRelationServic * @return */ public LogDTO addLog(GlobalUserRoleRelationUpdateRequest request) { + UserRole userRole = userRoleMapper.selectByPrimaryKey(request.getRoleId()); + List userIds = request.getUserIds(); + List users = baseUserMapper.selectUserOptionByIds(userIds); LogDTO dto = new LogDTO( - "system", - "", - null, + OperationLogConstants.SYSTEM, + OperationLogConstants.SYSTEM, + userRole.getId(), null, OperationLogType.ADD.name(), OperationLogModule.SYSTEM_USER_ROLE_RELATION, - request.getUserId()); + userRole.getName()); - dto.setPath(PRE_URI + "/add"); - dto.setMethod(HttpMethodConstants.POST.name()); - dto.setOriginalValue(JSON.toJSONBytes(request)); + dto.setOriginalValue(JSON.toJSONBytes(users)); return dto; } @@ -54,22 +63,22 @@ public class GlobalUserRoleRelationLogService extends BaseUserRoleRelationServic */ 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()); + UserRole userRole = userRoleMapper.selectByPrimaryKey(userRoleRelation.getRoleId()); + LogDTO dto = new LogDTO( + OperationLogConstants.SYSTEM, + OperationLogConstants.SYSTEM, + userRole.getId(), + null, + OperationLogType.DELETE.name(), + OperationLogModule.SYSTEM_USER_ROLE_RELATION, + userRole.getName()); - dto.setPath("/delete"); - dto.setMethod(HttpMethodConstants.POST.name()); - - dto.setOriginalValue(JSON.toJSONBytes(userRoleRelation)); - return dto; - } - return null; + UserDTO userDTO = baseUserMapper.selectById(userRoleRelation.getUserId()); + OptionDTO optionDTO = new OptionDTO(); + optionDTO.setId(userDTO.getId()); + optionDTO.setName(userDTO.getName()); + // 记录用户id和name + dto.setOriginalValue(JSON.toJSONBytes(optionDTO)); + return dto; } } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/GlobalUserRoleRelationService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/GlobalUserRoleRelationService.java index 7c1e010471..1512f8909a 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/GlobalUserRoleRelationService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/GlobalUserRoleRelationService.java @@ -1,12 +1,13 @@ package io.metersphere.system.service; import io.metersphere.sdk.dto.UserRoleRelationUserDTO; +import io.metersphere.sdk.dto.request.GlobalUserRoleRelationUpdateRequest; import io.metersphere.sdk.service.BaseUserRoleRelationService; +import io.metersphere.sdk.util.BeanUtils; import io.metersphere.system.domain.UserRole; import io.metersphere.system.domain.UserRoleRelation; import io.metersphere.system.dto.request.GlobalUserRoleRelationQueryRequest; import io.metersphere.system.mapper.ExtUserRoleRelationMapper; -import io.metersphere.system.mapper.UserRoleRelationMapper; import jakarta.annotation.Resource; import org.springframework.stereotype.Service; @@ -18,9 +19,6 @@ import java.util.List; */ @Service public class GlobalUserRoleRelationService extends BaseUserRoleRelationService { - - @Resource - private UserRoleRelationMapper userRoleRelationMapper; @Resource private ExtUserRoleRelationMapper extUserRoleRelationMapper; @Resource @@ -33,13 +31,17 @@ public class GlobalUserRoleRelationService extends BaseUserRoleRelationService { return extUserRoleRelationMapper.listGlobal(request); } - @Override - public UserRoleRelation add(UserRoleRelation userRoleRelation) { - UserRole userRole = globalUserRoleService.get(userRoleRelation.getRoleId()); + public void add(GlobalUserRoleRelationUpdateRequest request) { + UserRole userRole = globalUserRoleService.get(request.getRoleId()); globalUserRoleService.checkSystemUserGroup(userRole); globalUserRoleService.checkGlobalUserRole(userRole); - userRoleRelation.setSourceId(GlobalUserRoleService.SYSTEM_TYPE); - return super.add(userRoleRelation); + request.getUserIds().forEach(userId -> { + UserRoleRelation userRoleRelation = new UserRoleRelation(); + BeanUtils.copyBean(userRoleRelation, request); + userRoleRelation.setUserId(userId); + userRoleRelation.setSourceId(GlobalUserRoleService.SYSTEM_TYPE); + super.add(userRoleRelation); + }); } @Override diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/OrganizationUserRoleLogService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/OrganizationUserRoleLogService.java index 20e32ee761..2c6b84ca07 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/OrganizationUserRoleLogService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/OrganizationUserRoleLogService.java @@ -1,6 +1,5 @@ package io.metersphere.system.service; -import io.metersphere.sdk.constants.HttpMethodConstants; import io.metersphere.sdk.dto.LogDTO; import io.metersphere.sdk.dto.request.PermissionSettingUpdateRequest; import io.metersphere.sdk.log.constants.OperationLogModule; @@ -21,8 +20,6 @@ public class OrganizationUserRoleLogService { @Resource private UserRoleMapper userRoleMapper; - private static final String PRE_URI = "/user/role/organization"; - /** * 新增组织-用户组 * @param request 接口请求参数 @@ -37,8 +34,7 @@ public class OrganizationUserRoleLogService { OperationLogType.ADD.name(), OperationLogModule.ORGANIZATION_USER_ROLE, request.getName()); - dto.setPath(PRE_URI + "/add"); - dto.setMethod(HttpMethodConstants.POST.name()); + dto.setOriginalValue(JSON.toJSONBytes(request)); return dto; } @@ -57,8 +53,7 @@ public class OrganizationUserRoleLogService { OperationLogType.UPDATE.name(), OperationLogModule.ORGANIZATION_USER_ROLE, request.getName()); - dto.setPath(PRE_URI + "/update"); - dto.setMethod(HttpMethodConstants.POST.name()); + dto.setOriginalValue(JSON.toJSONBytes(request)); return dto; } @@ -78,8 +73,7 @@ public class OrganizationUserRoleLogService { OperationLogType.DELETE.name(), OperationLogModule.ORGANIZATION_USER_ROLE, userRole.getName()); - dto.setPath(PRE_URI + "/delete"); - dto.setMethod(HttpMethodConstants.GET.name()); + dto.setOriginalValue(JSON.toJSONBytes(userRole)); return dto; } @@ -92,8 +86,6 @@ public class OrganizationUserRoleLogService { public LogDTO updatePermissionSettingLog(PermissionSettingUpdateRequest request) { LogDTO dto = getLog(request.getUserRoleId()); dto.setType(OperationLogType.UPDATE.name()); - dto.setPath(PRE_URI + "/update"); - dto.setMethod(HttpMethodConstants.POST.name()); dto.setOriginalValue(JSON.toJSONBytes(request)); return dto; } @@ -106,8 +98,6 @@ public class OrganizationUserRoleLogService { public LogDTO editMemberLog(OrganizationUserRoleMemberEditRequest request) { LogDTO dto = getLog(request.getUserRoleId()); dto.setType(OperationLogType.UPDATE.name()); - dto.setPath(PRE_URI + "/update"); - dto.setMethod(HttpMethodConstants.POST.name()); dto.setOriginalValue(JSON.toJSONBytes(request)); return dto; } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/SystemProjectLogService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/SystemProjectLogService.java index ba2596bdd2..7d924e5f6e 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/SystemProjectLogService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/SystemProjectLogService.java @@ -2,7 +2,7 @@ package io.metersphere.system.service; import io.metersphere.project.domain.Project; import io.metersphere.project.mapper.ProjectMapper; -import io.metersphere.sdk.constants.HttpMethodConstants; +import io.metersphere.sdk.constants.OperationLogConstants; import io.metersphere.sdk.dto.AddProjectRequest; import io.metersphere.sdk.dto.LogDTO; import io.metersphere.sdk.dto.UpdateProjectRequest; @@ -20,8 +20,6 @@ public class SystemProjectLogService { @Resource private ProjectMapper projectMapper; - private static final String PRE_URI = "/system/project"; - /** * 添加接口日志 * @@ -29,16 +27,14 @@ public class SystemProjectLogService { */ public LogDTO addLog(AddProjectRequest project) { LogDTO dto = new LogDTO( - "system", - "", + OperationLogConstants.SYSTEM, + OperationLogConstants.SYSTEM, null, null, OperationLogType.ADD.name(), OperationLogModule.SYSTEM_PROJECT, project.getName()); - dto.setPath(PRE_URI + "/add"); - dto.setMethod(HttpMethodConstants.POST.name()); dto.setOriginalValue(JSON.toJSONBytes(project)); return dto; } @@ -51,15 +47,13 @@ public class SystemProjectLogService { Project project = projectMapper.selectByPrimaryKey(request.getId()); if (project != null) { LogDTO dto = new LogDTO( - "system", - "", + OperationLogConstants.SYSTEM, + OperationLogConstants.SYSTEM, project.getId(), project.getCreateUser(), OperationLogType.UPDATE.name(), OperationLogModule.SYSTEM_PROJECT, "编辑全局用户组对应的权限配置"); - dto.setPath("/update"); - dto.setMethod(HttpMethodConstants.POST.name()); dto.setOriginalValue(JSON.toJSONBytes(project)); return dto; @@ -71,15 +65,13 @@ public class SystemProjectLogService { Project project = projectMapper.selectByPrimaryKey(id); if (project != null) { LogDTO dto = new LogDTO( - "system", - "", + OperationLogConstants.SYSTEM, + OperationLogConstants.SYSTEM, project.getId(), project.getCreateUser(), OperationLogType.RECOVER.name(), OperationLogModule.SYSTEM_PROJECT, "编辑全局用户组对应的权限配置"); - dto.setPath("/revoke"); - dto.setMethod(HttpMethodConstants.GET.name()); dto.setOriginalValue(JSON.toJSONBytes(project)); return dto; @@ -98,16 +90,14 @@ public class SystemProjectLogService { Project project = projectMapper.selectByPrimaryKey(id); if (project != null) { LogDTO dto = new LogDTO( - "system", - "", + OperationLogConstants.SYSTEM, + OperationLogConstants.SYSTEM, id, project.getCreateUser(), OperationLogType.DELETE.name(), OperationLogModule.SYSTEM_PROJECT, project.getName()); - dto.setPath("/delete"); - dto.setMethod(HttpMethodConstants.GET.name()); dto.setOriginalValue(JSON.toJSONBytes(project)); return dto; } diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/GlobalUserRoleRelationControllerTests.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/GlobalUserRoleRelationControllerTests.java index 87c16a05fd..0b9b932ca8 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/GlobalUserRoleRelationControllerTests.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/GlobalUserRoleRelationControllerTests.java @@ -21,6 +21,7 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMock import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.web.servlet.MvcResult; +import java.util.Arrays; import java.util.List; import java.util.Set; import java.util.UUID; @@ -98,32 +99,32 @@ class GlobalUserRoleRelationControllerTests extends BaseTest { // @@请求成功 GlobalUserRoleRelationUpdateRequest request = new GlobalUserRoleRelationUpdateRequest(); - request.setUserId(ADMIN.getValue()); + request.setUserIds(Arrays.asList(ADMIN.getValue())); request.setRoleId(nonInternalUserRole.getId()); this.requestPostWithOk(DEFAULT_ADD, request); UserRoleRelationExample example = new UserRoleRelationExample(); example.createCriteria() .andRoleIdEqualTo(request.getRoleId()) - .andUserIdEqualTo(request.getUserId()); + .andUserIdEqualTo(ADMIN.getValue()); Assertions.assertTrue(CollectionUtils.isNotEmpty(userRoleRelationMapper.selectByExample(example))); addUserRoleRelation = userRoleRelationMapper.selectByExample(example).get(0); // @@校验日志 - checkLog(addUserRoleRelation.getId(), OperationLogType.ADD); + checkLog(addUserRoleRelation.getRoleId(), OperationLogType.ADD); // @@重复添加校验 - request.setUserId(ADMIN.getValue()); + request.setUserIds(Arrays.asList(ADMIN.getValue())); request.setRoleId(ADMIN.getValue()); assertErrorCode(this.requestPost(DEFAULT_ADD, request), USER_ROLE_RELATION_EXIST); // @@操作非系统用户组异常 - request.setUserId(ADMIN.getValue()); + request.setUserIds(Arrays.asList(ADMIN.getValue())); request.setRoleId(ORG_ADMIN.getValue()); assertErrorCode(this.requestPost(DEFAULT_ADD, request), GLOBAL_USER_ROLE_RELATION_SYSTEM_PERMISSION); // @@操作非全局用户组异常 UserRole nonGlobalUserRole = getNonGlobalUserRole(); - request.setUserId(ADMIN.getValue()); + request.setUserIds(Arrays.asList(ADMIN.getValue())); request.setRoleId(nonGlobalUserRole.getId()); assertErrorCode(this.requestPost(DEFAULT_ADD, request), GLOBAL_USER_ROLE_PERMISSION); @@ -143,7 +144,7 @@ class GlobalUserRoleRelationControllerTests extends BaseTest { Assertions.assertNull(userRoleRelation); // @@校验日志 - checkLog(addUserRoleRelation.getId(), OperationLogType.DELETE); + checkLog(addUserRoleRelation.getRoleId(), OperationLogType.DELETE); // @@操作非系统级别用户组异常 assertErrorCode(this.requestGet(DEFAULT_DELETE, getNonSystemUserRoleRelation().getId()), diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/param/GlobalUserRoleRelationUpdateRequestDefinition.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/param/GlobalUserRoleRelationUpdateRequestDefinition.java index 93711d59e2..9abc2ac285 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/param/GlobalUserRoleRelationUpdateRequestDefinition.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/param/GlobalUserRoleRelationUpdateRequestDefinition.java @@ -3,18 +3,23 @@ package io.metersphere.system.controller.param; import io.metersphere.validation.groups.Created; import io.metersphere.validation.groups.Updated; import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotEmpty; import jakarta.validation.constraints.Size; import lombok.Data; +import java.util.List; + /** * @author jianxing */ @Data public class GlobalUserRoleRelationUpdateRequestDefinition { - @NotBlank(groups = {Created.class}) - @Size(min = 1, max = 50, groups = {Created.class, Updated.class}) - private String userId; + @NotEmpty(groups = {Created.class, Updated.class}) + private List< + @NotBlank(groups = {Created.class, Updated.class}) + @Size(groups = {Created.class, Updated.class}) + String> userIds; @NotBlank(groups = {Created.class}) @Size(min = 1, max = 50, groups = {Created.class, Updated.class})