diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtUserGroupMapper.java b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtUserGroupMapper.java index 44f3b0153d..8d2bf2b0b5 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtUserGroupMapper.java +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtUserGroupMapper.java @@ -6,6 +6,7 @@ import io.metersphere.base.domain.User; import io.metersphere.controller.request.member.QueryMemberRequest; import io.metersphere.controller.request.organization.QueryOrgMemberRequest; import io.metersphere.dto.UserGroupDTO; +import io.metersphere.dto.UserGroupHelpDTO; import org.apache.ibatis.annotations.Param; import java.util.List; @@ -21,4 +22,6 @@ public interface ExtUserGroupMapper { List getWorkspaceMemberGroups(@Param("workspaceId") String workspaceId, @Param("userId") String userId); List getMemberList(@Param("member") QueryMemberRequest request); + + List getUserRoleHelpList(@Param("userId") String userId); } diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtUserGroupMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtUserGroupMapper.xml index 07c3de60ac..896495689a 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtUserGroupMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtUserGroupMapper.xml @@ -48,4 +48,22 @@ order by user_group.update_time desc) temp + \ No newline at end of file diff --git a/backend/src/main/java/io/metersphere/controller/UserController.java b/backend/src/main/java/io/metersphere/controller/UserController.java index 3ee2661443..c26a178e31 100644 --- a/backend/src/main/java/io/metersphere/controller/UserController.java +++ b/backend/src/main/java/io/metersphere/controller/UserController.java @@ -230,7 +230,7 @@ public class UserController { @MsAuditLog(module = "workspace_member", type = OperLogConstants.CREATE, title = "添加工作空间成员") public void addMember(@RequestBody AddMemberRequest request) { String wsId = request.getWorkspaceId(); - workspaceService.checkWorkspaceOwner(wsId); +// workspaceService.checkWorkspaceOwner(wsId); userService.addMember(request); } @@ -241,7 +241,7 @@ public class UserController { @RequiresRoles(value = {RoleConstants.TEST_MANAGER, RoleConstants.ORG_ADMIN}, logical = Logical.OR) @MsAuditLog(module = "workspace_member", type = OperLogConstants.DELETE, title = "删除工作空间成员") public void deleteMember(@PathVariable String workspaceId, @PathVariable String userId) { - workspaceService.checkWorkspaceOwner(workspaceId); +// workspaceService.checkWorkspaceOwner(workspaceId); String currentUserId = SessionUtils.getUser().getId(); if (StringUtils.equals(userId, currentUserId)) { MSException.throwException(Translator.get("cannot_remove_current")); diff --git a/backend/src/main/java/io/metersphere/controller/WorkspaceController.java b/backend/src/main/java/io/metersphere/controller/WorkspaceController.java index 3fee82a1a1..efd3cbe526 100644 --- a/backend/src/main/java/io/metersphere/controller/WorkspaceController.java +++ b/backend/src/main/java/io/metersphere/controller/WorkspaceController.java @@ -57,7 +57,7 @@ public class WorkspaceController { @RequiresRoles(RoleConstants.ORG_ADMIN) @MsAuditLog(module = "system_workspace", type = OperLogConstants.UPDATE, beforeEvent = "#msClass.getLogDetails(#workspace.id)", content = "#msClass.getLogDetails(#workspace.id)", msClass = WorkspaceService.class) public Workspace updateWorkspace(@RequestBody Workspace workspace) { - workspaceService.checkWorkspaceOwnerByOrgAdmin(workspace.getId()); +// workspaceService.checkWorkspaceOwnerByOrgAdmin(workspace.getId()); return workspaceService.saveWorkspace(workspace); } @@ -80,7 +80,7 @@ public class WorkspaceController { @RequiresRoles(RoleConstants.ORG_ADMIN) @MsAuditLog(module = "system_workspace", type = OperLogConstants.DELETE, beforeEvent = "#msClass.getLogDetails(#workspaceId)", msClass = WorkspaceService.class) public void deleteWorkspace(@PathVariable String workspaceId) { - workspaceService.checkWorkspaceOwnerByOrgAdmin(workspaceId); +// workspaceService.checkWorkspaceOwnerByOrgAdmin(workspaceId); userService.refreshSessionUser("workspace", workspaceId); workspaceService.deleteWorkspace(workspaceId); } diff --git a/backend/src/main/java/io/metersphere/dto/UserGroupHelpDTO.java b/backend/src/main/java/io/metersphere/dto/UserGroupHelpDTO.java new file mode 100644 index 0000000000..5f8cf3949e --- /dev/null +++ b/backend/src/main/java/io/metersphere/dto/UserGroupHelpDTO.java @@ -0,0 +1,12 @@ +package io.metersphere.dto; + +import lombok.Data; + +@Data +public class UserGroupHelpDTO { + private String groupId; + private String groupName; + private String sourceId; + private String sourceName; + private String parentId; +} diff --git a/backend/src/main/java/io/metersphere/service/CheckPermissionService.java b/backend/src/main/java/io/metersphere/service/CheckPermissionService.java index cf7d210370..cd50a971c5 100644 --- a/backend/src/main/java/io/metersphere/service/CheckPermissionService.java +++ b/backend/src/main/java/io/metersphere/service/CheckPermissionService.java @@ -1,10 +1,11 @@ package io.metersphere.service; +import io.metersphere.base.domain.Group; import io.metersphere.base.domain.Project; -import io.metersphere.base.domain.UserRole; +import io.metersphere.base.domain.UserGroup; import io.metersphere.base.mapper.ProjectMapper; import io.metersphere.base.mapper.ext.*; -import io.metersphere.commons.constants.RoleConstants; +import io.metersphere.commons.constants.UserGroupType; import io.metersphere.commons.utils.SessionUtils; import io.metersphere.i18n.Translator; import org.apache.commons.collections4.CollectionUtils; @@ -12,6 +13,7 @@ import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import javax.annotation.Resource; +import java.util.List; import java.util.Objects; import java.util.Set; import java.util.stream.Collectors; @@ -32,18 +34,18 @@ public class CheckPermissionService { private ExtTestCaseReviewMapper extTestCaseReviewMapper; - public void checkReadOnlyUser() { - String currentWorkspaceId = SessionUtils.getCurrentWorkspaceId(); - Set collect = Objects.requireNonNull(SessionUtils.getUser()).getUserRoles().stream() - .filter(ur -> - StringUtils.equals(ur.getRoleId(), RoleConstants.TEST_VIEWER)) - .map(UserRole::getSourceId) - .filter(sourceId -> StringUtils.equals(currentWorkspaceId, sourceId)) - .collect(Collectors.toSet()); - if (CollectionUtils.isNotEmpty(collect)) { - throw new RuntimeException(Translator.get("check_owner_read_only")); - } - } +// public void checkReadOnlyUser() { +// String currentWorkspaceId = SessionUtils.getCurrentWorkspaceId(); +// Set collect = Objects.requireNonNull(SessionUtils.getUser()).getUserRoles().stream() +// .filter(ur -> +// StringUtils.equals(ur.getRoleId(), RoleConstants.TEST_VIEWER)) +// .map(UserRole::getSourceId) +// .filter(sourceId -> StringUtils.equals(currentWorkspaceId, sourceId)) +// .collect(Collectors.toSet()); +// if (CollectionUtils.isNotEmpty(collect)) { +// throw new RuntimeException(Translator.get("check_owner_read_only")); +// } +// } public void checkProjectOwner(String projectId) { Set workspaceIds = getUserRelatedWorkspaceIds(); @@ -60,10 +62,14 @@ public class CheckPermissionService { } private Set getUserRelatedWorkspaceIds() { - return Objects.requireNonNull(SessionUtils.getUser()).getUserRoles().stream() - .filter(ur -> - StringUtils.equalsAny(ur.getRoleId(), RoleConstants.TEST_MANAGER, RoleConstants.TEST_USER, RoleConstants.TEST_VIEWER)) - .map(UserRole::getSourceId) + List groupIds = Objects.requireNonNull(SessionUtils.getUser()).getGroups() + .stream() + .filter(g -> StringUtils.equals(g.getType(), UserGroupType.WORKSPACE)) + .map(Group::getId) + .collect(Collectors.toList()); + return Objects.requireNonNull(SessionUtils.getUser()).getUserGroups().stream() + .filter(ur -> groupIds.contains(ur.getGroupId())) + .map(UserGroup::getSourceId) .collect(Collectors.toSet()); } diff --git a/backend/src/main/java/io/metersphere/service/OrganizationService.java b/backend/src/main/java/io/metersphere/service/OrganizationService.java index b143640832..dcc3353734 100644 --- a/backend/src/main/java/io/metersphere/service/OrganizationService.java +++ b/backend/src/main/java/io/metersphere/service/OrganizationService.java @@ -12,10 +12,7 @@ import io.metersphere.commons.exception.MSException; import io.metersphere.commons.user.SessionUser; import io.metersphere.commons.utils.SessionUtils; import io.metersphere.controller.request.OrganizationRequest; -import io.metersphere.dto.OrganizationMemberDTO; -import io.metersphere.dto.OrganizationResource; -import io.metersphere.dto.UserDTO; -import io.metersphere.dto.UserRoleHelpDTO; +import io.metersphere.dto.*; import io.metersphere.i18n.Translator; import io.metersphere.log.utils.ReflexObjectUtil; import io.metersphere.log.vo.DetailColumn; @@ -128,9 +125,9 @@ public class OrganizationService { } public List getOrganizationListByUserId(String userId) { - List userRoleHelpList = extUserRoleMapper.getUserRoleHelpList(userId); + List userGroupHelpDTOList = extUserGroupMapper.getUserRoleHelpList(userId); List list = new ArrayList<>(); - userRoleHelpList.forEach(r -> { + userGroupHelpDTOList.forEach(r -> { if (StringUtils.isEmpty(r.getParentId())) { list.add(r.getSourceId()); } else { @@ -181,9 +178,14 @@ public class OrganizationService { public void checkOrgOwner(String organizationId) { SessionUser sessionUser = SessionUtils.getUser(); UserDTO user = userService.getUserDTO(sessionUser.getId()); - List collect = user.getUserRoles().stream() - .filter(ur -> RoleConstants.ORG_ADMIN.equals(ur.getRoleId()) || RoleConstants.ORG_MEMBER.equals(ur.getRoleId())) - .map(UserRole::getSourceId) + List groupIds = user.getGroups() + .stream() + .filter(g -> StringUtils.equals(g.getType(), UserGroupType.ORGANIZATION)) + .map(Group::getId) + .collect(Collectors.toList()); + List collect = user.getUserGroups().stream() + .filter(ur -> groupIds.contains(ur.getGroupId())) + .map(UserGroup::getSourceId) .collect(Collectors.toList()); if (!collect.contains(organizationId)) { MSException.throwException(Translator.get("organization_does_not_belong_to_user")); diff --git a/backend/src/main/java/io/metersphere/service/UserService.java b/backend/src/main/java/io/metersphere/service/UserService.java index e998d2f450..1f11949e7f 100644 --- a/backend/src/main/java/io/metersphere/service/UserService.java +++ b/backend/src/main/java/io/metersphere/service/UserService.java @@ -717,9 +717,21 @@ public class UserService { UserDTO user = (UserDTO) subject.getSession().getAttribute(ATTR_USER); // 自动选中组织,工作空间 if (StringUtils.isEmpty(user.getLastOrganizationId())) { - List userRoles = user.getUserRoles(); - List test = userRoles.stream().filter(ur -> ur.getRoleId().startsWith("test")).collect(Collectors.toList()); - List org = userRoles.stream().filter(ur -> ur.getRoleId().startsWith("org")).collect(Collectors.toList()); + List orgIds = user.getGroups() + .stream() + .filter(ug -> StringUtils.equals(ug.getType(), UserGroupType.ORGANIZATION)) + .map(Group::getId) + .collect(Collectors.toList()); + List testIds = user.getGroups() + .stream() + .filter(ug -> StringUtils.equals(ug.getType(), UserGroupType.WORKSPACE)) + .map(Group::getId) + .collect(Collectors.toList()); + List userGroups = user.getUserGroups(); + List org = userGroups.stream().filter(ug -> orgIds.contains(ug.getGroupId())) + .collect(Collectors.toList()); + List test = userGroups.stream().filter(ug -> testIds.contains(ug.getGroupId())) + .collect(Collectors.toList()); if (test.size() > 0) { String wsId = test.get(0).getSourceId(); switchUserRole("workspace", wsId); diff --git a/backend/src/main/java/io/metersphere/service/WorkspaceService.java b/backend/src/main/java/io/metersphere/service/WorkspaceService.java index 2c892b1543..5cd71a9ba1 100644 --- a/backend/src/main/java/io/metersphere/service/WorkspaceService.java +++ b/backend/src/main/java/io/metersphere/service/WorkspaceService.java @@ -126,46 +126,48 @@ public class WorkspaceService { * ORG_ADMIN需要检查是否有操作此工作空间的权限 */ public void checkWorkspaceOwnerByOrgAdmin(String workspaceId) { - checkWorkspaceIsExist(workspaceId); - WorkspaceExample example = new WorkspaceExample(); - SessionUser sessionUser = SessionUtils.getUser(); - UserDTO user = userService.getUserDTO(sessionUser.getId()); - List orgIds = user.getUserRoles().stream() - .filter(ur -> RoleConstants.ORG_ADMIN.equals(ur.getRoleId())) - .map(UserRole::getSourceId) - .collect(Collectors.toList()); - example.createCriteria() - .andOrganizationIdIn(orgIds) - .andIdEqualTo(workspaceId); - if (workspaceMapper.countByExample(example) == 0) { - MSException.throwException(Translator.get("workspace_does_not_belong_to_user")); - } + // todo +// checkWorkspaceIsExist(workspaceId); +// WorkspaceExample example = new WorkspaceExample(); +// SessionUser sessionUser = SessionUtils.getUser(); +// UserDTO user = userService.getUserDTO(sessionUser.getId()); +// List orgIds = user.getUserRoles().stream() +// .filter(ur -> RoleConstants.ORG_ADMIN.equals(ur.getRoleId())) +// .map(UserRole::getSourceId) +// .collect(Collectors.toList()); +// example.createCriteria() +// .andOrganizationIdIn(orgIds) +// .andIdEqualTo(workspaceId); +// if (workspaceMapper.countByExample(example) == 0) { +// MSException.throwException(Translator.get("workspace_does_not_belong_to_user")); +// } } public void checkWorkspaceOwner(String workspaceId) { - checkWorkspaceIsExist(workspaceId); - int size = 0; - WorkspaceExample example = new WorkspaceExample(); - SessionUser sessionUser = SessionUtils.getUser(); - UserDTO user = userService.getUserDTO(sessionUser.getId()); - List orgIds = user.getUserRoles().stream() - .filter(ur -> RoleConstants.ORG_ADMIN.equals(ur.getRoleId())) - .map(UserRole::getSourceId) - .collect(Collectors.toList()); - if (!CollectionUtils.isEmpty(orgIds)) { - example.createCriteria() - .andOrganizationIdIn(orgIds) - .andIdEqualTo(workspaceId); - size = (int) workspaceMapper.countByExample(example); - } - List wsIds = user.getUserRoles().stream() - .filter(ur -> RoleConstants.TEST_MANAGER.equals(ur.getRoleId())) - .map(UserRole::getSourceId) - .collect(Collectors.toList()); - boolean contains = wsIds.contains(workspaceId); - if (size == 0 && !contains) { - MSException.throwException(Translator.get("workspace_does_not_belong_to_user")); - } + // todo +// checkWorkspaceIsExist(workspaceId); +// int size = 0; +// WorkspaceExample example = new WorkspaceExample(); +// SessionUser sessionUser = SessionUtils.getUser(); +// UserDTO user = userService.getUserDTO(sessionUser.getId()); +// List orgIds = user.getUserRoles().stream() +// .filter(ur -> RoleConstants.ORG_ADMIN.equals(ur.getRoleId())) +// .map(UserRole::getSourceId) +// .collect(Collectors.toList()); +// if (!CollectionUtils.isEmpty(orgIds)) { +// example.createCriteria() +// .andOrganizationIdIn(orgIds) +// .andIdEqualTo(workspaceId); +// size = (int) workspaceMapper.countByExample(example); +// } +// List wsIds = user.getUserRoles().stream() +// .filter(ur -> RoleConstants.TEST_MANAGER.equals(ur.getRoleId())) +// .map(UserRole::getSourceId) +// .collect(Collectors.toList()); +// boolean contains = wsIds.contains(workspaceId); +// if (size == 0 && !contains) { +// MSException.throwException(Translator.get("workspace_does_not_belong_to_user")); +// } } public void checkWorkspaceIsExist(String workspaceId) { @@ -194,13 +196,13 @@ public class WorkspaceService { WorkspaceExample workspaceExample = new WorkspaceExample(); workspaceExample.createCriteria().andOrganizationIdEqualTo(orgId); List workspaces = workspaceMapper.selectByExample(workspaceExample); - UserRoleExample userRoleExample = new UserRoleExample(); - userRoleExample.createCriteria().andUserIdEqualTo(useId); - List userRoles = userRoleMapper.selectByExample(userRoleExample); + UserGroupExample userGroupExample = new UserGroupExample(); + userGroupExample.createCriteria().andUserIdEqualTo(useId); + List userGroups = userGroupMapper.selectByExample(userGroupExample); List resultWorkspaceList = new ArrayList<>(); - userRoles.forEach(userRole -> { + userGroups.forEach(userGroup -> { workspaces.forEach(workspace -> { - if (StringUtils.equals(userRole.getSourceId(), workspace.getId())) { + if (StringUtils.equals(userGroup.getSourceId(), workspace.getId())) { if (!resultWorkspaceList.contains(workspace)) { resultWorkspaceList.add(workspace); } diff --git a/backend/src/main/java/io/metersphere/track/service/TestCaseService.java b/backend/src/main/java/io/metersphere/track/service/TestCaseService.java index 5c1056b331..f6ba8cff84 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestCaseService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestCaseService.java @@ -11,6 +11,7 @@ import io.metersphere.base.mapper.ext.ExtTestCaseMapper; import io.metersphere.commons.constants.RoleConstants; import io.metersphere.commons.constants.TestCaseConstants; import io.metersphere.commons.constants.TestCaseReviewStatus; +import io.metersphere.commons.constants.UserGroupType; import io.metersphere.commons.exception.MSException; import io.metersphere.commons.user.SessionUser; import io.metersphere.commons.utils.*; @@ -105,6 +106,10 @@ public class TestCaseService { TestCaseFileMapper testCaseFileMapper; @Resource TestCaseTestMapper testCaseTestMapper; + @Resource + private GroupMapper groupMapper; + @Resource + private UserGroupMapper userGroupMapper; private void setNode(TestCaseWithBLOBs testCase) { if (StringUtils.isEmpty(testCase.getNodeId()) || "default-module".equals(testCase.getNodeId())) { @@ -450,12 +455,16 @@ public class TestCaseService { } } else { - UserRoleExample userRoleExample = new UserRoleExample(); - userRoleExample.createCriteria() - .andRoleIdIn(Arrays.asList(RoleConstants.TEST_MANAGER, RoleConstants.TEST_USER)) - .andSourceIdEqualTo(currentWorkspaceId); + GroupExample groupExample = new GroupExample(); + groupExample.createCriteria().andTypeIn(Arrays.asList(UserGroupType.WORKSPACE, UserGroupType.PROJECT)); + List groups = groupMapper.selectByExample(groupExample); + List groupIds = groups.stream().map(Group::getId).collect(Collectors.toList()); - Set userIds = userRoleMapper.selectByExample(userRoleExample).stream().map(UserRole::getUserId).collect(Collectors.toSet()); + UserGroupExample userGroupExample = new UserGroupExample(); + userGroupExample.createCriteria() + .andGroupIdIn(groupIds) + .andSourceIdEqualTo(currentWorkspaceId); + Set userIds = userGroupMapper.selectByExample(userGroupExample).stream().map(UserGroup::getUserId).collect(Collectors.toSet()); try { //根据本地语言环境选择用哪种数据对象进行存放读取的数据 @@ -1115,12 +1124,16 @@ public class TestCaseService { MSException.throwException(e.getMessage()); } } else { - UserRoleExample userRoleExample = new UserRoleExample(); - userRoleExample.createCriteria() - .andRoleIdIn(Arrays.asList(RoleConstants.TEST_MANAGER, RoleConstants.TEST_USER)) - .andSourceIdEqualTo(currentWorkspaceId); + GroupExample groupExample = new GroupExample(); + groupExample.createCriteria().andTypeIn(Arrays.asList(UserGroupType.WORKSPACE, UserGroupType.PROJECT)); + List groups = groupMapper.selectByExample(groupExample); + List groupIds = groups.stream().map(Group::getId).collect(Collectors.toList()); - Set userIds = userRoleMapper.selectByExample(userRoleExample).stream().map(UserRole::getUserId).collect(Collectors.toSet()); + UserGroupExample userGroupExample = new UserGroupExample(); + userGroupExample.createCriteria() + .andGroupIdIn(groupIds) + .andSourceIdEqualTo(currentWorkspaceId); + Set userIds = userGroupMapper.selectByExample(userGroupExample).stream().map(UserGroup::getUserId).collect(Collectors.toSet()); try { //根据本地语言环境选择用哪种数据对象进行存放读取的数据