From 232e8d09f3d64e42f342a6976605bde452e11d23 Mon Sep 17 00:00:00 2001 From: shiziyuan9527 Date: Mon, 17 May 2021 18:05:32 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E7=94=A8=E6=88=B7=E7=BB=84=E5=92=8C?= =?UTF-8?q?=E6=9D=83=E9=99=90):=20=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commons/constants/UserGroupType.java | 8 +-- .../metersphere/dto/GroupPermissionDTO.java | 6 +- .../io/metersphere/service/GroupService.java | 68 +++++-------------- .../settings/system/group/EditPermission.vue | 26 +------ .../settings/system/group/EditUserGroup.vue | 8 +-- .../settings/system/group/GroupPermission.vue | 6 +- frontend/src/common/js/table-constants.js | 8 +-- 7 files changed, 37 insertions(+), 93 deletions(-) diff --git a/backend/src/main/java/io/metersphere/commons/constants/UserGroupType.java b/backend/src/main/java/io/metersphere/commons/constants/UserGroupType.java index b0009d66c9..38a1e50810 100644 --- a/backend/src/main/java/io/metersphere/commons/constants/UserGroupType.java +++ b/backend/src/main/java/io/metersphere/commons/constants/UserGroupType.java @@ -1,8 +1,8 @@ package io.metersphere.commons.constants; public class UserGroupType { - public static final String SYSTEM = "system"; - public static final String ORGANIZATION = "organization"; - public static final String WORKSPACE = "workspace"; - public static final String PROJECT = "project"; + public static final String SYSTEM = "SYSTEM"; + public static final String ORGANIZATION = "ORGANIZATION"; + public static final String WORKSPACE = "WORKSPACE"; + public static final String PROJECT = "PROJECT"; } diff --git a/backend/src/main/java/io/metersphere/dto/GroupPermissionDTO.java b/backend/src/main/java/io/metersphere/dto/GroupPermissionDTO.java index acb2a4531e..9cb9cb940c 100644 --- a/backend/src/main/java/io/metersphere/dto/GroupPermissionDTO.java +++ b/backend/src/main/java/io/metersphere/dto/GroupPermissionDTO.java @@ -2,12 +2,10 @@ package io.metersphere.dto; import lombok.Data; +import java.util.ArrayList; import java.util.List; @Data public class GroupPermissionDTO { - private List system; - private List organization; - private List workspace; - private List project; + private List permissions = new ArrayList<>(); } diff --git a/backend/src/main/java/io/metersphere/service/GroupService.java b/backend/src/main/java/io/metersphere/service/GroupService.java index 4c284866ca..ee16a52683 100644 --- a/backend/src/main/java/io/metersphere/service/GroupService.java +++ b/backend/src/main/java/io/metersphere/service/GroupService.java @@ -47,6 +47,13 @@ public class GroupService { @Resource private UserGroupPermissionMapper userGroupPermissionMapper; + private static final Map> map = new HashMap>(4){{ + put(UserGroupType.SYSTEM, Arrays.asList(UserGroupType.SYSTEM, UserGroupType.ORGANIZATION, UserGroupType.WORKSPACE, UserGroupType.PROJECT)); + put(UserGroupType.ORGANIZATION, Arrays.asList(UserGroupType.ORGANIZATION, UserGroupType.WORKSPACE, UserGroupType.PROJECT)); + put(UserGroupType.WORKSPACE, Arrays.asList(UserGroupType.WORKSPACE, UserGroupType.PROJECT)); + put(UserGroupType.PROJECT, Collections.singletonList(UserGroupType.PROJECT)); + }}; + public Pager> getGroupList(EditGroupRequest request) { SessionUser user = SessionUtils.getUser(); List userGroup = extUserGroupMapper.getUserGroup(user.getId()); @@ -105,7 +112,11 @@ public class GroupService { group = JSON.parseObject(permission, GroupJson.class); List resource = group.getResource(); List permissions = group.getPermissions(); - getPermission(resource, permissions, type, dto, permissionList); + List dtoPermissions = dto.getPermissions(); + List groups = map.get(type); + for (String gp : groups) { + dtoPermissions.addAll(getResourcePermission(resource, permissions, gp, permissionList)); + } } catch (IOException e) { e.printStackTrace(); } @@ -141,12 +152,6 @@ public class GroupService { sqlSession.flushStatements(); } - - - - - - private List getResourcePermission(List resource, List permissions, String type, List permissionList) { List dto = new ArrayList<>(); List resources = resource.stream().filter(g -> g.getId().startsWith(type)).collect(Collectors.toList()); @@ -187,59 +192,18 @@ public class GroupService { return new Pager<>(); } - - private void getPermission(List resource, List permissions, String type, GroupPermissionDTO dto, List permissionList) { - dto.setSystem(getResourcePermission(resource, permissions, "SYSTEM", permissionList)); - dto.setOrganization(getResourcePermission(resource, permissions, "ORGANIZATION", permissionList)); - dto.setWorkspace(getResourcePermission(resource, permissions, "WORKSPACE", permissionList)); - dto.setProject(getResourcePermission(resource, permissions, "PROJECT", permissionList)); - switch (type) { - case "SYSTEM": - dto.setSystem(getResourcePermission(resource, permissions, "SYSTEM", permissionList)); - dto.setOrganization(getResourcePermission(resource, permissions, "ORGANIZATION", permissionList)); - dto.setWorkspace(getResourcePermission(resource, permissions, "WORKSPACE", permissionList)); - dto.setProject(getResourcePermission(resource, permissions, "PROJECT", permissionList)); - break; - case "ORGANIZATION": - dto.setOrganization(getResourcePermission(resource, permissions, "ORGANIZATION", permissionList)); - dto.setWorkspace(getResourcePermission(resource, permissions, "WORKSPACE", permissionList)); - dto.setProject(getResourcePermission(resource, permissions, "PROJECT", permissionList)); - break; - case "WORKSPACE": - dto.setWorkspace(getResourcePermission(resource, permissions, "WORKSPACE", permissionList)); - dto.setProject(getResourcePermission(resource, permissions, "PROJECT", permissionList)); - break; - case "PROJECT": - dto.setProject(getResourcePermission(resource, permissions, "PROJECT", permissionList)); - break; - default: - } - } - private Pager> getUserGroup(String groupType, EditGroupRequest request) { - List types = new ArrayList<>(); + List types; String orgId = SessionUtils.getCurrentOrganizationId(); List scopes = Arrays.asList("global", orgId); int goPage = request.getGoPage(); int pageSize = request.getPageSize(); Page page = PageHelper.startPage(goPage, pageSize, true); - switch (groupType) { - case UserGroupType.SYSTEM: - types = Arrays.asList(UserGroupType.SYSTEM,UserGroupType.ORGANIZATION, UserGroupType.WORKSPACE, UserGroupType.PROJECT); - scopes = new ArrayList<>(); - break; - case UserGroupType.ORGANIZATION: - types = Arrays.asList(UserGroupType.ORGANIZATION, UserGroupType.WORKSPACE, UserGroupType.PROJECT); - break; - case UserGroupType.WORKSPACE: - types = Arrays.asList(UserGroupType.WORKSPACE, UserGroupType.PROJECT); - break; - case UserGroupType.PROJECT: - types.add(UserGroupType.PROJECT); - break; - default: + if (StringUtils.equals(groupType, UserGroupType.SYSTEM)) { + scopes = new ArrayList<>(); } + types = map.get(groupType); request.setTypes(types); request.setScopes(scopes); List groups = extGroupMapper.getGroupList(request); diff --git a/frontend/src/business/components/settings/system/group/EditPermission.vue b/frontend/src/business/components/settings/system/group/EditPermission.vue index 9e516124c8..f7828f94db 100644 --- a/frontend/src/business/components/settings/system/group/EditPermission.vue +++ b/frontend/src/business/components/settings/system/group/EditPermission.vue @@ -52,16 +52,6 @@ export default { result: {} } }, - computed: { - map() { - return new Map([ - ['system', ['system', 'organization', 'workspace', 'project']], - ['organization', ['organization', 'workspace', 'project']], - ['workspace', ['workspace', 'project']], - ['project', ['project']], - ]) - } - }, components: { GroupPermission, GroupOperator @@ -71,26 +61,16 @@ export default { this.tableData = []; this.dialogVisible = true; this.group = Object.assign({}, row); - this.getGroupJson(this.group.type); + this.getGroupJson(); }, - getGroupJson(type) { + getGroupJson() { this.result = this.$post("/user/group/permission", this.group, result => { let data = result.data; if (data) { - this.tableData = this._getGroupPermission(data, type); + this.tableData = data.permissions; } }) }, - _getGroupPermission(data, type) { - let arr = []; - let group = this.map.get(type); - if (group) { - group.forEach(g => { - arr.push(...data[g]); - }); - } - return arr; - }, onSubmit() { let param = {}; let permissions = []; diff --git a/frontend/src/business/components/settings/system/group/EditUserGroup.vue b/frontend/src/business/components/settings/system/group/EditUserGroup.vue index baff48e730..3e55fd0256 100644 --- a/frontend/src/business/components/settings/system/group/EditUserGroup.vue +++ b/frontend/src/business/components/settings/system/group/EditUserGroup.vue @@ -12,10 +12,10 @@ - - - - + + + + diff --git a/frontend/src/business/components/settings/system/group/GroupPermission.vue b/frontend/src/business/components/settings/system/group/GroupPermission.vue index 0273bab244..2b2291fe49 100644 --- a/frontend/src/business/components/settings/system/group/GroupPermission.vue +++ b/frontend/src/business/components/settings/system/group/GroupPermission.vue @@ -1,7 +1,9 @@ diff --git a/frontend/src/common/js/table-constants.js b/frontend/src/common/js/table-constants.js index aa65f99ee6..6e4cbaa046 100644 --- a/frontend/src/common/js/table-constants.js +++ b/frontend/src/common/js/table-constants.js @@ -71,8 +71,8 @@ export const ISSUE_STATUS_MAP = { export const USER_GROUP_SCOPE = { // todo i18n - 'system': '系统', - 'organization': '组织', - 'workspace': '工作空间', - 'project': '项目' + 'SYSTEM': '系统', + 'ORGANIZATION': '组织', + 'WORKSPACE': '工作空间', + 'PROJECT': '项目' }