diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/controller/ProjectUserRoleController.java b/backend/services/project-management/src/main/java/io/metersphere/project/controller/ProjectUserRoleController.java index c09942c2c5..4705835ef2 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/controller/ProjectUserRoleController.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/controller/ProjectUserRoleController.java @@ -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 getMember(@PathVariable String projectId, @PathVariable String roleId) { - return projectUserRoleService.getMember(projectId, roleId); + public List getMember(@PathVariable String projectId, + @PathVariable String roleId, + @Schema(description = "查询关键字,根据邮箱和用户名查询") + @RequestParam(required = false) String keyword) { + return userRoleService.getMember(projectId, roleId, keyword); } @PostMapping("/list-member") diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/service/ProjectUserRoleService.java b/backend/services/project-management/src/main/java/io/metersphere/project/service/ProjectUserRoleService.java index 607c95f667..20bdc6f968 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/service/ProjectUserRoleService.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/service/ProjectUserRoleService.java @@ -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 getMember(String projectId, String roleId) { - List userExtends = new ArrayList<>(); - // 查询项目下所有用户关系 - UserRoleRelationExample example = new UserRoleRelationExample(); - example.createCriteria().andSourceIdEqualTo(projectId); - List userRoleRelations = userRoleRelationMapper.selectByExample(example); - if (CollectionUtils.isNotEmpty(userRoleRelations)) { - Map> 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 userIds = userExtends.stream().map(UserExtend::getId).toList(); - UserExample userExample = new UserExample(); - userExample.createCriteria().andIdIn(userIds).andDeletedEqualTo(false); - List users = userMapper.selectByExample(userExample); - if (CollectionUtils.isNotEmpty(users)) { - Map 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 listMember(ProjectUserRoleMemberRequest request) { return extProjectUserRoleMapper.listProjectRoleMember(request); } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/OrganizationUserRoleController.java b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/OrganizationUserRoleController.java index 7508bef783..9ac6203e4a 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/OrganizationUserRoleController.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/OrganizationUserRoleController.java @@ -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 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 getMember(@PathVariable String organizationId, + @PathVariable String roleId, + @Schema(description = "查询关键字,根据邮箱和用户名查询") + @RequestParam(required = false) String keyword) { + return userRoleService.getMember(organizationId, roleId, keyword); } @PostMapping("/list-member") diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtUserMapper.java b/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtUserMapper.java index 34ad82f0e3..dad6196575 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtUserMapper.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtUserMapper.java @@ -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 getUserListByOrgId(@Param("sourceId") String sourceId, @Param("keyword") String keyword); List selectUserList(@Param("keyword") String keyword); + + /** + * 获取用户组下的用户(组织用户或项目用户) + * + * @param ids 用户ID集合 + * @param keyword 关键字 + * @return 用户列表 + */ + List getRoleUserByParam(@Param("ids") List ids, @Param("keyword") String keyword); } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtUserMapper.xml b/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtUserMapper.xml index 065aaa9a40..c05a7f5ac5 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtUserMapper.xml +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtUserMapper.xml @@ -45,5 +45,15 @@ limit 100 - + \ No newline at end of file diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/OrganizationUserRoleService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/OrganizationUserRoleService.java index 7ac3cbef46..35333d5659 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/OrganizationUserRoleService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/OrganizationUserRoleService.java @@ -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 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 getMember(String organizationId, String roleId) { - List userExtends = new ArrayList<>(); - // 查询组织下所有用户关系 - UserRoleRelationExample example = new UserRoleRelationExample(); - example.createCriteria().andSourceIdEqualTo(organizationId); - List userRoleRelations = userRoleRelationMapper.selectByExample(example); - if (CollectionUtils.isNotEmpty(userRoleRelations)) { - Map> 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 userIds = userExtends.stream().map(UserExtend::getId).toList(); - UserExample userExample = new UserExample(); - userExample.createCriteria().andIdIn(userIds).andDeletedEqualTo(false); - List users = userMapper.selectByExample(userExample); - if (CollectionUtils.isNotEmpty(users)) { - Map 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 listMember(OrganizationUserRoleMemberRequest request) { return extUserRoleMapper.listOrganizationRoleMember(request); } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/UserRoleService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/UserRoleService.java index 9116b4ae57..e193e3e94e 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/UserRoleService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/UserRoleService.java @@ -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 selectByUserRoleRelations(List userRoleRelations) { if (CollectionUtils.isNotEmpty(userRoleRelations)) { @@ -29,4 +37,43 @@ public class UserRoleService { return new ArrayList<>(); } } + + public List getMember(String sourceId, String roleId, String keyword) { + List userExtends = new ArrayList<>(); + // 查询组织或项目下所有用户关系 + UserRoleRelationExample example = new UserRoleRelationExample(); + example.createCriteria().andSourceIdEqualTo(sourceId); + List userRoleRelations = userRoleRelationMapper.selectByExample(example); + if (org.apache.commons.collections.CollectionUtils.isNotEmpty(userRoleRelations)) { + Map> 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 userIds = userExtends.stream().map(UserExtend::getId).toList(); + List users = extUserMapper.getRoleUserByParam(userIds, keyword); + if (org.apache.commons.collections.CollectionUtils.isNotEmpty(users)) { + Map 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; + } }