diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/ExcludeOptionDTO.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/ExcludeOptionDTO.java new file mode 100644 index 0000000000..c3a3952c16 --- /dev/null +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/ExcludeOptionDTO.java @@ -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 ExcludeOptionDTO extends OptionDTO { + @Schema(description = "是否已经关联过") + private Boolean exclude = false; +} 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 index e1d673854b..7acc34845a 100644 --- 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 @@ -1,5 +1,6 @@ package io.metersphere.sdk.dto; +import io.swagger.v3.oas.annotations.media.Schema; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @@ -8,6 +9,8 @@ import lombok.NoArgsConstructor; @AllArgsConstructor @NoArgsConstructor public class OptionDTO { + @Schema(description = "选项ID") private String id; + @Schema(description = "选项名称") private String name; } 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 91ca8ba751..22d0d72f1b 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.ExcludeOptionDTO; import io.metersphere.sdk.dto.OptionDTO; import io.metersphere.sdk.dto.UserDTO; import io.metersphere.system.domain.User; @@ -28,4 +29,6 @@ public interface BaseUserMapper { long deleteUser(String id, String deleteUser, long deleteTime); List selectUserOptionByIds(List userIds); + + List getExcludeSelectOption(); } 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 42a64913a6..28a40bde22 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 @@ -91,4 +91,8 @@ #{id} + \ No newline at end of file diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/BaseUserRoleRelationMapper.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/BaseUserRoleRelationMapper.java index e0638c0320..1607148881 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/BaseUserRoleRelationMapper.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/BaseUserRoleRelationMapper.java @@ -1,5 +1,6 @@ package io.metersphere.sdk.mapper; +import io.metersphere.sdk.dto.ExcludeOptionDTO; import io.metersphere.system.domain.UserRoleRelation; import org.apache.ibatis.annotations.Param; @@ -8,5 +9,7 @@ import java.util.List; public interface BaseUserRoleRelationMapper { List getUserIdAndSourceIdByUserIds(@Param("userIds") List userIds); - List getUserIdRoleId(@Param("roleId") String roleId); + List getUserIdByRoleId(@Param("roleId") String roleId); + + List getSelectOption(@Param("roleId") String roleId); } diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/BaseUserRoleRelationMapper.xml b/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/BaseUserRoleRelationMapper.xml index c1a72f3dc2..a570573f79 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/BaseUserRoleRelationMapper.xml +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/BaseUserRoleRelationMapper.xml @@ -9,9 +9,13 @@ #{userId} - select user_id from user_role_relation where role_id = #{roleId} + \ No newline at end of file diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/BaseUserRoleRelationService.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/BaseUserRoleRelationService.java index fdf3c954a8..51ba6e53da 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/BaseUserRoleRelationService.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/BaseUserRoleRelationService.java @@ -1,5 +1,6 @@ package io.metersphere.sdk.service; +import io.metersphere.sdk.dto.ExcludeOptionDTO; import io.metersphere.sdk.exception.MSException; import io.metersphere.sdk.mapper.BaseUserRoleRelationMapper; import io.metersphere.system.domain.UserRole; @@ -15,7 +16,9 @@ import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.List; +import java.util.Set; import java.util.UUID; +import java.util.stream.Collectors; import static io.metersphere.sdk.constants.InternalUserRole.ADMIN; import static io.metersphere.sdk.controller.handler.result.CommonResultCode.*; @@ -34,6 +37,8 @@ public class BaseUserRoleRelationService { @Resource @Lazy protected BaseUserRoleService baseUserRoleService; + @Resource + private BaseUserService baseUserService; protected UserRoleRelation add(UserRoleRelation userRoleRelation) { checkExist(userRoleRelation); @@ -102,8 +107,8 @@ public class BaseUserRoleRelationService { return null; } - public List getUserIdRoleId(String roleId) { - return baseUserRoleRelationMapper.getUserIdRoleId(roleId); + public List getUserIdByRoleId(String roleId) { + return baseUserRoleRelationMapper.getUserIdByRoleId(roleId); } public List getUserIdAndSourceIdByUserIds(List userIds) { @@ -119,4 +124,26 @@ public class BaseUserRoleRelationService { } userRoleRelationMapper.batchInsert(addRelations); } + + /** + * 获取关联用户的下拉框选项 + * 已经关联过的用户,exclude 标记为 true + * @param roleId + * @return + */ + public List getExcludeSelectOption(String roleId) { + // 查询所有用户选项 + List selectOptions = baseUserService.getExcludeSelectOption(); + // 查询已经关联的用户ID + Set excludeUserIds = baseUserRoleRelationMapper.getUserIdByRoleId(roleId) + .stream() + .collect(Collectors.toSet()); + // 标记已经关联的用户 + selectOptions.forEach((excludeOption) -> { + if (excludeUserIds.contains(excludeOption.getId())) { + excludeOption.setExclude(true); + } + }); + return selectOptions; + } } diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/BaseUserRoleService.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/BaseUserRoleService.java index 4cfb671827..7a2ac6d797 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/BaseUserRoleService.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/BaseUserRoleService.java @@ -199,7 +199,7 @@ public class BaseUserRoleService { public void checkOneLimitRole(String roleId, String defaultRoleId, String currentUserId) { // 查询要删除的用户组关联的用户ID - List userIds = baseUserRoleRelationService.getUserIdRoleId(roleId); + List userIds = baseUserRoleRelationService.getUserIdByRoleId(roleId); if (CollectionUtils.isEmpty(userIds)) { return; diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/BaseUserService.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/BaseUserService.java index 675689cafb..60fd601580 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/BaseUserService.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/BaseUserService.java @@ -407,4 +407,8 @@ public class BaseUserService { return false; } + + public List getExcludeSelectOption() { + return baseUserMapper.getExcludeSelectOption(); + } } 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 6f7c85a961..e57b25b809 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 @@ -3,6 +3,7 @@ package io.metersphere.system.controller; import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; import io.metersphere.sdk.constants.PermissionConstants; +import io.metersphere.sdk.dto.ExcludeOptionDTO; import io.metersphere.sdk.dto.UserRoleRelationUserDTO; import io.metersphere.sdk.dto.request.GlobalUserRoleRelationUpdateRequest; import io.metersphere.sdk.log.annotation.Log; @@ -16,6 +17,8 @@ import io.metersphere.system.service.GlobalUserRoleRelationLogService; import io.metersphere.system.service.GlobalUserRoleRelationService; import io.metersphere.validation.groups.Created; import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.annotation.Resource; import org.apache.shiro.authz.annotation.RequiresPermissions; @@ -69,4 +72,12 @@ public class GlobalUserRoleRelationController { public void delete(@PathVariable String id) { globalUserRoleRelationService.delete(id); } + + @GetMapping("/user/option/{roleId}") + @Operation(summary = "系统用户组-用户下拉选项") + @RequiresPermissions(value = {PermissionConstants.SYSTEM_USER_ROLE_READ}) + public List getSelectOption(@Parameter(description = "用户组ID", schema = @Schema(requiredMode = Schema.RequiredMode.REQUIRED)) + @PathVariable String roleId) { + return globalUserRoleRelationService.getExcludeSelectOption(roleId); + } } 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 b4f9887b55..ab874f6349 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 @@ -2,10 +2,13 @@ package io.metersphere.system.controller; import io.metersphere.sdk.base.BaseTest; import io.metersphere.sdk.constants.PermissionConstants; +import io.metersphere.sdk.dto.ExcludeOptionDTO; import io.metersphere.sdk.dto.UserRoleRelationUserDTO; import io.metersphere.sdk.dto.request.GlobalUserRoleRelationUpdateRequest; import io.metersphere.sdk.log.constants.OperationLogType; +import io.metersphere.sdk.mapper.BaseUserRoleRelationMapper; import io.metersphere.sdk.service.BaseUserRoleService; +import io.metersphere.sdk.service.BaseUserService; import io.metersphere.sdk.util.Pager; import io.metersphere.system.controller.param.GlobalUserRoleRelationQueryRequestDefinition; import io.metersphere.system.controller.param.GlobalUserRoleRelationUpdateRequestDefinition; @@ -42,9 +45,16 @@ class GlobalUserRoleRelationControllerTests extends BaseTest { // 保存创建的数据,方便之后的修改和删除测试使用 private static UserRoleRelation addUserRoleRelation; + + protected static final String USER_OPTION = "user/option/{0}"; + @Resource private UserRoleMapper userRoleMapper; @Resource + private BaseUserService baseUserService; + @Resource + private BaseUserRoleRelationMapper baseUserRoleRelationMapper; + @Resource private UserRoleRelationMapper userRoleRelationMapper; @Override protected String getBasePath() { @@ -135,6 +145,25 @@ class GlobalUserRoleRelationControllerTests extends BaseTest { @Test @Order(1) + public void getExcludeSelectOption() throws Exception { + // @@正常请求 + MvcResult mvcResult = this.requestGetWithOkAndReturn(USER_OPTION, ADMIN.getValue()); + // 校验请求数据 + List options = getResultDataArray(mvcResult, ExcludeOptionDTO.class); + List excludeSelectOption = baseUserService.getExcludeSelectOption(); + Set excludeUserIds = baseUserRoleRelationMapper.getUserIdByRoleId(ADMIN.getValue()) + .stream() + .collect(Collectors.toSet()); + // 校验数量 + Assertions.assertTrue(options.size() == excludeSelectOption.size()); + options.forEach(item -> { + // 校验 exclude 字段 + Assertions.assertTrue(item.getExclude() == excludeUserIds.contains(item.getId())); + }); + } + + @Test + @Order(2) void delete() throws Exception { // @@请求成功 this.requestGetWithOk(DEFAULT_DELETE, addUserRoleRelation.getId());