refactor(用户组和权限): 优化

This commit is contained in:
shiziyuan9527 2021-05-17 18:05:32 +08:00 committed by 刘瑞斌
parent d0fc6de7df
commit 232e8d09f3
7 changed files with 37 additions and 93 deletions

View File

@ -1,8 +1,8 @@
package io.metersphere.commons.constants; package io.metersphere.commons.constants;
public class UserGroupType { public class UserGroupType {
public static final String SYSTEM = "system"; public static final String SYSTEM = "SYSTEM";
public static final String ORGANIZATION = "organization"; public static final String ORGANIZATION = "ORGANIZATION";
public static final String WORKSPACE = "workspace"; public static final String WORKSPACE = "WORKSPACE";
public static final String PROJECT = "project"; public static final String PROJECT = "PROJECT";
} }

View File

@ -2,12 +2,10 @@ package io.metersphere.dto;
import lombok.Data; import lombok.Data;
import java.util.ArrayList;
import java.util.List; import java.util.List;
@Data @Data
public class GroupPermissionDTO { public class GroupPermissionDTO {
private List<GroupResourceDTO> system; private List<GroupResourceDTO> permissions = new ArrayList<>();
private List<GroupResourceDTO> organization;
private List<GroupResourceDTO> workspace;
private List<GroupResourceDTO> project;
} }

View File

@ -47,6 +47,13 @@ public class GroupService {
@Resource @Resource
private UserGroupPermissionMapper userGroupPermissionMapper; private UserGroupPermissionMapper userGroupPermissionMapper;
private static final Map<String, List<String>> map = new HashMap<String, List<String>>(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<List<GroupDTO>> getGroupList(EditGroupRequest request) { public Pager<List<GroupDTO>> getGroupList(EditGroupRequest request) {
SessionUser user = SessionUtils.getUser(); SessionUser user = SessionUtils.getUser();
List<UserGroupDTO> userGroup = extUserGroupMapper.getUserGroup(user.getId()); List<UserGroupDTO> userGroup = extUserGroupMapper.getUserGroup(user.getId());
@ -105,7 +112,11 @@ public class GroupService {
group = JSON.parseObject(permission, GroupJson.class); group = JSON.parseObject(permission, GroupJson.class);
List<GroupResource> resource = group.getResource(); List<GroupResource> resource = group.getResource();
List<GroupPermission> permissions = group.getPermissions(); List<GroupPermission> permissions = group.getPermissions();
getPermission(resource, permissions, type, dto, permissionList); List<GroupResourceDTO> dtoPermissions = dto.getPermissions();
List<String> groups = map.get(type);
for (String gp : groups) {
dtoPermissions.addAll(getResourcePermission(resource, permissions, gp, permissionList));
}
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -141,12 +152,6 @@ public class GroupService {
sqlSession.flushStatements(); sqlSession.flushStatements();
} }
private List<GroupResourceDTO> getResourcePermission(List<GroupResource> resource, List<GroupPermission> permissions, String type, List<String> permissionList) { private List<GroupResourceDTO> getResourcePermission(List<GroupResource> resource, List<GroupPermission> permissions, String type, List<String> permissionList) {
List<GroupResourceDTO> dto = new ArrayList<>(); List<GroupResourceDTO> dto = new ArrayList<>();
List<GroupResource> resources = resource.stream().filter(g -> g.getId().startsWith(type)).collect(Collectors.toList()); List<GroupResource> resources = resource.stream().filter(g -> g.getId().startsWith(type)).collect(Collectors.toList());
@ -188,58 +193,17 @@ public class GroupService {
return new Pager<>(); return new Pager<>();
} }
private void getPermission(List<GroupResource> resource, List<GroupPermission> permissions, String type, GroupPermissionDTO dto, List<String> 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<List<GroupDTO>> getUserGroup(String groupType, EditGroupRequest request) { private Pager<List<GroupDTO>> getUserGroup(String groupType, EditGroupRequest request) {
List<String> types = new ArrayList<>(); List<String> types;
String orgId = SessionUtils.getCurrentOrganizationId(); String orgId = SessionUtils.getCurrentOrganizationId();
List<String> scopes = Arrays.asList("global", orgId); List<String> scopes = Arrays.asList("global", orgId);
int goPage = request.getGoPage(); int goPage = request.getGoPage();
int pageSize = request.getPageSize(); int pageSize = request.getPageSize();
Page<Object> page = PageHelper.startPage(goPage, pageSize, true); Page<Object> page = PageHelper.startPage(goPage, pageSize, true);
switch (groupType) { if (StringUtils.equals(groupType, UserGroupType.SYSTEM)) {
case UserGroupType.SYSTEM: scopes = new ArrayList<>();
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:
} }
types = map.get(groupType);
request.setTypes(types); request.setTypes(types);
request.setScopes(scopes); request.setScopes(scopes);
List<GroupDTO> groups = extGroupMapper.getGroupList(request); List<GroupDTO> groups = extGroupMapper.getGroupList(request);

View File

@ -52,16 +52,6 @@ export default {
result: {} result: {}
} }
}, },
computed: {
map() {
return new Map([
['system', ['system', 'organization', 'workspace', 'project']],
['organization', ['organization', 'workspace', 'project']],
['workspace', ['workspace', 'project']],
['project', ['project']],
])
}
},
components: { components: {
GroupPermission, GroupPermission,
GroupOperator GroupOperator
@ -71,26 +61,16 @@ export default {
this.tableData = []; this.tableData = [];
this.dialogVisible = true; this.dialogVisible = true;
this.group = Object.assign({}, row); 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 => { this.result = this.$post("/user/group/permission", this.group, result => {
let data = result.data; let data = result.data;
if (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() { onSubmit() {
let param = {}; let param = {};
let permissions = []; let permissions = [];

View File

@ -12,10 +12,10 @@
<el-col :span="11" :offset="2"> <el-col :span="11" :offset="2">
<el-form-item label="所属类型"> <el-form-item label="所属类型">
<el-select v-model="form.type" placeholder="请选择所属类型" style="width: 100%"> <el-select v-model="form.type" placeholder="请选择所属类型" style="width: 100%">
<el-option label="系统" value="system"></el-option> <el-option label="系统" value="SYSTEM"></el-option>
<el-option label="组织" value="organization"></el-option> <el-option label="组织" value="ORGANIZATION"></el-option>
<el-option label="工作空间" value="workspace"></el-option> <el-option label="工作空间" value="WORKSPACE"></el-option>
<el-option label="项目" value="project"></el-option> <el-option label="项目" value="PROJECT"></el-option>
</el-select> </el-select>
</el-form-item> </el-form-item>
</el-col> </el-col>

View File

@ -1,7 +1,9 @@
<template> <template>
<div> <div>
<span v-for="(permission, index) in permissions" :key="index" style="margin-left: 25px;"> <span v-for="(permission, index) in permissions" :key="index" style="margin-right: 25px;">
<el-checkbox v-model="permission['checked']" @change="change($event, permission)">{{ permission.name }}</el-checkbox> <el-checkbox v-model="permission['checked']" @change="change($event, permission)">
{{ permission.name }}
</el-checkbox>
</span> </span>
</div> </div>
</template> </template>

View File

@ -71,8 +71,8 @@ export const ISSUE_STATUS_MAP = {
export const USER_GROUP_SCOPE = { export const USER_GROUP_SCOPE = {
// todo i18n // todo i18n
'system': '系统', 'SYSTEM': '系统',
'organization': '组织', 'ORGANIZATION': '组织',
'workspace': '工作空间', 'WORKSPACE': '工作空间',
'project': '项目' 'PROJECT': '项目'
} }