refactor(系统设置): 用户组查询用户接口支持远程检索

This commit is contained in:
AgAngle 2023-09-01 10:51:20 +08:00 committed by fit2-zhao
parent b6567a8318
commit 7e4bbd595d
8 changed files with 61 additions and 32 deletions

View File

@ -0,0 +1,18 @@
package io.metersphere.sdk.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* 加载选项时标记是否已经关联过前端需要 exclude 判断禁止重复关联
* @author jianxing
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserExcludeOptionDTO extends ExcludeOptionDTO {
@Schema(description = "邮箱")
private String email;
}

View File

@ -1,8 +1,8 @@
package io.metersphere.sdk.mapper; package io.metersphere.sdk.mapper;
import io.metersphere.sdk.dto.ExcludeOptionDTO;
import io.metersphere.sdk.dto.OptionDTO; import io.metersphere.sdk.dto.OptionDTO;
import io.metersphere.sdk.dto.UserDTO; import io.metersphere.sdk.dto.UserDTO;
import io.metersphere.sdk.dto.UserExcludeOptionDTO;
import io.metersphere.system.domain.User; import io.metersphere.system.domain.User;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
@ -31,9 +31,7 @@ public interface BaseUserMapper {
List<OptionDTO> selectUserOptionByIds(List<String> userIds); List<OptionDTO> selectUserOptionByIds(List<String> userIds);
List<ExcludeOptionDTO> getExcludeSelectOption(); List<UserExcludeOptionDTO> getExcludeSelectOptionWithLimit(@Param("keyword") String keyword);
List<OptionDTO> getSelectOptionByIdsWithDeleted(List<String> ids); List<OptionDTO> getSelectOptionByIdsWithDeleted(List<String> ids);
List<OptionDTO> getSelectOptionWithDeleted();
} }

View File

@ -93,10 +93,14 @@
#{id} #{id}
</foreach> </foreach>
</select> </select>
<select id="getExcludeSelectOption" resultType="io.metersphere.sdk.dto.ExcludeOptionDTO"> <select id="getExcludeSelectOptionWithLimit" resultType="io.metersphere.sdk.dto.UserExcludeOptionDTO">
select id, name select id, name, email
from user from user
where deleted = 0 where deleted = 0
<if test="keyword != null and keyword != ''">
and (name LIKE CONCAT('%', #{keyword}, '%') or email like CONCAT('%', #{keyword}, '%'))
</if>
limit 100
</select> </select>
<select id="getSelectOptionByIdsWithDeleted" resultType="io.metersphere.sdk.dto.OptionDTO"> <select id="getSelectOptionByIdsWithDeleted" resultType="io.metersphere.sdk.dto.OptionDTO">
SELECT id, name SELECT id, name
@ -107,8 +111,4 @@
#{id} #{id}
</foreach> </foreach>
</select> </select>
<select id="getSelectOptionWithDeleted" resultType="io.metersphere.sdk.dto.OptionDTO">
select id, name
from user
</select>
</mapper> </mapper>

View File

@ -1,6 +1,6 @@
package io.metersphere.sdk.service; package io.metersphere.sdk.service;
import io.metersphere.sdk.dto.ExcludeOptionDTO; import io.metersphere.sdk.dto.UserExcludeOptionDTO;
import io.metersphere.sdk.exception.MSException; import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.mapper.BaseUserRoleRelationMapper; import io.metersphere.sdk.mapper.BaseUserRoleRelationMapper;
import io.metersphere.system.domain.UserRole; import io.metersphere.system.domain.UserRole;
@ -122,9 +122,9 @@ public class BaseUserRoleRelationService {
* @param roleId * @param roleId
* @return * @return
*/ */
public List<ExcludeOptionDTO> getExcludeSelectOption(String roleId) { public List<UserExcludeOptionDTO> getExcludeSelectOptionWithLimit(String roleId, String keyword) {
// 查询所有用户选项 // 查询所有用户选项
List<ExcludeOptionDTO> selectOptions = baseUserService.getExcludeSelectOption(); List<UserExcludeOptionDTO> selectOptions = baseUserService.getExcludeSelectOptionWithLimit(keyword);
// 查询已经关联的用户ID // 查询已经关联的用户ID
Set<String> excludeUserIds = baseUserRoleRelationMapper.getUserIdByRoleId(roleId) Set<String> excludeUserIds = baseUserRoleRelationMapper.getUserIdByRoleId(roleId)
.stream() .stream()

View File

@ -425,12 +425,8 @@ public class BaseUserService {
return false; return false;
} }
public List<ExcludeOptionDTO> getExcludeSelectOption() { public List<UserExcludeOptionDTO> getExcludeSelectOptionWithLimit(String keyword) {
return baseUserMapper.getExcludeSelectOption(); return baseUserMapper.getExcludeSelectOptionWithLimit(keyword);
}
public List<OptionDTO> getSelectOptionWithDeleted() {
return baseUserMapper.getSelectOptionWithDeleted();
} }
public List<OptionDTO> getSelectOptionByIdsWithDeleted(List<String> ids) { public List<OptionDTO> getSelectOptionByIdsWithDeleted(List<String> ids) {

View File

@ -3,7 +3,7 @@ package io.metersphere.system.controller;
import com.github.pagehelper.Page; import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageHelper;
import io.metersphere.sdk.constants.PermissionConstants; import io.metersphere.sdk.constants.PermissionConstants;
import io.metersphere.sdk.dto.ExcludeOptionDTO; import io.metersphere.sdk.dto.UserExcludeOptionDTO;
import io.metersphere.sdk.dto.UserRoleRelationUserDTO; 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;
@ -16,7 +16,6 @@ import io.metersphere.system.service.GlobalUserRoleRelationLogService;
import io.metersphere.system.service.GlobalUserRoleRelationService; import io.metersphere.system.service.GlobalUserRoleRelationService;
import io.metersphere.validation.groups.Created; import io.metersphere.validation.groups.Created;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
@ -66,8 +65,10 @@ public class GlobalUserRoleRelationController {
@GetMapping("/user/option/{roleId}") @GetMapping("/user/option/{roleId}")
@Operation(summary = "系统设置-系统-用户组-用户关联关系-获取需要关联的用户选项") @Operation(summary = "系统设置-系统-用户组-用户关联关系-获取需要关联的用户选项")
@RequiresPermissions(value = {PermissionConstants.SYSTEM_USER_ROLE_READ}) @RequiresPermissions(value = {PermissionConstants.SYSTEM_USER_ROLE_READ})
public List<ExcludeOptionDTO> getSelectOption(@Parameter(description = "用户组ID", schema = @Schema(requiredMode = Schema.RequiredMode.REQUIRED)) public List<UserExcludeOptionDTO> getSelectOption(@Schema(description = "用户组ID", requiredMode = Schema.RequiredMode.REQUIRED)
@PathVariable String roleId) { @PathVariable String roleId,
return globalUserRoleRelationService.getExcludeSelectOption(roleId); @Schema(description = "查询关键字,根据邮箱和用户名查询", requiredMode = Schema.RequiredMode.REQUIRED)
@RequestParam(value = "keyword", required = false) String keyword) {
return globalUserRoleRelationService.getExcludeSelectOption(roleId, keyword);
} }
} }

View File

@ -1,8 +1,8 @@
package io.metersphere.system.service; package io.metersphere.system.service;
import io.metersphere.sdk.constants.UserRoleScope; import io.metersphere.sdk.constants.UserRoleScope;
import io.metersphere.sdk.dto.ExcludeOptionDTO;
import io.metersphere.sdk.dto.TableBatchProcessResponse; import io.metersphere.sdk.dto.TableBatchProcessResponse;
import io.metersphere.sdk.dto.UserExcludeOptionDTO;
import io.metersphere.sdk.dto.UserRoleRelationUserDTO; import io.metersphere.sdk.dto.UserRoleRelationUserDTO;
import io.metersphere.sdk.dto.request.GlobalUserRoleRelationUpdateRequest; import io.metersphere.sdk.dto.request.GlobalUserRoleRelationUpdateRequest;
import io.metersphere.sdk.exception.MSException; import io.metersphere.sdk.exception.MSException;
@ -147,8 +147,8 @@ public class GlobalUserRoleRelationService extends BaseUserRoleRelationService {
} }
} }
public List<ExcludeOptionDTO> getExcludeSelectOption(String roleId) { public List<UserExcludeOptionDTO> getExcludeSelectOption(String roleId, String keyword) {
baseUserRoleService.getWithCheck(roleId); baseUserRoleService.getWithCheck(roleId);
return super.getExcludeSelectOption(roleId); return super.getExcludeSelectOptionWithLimit(roleId, keyword);
} }
} }

View File

@ -3,7 +3,7 @@ package io.metersphere.system.controller;
import io.metersphere.sdk.base.BaseTest; import io.metersphere.sdk.base.BaseTest;
import io.metersphere.sdk.constants.PermissionConstants; import io.metersphere.sdk.constants.PermissionConstants;
import io.metersphere.sdk.constants.UserRoleScope; import io.metersphere.sdk.constants.UserRoleScope;
import io.metersphere.sdk.dto.ExcludeOptionDTO; import io.metersphere.sdk.dto.UserExcludeOptionDTO;
import io.metersphere.sdk.dto.UserRoleRelationUserDTO; 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.constants.OperationLogType; import io.metersphere.sdk.log.constants.OperationLogType;
@ -22,6 +22,7 @@ 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.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.*; import org.junit.jupiter.api.*;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
@ -155,8 +156,20 @@ class GlobalUserRoleRelationControllerTests extends BaseTest {
// @@正常请求 // @@正常请求
MvcResult mvcResult = this.requestGetWithOkAndReturn(USER_OPTION, ADMIN.getValue()); MvcResult mvcResult = this.requestGetWithOkAndReturn(USER_OPTION, ADMIN.getValue());
// 校验请求数据 // 校验请求数据
List<ExcludeOptionDTO> options = getResultDataArray(mvcResult, ExcludeOptionDTO.class); assertSelectOptionResult(mvcResult, null);
List<ExcludeOptionDTO> excludeSelectOption = baseUserService.getExcludeSelectOption();
// 校验检索
String keyword = "a";
MvcResult searchMvcResult = this.requestGetWithOkAndReturn(USER_OPTION + "?keyword={1}", ADMIN.getValue(), keyword);
assertSelectOptionResult(searchMvcResult, keyword);
// @@校验 NOT_FOUND 异常
assertErrorCode(this.requestGet(USER_OPTION, "111"), NOT_FOUND);
}
private void assertSelectOptionResult(MvcResult mvcResult, String keyword) throws Exception {
List<UserExcludeOptionDTO> options = getResultDataArray(mvcResult, UserExcludeOptionDTO.class);
List<UserExcludeOptionDTO> excludeSelectOption = baseUserService.getExcludeSelectOptionWithLimit(keyword);
Set<String> excludeUserIds = baseUserRoleRelationMapper.getUserIdByRoleId(ADMIN.getValue()) Set<String> excludeUserIds = baseUserRoleRelationMapper.getUserIdByRoleId(ADMIN.getValue())
.stream() .stream()
.collect(Collectors.toSet()); .collect(Collectors.toSet());
@ -174,8 +187,11 @@ class GlobalUserRoleRelationControllerTests extends BaseTest {
Assertions.assertTrue(item.getExclude() == excludeUserIds.contains(item.getId())); Assertions.assertTrue(item.getExclude() == excludeUserIds.contains(item.getId()));
}); });
// @@校验 NOT_FOUND 异常 // 校验检索
assertErrorCode(this.requestGet(USER_OPTION, "111"), NOT_FOUND); if (StringUtils.isNotBlank(keyword)) {
Assertions.assertTrue(options.stream().anyMatch(item -> StringUtils.containsIgnoreCase(item.getName(), keyword)
|| StringUtils.containsIgnoreCase(item.getEmail(), keyword)));
}
} }
@Test @Test