fix(系统设置): 修复用户管理页面所属项目的展示问题

This commit is contained in:
song-tianyang 2023-08-23 17:53:47 +08:00 committed by 刘瑞斌
parent be995c15c6
commit 7cb734b733
13 changed files with 350 additions and 163 deletions

View File

@ -0,0 +1,28 @@
package io.metersphere.sdk.dto.builder;
import io.metersphere.sdk.dto.LogDTO;
import lombok.Builder;
@Builder
public class LogDTOBuilder {
private String projectId;
private String organizationId;
private String sourceId;
private String createUser;
private String type;
private String method;
private String module;
private String content;
private String path;
private byte[] originalValue;
private byte[] modifiedValue;
public LogDTO getLogDTO() {
LogDTO logDTO = new LogDTO(projectId, organizationId, sourceId, createUser, type, module, content);
logDTO.setPath(path);
logDTO.setMethod(method);
logDTO.setOriginalValue(originalValue);
logDTO.setModifiedValue(modifiedValue);
return logDTO;
}
}

View File

@ -9,7 +9,7 @@ import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface BaseUserMapper {
UserDTO selectByEmail(String email);
UserDTO selectDTOByKeyword(String keyword);
UserDTO selectById(String id);

View File

@ -16,11 +16,12 @@
WHERE user.id = #{id}
</select>
<select id="selectByEmail" resultType="io.metersphere.sdk.dto.UserDTO">
<select id="selectDTOByKeyword" resultType="io.metersphere.sdk.dto.UserDTO">
SELECT *
FROM user
LEFT JOIN user_extend ON user.id = user_extend.id
WHERE user.email = #{email} AND deleted IS FALSE
WHERE (user.email = #{keyword} OR user.id = #{keyword})
AND deleted IS FALSE
</select>
<select id="findAll" resultType="io.metersphere.system.domain.User">

View File

@ -52,12 +52,14 @@ public class UserController {
private OrganizationService organizationService;
@Resource
private SystemProjectService systemProjectService;
@Resource
private UserLogService userLogService;
@GetMapping("/get/{email}")
@Operation(summary = "通过email查找用户")
@GetMapping("/get/{keyword}")
@Operation(summary = "通过email或id查找用户")
@RequiresPermissions(PermissionConstants.SYSTEM_USER_READ)
public UserDTO getUser(@PathVariable String email) {
return userService.getUserDTOByEmail(email);
public UserDTO getUser(@PathVariable String keyword) {
return userService.getUserDTOByKeyword(keyword);
}
@PostMapping("/add")
@ -70,7 +72,7 @@ public class UserController {
@PostMapping("/update")
@Operation(summary = "修改用户")
@RequiresPermissions(PermissionConstants.SYSTEM_USER_READ_UPDATE)
@Log(type = OperationLogType.UPDATE, expression = "#msClass.updateLog(#request)", msClass = UserService.class)
@Log(type = OperationLogType.UPDATE, expression = "#msClass.updateLog(#request)", msClass = UserLogService.class)
public UserEditRequest updateUser(@Validated({Updated.class}) @RequestBody UserEditRequest request) {
return userService.updateUser(request, SessionUtils.getUserId());
}
@ -87,7 +89,7 @@ public class UserController {
@PostMapping("/update/enable")
@Operation(summary = "启用/禁用用户")
@RequiresPermissions(PermissionConstants.SYSTEM_USER_READ_UPDATE)
@Log(type = OperationLogType.UPDATE, expression = "#msClass.batchUpdateLog(#request)", msClass = UserService.class)
@Log(type = OperationLogType.UPDATE, expression = "#msClass.batchUpdateLog(#request)", msClass = UserLogService.class)
public TableBatchProcessResponse updateUserEnable(@Validated @RequestBody UserChangeEnableRequest request) {
return userService.updateUserEnable(request, SessionUtils.getSessionId());
}
@ -101,7 +103,7 @@ public class UserController {
@PostMapping("/delete")
@Operation(summary = "删除用户")
@Log(type = OperationLogType.DELETE, expression = "#msClass.deleteLog(#request)", msClass = UserService.class)
@Log(type = OperationLogType.DELETE, expression = "#msClass.deleteLog(#request)", msClass = UserLogService.class)
@RequiresPermissions(PermissionConstants.SYSTEM_USER_READ_DELETE)
public TableBatchProcessResponse deleteUser(@Validated @RequestBody TableBatchProcessDTO request) {
return userService.deleteUser(request, SessionUtils.getUserId());
@ -110,7 +112,7 @@ public class UserController {
@PostMapping("/reset/password")
@Operation(summary = "重置用户密码")
@RequiresPermissions(PermissionConstants.SYSTEM_USER_READ_UPDATE)
@Log(type = OperationLogType.UPDATE, expression = "#msClass.resetPasswordLog(#request)", msClass = UserService.class)
@Log(type = OperationLogType.UPDATE, expression = "#msClass.resetPasswordLog(#request)", msClass = UserLogService.class)
public TableBatchProcessResponse resetPassword(@Validated @RequestBody TableBatchProcessDTO request) {
return userService.resetPassword(request, SessionUtils.getUserId());
}
@ -141,7 +143,7 @@ public class UserController {
@Operation(summary = "批量添加用户到多个用户组中")
@RequiresPermissions(PermissionConstants.SYSTEM_USER_READ_UPDATE)
@Log(type = OperationLogType.ADD, expression = "#msClass.batchAddLog(#request)", msClass = GlobalUserRoleRelationLogService.class)
public TableBatchProcessResponse batchAdd(@Validated({Created.class}) @RequestBody UserRoleBatchRelationRequest request) {
public TableBatchProcessResponse batchAddUserGroupRole(@Validated({Created.class}) @RequestBody UserRoleBatchRelationRequest request) {
return globalUserRoleRelationService.batchAdd(request, SessionUtils.getUserId());
}
@ -153,6 +155,7 @@ public class UserController {
request.setProjectIds(userRoleBatchRelationRequest.getRoleIds());
request.setUserIds(userRoleBatchRelationRequest.getSelectIds());
systemProjectService.addProjectMember(request, SessionUtils.getUserId());
userLogService.batchAddProjectLog(userRoleBatchRelationRequest, SessionUtils.getUserId());
return new TableBatchProcessResponse(userRoleBatchRelationRequest.getSelectIds().size(), userRoleBatchRelationRequest.getSelectIds().size());
}
@ -166,6 +169,7 @@ public class UserController {
request.setOrganizationIds(userRoleBatchRelationRequest.getRoleIds());
request.setUserIds(userRoleBatchRelationRequest.getSelectIds());
organizationService.addMemberBySystem(request, SessionUtils.getUserId());
userLogService.batchAddOrgLog(userRoleBatchRelationRequest, SessionUtils.getUserId());
return new TableBatchProcessResponse(userRoleBatchRelationRequest.getSelectIds().size(), userRoleBatchRelationRequest.getSelectIds().size());
}
}

View File

@ -8,7 +8,7 @@ import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface ExtUserRoleRelationMapper {
List<UserRoleRelation> listByUserIdAndScope(@Param("userIds") List<String> userIdList);
List<UserRoleRelation> selectGlobalRoleByUserIdList(@Param("userIds") List<String> userIdList);
List<UserRoleRelation> selectGlobalRoleByUserId(String userId);

View File

@ -2,13 +2,16 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="io.metersphere.system.mapper.ExtUserRoleRelationMapper">
<select id="listByUserIdAndScope" resultType="io.metersphere.system.domain.UserRoleRelation">
<select id="selectGlobalRoleByUserIdList" resultType="io.metersphere.system.domain.UserRoleRelation">
SELECT * FROM
user_role_relation
WHERE
<foreach collection="userIds" item="userId" open="(" close=")" separator="OR">
user_id = #{userId}
</foreach>
AND role_id IN (
SELECT id FROM user_role WHERE scope_id = 'global'
)
</select>
<select id="selectGlobalRoleByUserId" resultType="io.metersphere.system.domain.UserRoleRelation">
SELECT * FROM

View File

@ -34,6 +34,8 @@ public class GlobalUserRoleRelationLogService {
private BaseUserMapper baseUserMapper;
@Resource
private UserRoleMapper userRoleMapper;
@Resource
private UserService userService;
/**
* 添加接口日志
@ -62,7 +64,7 @@ public class GlobalUserRoleRelationLogService {
UserRoleExample example = new UserRoleExample();
example.createCriteria().andIdIn(request.getRoleIds());
List<UserRole> userRoles = userRoleMapper.selectByExample(example);
List<String> userIds = request.getSelectIds();
List<String> userIds = userService.getBatchUserIds(request);
List<OptionDTO> users = baseUserMapper.selectUserOptionByIds(userIds);
List<LogDTO> returnList = new ArrayList<>();

View File

@ -191,7 +191,6 @@ public class OrganizationService {
userRoleRelationMapper.batchInsert(userRoleRelations);
}
}
/**
* 删除组织成员
*

View File

@ -0,0 +1,191 @@
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.TableBatchProcessDTO;
import io.metersphere.sdk.dto.builder.LogDTOBuilder;
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.User;
import io.metersphere.system.domain.UserExample;
import io.metersphere.system.mapper.UserMapper;
import io.metersphere.system.request.user.UserEditRequest;
import io.metersphere.system.request.user.UserRoleBatchRelationRequest;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@Service
@Transactional(rollbackFor = Exception.class)
public class UserLogService {
@Resource
private UserMapper userMapper;
@Resource
private UserService userService;
@Resource
private OperationLogService operationLogService;
//批量添加用户记录日志
public List<LogDTO> getBatchAddLogs(@Valid List<User> userList) {
List<LogDTO> logs = new ArrayList<>();
userList.forEach(user -> {
LogDTO log = new LogDTO();
log.setId(UUID.randomUUID().toString());
log.setCreateUser(user.getCreateUser());
log.setProjectId(OperationLogConstants.SYSTEM);
log.setOrganizationId(OperationLogConstants.SYSTEM);
log.setType(OperationLogType.ADD.name());
log.setModule(OperationLogModule.SYSTEM_USER);
log.setMethod("addUser");
log.setCreateTime(user.getCreateTime());
log.setSourceId(user.getId());
log.setContent(user.getName() + "(" + user.getEmail() + ")");
log.setOriginalValue(JSON.toJSONBytes(user));
logs.add(log);
});
return logs;
}
public LogDTO updateLog(UserEditRequest request) {
User user = userMapper.selectByPrimaryKey(request.getId());
if (user != null) {
LogDTO dto = new LogDTO(
OperationLogConstants.SYSTEM,
OperationLogConstants.SYSTEM,
request.getId(),
null,
OperationLogType.UPDATE.name(),
OperationLogModule.SYSTEM_USER,
JSON.toJSONString(user));
dto.setPath("/update");
dto.setMethod(HttpMethodConstants.POST.name());
dto.setOriginalValue(JSON.toJSONBytes(user));
return dto;
}
return null;
}
public List<LogDTO> batchUpdateLog(TableBatchProcessDTO request) {
List<LogDTO> logDTOList = new ArrayList<>();
request.setSelectIds(userService.getBatchUserIds(request));
List<User> userList = userService.selectByIdList(request.getSelectIds());
for (User user : userList) {
LogDTO dto = new LogDTO(
OperationLogConstants.SYSTEM,
OperationLogConstants.SYSTEM,
user.getId(),
null,
OperationLogType.UPDATE.name(),
OperationLogModule.SYSTEM_USER,
JSON.toJSONString(user));
dto.setMethod(HttpMethodConstants.POST.name());
dto.setOriginalValue(JSON.toJSONBytes(user));
logDTOList.add(dto);
}
return logDTOList;
}
/**
* @param request 批量重置密码 用于记录Log使用
*/
public List<LogDTO> resetPasswordLog(TableBatchProcessDTO request) {
request.setSelectIds(userService.getBatchUserIds(request));
List<LogDTO> returnList = new ArrayList<>();
UserExample example = new UserExample();
example.createCriteria().andIdIn(request.getSelectIds());
List<User> userList = userMapper.selectByExample(example);
for (User user : userList) {
LogDTO dto = new LogDTO(
OperationLogConstants.SYSTEM,
OperationLogConstants.SYSTEM,
user.getId(),
null,
OperationLogType.UPDATE.name(),
OperationLogModule.SYSTEM_USER,
user.getName());
dto.setPath("/reset/password");
dto.setMethod(HttpMethodConstants.POST.name());
dto.setOriginalValue(JSON.toJSONBytes(user));
returnList.add(dto);
}
return returnList;
}
public List<LogDTO> deleteLog(TableBatchProcessDTO request) {
List<LogDTO> logDTOList = new ArrayList<>();
request.getSelectIds().forEach(item -> {
User user = userMapper.selectByPrimaryKey(item);
if (user != null) {
LogDTO dto = new LogDTO(
OperationLogConstants.SYSTEM,
OperationLogConstants.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;
}
public void batchAddProjectLog(UserRoleBatchRelationRequest request, String operator) {
List<LogDTO> logs = new ArrayList<>();
List<String> userIds = userService.getBatchUserIds(request);
List<User> userList = userService.selectByIdList(userIds);
for (User user : userList) {
//用户管理处修改了用户的组织
LogDTO log = LogDTOBuilder.builder()
.projectId(OperationLogConstants.SYSTEM)
.createUser(operator)
.organizationId(OperationLogConstants.SYSTEM)
.sourceId(user.getId())
.type(OperationLogType.UPDATE.name())
.module(OperationLogModule.SYSTEM_USER)
.content(user.getName())
.path("/system/user/add-project-member")
.modifiedValue(JSON.toJSONBytes(request.getRoleIds()))
.build().getLogDTO();
logs.add(log);
}
operationLogService.batchAdd(logs);
}
public void batchAddOrgLog(UserRoleBatchRelationRequest request, String operator) {
List<LogDTO> logs = new ArrayList<>();
List<String> userIds = userService.getBatchUserIds(request);
List<User> userList = userService.selectByIdList(userIds);
for (User user : userList) {
//用户管理处修改了用户的组织
LogDTO log = LogDTOBuilder.builder()
.projectId(OperationLogConstants.SYSTEM)
.module(OperationLogModule.SYSTEM_USER)
.createUser(operator)
.organizationId(OperationLogConstants.SYSTEM)
.sourceId(user.getId())
.type(OperationLogType.UPDATE.name())
.content(user.getName())
.path("/system/user/add-org-member")
.method(HttpMethodConstants.POST.name())
.modifiedValue(JSON.toJSONBytes(request.getRoleIds()))
.build().getLogDTO();
logs.add(log);
}
operationLogService.batchAdd(logs);
}
}

View File

@ -2,6 +2,7 @@ package io.metersphere.system.service;
import io.metersphere.sdk.constants.OperationLogConstants;
import io.metersphere.sdk.constants.UserRoleEnum;
import io.metersphere.sdk.constants.UserRoleScope;
import io.metersphere.sdk.dto.LogDTO;
import io.metersphere.sdk.log.constants.OperationLogModule;
import io.metersphere.sdk.log.constants.OperationLogType;
@ -94,7 +95,7 @@ public class UserRoleRelationService {
userRoleRelation.setId(UUID.randomUUID().toString());
userRoleRelation.setUserId(user.getId());
userRoleRelation.setRoleId(userRoleId);
userRoleRelation.setSourceId("system");
userRoleRelation.setSourceId(UserRoleScope.SYSTEM);
userRoleRelation.setCreateTime(operationTime);
userRoleRelation.setCreateUser(user.getCreateUser());
userRoleRelationSaveList.add(userRoleRelation);
@ -120,9 +121,9 @@ public class UserRoleRelationService {
}
public Map<String, UserTableResponse> selectGlobalUserRoleAndOrganization(@Valid @NotEmpty List<String> userIdList) {
List<UserRoleRelation> userRoleRelationList = extUserRoleRelationMapper.listByUserIdAndScope(userIdList);
List<String> userRoleIdList = userRoleRelationList.stream().map(UserRoleRelation::getRoleId).collect(Collectors.toList());
List<String> sourceIdList = userRoleRelationList.stream().map(UserRoleRelation::getSourceId).collect(Collectors.toList());
List<UserRoleRelation> userRoleRelationList = extUserRoleRelationMapper.selectGlobalRoleByUserIdList(userIdList);
List<String> userRoleIdList = userRoleRelationList.stream().map(UserRoleRelation::getRoleId).distinct().collect(Collectors.toList());
List<String> sourceIdList = userRoleRelationList.stream().map(UserRoleRelation::getSourceId).distinct().collect(Collectors.toList());
Map<String, UserRole> userRoleMap = new HashMap<>();
Map<String, Organization> organizationMap = new HashMap<>();
if (CollectionUtils.isNotEmpty(userRoleIdList)) {
@ -146,9 +147,13 @@ public class UserRoleRelationService {
returnMap.put(userRoleRelation.getUserId(), userInfo);
}
UserRole userRole = userRoleMap.get(userRoleRelation.getRoleId());
if (userRole != null && StringUtils.equalsIgnoreCase(userRole.getType(), UserRoleScope.SYSTEM)) {
userInfo.getUserRoleList().add(userRole);
}
Organization organization = organizationMap.get(userRoleRelation.getSourceId());
userInfo.getUserRoleList().add(userRole);
userInfo.getOrganizationList().add(organization);
if (organization != null) {
userInfo.getOrganizationList().add(organization);
}
}
return returnMap;
}
@ -176,7 +181,7 @@ public class UserRoleRelationService {
userRoleRelation.setId(UUID.randomUUID().toString());
userRoleRelation.setUserId(user.getId());
userRoleRelation.setRoleId(roleId);
userRoleRelation.setSourceId("system");
userRoleRelation.setSourceId(UserRoleScope.SYSTEM);
userRoleRelation.setCreateTime(System.currentTimeMillis());
userRoleRelation.setCreateUser(operator);
saveList.add(userRoleRelation);

View File

@ -1,15 +1,14 @@
package io.metersphere.system.service;
import com.alibaba.excel.EasyExcelFactory;
import io.metersphere.sdk.constants.HttpMethodConstants;
import io.metersphere.sdk.constants.OperationLogConstants;
import io.metersphere.sdk.dto.*;
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.mapper.BaseUserMapper;
import io.metersphere.sdk.util.*;
import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.sdk.util.CodingUtil;
import io.metersphere.sdk.util.LogUtils;
import io.metersphere.sdk.util.Translator;
import io.metersphere.system.domain.User;
import io.metersphere.system.domain.UserExample;
import io.metersphere.system.dto.UserBatchCreateDTO;
@ -35,6 +34,7 @@ import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
@ -65,26 +65,9 @@ public class UserService {
@Resource
private SqlSessionFactory sqlSessionFactory;
//批量添加用户记录日志
public List<LogDTO> getBatchAddLogs(@Valid List<User> userList) {
List<LogDTO> logs = new ArrayList<>();
userList.forEach(user -> {
LogDTO log = new LogDTO();
log.setId(UUID.randomUUID().toString());
log.setCreateUser(user.getCreateUser());
log.setProjectId(OperationLogConstants.SYSTEM);
log.setOrganizationId(OperationLogConstants.SYSTEM);
log.setType(OperationLogType.ADD.name());
log.setModule(OperationLogModule.SYSTEM_USER);
log.setMethod("addUser");
log.setCreateTime(user.getCreateTime());
log.setSourceId(user.getId());
log.setContent(user.getName() + "(" + user.getEmail() + ")");
log.setOriginalValue(JSON.toJSONBytes(user));
logs.add(log);
});
return logs;
}
@Resource
@Lazy
private UserLogService userLogService;
public List<User> selectByIdList(@NotEmpty List<String> userIdList) {
UserExample example = new UserExample();
@ -142,13 +125,13 @@ public class UserService {
}
userRoleRelationService.batchSave(userCreateDTO.getUserRoleIdList(), saveUserList);
//写入操作日志
operationLogService.batchAdd(this.getBatchAddLogs(saveUserList));
operationLogService.batchAdd(userLogService.getBatchAddLogs(saveUserList));
return userCreateDTO;
}
public UserDTO getUserDTOByEmail(String email) {
UserDTO userDTO = baseUserMapper.selectByEmail(email);
public UserDTO getUserDTOByKeyword(String email) {
UserDTO userDTO = baseUserMapper.selectDTOByKeyword(email);
if (userDTO != null) {
userDTO.setUserRoleRelations(
userRoleRelationService.selectByUserId(userDTO.getId())
@ -318,95 +301,6 @@ public class UserService {
return insertIndex;
}
public LogDTO updateLog(UserEditRequest request) {
User user = userMapper.selectByPrimaryKey(request.getId());
if (user != null) {
LogDTO dto = new LogDTO(
OperationLogConstants.SYSTEM,
OperationLogConstants.SYSTEM,
request.getId(),
null,
OperationLogType.UPDATE.name(),
OperationLogModule.SYSTEM_USER,
JSON.toJSONString(user));
dto.setPath("/update");
dto.setMethod(HttpMethodConstants.POST.name());
dto.setOriginalValue(JSON.toJSONBytes(user));
return dto;
}
return null;
}
public List<LogDTO> batchUpdateLog(TableBatchProcessDTO request) {
List<LogDTO> logDTOList = new ArrayList<>();
request.setSelectIds(this.getBatchUserIds(request));
List<User> userList = this.selectByIdList(request.getSelectIds());
for (User user : userList) {
LogDTO dto = new LogDTO(
OperationLogConstants.SYSTEM,
OperationLogConstants.SYSTEM,
user.getId(),
null,
OperationLogType.UPDATE.name(),
OperationLogModule.SYSTEM_USER,
JSON.toJSONString(user));
dto.setMethod(HttpMethodConstants.POST.name());
dto.setOriginalValue(JSON.toJSONBytes(user));
logDTOList.add(dto);
}
return logDTOList;
}
/**
* @param request 批量重置密码 用于记录Log使用
*/
public List<LogDTO> resetPasswordLog(TableBatchProcessDTO request) {
request.setSelectIds(this.getBatchUserIds(request));
List<LogDTO> returnList = new ArrayList<>();
UserExample example = new UserExample();
example.createCriteria().andIdIn(request.getSelectIds());
List<User> userList = userMapper.selectByExample(example);
for (User user : userList) {
LogDTO dto = new LogDTO(
OperationLogConstants.SYSTEM,
OperationLogConstants.SYSTEM,
user.getId(),
null,
OperationLogType.UPDATE.name(),
OperationLogModule.SYSTEM_USER,
user.getName());
dto.setPath("/reset/password");
dto.setMethod(HttpMethodConstants.POST.name());
dto.setOriginalValue(JSON.toJSONBytes(user));
returnList.add(dto);
}
return returnList;
}
public List<LogDTO> deleteLog(TableBatchProcessDTO request) {
List<LogDTO> logDTOList = new ArrayList<>();
request.getSelectIds().forEach(item -> {
User user = userMapper.selectByPrimaryKey(item);
if (user != null) {
LogDTO dto = new LogDTO(
OperationLogConstants.SYSTEM,
OperationLogConstants.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;
}
public List<User> getUserList() {
UserExample example = new UserExample();
example.setOrderByClause("update_time desc");

View File

@ -207,36 +207,69 @@ public class UserControllerTests extends BaseTest {
@Test
@Order(3)
public void testPageSuccess() throws Exception {
List<String> userRoleIdList = USER_ROLE_LIST.stream().map(UserSelectOption::getId).collect(Collectors.toList());
this.checkUserList();
BasePageRequest basePageRequest = UserParamUtils.getDefaultPageRequest();
MvcResult mvcResult = userRequestUtils.responsePost(userRequestUtils.URL_USER_PAGE, basePageRequest);
String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);
ResultHolder resultHolder = JSON.parseObject(returnData, ResultHolder.class);
//返回请求正常
Assertions.assertNotNull(resultHolder);
Pager<?> returnPager = JSON.parseObject(JSON.toJSONString(resultHolder.getData()), Pager.class);
Pager<?> returnPager = userRequestUtils.selectUserPage(basePageRequest);
//返回值不为空
Assertions.assertNotNull(returnPager);
//返回值的页码和当前页码相同
Assertions.assertEquals(returnPager.getCurrent(), basePageRequest.getCurrent());
//返回的数据量不超过规定要返回的数据量相同
Assertions.assertTrue(JSON.parseArray(JSON.toJSONString(returnPager.getList())).size() <= basePageRequest.getPageSize());
List<UserTableResponse> userList = JSON.parseArray(JSON.toJSONString(returnPager.getList()), UserTableResponse.class);
//用户组不存在非全局用户组
for (UserTableResponse response : userList) {
if (CollectionUtils.isNotEmpty(response.getUserRoleList())) {
response.getUserRoleList().forEach(role -> {
Assertions.assertTrue(userRoleIdList.contains(role.getId()));
});
}
}
//页码为50
basePageRequest = UserParamUtils.getDefaultPageRequest();
basePageRequest.setPageSize(50);
returnPager = userRequestUtils.selectUserPage(basePageRequest);
//返回值不为空
Assertions.assertNotNull(returnPager);
//返回值的页码和当前页码相同
Assertions.assertEquals(returnPager.getCurrent(), basePageRequest.getCurrent());
//返回的数据量不超过规定要返回的数据量相同
Assertions.assertTrue(JSON.parseArray(JSON.toJSONString(returnPager.getList())).size() <= basePageRequest.getPageSize());
//用户组不存在非全局用户组
userList = JSON.parseArray(JSON.toJSONString(returnPager.getList()), UserTableResponse.class);
for (UserTableResponse response : userList) {
if (CollectionUtils.isNotEmpty(response.getUserRoleList())) {
response.getUserRoleList().forEach(role -> {
Assertions.assertTrue(userRoleIdList.contains(role.getId()));
});
}
}
//测试根据创建时间倒叙排列
basePageRequest = UserParamUtils.getDefaultPageRequest();
basePageRequest.setSort(new HashMap<>() {{
put("createTime", "desc");
}});
mvcResult = userRequestUtils.responsePost(userRequestUtils.URL_USER_PAGE, basePageRequest);
returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);
resultHolder = JSON.parseObject(returnData, ResultHolder.class);
returnPager = JSON.parseObject(JSON.toJSONString(resultHolder.getData()), Pager.class);
returnPager = userRequestUtils.selectUserPage(basePageRequest);
//第一个数据的createTime是最大的
List<UserTableResponse> userInfoList = JSON.parseArray(JSON.toJSONString(returnPager.getList()), UserTableResponse.class);
long firstCreateTime = userInfoList.get(0).getCreateTime();
for (UserTableResponse userInfo : userInfoList) {
Assertions.assertFalse(userInfo.getCreateTime() > firstCreateTime);
}
//用户组不存在非全局用户组
userList = JSON.parseArray(JSON.toJSONString(returnPager.getList()), UserTableResponse.class);
for (UserTableResponse response : userList) {
if (CollectionUtils.isNotEmpty(response.getUserRoleList())) {
response.getUserRoleList().forEach(role -> {
Assertions.assertTrue(userRoleIdList.contains(role.getId()));
});
}
}
}
@Test
@ -766,9 +799,10 @@ public class UserControllerTests extends BaseTest {
this.testGetProjectAndOrganization();
}
List<UserCreateInfo> last50Users = USER_LIST.subList(USER_LIST.size() - 50, USER_LIST.size());
List<String> userIds = this.selectUserTableIds(50);
UserRoleBatchRelationRequest request = new UserRoleBatchRelationRequest();
request.setSelectIds(last50Users.stream().map(UserCreateInfo::getId).collect(Collectors.toList()));
request.setSelectIds(userIds);
request.setRoleIds(PROJECT_LIST.stream().map(UserSelectOption::getId).collect(Collectors.toList()));
//排除树结构中的组织ID
request.getRoleIds().removeAll(ORG_LIST.stream().map(UserSelectOption::getId).collect(Collectors.toList()));
@ -797,9 +831,10 @@ public class UserControllerTests extends BaseTest {
for (UserSelectOption option : USER_ROLE_LIST) {
this.checkLog(option.getId(), OperationLogType.ADD);
}
//检查用户表格不会加载出来非全局用户组
this.testPageSuccess();
}
@Test
@Order(13)
public void testAddOrganization() throws Exception {
@ -811,11 +846,12 @@ public class UserControllerTests extends BaseTest {
this.testGetProjectAndOrganization();
}
List<UserCreateInfo> last50Users = USER_LIST.subList(USER_LIST.size() - 50, USER_LIST.size());
List<String> userIds = this.selectUserTableIds(50);
UserRoleBatchRelationRequest request = new UserRoleBatchRelationRequest();
request.setSelectIds(last50Users.stream().map(UserCreateInfo::getId).collect(Collectors.toList()));
request.setSelectIds(userIds);
request.setRoleIds(ORG_LIST.stream().map(UserSelectOption::getId).collect(Collectors.toList()));
this.requestPost(userRequestUtils.URL_ADD_ORGANIZATION_MEMBER, request);
this.requestPostWithOk(userRequestUtils.URL_ADD_ORGANIZATION_MEMBER, request);
//检查有权限的数据量是否一致
UserRoleRelationExample checkExample = new UserRoleRelationExample();
for (String orgId : request.getRoleIds()) {
@ -829,9 +865,11 @@ public class UserControllerTests extends BaseTest {
}
}
//检查日志
for (UserSelectOption option : USER_ROLE_LIST) {
this.checkLog(option.getId(), OperationLogType.ADD);
for (String userID : request.getSelectIds()) {
this.checkLog(userID, OperationLogType.UPDATE);
}
//检查用户表格加载组织
this.testPageSuccess();
}
@Test
@ -889,20 +927,21 @@ public class UserControllerTests extends BaseTest {
@Order(99)
public void testUserDeleteSuccess() throws Exception {
this.checkUserList();
//删除指定的用户
{
UserCreateInfo deleteUser = USER_LIST.get(0);
//删除USER_LIST用户
TableBatchProcessDTO request = new TableBatchProcessDTO();
request.setSelectIds(Collections.singletonList(deleteUser.getId()));
request.setSelectIds(USER_LIST.stream().map(UserCreateInfo::getId).collect(Collectors.toList()));
TableBatchProcessResponse response = userRequestUtils.parseObjectFromMvcResult(
userRequestUtils.responsePost(userRequestUtils.URL_USER_DELETE, request), TableBatchProcessResponse.class);
Assertions.assertEquals(request.getSelectIds().size(), response.getTotalCount());
Assertions.assertEquals(request.getSelectIds().size(), response.getSuccessCount());
//检查数据库
List<UserCreateInfo> removeList = new ArrayList<>();
for (UserCreateInfo deleteUser : USER_LIST) {
User user = userMapper.selectByPrimaryKey(deleteUser.getId());
Assertions.assertTrue(user.getDeleted());
USER_LIST.remove(deleteUser);
removeList.add(deleteUser);
}
USER_LIST.removeAll(removeList);
}
//删除失败的方法要放在删除成功方法后面执行
@ -941,6 +980,16 @@ public class UserControllerTests extends BaseTest {
USER_ROLE_LIST.addAll(userRoleList);
}
//查找表格用户信息的ID
private List<String> selectUserTableIds(int pageSize) throws Exception {
BasePageRequest basePageRequest = UserParamUtils.getDefaultPageRequest();
basePageRequest.setPageSize(pageSize);
Pager<?> returnPager = userRequestUtils.selectUserPage(basePageRequest);
//用户组不存在非全局用户组
List<UserTableResponse> userList = JSON.parseArray(JSON.toJSONString(returnPager.getList()), UserTableResponse.class);
return userList.stream().map(User::getId).collect(Collectors.toList());
}
//成功入库的用户保存内存中其他用例会使用到
private void addUser2List(MvcResult mvcResult) throws Exception {
UserBatchCreateDTO userMaintainRequest = userRequestUtils.parseObjectFromMvcResult(mvcResult, UserBatchCreateDTO.class);

View File

@ -2,7 +2,9 @@ package io.metersphere.system.utils.user;
import io.metersphere.sdk.constants.SessionConstants;
import io.metersphere.sdk.controller.handler.ResultHolder;
import io.metersphere.sdk.dto.BasePageRequest;
import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.util.Pager;
import org.junit.jupiter.api.Assertions;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
@ -125,4 +127,13 @@ public class UserRequestUtils {
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andReturn();
}
public Pager<?> selectUserPage(BasePageRequest basePageRequest) throws Exception {
MvcResult mvcResult = this.responsePost(this.URL_USER_PAGE, basePageRequest);
String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);
ResultHolder resultHolder = JSON.parseObject(returnData, ResultHolder.class);
//返回请求正常
Assertions.assertNotNull(resultHolder);
return JSON.parseObject(JSON.toJSONString(resultHolder.getData()), Pager.class);
}
}