refactor(系统设置): 优化用户组下拉成员模糊匹配

This commit is contained in:
song-cc-rock 2023-09-15 16:39:12 +08:00 committed by 刘瑞斌
parent cea25c2011
commit 988de5f9f7
7 changed files with 105 additions and 111 deletions

View File

@ -21,6 +21,7 @@ import io.metersphere.sdk.util.Pager;
import io.metersphere.sdk.util.SessionUtils;
import io.metersphere.system.domain.User;
import io.metersphere.system.domain.UserRole;
import io.metersphere.system.service.UserRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
@ -40,6 +41,9 @@ public class ProjectUserRoleController {
@Resource
ProjectUserRoleService projectUserRoleService;
@Resource
UserRoleService userRoleService;
@PostMapping("/list")
@Operation(summary = "项目管理-项目与权限-用户组-获取用户组列表")
@ -97,13 +101,16 @@ public class ProjectUserRoleController {
@GetMapping("/get-member/option/{projectId}/{roleId}")
@Operation(summary = "项目管理-项目与权限-用户组-获取成员下拉选项")
@RequiresPermissions(value = {PermissionConstants.PROJECT_GROUP_READ})
@Parameters({
@Parameter(name = "projectId", description = "当前项目ID", schema = @Schema(requiredMode = Schema.RequiredMode.REQUIRED)),
@Parameter(name = "roleId", description = "用户组ID", schema = @Schema(requiredMode = Schema.RequiredMode.REQUIRED))
})
@RequiresPermissions(value = {PermissionConstants.PROJECT_GROUP_READ})
public List<UserExtend> getMember(@PathVariable String projectId, @PathVariable String roleId) {
return projectUserRoleService.getMember(projectId, roleId);
public List<UserExtend> getMember(@PathVariable String projectId,
@PathVariable String roleId,
@Schema(description = "查询关键字,根据邮箱和用户名查询")
@RequestParam(required = false) String keyword) {
return userRoleService.getMember(projectId, roleId, keyword);
}
@PostMapping("/list-member")

View File

@ -8,20 +8,20 @@ import io.metersphere.project.request.ProjectUserRoleRequest;
import io.metersphere.sdk.constants.InternalUserRole;
import io.metersphere.sdk.constants.UserRoleType;
import io.metersphere.sdk.dto.PermissionDefinitionItem;
import io.metersphere.sdk.dto.UserExtend;
import io.metersphere.sdk.dto.request.PermissionSettingUpdateRequest;
import io.metersphere.sdk.exception.MSException;
import io.metersphere.system.service.BaseUserRoleService;
import io.metersphere.system.uid.UUID;
import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.sdk.util.Translator;
import io.metersphere.system.domain.*;
import io.metersphere.system.domain.User;
import io.metersphere.system.domain.UserRole;
import io.metersphere.system.domain.UserRoleRelation;
import io.metersphere.system.domain.UserRoleRelationExample;
import io.metersphere.system.mapper.UserMapper;
import io.metersphere.system.mapper.UserRoleMapper;
import io.metersphere.system.mapper.UserRoleRelationMapper;
import io.metersphere.system.service.BaseUserRoleService;
import io.metersphere.system.uid.UUID;
import jakarta.annotation.Resource;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -100,47 +100,6 @@ public class ProjectUserRoleService extends BaseUserRoleService {
super.delete(userRole, InternalUserRole.PROJECT_MEMBER.getValue(), currentUserId, userRole.getScopeId());
}
public List<UserExtend> getMember(String projectId, String roleId) {
List<UserExtend> userExtends = new ArrayList<>();
// 查询项目下所有用户关系
UserRoleRelationExample example = new UserRoleRelationExample();
example.createCriteria().andSourceIdEqualTo(projectId);
List<UserRoleRelation> userRoleRelations = userRoleRelationMapper.selectByExample(example);
if (CollectionUtils.isNotEmpty(userRoleRelations)) {
Map<String, List<String>> userRoleMap = userRoleRelations.stream().collect(Collectors.groupingBy(UserRoleRelation::getUserId,
Collectors.mapping(UserRoleRelation::getRoleId, Collectors.toList())));
userRoleMap.forEach((k, v) -> {
UserExtend userExtend = new UserExtend();
userExtend.setId(k);
v.forEach(roleItem -> {
if (StringUtils.equals(roleItem, roleId)) {
// 该用户已存在用户组关系, 设置为选中状态
userExtend.setCheckRoleFlag(true);
}
});
userExtends.add(userExtend);
});
// 设置用户信息, 用户不存在或者已删除, 则不展示
List<String> userIds = userExtends.stream().map(UserExtend::getId).toList();
UserExample userExample = new UserExample();
userExample.createCriteria().andIdIn(userIds).andDeletedEqualTo(false);
List<User> users = userMapper.selectByExample(userExample);
if (CollectionUtils.isNotEmpty(users)) {
Map<String, User> userMap = users.stream().collect(Collectors.toMap(User::getId, user -> user));
userExtends.removeIf(userExtend -> {
if (userMap.containsKey(userExtend.getId())) {
BeanUtils.copyBean(userExtend, userMap.get(userExtend.getId()));
return false;
}
return true;
});
} else {
userExtends.clear();
}
}
return userExtends;
}
public List<User> listMember(ProjectUserRoleMemberRequest request) {
return extProjectUserRoleMapper.listProjectRoleMember(request);
}

View File

@ -4,6 +4,7 @@ import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.metersphere.sdk.constants.PermissionConstants;
import io.metersphere.sdk.dto.PermissionDefinitionItem;
import io.metersphere.sdk.dto.UserExtend;
import io.metersphere.sdk.dto.request.PermissionSettingUpdateRequest;
import io.metersphere.system.log.annotation.Log;
import io.metersphere.system.log.constants.OperationLogType;
@ -13,14 +14,15 @@ import io.metersphere.sdk.util.Pager;
import io.metersphere.sdk.util.SessionUtils;
import io.metersphere.system.domain.User;
import io.metersphere.system.domain.UserRole;
import io.metersphere.sdk.dto.UserExtend;
import io.metersphere.system.request.OrganizationUserRoleEditRequest;
import io.metersphere.system.request.OrganizationUserRoleMemberEditRequest;
import io.metersphere.system.request.OrganizationUserRoleMemberRequest;
import io.metersphere.system.service.OrganizationUserRoleLogService;
import io.metersphere.system.service.OrganizationUserRoleService;
import io.metersphere.system.service.UserRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
@ -40,6 +42,8 @@ public class OrganizationUserRoleController {
@Resource
OrganizationUserRoleService organizationUserRoleService;
@Resource
UserRoleService userRoleService;
@GetMapping("/list/{organizationId}")
@Operation(summary = "系统设置-组织-用户组-获取用户组列表")
@ -98,8 +102,15 @@ public class OrganizationUserRoleController {
@GetMapping("/get-member/option/{organizationId}/{roleId}")
@Operation(summary = "系统设置-组织-用户组-获取成员下拉选项")
@RequiresPermissions(value = {PermissionConstants.ORGANIZATION_USER_ROLE_READ})
public List<UserExtend> getMember(@PathVariable String organizationId, @PathVariable String roleId) {
return organizationUserRoleService.getMember(organizationId, roleId);
@Parameters({
@Parameter(name = "organizationId", description = "组织ID", schema = @Schema(requiredMode = Schema.RequiredMode.REQUIRED)),
@Parameter(name = "roleId", description = "用户组ID", schema = @Schema(requiredMode = Schema.RequiredMode.REQUIRED))
})
public List<UserExtend> getMember(@PathVariable String organizationId,
@PathVariable String roleId,
@Schema(description = "查询关键字,根据邮箱和用户名查询")
@RequestParam(required = false) String keyword) {
return userRoleService.getMember(organizationId, roleId, keyword);
}
@PostMapping("/list-member")

View File

@ -1,7 +1,7 @@
package io.metersphere.system.mapper;
import io.metersphere.system.domain.User;
import io.metersphere.sdk.dto.UserExtend;
import io.metersphere.system.domain.User;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@ -13,4 +13,13 @@ public interface ExtUserMapper {
List<User> getUserListByOrgId(@Param("sourceId") String sourceId, @Param("keyword") String keyword);
List<User> selectUserList(@Param("keyword") String keyword);
/**
* 获取用户组下的用户(组织用户或项目用户)
*
* @param ids 用户ID集合
* @param keyword 关键字
* @return 用户列表
*/
List<User> getRoleUserByParam(@Param("ids") List<String> ids, @Param("keyword") String keyword);
}

View File

@ -45,5 +45,15 @@
limit 100
</select>
<select id="getRoleUserByParam" resultType="io.metersphere.system.domain.User">
select * from user
where deleted = 0 and id in
<foreach collection="ids" item="id" open="(" separator="," close=")">
#{id}
</foreach>
<if test="keyword != null and keyword != ''">
and (name like concat('%', #{keyword}, '%') or email like concat('%', #{keyword}, '%'))
</if>
limit 100
</select>
</mapper>

View File

@ -4,30 +4,20 @@ import io.metersphere.sdk.constants.InternalUserRole;
import io.metersphere.sdk.constants.UserRoleEnum;
import io.metersphere.sdk.constants.UserRoleType;
import io.metersphere.sdk.dto.PermissionDefinitionItem;
import io.metersphere.sdk.dto.UserExtend;
import io.metersphere.sdk.dto.request.PermissionSettingUpdateRequest;
import io.metersphere.sdk.exception.MSException;
import io.metersphere.system.uid.UUID;
import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.sdk.util.Translator;
import io.metersphere.system.domain.*;
import io.metersphere.system.mapper.ExtUserRoleMapper;
import io.metersphere.system.mapper.UserMapper;
import io.metersphere.system.mapper.UserRoleMapper;
import io.metersphere.system.mapper.UserRoleRelationMapper;
import io.metersphere.system.mapper.*;
import io.metersphere.system.request.OrganizationUserRoleMemberEditRequest;
import io.metersphere.system.request.OrganizationUserRoleMemberRequest;
import io.metersphere.system.uid.UUID;
import jakarta.annotation.Resource;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static io.metersphere.system.controller.result.SystemResultCode.NO_ORG_USER_ROLE_PERMISSION;
@ -48,6 +38,8 @@ public class OrganizationUserRoleService extends BaseUserRoleService {
ExtUserRoleMapper extUserRoleMapper;
@Resource
UserRoleRelationMapper userRoleRelationMapper;
@Resource
ExtUserMapper extUserMapper;
public List<UserRole> list(String organizationId) {
UserRoleExample example = new UserRoleExample();
@ -83,47 +75,6 @@ public class OrganizationUserRoleService extends BaseUserRoleService {
super.delete(userRole, InternalUserRole.ORG_MEMBER.getValue(), currentUserId, userRole.getScopeId());
}
public List<UserExtend> getMember(String organizationId, String roleId) {
List<UserExtend> userExtends = new ArrayList<>();
// 查询组织下所有用户关系
UserRoleRelationExample example = new UserRoleRelationExample();
example.createCriteria().andSourceIdEqualTo(organizationId);
List<UserRoleRelation> userRoleRelations = userRoleRelationMapper.selectByExample(example);
if (CollectionUtils.isNotEmpty(userRoleRelations)) {
Map<String, List<String>> userRoleMap = userRoleRelations.stream().collect(Collectors.groupingBy(UserRoleRelation::getUserId,
Collectors.mapping(UserRoleRelation::getRoleId, Collectors.toList())));
userRoleMap.forEach((k, v) -> {
UserExtend userExtend = new UserExtend();
userExtend.setId(k);
v.forEach(roleItem -> {
if (StringUtils.equals(roleItem, roleId)) {
// 该用户已存在用户组关系, 设置为选中状态
userExtend.setCheckRoleFlag(true);
}
});
userExtends.add(userExtend);
});
// 设置用户信息, 用户不存在或者已删除, 则不展示
List<String> userIds = userExtends.stream().map(UserExtend::getId).toList();
UserExample userExample = new UserExample();
userExample.createCriteria().andIdIn(userIds).andDeletedEqualTo(false);
List<User> users = userMapper.selectByExample(userExample);
if (CollectionUtils.isNotEmpty(users)) {
Map<String, User> userMap = users.stream().collect(Collectors.toMap(User::getId, user -> user));
userExtends.removeIf(userExtend -> {
if (userMap.containsKey(userExtend.getId())) {
BeanUtils.copyBean(userExtend, userMap.get(userExtend.getId()));
return false;
}
return true;
});
} else {
userExtends.clear();
}
}
return userExtends;
}
public List<User> listMember(OrganizationUserRoleMemberRequest request) {
return extUserRoleMapper.listOrganizationRoleMember(request);
}

View File

@ -1,23 +1,31 @@
package io.metersphere.system.service;
import io.metersphere.system.domain.UserRole;
import io.metersphere.system.domain.UserRoleExample;
import io.metersphere.system.domain.UserRoleRelation;
import io.metersphere.sdk.dto.UserExtend;
import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.system.domain.*;
import io.metersphere.system.mapper.ExtUserMapper;
import io.metersphere.system.mapper.UserRoleMapper;
import io.metersphere.system.mapper.UserRoleRelationMapper;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Service
@Transactional(rollbackFor = Exception.class)
public class UserRoleService {
@Resource
private ExtUserMapper extUserMapper;
@Resource
private UserRoleMapper userRoleMapper;
@Resource
private UserRoleRelationMapper userRoleRelationMapper;
public List<UserRole> selectByUserRoleRelations(List<UserRoleRelation> userRoleRelations) {
if (CollectionUtils.isNotEmpty(userRoleRelations)) {
@ -29,4 +37,43 @@ public class UserRoleService {
return new ArrayList<>();
}
}
public List<UserExtend> getMember(String sourceId, String roleId, String keyword) {
List<UserExtend> userExtends = new ArrayList<>();
// 查询组织或项目下所有用户关系
UserRoleRelationExample example = new UserRoleRelationExample();
example.createCriteria().andSourceIdEqualTo(sourceId);
List<UserRoleRelation> userRoleRelations = userRoleRelationMapper.selectByExample(example);
if (org.apache.commons.collections.CollectionUtils.isNotEmpty(userRoleRelations)) {
Map<String, List<String>> userRoleMap = userRoleRelations.stream().collect(Collectors.groupingBy(UserRoleRelation::getUserId,
Collectors.mapping(UserRoleRelation::getRoleId, Collectors.toList())));
userRoleMap.forEach((k, v) -> {
UserExtend userExtend = new UserExtend();
userExtend.setId(k);
v.forEach(roleItem -> {
if (StringUtils.equals(roleItem, roleId)) {
// 该用户已存在用户组关系, 设置为选中状态
userExtend.setCheckRoleFlag(true);
}
});
userExtends.add(userExtend);
});
// 设置用户信息, 用户不存在或者已删除, 则不展示
List<String> userIds = userExtends.stream().map(UserExtend::getId).toList();
List<User> users = extUserMapper.getRoleUserByParam(userIds, keyword);
if (org.apache.commons.collections.CollectionUtils.isNotEmpty(users)) {
Map<String, User> userMap = users.stream().collect(Collectors.toMap(User::getId, user -> user));
userExtends.removeIf(userExtend -> {
if (userMap.containsKey(userExtend.getId())) {
BeanUtils.copyBean(userExtend, userMap.get(userExtend.getId()));
return false;
}
return true;
});
} else {
userExtends.clear();
}
}
return userExtends;
}
}