From 22446027e01a4938975ce843b023f27cce096347 Mon Sep 17 00:00:00 2001 From: song-cc-rock Date: Fri, 15 Sep 2023 17:59:45 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E7=B3=BB=E7=BB=9F=E8=AE=BE=E7=BD=AE):?= =?UTF-8?q?=20=E4=BC=98=E5=8C=96=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E5=8F=8A=E8=A1=A5=E5=85=85=E8=A6=86=E7=9B=96?= =?UTF-8?q?=E7=8E=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mapper/ExtProjectUserRoleMapper.java | 3 ++- .../project/mapper/ExtProjectUserRoleMapper.xml | 2 +- .../project/service/ProjectMemberService.java | 10 +++++----- .../project/service/ProjectUserRoleService.java | 5 +---- .../system/service/OrganizationService.java | 17 +++++++---------- .../SystemOrganizationControllerTests.java | 12 ++++++------ .../test/resources/dml/init_organization.sql | 4 +++- 7 files changed, 25 insertions(+), 28 deletions(-) diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/mapper/ExtProjectUserRoleMapper.java b/backend/services/project-management/src/main/java/io/metersphere/project/mapper/ExtProjectUserRoleMapper.java index 5305d32240..9a7bee50ad 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/mapper/ExtProjectUserRoleMapper.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/mapper/ExtProjectUserRoleMapper.java @@ -26,9 +26,10 @@ public interface ExtProjectUserRoleMapper { * 根据用户组ID获取用户组成员关系 * * @param roleIds 用户组ID集合 + * @param projectId 项目ID * @return 用户组成员关系 */ - List getRelationByRoleIds(@Param("roleIds") List roleIds); + List getRelationByRoleIds(@Param("projectId") String projectId, @Param("roleIds") List roleIds); /** * 获取项目成员列表 diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/mapper/ExtProjectUserRoleMapper.xml b/backend/services/project-management/src/main/java/io/metersphere/project/mapper/ExtProjectUserRoleMapper.xml index 1acda34176..a079efcba1 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/mapper/ExtProjectUserRoleMapper.xml +++ b/backend/services/project-management/src/main/java/io/metersphere/project/mapper/ExtProjectUserRoleMapper.xml @@ -17,7 +17,7 @@ select urr.* from user_role_relation urr left join user u on urr.user_id = u.id - where u.deleted = 0 and urr.role_id in + where u.deleted = 0 and urr.source_id = #{projectId} and urr.role_id in #{id} diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/service/ProjectMemberService.java b/backend/services/project-management/src/main/java/io/metersphere/project/service/ProjectMemberService.java index 7acdf68551..27ed30af20 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/service/ProjectMemberService.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/service/ProjectMemberService.java @@ -15,17 +15,17 @@ import io.metersphere.sdk.dto.LogDTO; import io.metersphere.sdk.dto.OptionDTO; import io.metersphere.sdk.dto.UserExtend; import io.metersphere.sdk.exception.MSException; -import io.metersphere.system.log.constants.OperationLogModule; -import io.metersphere.system.log.constants.OperationLogType; -import io.metersphere.system.log.service.OperationLogService; -import io.metersphere.system.uid.UUID; import io.metersphere.sdk.util.BeanUtils; import io.metersphere.sdk.util.JSON; import io.metersphere.sdk.util.Translator; import io.metersphere.system.domain.*; +import io.metersphere.system.log.constants.OperationLogModule; +import io.metersphere.system.log.constants.OperationLogType; +import io.metersphere.system.log.service.OperationLogService; import io.metersphere.system.mapper.UserMapper; import io.metersphere.system.mapper.UserRoleMapper; import io.metersphere.system.mapper.UserRoleRelationMapper; +import io.metersphere.system.uid.UUID; import jakarta.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -353,7 +353,7 @@ public class ProjectMemberService { private boolean isUserOrRoleNotExist(String userId, String roleId) { UserExample example = new UserExample(); example.createCriteria().andIdEqualTo(userId).andDeletedEqualTo(false); - return userMapper.selectByExample(example) == null || userRoleMapper.selectByPrimaryKey(roleId) == null; + return CollectionUtils.isEmpty(userMapper.selectByExample(example)) || userRoleMapper.selectByPrimaryKey(roleId) == null; } /** 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 20bdc6f968..2773522b89 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 @@ -56,7 +56,7 @@ public class ProjectUserRoleService extends BaseUserRoleService { return new ArrayList<>(); } List roleIds = roles.stream().map(ProjectUserRoleDTO::getId).toList(); - List relations = extProjectUserRoleMapper.getRelationByRoleIds(roleIds); + List relations = extProjectUserRoleMapper.getRelationByRoleIds(request.getProjectId(), roleIds); if (CollectionUtils.isNotEmpty(relations)) { Map countMap = relations.stream().collect(Collectors.groupingBy(UserRoleRelation::getRoleId, Collectors.counting())); roles.forEach(role -> { @@ -139,9 +139,6 @@ public class ProjectUserRoleService extends BaseUserRoleService { public List getPermissionSetting(String id) { UserRole userRole = get(id); - if (userRole == null) { - throw new MSException(Translator.get("user_role_not_exist")); - } checkProjectUserRole(userRole); return getPermissionSetting(userRole); } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/OrganizationService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/OrganizationService.java index caf6b64fc3..002f277b10 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/OrganizationService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/OrganizationService.java @@ -8,18 +8,18 @@ import io.metersphere.sdk.dto.LogDTO; import io.metersphere.sdk.dto.OptionDTO; import io.metersphere.sdk.dto.UserExtend; import io.metersphere.sdk.exception.MSException; -import io.metersphere.system.log.constants.OperationLogModule; -import io.metersphere.system.log.constants.OperationLogType; -import io.metersphere.system.log.service.OperationLogService; -import io.metersphere.system.uid.UUID; import io.metersphere.sdk.util.BeanUtils; import io.metersphere.sdk.util.JSON; -import io.metersphere.system.utils.ServiceUtils; import io.metersphere.sdk.util.Translator; import io.metersphere.system.domain.*; import io.metersphere.system.dto.*; +import io.metersphere.system.log.constants.OperationLogModule; +import io.metersphere.system.log.constants.OperationLogType; +import io.metersphere.system.log.service.OperationLogService; import io.metersphere.system.mapper.*; import io.metersphere.system.request.*; +import io.metersphere.system.uid.UUID; +import io.metersphere.system.utils.ServiceUtils; import jakarta.annotation.Resource; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; @@ -83,8 +83,8 @@ public class OrganizationService { if (CollectionUtils.isEmpty(organizationDTOS)) { return new ArrayList<>(); } - List organizations = buildExtraInfo(organizationDTOS); - return buildOrgAdminInfo(organizations); + List organizations = buildOrgAdminInfo(organizationDTOS); + return buildExtraInfo(organizations); } /** @@ -794,9 +794,6 @@ public class OrganizationService { * @return 组织列表 */ private List buildOrgAdminInfo(List organizationDTOS) { - if (CollectionUtils.isEmpty(organizationDTOS)) { - return organizationDTOS; - } organizationDTOS.forEach(organizationDTO -> { List orgAdminList = extOrganizationMapper.getOrgAdminList(organizationDTO.getId()); organizationDTO.setOrgAdmins(orgAdminList); diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/SystemOrganizationControllerTests.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/SystemOrganizationControllerTests.java index ffefc36d06..fce51fdd4f 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/SystemOrganizationControllerTests.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/SystemOrganizationControllerTests.java @@ -1,15 +1,15 @@ package io.metersphere.system.controller; -import io.metersphere.system.base.BaseTest; import io.metersphere.sdk.constants.PermissionConstants; import io.metersphere.sdk.constants.SessionConstants; -import io.metersphere.system.controller.handler.ResultHolder; import io.metersphere.sdk.dto.ProjectDTO; -import io.metersphere.system.log.constants.OperationLogType; +import io.metersphere.sdk.dto.UserExtend; import io.metersphere.sdk.util.JSON; import io.metersphere.sdk.util.Pager; +import io.metersphere.system.base.BaseTest; +import io.metersphere.system.controller.handler.ResultHolder; import io.metersphere.system.dto.OrganizationDTO; -import io.metersphere.sdk.dto.UserExtend; +import io.metersphere.system.log.constants.OperationLogType; import io.metersphere.system.request.OrganizationMemberRequest; import io.metersphere.system.request.OrganizationRequest; import io.metersphere.system.request.ProjectRequest; @@ -184,9 +184,9 @@ public class SystemOrganizationControllerTests extends BaseTest{ String sortData = sortResult.getResponse().getContentAsString(StandardCharsets.UTF_8); ResultHolder sortHolder = JSON.parseObject(sortData, ResultHolder.class); Pager sortPageData = JSON.parseObject(JSON.toJSONString(sortHolder.getData()), Pager.class); - // 返回值中取出第一条ID最大的数据, 并判断是否是default-admin + // 返回值中取出第一条ID最大的数据, 并判断是否是admin UserExtend userExtend1 = JSON.parseArray(JSON.toJSONString(sortPageData.getList()), UserExtend.class).get(0); - Assertions.assertTrue(StringUtils.contains(userExtend1.getId(), "default-admin")); + Assertions.assertTrue(StringUtils.contains(userExtend1.getId(), "admin")); // 权限校验 requestPostPermissionsTest(List.of(PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ, PermissionConstants.SYSTEM_USER_READ), ORGANIZATION_LIST_MEMBER, organizationRequest); diff --git a/backend/services/system-setting/src/test/resources/dml/init_organization.sql b/backend/services/system-setting/src/test/resources/dml/init_organization.sql index e69ebaa1a4..06197ed129 100644 --- a/backend/services/system-setting/src/test/resources/dml/init_organization.sql +++ b/backend/services/system-setting/src/test/resources/dml/init_organization.sql @@ -14,6 +14,8 @@ INSERT INTO organization(id, num, name, description, create_time, update_time, c INSERT INTO user(id, name, email, password, create_time, update_time, language, last_organization_id, phone, source, last_project_id, create_user, update_user) VALUE ('default-admin', 'default-Administrator', 'admin-default@metersphere.io', MD5('metersphere'), UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, NULL, NUll, '', 'LOCAL', NULL, 'admin', 'admin'); INSERT INTO user_role_relation (id, user_id, role_id, source_id, organization_id, create_time, create_user) VALUE - (UUID(), 'default-admin', 'org_admin', 'default-organization-2', 'default-organization-2', UNIX_TIMESTAMP() * 1000, 'admin'); + (UUID(), 'admin', 'org_admin', 'default-organization-2', 'default-organization-2', UNIX_TIMESTAMP() * 1000, 'admin'); +INSERT INTO user_role_relation (id, user_id, role_id, source_id, organization_id, create_time, create_user) VALUE + (UUID(), 'default-admin', 'org_admin', 'default-organization-3', 'default-organization-3', UNIX_TIMESTAMP() * 1000, 'admin'); INSERT INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUE ('default-project', null, 'default-organization-2', '默认项目', '系统默认创建的项目', 'admin', 'admin', UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000); \ No newline at end of file