fix(系统设置): 添加用户组关联用户时,查询用户选项接口

This commit is contained in:
jianxing 2023-08-21 18:34:52 +08:00 committed by fit2-zhao
parent 57c6b654df
commit b8784029d2
11 changed files with 111 additions and 5 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 ExcludeOptionDTO extends OptionDTO {
@Schema(description = "是否已经关联过")
private Boolean exclude = false;
}

View File

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

View File

@ -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<OptionDTO> selectUserOptionByIds(List<String> userIds);
List<ExcludeOptionDTO> getExcludeSelectOption();
}

View File

@ -91,4 +91,8 @@
#{id}
</foreach>
</select>
<select id="getExcludeSelectOption" resultType="io.metersphere.sdk.dto.ExcludeOptionDTO">
select id, name
from user
</select>
</mapper>

View File

@ -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<UserRoleRelation> getUserIdAndSourceIdByUserIds(@Param("userIds") List<String> userIds);
List<String> getUserIdRoleId(@Param("roleId") String roleId);
List<String> getUserIdByRoleId(@Param("roleId") String roleId);
List<ExcludeOptionDTO> getSelectOption(@Param("roleId") String roleId);
}

View File

@ -9,9 +9,13 @@
#{userId}
</foreach>
</select>
<select id="getUserIdRoleId" resultType="java.lang.String">
<select id="getUserIdByRoleId" resultType="java.lang.String">
select user_id
from user_role_relation
where role_id = #{roleId}
</select>
<select id="getSelectOption" resultType="io.metersphere.sdk.dto.ExcludeOptionDTO">
select user_id as id, user_name as name
from user
</select>
</mapper>

View File

@ -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<String> getUserIdRoleId(String roleId) {
return baseUserRoleRelationMapper.getUserIdRoleId(roleId);
public List<String> getUserIdByRoleId(String roleId) {
return baseUserRoleRelationMapper.getUserIdByRoleId(roleId);
}
public List<UserRoleRelation> getUserIdAndSourceIdByUserIds(List<String> userIds) {
@ -119,4 +124,26 @@ public class BaseUserRoleRelationService {
}
userRoleRelationMapper.batchInsert(addRelations);
}
/**
* 获取关联用户的下拉框选项
* 已经关联过的用户exclude 标记为 true
* @param roleId
* @return
*/
public List<ExcludeOptionDTO> getExcludeSelectOption(String roleId) {
// 查询所有用户选项
List<ExcludeOptionDTO> selectOptions = baseUserService.getExcludeSelectOption();
// 查询已经关联的用户ID
Set<String> excludeUserIds = baseUserRoleRelationMapper.getUserIdByRoleId(roleId)
.stream()
.collect(Collectors.toSet());
// 标记已经关联的用户
selectOptions.forEach((excludeOption) -> {
if (excludeUserIds.contains(excludeOption.getId())) {
excludeOption.setExclude(true);
}
});
return selectOptions;
}
}

View File

@ -199,7 +199,7 @@ public class BaseUserRoleService {
public void checkOneLimitRole(String roleId, String defaultRoleId, String currentUserId) {
// 查询要删除的用户组关联的用户ID
List<String> userIds = baseUserRoleRelationService.getUserIdRoleId(roleId);
List<String> userIds = baseUserRoleRelationService.getUserIdByRoleId(roleId);
if (CollectionUtils.isEmpty(userIds)) {
return;

View File

@ -407,4 +407,8 @@ public class BaseUserService {
return false;
}
public List<ExcludeOptionDTO> getExcludeSelectOption() {
return baseUserMapper.getExcludeSelectOption();
}
}

View File

@ -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<ExcludeOptionDTO> getSelectOption(@Parameter(description = "用户组ID", schema = @Schema(requiredMode = Schema.RequiredMode.REQUIRED))
@PathVariable String roleId) {
return globalUserRoleRelationService.getExcludeSelectOption(roleId);
}
}

View File

@ -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<ExcludeOptionDTO> options = getResultDataArray(mvcResult, ExcludeOptionDTO.class);
List<ExcludeOptionDTO> excludeSelectOption = baseUserService.getExcludeSelectOption();
Set<String> 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());