fix(系统设置): 用户组添加用户接口参数和操作日志优化

--task=1012267 --user=陈建星 用户组管理-后台 https://www.tapd.cn/55049933/s/1398948
This commit is contained in:
jianxing 2023-07-28 16:04:34 +08:00 committed by fit2-zhao
parent 407039162c
commit f7f380d0b3
18 changed files with 193 additions and 154 deletions

View File

@ -0,0 +1,5 @@
package io.metersphere.sdk.constants;
public class OperationLogConstants {
public static final String SYSTEM = "SYSTEM";
}

View File

@ -0,0 +1,9 @@
package io.metersphere.sdk.dto;
import lombok.Data;
@Data
public class OptionDTO {
private String id;
private String name;
}

View File

@ -3,10 +3,14 @@ package io.metersphere.sdk.dto.request;
import io.metersphere.validation.groups.Created; import io.metersphere.validation.groups.Created;
import io.metersphere.validation.groups.Updated; import io.metersphere.validation.groups.Updated;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.Size; import jakarta.validation.constraints.Size;
import lombok.Data; import lombok.Data;
import java.util.List;
/** /**
* @author jianxing * @author jianxing
*/ */
@ -14,12 +18,19 @@ import lombok.Data;
public class GlobalUserRoleRelationUpdateRequest { public class GlobalUserRoleRelationUpdateRequest {
@Schema(title = "用户ID", requiredMode = Schema.RequiredMode.REQUIRED) @Schema(title = "用户ID", requiredMode = Schema.RequiredMode.REQUIRED)
@NotBlank(message = "{user_role_relation.user_id.not_blank}", groups = {Created.class}) @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}) @Size(min = 1, max = 50, message = "{user_role_relation.user_id.length_range}", groups = {Created.class, Updated.class})
private String userId; String
> userIds;
@Schema(title = "组ID", requiredMode = Schema.RequiredMode.REQUIRED) @Schema(title = "组ID", requiredMode = Schema.RequiredMode.REQUIRED)
@NotBlank(message = "{user_role_relation.role_id.not_blank}", groups = {Created.class}) @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}) @Size(min = 1, max = 50, message = "{user_role_relation.role_id.length_range}", groups = {Created.class, Updated.class})
private String roleId; private String roleId;
@Schema(hidden = true)
private String createUser;
} }

View File

@ -8,6 +8,7 @@ import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.util.LogUtils; import io.metersphere.sdk.util.LogUtils;
import io.metersphere.sdk.util.SessionUtils; import io.metersphere.sdk.util.SessionUtils;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils; 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.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext; import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.stereotype.Component; 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.lang.reflect.Method;
import java.util.ArrayList; import java.util.*;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
@ -223,6 +224,8 @@ public class OperationLogAspect {
logDTO.setCreateUser(StringUtils.defaultIfBlank(logDTO.getCreateUser(), localUser.get())); logDTO.setCreateUser(StringUtils.defaultIfBlank(logDTO.getCreateUser(), localUser.get()));
logDTO.setOrganizationId(StringUtils.defaultIfBlank(logDTO.getOrganizationId(), localOrganizationId.get())); logDTO.setOrganizationId(StringUtils.defaultIfBlank(logDTO.getOrganizationId(), localOrganizationId.get()));
logDTO.setProjectId(StringUtils.defaultIfBlank(logDTO.getProjectId(), localProjectId.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();
}
} }

View File

@ -3,13 +3,13 @@ package io.metersphere.sdk.log.service;
import io.metersphere.project.domain.Project; import io.metersphere.project.domain.Project;
import io.metersphere.sdk.domain.OperationLogBlob; import io.metersphere.sdk.domain.OperationLogBlob;
import io.metersphere.sdk.dto.LogDTO; import io.metersphere.sdk.dto.LogDTO;
import io.metersphere.sdk.dto.OptionDTO;
import io.metersphere.sdk.exception.MSException; import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.log.vo.OperationLogRequest; import io.metersphere.sdk.log.vo.OperationLogRequest;
import io.metersphere.sdk.log.vo.OperationLogResponse; import io.metersphere.sdk.log.vo.OperationLogResponse;
import io.metersphere.sdk.mapper.*; import io.metersphere.sdk.mapper.*;
import io.metersphere.sdk.util.Translator; import io.metersphere.sdk.util.Translator;
import io.metersphere.system.domain.Organization; import io.metersphere.system.domain.Organization;
import io.metersphere.system.domain.User;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -105,8 +105,8 @@ public class OperationLogService {
List<String> userIds = list.stream().map(OperationLogResponse::getCreateUser).collect(Collectors.toList()); List<String> userIds = list.stream().map(OperationLogResponse::getCreateUser).collect(Collectors.toList());
List<String> projectIds = list.stream().map(OperationLogResponse::getProjectId).collect(Collectors.toList()); List<String> projectIds = list.stream().map(OperationLogResponse::getProjectId).collect(Collectors.toList());
List<String> organizationIds = list.stream().map(OperationLogResponse::getOrganizationId).collect(Collectors.toList()); List<String> organizationIds = list.stream().map(OperationLogResponse::getOrganizationId).collect(Collectors.toList());
List<User> userList = baseUserMapper.selectUserByIdList(userIds); List<OptionDTO> userList = baseUserMapper.selectUserOptionByIds(userIds);
Map<String, String> userMap = userList.stream().collect(Collectors.toMap(User::getId, User::getName)); Map<String, String> userMap = userList.stream().collect(Collectors.toMap(OptionDTO::getId, OptionDTO::getName));
List<Project> projects = baseProjectMapper.selectProjectByIdList(projectIds); List<Project> projects = baseProjectMapper.selectProjectByIdList(projectIds);
Map<String, String> projectMap = projects.stream().collect(Collectors.toMap(Project::getId, Project::getName)); Map<String, String> projectMap = projects.stream().collect(Collectors.toMap(Project::getId, Project::getName));
List<Organization> organizations = baseOrganizationMapper.selectOrganizationByIdList(organizationIds); List<Organization> organizations = baseOrganizationMapper.selectOrganizationByIdList(organizationIds);

View File

@ -1,5 +1,6 @@
package io.metersphere.sdk.mapper; package io.metersphere.sdk.mapper;
import io.metersphere.sdk.dto.OptionDTO;
import io.metersphere.sdk.dto.UserDTO; import io.metersphere.sdk.dto.UserDTO;
import io.metersphere.system.domain.User; import io.metersphere.system.domain.User;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
@ -25,7 +26,6 @@ public interface BaseUserMapper {
List<String> selectUnDeletedUserIdByIdList(@Param("idList") List<String> userIdList); List<String> selectUnDeletedUserIdByIdList(@Param("idList") List<String> userIdList);
List<User> selectUserByIdList(List<String> userIds);
long deleteUser(String id); long deleteUser(String id);
List<OptionDTO> selectUserOptionByIds(List<String> userIds);
} }

View File

@ -71,7 +71,7 @@
</foreach> </foreach>
</select> </select>
<select id="selectUserByIdList" resultType="io.metersphere.system.domain.User"> <select id="selectUserOptionByIds" resultType="io.metersphere.sdk.dto.OptionDTO">
SELECT id, name SELECT id, name
FROM user FROM user
WHERE id IN WHERE id IN

View File

@ -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);
}
}

View File

@ -24,6 +24,7 @@ public class ParamGeneratorFactory {
private static final Map<String, ParamGenerator> paramGeneratorMap = new HashMap<>() {{ private static final Map<String, ParamGenerator> paramGeneratorMap = new HashMap<>() {{
put(Size.class.getName(), new SizeParamGenerator()); put(Size.class.getName(), new SizeParamGenerator());
put(NotBlank.class.getName(), new NotBlankParamGenerator()); put(NotBlank.class.getName(), new NotBlankParamGenerator());
put(NotEmpty.class.getName(), new NotEmptyParamGenerator());
put(NotNull.class.getName(), new NotNullParamGenerator()); put(NotNull.class.getName(), new NotNullParamGenerator());
put(EnumValue.class.getName(), new EnumValueParamGenerator()); put(EnumValue.class.getName(), new EnumValueParamGenerator());
put(Min.class.getName(), new MinParamGenerator()); put(Min.class.getName(), new MinParamGenerator());

View File

@ -7,11 +7,9 @@ import io.metersphere.sdk.dto.UserRoleRelationUserDTO;
import io.metersphere.sdk.dto.request.GlobalUserRoleRelationUpdateRequest; import io.metersphere.sdk.dto.request.GlobalUserRoleRelationUpdateRequest;
import io.metersphere.sdk.log.annotation.Log; import io.metersphere.sdk.log.annotation.Log;
import io.metersphere.sdk.log.constants.OperationLogType; import io.metersphere.sdk.log.constants.OperationLogType;
import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.sdk.util.PageUtils; import io.metersphere.sdk.util.PageUtils;
import io.metersphere.sdk.util.Pager; import io.metersphere.sdk.util.Pager;
import io.metersphere.sdk.util.SessionUtils; import io.metersphere.sdk.util.SessionUtils;
import io.metersphere.system.domain.UserRoleRelation;
import io.metersphere.system.dto.request.GlobalUserRoleRelationQueryRequest; import io.metersphere.system.dto.request.GlobalUserRoleRelationQueryRequest;
import io.metersphere.system.service.GlobalUserRoleRelationLogService; import io.metersphere.system.service.GlobalUserRoleRelationLogService;
import io.metersphere.system.service.GlobalUserRoleRelationService; import io.metersphere.system.service.GlobalUserRoleRelationService;
@ -49,11 +47,9 @@ public class GlobalUserRoleRelationController {
@Operation(summary = "创建全局用户组和用户的关联关系") @Operation(summary = "创建全局用户组和用户的关联关系")
@RequiresPermissions(PermissionConstants.SYSTEM_USER_ROLE_UPDATE) @RequiresPermissions(PermissionConstants.SYSTEM_USER_ROLE_UPDATE)
@Log(type = OperationLogType.ADD, expression = "#msClass.addLog(#request)", msClass = GlobalUserRoleRelationLogService.class) @Log(type = OperationLogType.ADD, expression = "#msClass.addLog(#request)", msClass = GlobalUserRoleRelationLogService.class)
public UserRoleRelation add(@Validated({Created.class}) @RequestBody GlobalUserRoleRelationUpdateRequest request) { public void add(@Validated({Created.class}) @RequestBody GlobalUserRoleRelationUpdateRequest request) {
UserRoleRelation userRoleRelation = new UserRoleRelation(); request.setCreateUser(SessionUtils.getUserId());
BeanUtils.copyBean(userRoleRelation, request); globalUserRoleRelationService.add(request);
userRoleRelation.setCreateUser(SessionUtils.getUserId());
return globalUserRoleRelationService.add(userRoleRelation);
} }
@GetMapping("/delete/{id}") @GetMapping("/delete/{id}")

View File

@ -1,6 +1,7 @@
package io.metersphere.system.service; 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.LogDTO;
import io.metersphere.sdk.log.constants.OperationLogModule; import io.metersphere.sdk.log.constants.OperationLogModule;
import io.metersphere.sdk.log.constants.OperationLogType; import io.metersphere.sdk.log.constants.OperationLogType;
@ -18,8 +19,6 @@ public class AuthSourceLogService {
@Resource @Resource
private AuthSourceMapper authSourceMapper; private AuthSourceMapper authSourceMapper;
private static final String PRE_URI = "/system/authsource";
/** /**
* 添加接口日志 * 添加接口日志
* *
@ -28,16 +27,14 @@ public class AuthSourceLogService {
*/ */
public LogDTO addLog(AuthSourceRequest request) { public LogDTO addLog(AuthSourceRequest request) {
LogDTO dto = new LogDTO( LogDTO dto = new LogDTO(
"system", OperationLogConstants.SYSTEM,
"", OperationLogConstants.SYSTEM,
request.getId(), request.getId(),
null, null,
OperationLogType.ADD.name(), OperationLogType.ADD.name(),
OperationLogModule.SYSTEM_PARAMETER_SETTING, OperationLogModule.SYSTEM_PARAMETER_SETTING,
request.getName()); request.getName());
dto.setPath(PRE_URI + "/add");
dto.setMethod(HttpMethodConstants.POST.name());
dto.setModifiedValue(JSON.toJSONBytes(request)); dto.setModifiedValue(JSON.toJSONBytes(request));
return dto; return dto;
} }
@ -50,42 +47,38 @@ public class AuthSourceLogService {
*/ */
public LogDTO updateLog(AuthSourceRequest request) { public LogDTO updateLog(AuthSourceRequest request) {
AuthSource authSource = authSourceMapper.selectByPrimaryKey(request.getId()); AuthSource authSource = authSourceMapper.selectByPrimaryKey(request.getId());
LogDTO dto = null;
if (authSource != null) { if (authSource != null) {
LogDTO dto = new LogDTO( dto = new LogDTO(
"system", OperationLogConstants.SYSTEM,
"", OperationLogConstants.SYSTEM,
request.getId(), request.getId(),
null, null,
OperationLogType.UPDATE.name(), OperationLogType.UPDATE.name(),
OperationLogModule.SYSTEM_PARAMETER_SETTING, OperationLogModule.SYSTEM_PARAMETER_SETTING,
request.getName()); request.getName());
dto.setPath("/update");
dto.setMethod(HttpMethodConstants.POST.name());
dto.setOriginalValue(JSON.toJSONBytes(authSource)); dto.setOriginalValue(JSON.toJSONBytes(authSource));
return dto;
} }
return null; return dto;
} }
public LogDTO updateLog(String id) { public LogDTO updateLog(String id) {
AuthSource authSource = authSourceMapper.selectByPrimaryKey(id); AuthSource authSource = authSourceMapper.selectByPrimaryKey(id);
LogDTO dto = null;
if (authSource != null) { if (authSource != null) {
LogDTO dto = new LogDTO( dto = new LogDTO(
"system", OperationLogConstants.SYSTEM,
"", OperationLogConstants.SYSTEM,
id, id,
null, null,
OperationLogType.UPDATE.name(), OperationLogType.UPDATE.name(),
OperationLogModule.SYSTEM_PARAMETER_SETTING, OperationLogModule.SYSTEM_PARAMETER_SETTING,
authSource.getName()); authSource.getName());
dto.setPath("/update");
dto.setMethod(HttpMethodConstants.POST.name());
dto.setOriginalValue(JSON.toJSONBytes(authSource)); dto.setOriginalValue(JSON.toJSONBytes(authSource));
return dto;
} }
return null; return dto;
} }
/** /**
@ -100,17 +93,14 @@ public class AuthSourceLogService {
return null; return null;
} }
LogDTO dto = new LogDTO( LogDTO dto = new LogDTO(
"system", OperationLogConstants.SYSTEM,
"", OperationLogConstants.SYSTEM,
authSource.getId(), authSource.getId(),
null, null,
OperationLogType.DELETE.name(), OperationLogType.DELETE.name(),
OperationLogModule.SYSTEM_PARAMETER_SETTING, OperationLogModule.SYSTEM_PARAMETER_SETTING,
authSource.getName()); authSource.getName());
dto.setPath("/delete");
dto.setMethod(HttpMethodConstants.POST.name());
dto.setOriginalValue(JSON.toJSONBytes(authSource)); dto.setOriginalValue(JSON.toJSONBytes(authSource));
return dto; return dto;
} }

View File

@ -1,6 +1,6 @@
package io.metersphere.system.service; 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.LogDTO;
import io.metersphere.sdk.dto.request.PermissionSettingUpdateRequest; import io.metersphere.sdk.dto.request.PermissionSettingUpdateRequest;
import io.metersphere.sdk.dto.request.UserRoleUpdateRequest; import io.metersphere.sdk.dto.request.UserRoleUpdateRequest;
@ -21,12 +21,10 @@ import org.springframework.transaction.annotation.Transactional;
*/ */
@Service @Service
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public class GlobalUserRoleLogService extends BaseUserRoleService { public class GlobalUserRoleLogService {
@Resource @Resource
private BaseUserRoleService baseUserRoleService; 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) { public LogDTO addLog(UserRoleUpdateRequest request) {
LogDTO dto = new LogDTO( LogDTO dto = new LogDTO(
"system", OperationLogConstants.SYSTEM,
"", OperationLogConstants.SYSTEM,
request.getId(), null,
null, null,
OperationLogType.ADD.name(), OperationLogType.ADD.name(),
OperationLogModule.SYSTEM_PROJECT, OperationLogModule.SYSTEM_PROJECT,
request.getName()); request.getName());
dto.setPath(PRE_URI + "/permission/update");
dto.setMethod(HttpMethodConstants.POST.name());
dto.setOriginalValue(JSON.toJSONBytes(request)); dto.setOriginalValue(JSON.toJSONBytes(request));
return dto; return dto;
} }
@ -55,42 +51,38 @@ public class GlobalUserRoleLogService extends BaseUserRoleService {
*/ */
public LogDTO updateLog(UserRoleUpdateRequest request) { public LogDTO updateLog(UserRoleUpdateRequest request) {
UserRole userRole = baseUserRoleService.get(request.getId()); UserRole userRole = baseUserRoleService.get(request.getId());
LogDTO dto = null;
if (userRole != null) { if (userRole != null) {
LogDTO dto = new LogDTO( dto = new LogDTO(
"system", OperationLogConstants.SYSTEM,
"", OperationLogConstants.SYSTEM,
userRole.getId(), userRole.getId(),
userRole.getCreateUser(), userRole.getCreateUser(),
OperationLogType.UPDATE.name(), OperationLogType.UPDATE.name(),
OperationLogModule.SYSTEM_PROJECT, OperationLogModule.SYSTEM_PROJECT,
userRole.getName()); userRole.getName());
dto.setPath("/update");
dto.setMethod(HttpMethodConstants.POST.name());
dto.setOriginalValue(JSON.toJSONBytes(userRole)); dto.setOriginalValue(JSON.toJSONBytes(userRole));
return dto;
} }
return null; return dto;
} }
public LogDTO updateLog(PermissionSettingUpdateRequest request) { public LogDTO updateLog(PermissionSettingUpdateRequest request) {
UserRole userRole = get(request.getUserRoleId()); UserRole userRole = baseUserRoleService.get(request.getUserRoleId());
LogDTO dto = null;
if (userRole != null) { if (userRole != null) {
LogDTO dto = new LogDTO( dto = new LogDTO(
"system", OperationLogConstants.SYSTEM,
"", OperationLogConstants.SYSTEM,
request.getUserRoleId(), request.getUserRoleId(),
userRole.getCreateUser(), userRole.getCreateUser(),
OperationLogType.UPDATE.name(), OperationLogType.UPDATE.name(),
OperationLogModule.SYSTEM_PROJECT, OperationLogModule.SYSTEM_PROJECT,
userRole.getName()); userRole.getName());
dto.setPath("/update");
dto.setMethod(HttpMethodConstants.POST.name());
dto.setOriginalValue(JSON.toJSONBytes(request)); dto.setOriginalValue(JSON.toJSONBytes(request));
return dto;
} }
return null; return dto;
} }
@ -106,17 +98,14 @@ public class GlobalUserRoleLogService extends BaseUserRoleService {
return null; return null;
} }
LogDTO dto = new LogDTO( LogDTO dto = new LogDTO(
"system", OperationLogConstants.SYSTEM,
"", OperationLogConstants.SYSTEM,
userRole.getId(), userRole.getId(),
null, null,
OperationLogType.DELETE.name(), OperationLogType.DELETE.name(),
OperationLogModule.SYSTEM_PROJECT, OperationLogModule.SYSTEM_PROJECT,
userRole.getName()); userRole.getName());
dto.setPath("/delete");
dto.setMethod(HttpMethodConstants.POST.name());
dto.setOriginalValue(JSON.toJSONBytes(userRole)); dto.setOriginalValue(JSON.toJSONBytes(userRole));
return dto; return dto;
} }

View File

@ -1,28 +1,36 @@
package io.metersphere.system.service; 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.LogDTO;
import io.metersphere.sdk.dto.OptionDTO;
import io.metersphere.sdk.dto.UserDTO;
import io.metersphere.sdk.dto.request.GlobalUserRoleRelationUpdateRequest; import io.metersphere.sdk.dto.request.GlobalUserRoleRelationUpdateRequest;
import io.metersphere.sdk.log.constants.OperationLogModule; import io.metersphere.sdk.log.constants.OperationLogModule;
import io.metersphere.sdk.log.constants.OperationLogType; 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.sdk.util.JSON;
import io.metersphere.system.domain.UserRole;
import io.metersphere.system.domain.UserRoleRelation; import io.metersphere.system.domain.UserRoleRelation;
import io.metersphere.system.mapper.UserRoleMapper;
import io.metersphere.system.mapper.UserRoleRelationMapper; import io.metersphere.system.mapper.UserRoleRelationMapper;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List;
/** /**
* @author jianxing * @author jianxing
* @date : 2023-6-12 * @date : 2023-6-12
*/ */
@Service @Service
public class GlobalUserRoleRelationLogService extends BaseUserRoleRelationService { public class GlobalUserRoleRelationLogService {
@Resource @Resource
private UserRoleRelationMapper userRoleRelationMapper; private UserRoleRelationMapper userRoleRelationMapper;
@Resource
private static final String PRE_URI = "/user/role/relation/global"; private BaseUserMapper baseUserMapper;
@Resource
private UserRoleMapper userRoleMapper;
/** /**
* 添加接口日志 * 添加接口日志
@ -31,18 +39,19 @@ public class GlobalUserRoleRelationLogService extends BaseUserRoleRelationServic
* @return * @return
*/ */
public LogDTO addLog(GlobalUserRoleRelationUpdateRequest request) { public LogDTO addLog(GlobalUserRoleRelationUpdateRequest request) {
UserRole userRole = userRoleMapper.selectByPrimaryKey(request.getRoleId());
List<String> userIds = request.getUserIds();
List<OptionDTO> users = baseUserMapper.selectUserOptionByIds(userIds);
LogDTO dto = new LogDTO( LogDTO dto = new LogDTO(
"system", OperationLogConstants.SYSTEM,
"", OperationLogConstants.SYSTEM,
null, userRole.getId(),
null, null,
OperationLogType.ADD.name(), OperationLogType.ADD.name(),
OperationLogModule.SYSTEM_USER_ROLE_RELATION, OperationLogModule.SYSTEM_USER_ROLE_RELATION,
request.getUserId()); userRole.getName());
dto.setPath(PRE_URI + "/add"); dto.setOriginalValue(JSON.toJSONBytes(users));
dto.setMethod(HttpMethodConstants.POST.name());
dto.setOriginalValue(JSON.toJSONBytes(request));
return dto; return dto;
} }
@ -54,22 +63,22 @@ public class GlobalUserRoleRelationLogService extends BaseUserRoleRelationServic
*/ */
public LogDTO deleteLog(String id) { public LogDTO deleteLog(String id) {
UserRoleRelation userRoleRelation = userRoleRelationMapper.selectByPrimaryKey(id); UserRoleRelation userRoleRelation = userRoleRelationMapper.selectByPrimaryKey(id);
if (userRoleRelation != null) { UserRole userRole = userRoleMapper.selectByPrimaryKey(userRoleRelation.getRoleId());
LogDTO dto = new LogDTO( LogDTO dto = new LogDTO(
"system", OperationLogConstants.SYSTEM,
"", OperationLogConstants.SYSTEM,
id, userRole.getId(),
userRoleRelation.getCreateUser(), null,
OperationLogType.DELETE.name(), OperationLogType.DELETE.name(),
OperationLogModule.SYSTEM_USER_ROLE_RELATION, OperationLogModule.SYSTEM_USER_ROLE_RELATION,
userRoleRelation.getUserId()); userRole.getName());
dto.setPath("/delete"); UserDTO userDTO = baseUserMapper.selectById(userRoleRelation.getUserId());
dto.setMethod(HttpMethodConstants.POST.name()); OptionDTO optionDTO = new OptionDTO();
optionDTO.setId(userDTO.getId());
dto.setOriginalValue(JSON.toJSONBytes(userRoleRelation)); optionDTO.setName(userDTO.getName());
// 记录用户id和name
dto.setOriginalValue(JSON.toJSONBytes(optionDTO));
return dto; return dto;
} }
return null;
}
} }

View File

@ -1,12 +1,13 @@
package io.metersphere.system.service; package io.metersphere.system.service;
import io.metersphere.sdk.dto.UserRoleRelationUserDTO; import io.metersphere.sdk.dto.UserRoleRelationUserDTO;
import io.metersphere.sdk.dto.request.GlobalUserRoleRelationUpdateRequest;
import io.metersphere.sdk.service.BaseUserRoleRelationService; import io.metersphere.sdk.service.BaseUserRoleRelationService;
import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.system.domain.UserRole; import io.metersphere.system.domain.UserRole;
import io.metersphere.system.domain.UserRoleRelation; import io.metersphere.system.domain.UserRoleRelation;
import io.metersphere.system.dto.request.GlobalUserRoleRelationQueryRequest; import io.metersphere.system.dto.request.GlobalUserRoleRelationQueryRequest;
import io.metersphere.system.mapper.ExtUserRoleRelationMapper; import io.metersphere.system.mapper.ExtUserRoleRelationMapper;
import io.metersphere.system.mapper.UserRoleRelationMapper;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -18,9 +19,6 @@ import java.util.List;
*/ */
@Service @Service
public class GlobalUserRoleRelationService extends BaseUserRoleRelationService { public class GlobalUserRoleRelationService extends BaseUserRoleRelationService {
@Resource
private UserRoleRelationMapper userRoleRelationMapper;
@Resource @Resource
private ExtUserRoleRelationMapper extUserRoleRelationMapper; private ExtUserRoleRelationMapper extUserRoleRelationMapper;
@Resource @Resource
@ -33,13 +31,17 @@ public class GlobalUserRoleRelationService extends BaseUserRoleRelationService {
return extUserRoleRelationMapper.listGlobal(request); return extUserRoleRelationMapper.listGlobal(request);
} }
@Override public void add(GlobalUserRoleRelationUpdateRequest request) {
public UserRoleRelation add(UserRoleRelation userRoleRelation) { UserRole userRole = globalUserRoleService.get(request.getRoleId());
UserRole userRole = globalUserRoleService.get(userRoleRelation.getRoleId());
globalUserRoleService.checkSystemUserGroup(userRole); globalUserRoleService.checkSystemUserGroup(userRole);
globalUserRoleService.checkGlobalUserRole(userRole); globalUserRoleService.checkGlobalUserRole(userRole);
request.getUserIds().forEach(userId -> {
UserRoleRelation userRoleRelation = new UserRoleRelation();
BeanUtils.copyBean(userRoleRelation, request);
userRoleRelation.setUserId(userId);
userRoleRelation.setSourceId(GlobalUserRoleService.SYSTEM_TYPE); userRoleRelation.setSourceId(GlobalUserRoleService.SYSTEM_TYPE);
return super.add(userRoleRelation); super.add(userRoleRelation);
});
} }
@Override @Override

View File

@ -1,6 +1,5 @@
package io.metersphere.system.service; package io.metersphere.system.service;
import io.metersphere.sdk.constants.HttpMethodConstants;
import io.metersphere.sdk.dto.LogDTO; import io.metersphere.sdk.dto.LogDTO;
import io.metersphere.sdk.dto.request.PermissionSettingUpdateRequest; import io.metersphere.sdk.dto.request.PermissionSettingUpdateRequest;
import io.metersphere.sdk.log.constants.OperationLogModule; import io.metersphere.sdk.log.constants.OperationLogModule;
@ -21,8 +20,6 @@ public class OrganizationUserRoleLogService {
@Resource @Resource
private UserRoleMapper userRoleMapper; private UserRoleMapper userRoleMapper;
private static final String PRE_URI = "/user/role/organization";
/** /**
* 新增组织-用户组 * 新增组织-用户组
* @param request 接口请求参数 * @param request 接口请求参数
@ -37,8 +34,7 @@ public class OrganizationUserRoleLogService {
OperationLogType.ADD.name(), OperationLogType.ADD.name(),
OperationLogModule.ORGANIZATION_USER_ROLE, OperationLogModule.ORGANIZATION_USER_ROLE,
request.getName()); request.getName());
dto.setPath(PRE_URI + "/add");
dto.setMethod(HttpMethodConstants.POST.name());
dto.setOriginalValue(JSON.toJSONBytes(request)); dto.setOriginalValue(JSON.toJSONBytes(request));
return dto; return dto;
} }
@ -57,8 +53,7 @@ public class OrganizationUserRoleLogService {
OperationLogType.UPDATE.name(), OperationLogType.UPDATE.name(),
OperationLogModule.ORGANIZATION_USER_ROLE, OperationLogModule.ORGANIZATION_USER_ROLE,
request.getName()); request.getName());
dto.setPath(PRE_URI + "/update");
dto.setMethod(HttpMethodConstants.POST.name());
dto.setOriginalValue(JSON.toJSONBytes(request)); dto.setOriginalValue(JSON.toJSONBytes(request));
return dto; return dto;
} }
@ -78,8 +73,7 @@ public class OrganizationUserRoleLogService {
OperationLogType.DELETE.name(), OperationLogType.DELETE.name(),
OperationLogModule.ORGANIZATION_USER_ROLE, OperationLogModule.ORGANIZATION_USER_ROLE,
userRole.getName()); userRole.getName());
dto.setPath(PRE_URI + "/delete");
dto.setMethod(HttpMethodConstants.GET.name());
dto.setOriginalValue(JSON.toJSONBytes(userRole)); dto.setOriginalValue(JSON.toJSONBytes(userRole));
return dto; return dto;
} }
@ -92,8 +86,6 @@ public class OrganizationUserRoleLogService {
public LogDTO updatePermissionSettingLog(PermissionSettingUpdateRequest request) { public LogDTO updatePermissionSettingLog(PermissionSettingUpdateRequest request) {
LogDTO dto = getLog(request.getUserRoleId()); LogDTO dto = getLog(request.getUserRoleId());
dto.setType(OperationLogType.UPDATE.name()); dto.setType(OperationLogType.UPDATE.name());
dto.setPath(PRE_URI + "/update");
dto.setMethod(HttpMethodConstants.POST.name());
dto.setOriginalValue(JSON.toJSONBytes(request)); dto.setOriginalValue(JSON.toJSONBytes(request));
return dto; return dto;
} }
@ -106,8 +98,6 @@ public class OrganizationUserRoleLogService {
public LogDTO editMemberLog(OrganizationUserRoleMemberEditRequest request) { public LogDTO editMemberLog(OrganizationUserRoleMemberEditRequest request) {
LogDTO dto = getLog(request.getUserRoleId()); LogDTO dto = getLog(request.getUserRoleId());
dto.setType(OperationLogType.UPDATE.name()); dto.setType(OperationLogType.UPDATE.name());
dto.setPath(PRE_URI + "/update");
dto.setMethod(HttpMethodConstants.POST.name());
dto.setOriginalValue(JSON.toJSONBytes(request)); dto.setOriginalValue(JSON.toJSONBytes(request));
return dto; return dto;
} }

View File

@ -2,7 +2,7 @@ package io.metersphere.system.service;
import io.metersphere.project.domain.Project; import io.metersphere.project.domain.Project;
import io.metersphere.project.mapper.ProjectMapper; import io.metersphere.project.mapper.ProjectMapper;
import io.metersphere.sdk.constants.HttpMethodConstants; import io.metersphere.sdk.constants.OperationLogConstants;
import io.metersphere.sdk.dto.AddProjectRequest; import io.metersphere.sdk.dto.AddProjectRequest;
import io.metersphere.sdk.dto.LogDTO; import io.metersphere.sdk.dto.LogDTO;
import io.metersphere.sdk.dto.UpdateProjectRequest; import io.metersphere.sdk.dto.UpdateProjectRequest;
@ -20,8 +20,6 @@ public class SystemProjectLogService {
@Resource @Resource
private ProjectMapper projectMapper; private ProjectMapper projectMapper;
private static final String PRE_URI = "/system/project";
/** /**
* 添加接口日志 * 添加接口日志
* *
@ -29,16 +27,14 @@ public class SystemProjectLogService {
*/ */
public LogDTO addLog(AddProjectRequest project) { public LogDTO addLog(AddProjectRequest project) {
LogDTO dto = new LogDTO( LogDTO dto = new LogDTO(
"system", OperationLogConstants.SYSTEM,
"", OperationLogConstants.SYSTEM,
null, null,
null, null,
OperationLogType.ADD.name(), OperationLogType.ADD.name(),
OperationLogModule.SYSTEM_PROJECT, OperationLogModule.SYSTEM_PROJECT,
project.getName()); project.getName());
dto.setPath(PRE_URI + "/add");
dto.setMethod(HttpMethodConstants.POST.name());
dto.setOriginalValue(JSON.toJSONBytes(project)); dto.setOriginalValue(JSON.toJSONBytes(project));
return dto; return dto;
} }
@ -51,15 +47,13 @@ public class SystemProjectLogService {
Project project = projectMapper.selectByPrimaryKey(request.getId()); Project project = projectMapper.selectByPrimaryKey(request.getId());
if (project != null) { if (project != null) {
LogDTO dto = new LogDTO( LogDTO dto = new LogDTO(
"system", OperationLogConstants.SYSTEM,
"", OperationLogConstants.SYSTEM,
project.getId(), project.getId(),
project.getCreateUser(), project.getCreateUser(),
OperationLogType.UPDATE.name(), OperationLogType.UPDATE.name(),
OperationLogModule.SYSTEM_PROJECT, OperationLogModule.SYSTEM_PROJECT,
"编辑全局用户组对应的权限配置"); "编辑全局用户组对应的权限配置");
dto.setPath("/update");
dto.setMethod(HttpMethodConstants.POST.name());
dto.setOriginalValue(JSON.toJSONBytes(project)); dto.setOriginalValue(JSON.toJSONBytes(project));
return dto; return dto;
@ -71,15 +65,13 @@ public class SystemProjectLogService {
Project project = projectMapper.selectByPrimaryKey(id); Project project = projectMapper.selectByPrimaryKey(id);
if (project != null) { if (project != null) {
LogDTO dto = new LogDTO( LogDTO dto = new LogDTO(
"system", OperationLogConstants.SYSTEM,
"", OperationLogConstants.SYSTEM,
project.getId(), project.getId(),
project.getCreateUser(), project.getCreateUser(),
OperationLogType.RECOVER.name(), OperationLogType.RECOVER.name(),
OperationLogModule.SYSTEM_PROJECT, OperationLogModule.SYSTEM_PROJECT,
"编辑全局用户组对应的权限配置"); "编辑全局用户组对应的权限配置");
dto.setPath("/revoke");
dto.setMethod(HttpMethodConstants.GET.name());
dto.setOriginalValue(JSON.toJSONBytes(project)); dto.setOriginalValue(JSON.toJSONBytes(project));
return dto; return dto;
@ -98,16 +90,14 @@ public class SystemProjectLogService {
Project project = projectMapper.selectByPrimaryKey(id); Project project = projectMapper.selectByPrimaryKey(id);
if (project != null) { if (project != null) {
LogDTO dto = new LogDTO( LogDTO dto = new LogDTO(
"system", OperationLogConstants.SYSTEM,
"", OperationLogConstants.SYSTEM,
id, id,
project.getCreateUser(), project.getCreateUser(),
OperationLogType.DELETE.name(), OperationLogType.DELETE.name(),
OperationLogModule.SYSTEM_PROJECT, OperationLogModule.SYSTEM_PROJECT,
project.getName()); project.getName());
dto.setPath("/delete");
dto.setMethod(HttpMethodConstants.GET.name());
dto.setOriginalValue(JSON.toJSONBytes(project)); dto.setOriginalValue(JSON.toJSONBytes(project));
return dto; return dto;
} }

View File

@ -21,6 +21,7 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMock
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.MvcResult;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
@ -98,32 +99,32 @@ class GlobalUserRoleRelationControllerTests extends BaseTest {
// @@请求成功 // @@请求成功
GlobalUserRoleRelationUpdateRequest request = new GlobalUserRoleRelationUpdateRequest(); GlobalUserRoleRelationUpdateRequest request = new GlobalUserRoleRelationUpdateRequest();
request.setUserId(ADMIN.getValue()); request.setUserIds(Arrays.asList(ADMIN.getValue()));
request.setRoleId(nonInternalUserRole.getId()); request.setRoleId(nonInternalUserRole.getId());
this.requestPostWithOk(DEFAULT_ADD, request); this.requestPostWithOk(DEFAULT_ADD, request);
UserRoleRelationExample example = new UserRoleRelationExample(); UserRoleRelationExample example = new UserRoleRelationExample();
example.createCriteria() example.createCriteria()
.andRoleIdEqualTo(request.getRoleId()) .andRoleIdEqualTo(request.getRoleId())
.andUserIdEqualTo(request.getUserId()); .andUserIdEqualTo(ADMIN.getValue());
Assertions.assertTrue(CollectionUtils.isNotEmpty(userRoleRelationMapper.selectByExample(example))); Assertions.assertTrue(CollectionUtils.isNotEmpty(userRoleRelationMapper.selectByExample(example)));
addUserRoleRelation = userRoleRelationMapper.selectByExample(example).get(0); 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()); request.setRoleId(ADMIN.getValue());
assertErrorCode(this.requestPost(DEFAULT_ADD, request), USER_ROLE_RELATION_EXIST); 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()); request.setRoleId(ORG_ADMIN.getValue());
assertErrorCode(this.requestPost(DEFAULT_ADD, request), GLOBAL_USER_ROLE_RELATION_SYSTEM_PERMISSION); assertErrorCode(this.requestPost(DEFAULT_ADD, request), GLOBAL_USER_ROLE_RELATION_SYSTEM_PERMISSION);
// @@操作非全局用户组异常 // @@操作非全局用户组异常
UserRole nonGlobalUserRole = getNonGlobalUserRole(); UserRole nonGlobalUserRole = getNonGlobalUserRole();
request.setUserId(ADMIN.getValue()); request.setUserIds(Arrays.asList(ADMIN.getValue()));
request.setRoleId(nonGlobalUserRole.getId()); request.setRoleId(nonGlobalUserRole.getId());
assertErrorCode(this.requestPost(DEFAULT_ADD, request), GLOBAL_USER_ROLE_PERMISSION); assertErrorCode(this.requestPost(DEFAULT_ADD, request), GLOBAL_USER_ROLE_PERMISSION);
@ -143,7 +144,7 @@ class GlobalUserRoleRelationControllerTests extends BaseTest {
Assertions.assertNull(userRoleRelation); Assertions.assertNull(userRoleRelation);
// @@校验日志 // @@校验日志
checkLog(addUserRoleRelation.getId(), OperationLogType.DELETE); checkLog(addUserRoleRelation.getRoleId(), OperationLogType.DELETE);
// @@操作非系统级别用户组异常 // @@操作非系统级别用户组异常
assertErrorCode(this.requestGet(DEFAULT_DELETE, getNonSystemUserRoleRelation().getId()), assertErrorCode(this.requestGet(DEFAULT_DELETE, getNonSystemUserRoleRelation().getId()),

View File

@ -3,18 +3,23 @@ package io.metersphere.system.controller.param;
import io.metersphere.validation.groups.Created; import io.metersphere.validation.groups.Created;
import io.metersphere.validation.groups.Updated; import io.metersphere.validation.groups.Updated;
import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.Size; import jakarta.validation.constraints.Size;
import lombok.Data; import lombok.Data;
import java.util.List;
/** /**
* @author jianxing * @author jianxing
*/ */
@Data @Data
public class GlobalUserRoleRelationUpdateRequestDefinition { public class GlobalUserRoleRelationUpdateRequestDefinition {
@NotBlank(groups = {Created.class}) @NotEmpty(groups = {Created.class, Updated.class})
@Size(min = 1, max = 50, groups = {Created.class, Updated.class}) private List<
private String userId; @NotBlank(groups = {Created.class, Updated.class})
@Size(groups = {Created.class, Updated.class})
String> userIds;
@NotBlank(groups = {Created.class}) @NotBlank(groups = {Created.class})
@Size(min = 1, max = 50, groups = {Created.class, Updated.class}) @Size(min = 1, max = 50, groups = {Created.class, Updated.class})