refactor(用户组和权限): 设置权限

This commit is contained in:
shiziyuan9527 2021-05-17 16:50:45 +08:00 committed by 刘瑞斌
parent 859c67cf98
commit d0fc6de7df
9 changed files with 505 additions and 352 deletions

View File

@ -41,9 +41,15 @@ public class GroupController {
groupService.deleteGroup(id); groupService.deleteGroup(id);
} }
@GetMapping("/permission") @PostMapping("/permission")
public GroupPermissionDTO getGroupResource() { public GroupPermissionDTO getGroupResource(@RequestBody Group group) {
return groupService.getGroupResource(); return groupService.getGroupResource(group);
} }
@PostMapping("/permission/edit")
public void EditGroupPermission(@RequestBody EditGroupRequest editGroupRequest) {
groupService.editGroupPermission(editGroupRequest);
}
} }

View File

@ -1,6 +1,7 @@
package io.metersphere.controller.request.group; package io.metersphere.controller.request.group;
import io.metersphere.base.domain.Group; import io.metersphere.base.domain.Group;
import io.metersphere.dto.GroupPermission;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
@ -20,4 +21,8 @@ public class EditGroupRequest extends Group {
* 是否是全局用户组 * 是否是全局用户组
*/ */
private Boolean global; private Boolean global;
private List<GroupPermission> permissions;
private String userGroupId;
} }

View File

@ -7,4 +7,5 @@ public class GroupPermission {
private String id; private String id;
private String name; private String name;
private String resourceId; private String resourceId;
private Boolean checked = false;
} }

View File

@ -1,11 +1,11 @@
package io.metersphere.service; package io.metersphere.service;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page; import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageHelper;
import io.metersphere.base.domain.*; import io.metersphere.base.domain.*;
import io.metersphere.base.mapper.GroupMapper; import io.metersphere.base.mapper.GroupMapper;
import io.metersphere.base.mapper.UserGroupPermissionMapper;
import io.metersphere.base.mapper.ext.ExtGroupMapper; import io.metersphere.base.mapper.ext.ExtGroupMapper;
import io.metersphere.base.mapper.ext.ExtUserGroupMapper; import io.metersphere.base.mapper.ext.ExtUserGroupMapper;
import io.metersphere.commons.constants.UserGroupType; import io.metersphere.commons.constants.UserGroupType;
@ -16,8 +16,12 @@ import io.metersphere.commons.utils.Pager;
import io.metersphere.commons.utils.SessionUtils; import io.metersphere.commons.utils.SessionUtils;
import io.metersphere.controller.request.group.EditGroupRequest; import io.metersphere.controller.request.group.EditGroupRequest;
import io.metersphere.dto.*; import io.metersphere.dto.*;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -38,6 +42,10 @@ public class GroupService {
private GroupMapper groupMapper; private GroupMapper groupMapper;
@Resource @Resource
private ExtGroupMapper extGroupMapper; private ExtGroupMapper extGroupMapper;
@Resource
private SqlSessionFactory sqlSessionFactory;
@Resource
private UserGroupPermissionMapper userGroupPermissionMapper;
public Pager<List<GroupDTO>> getGroupList(EditGroupRequest request) { public Pager<List<GroupDTO>> getGroupList(EditGroupRequest request) {
SessionUser user = SessionUtils.getUser(); SessionUser user = SessionUtils.getUser();
@ -75,24 +83,29 @@ public class GroupService {
public void deleteGroup(String id) { public void deleteGroup(String id) {
groupMapper.deleteByPrimaryKey(id); groupMapper.deleteByPrimaryKey(id);
// todo use_group 关系 UserGroupPermissionExample example = new UserGroupPermissionExample();
example.createCriteria().andGroupIdEqualTo(id);
userGroupPermissionMapper.deleteByExample(example);
} }
public GroupPermissionDTO getGroupResource() { public GroupPermissionDTO getGroupResource(Group g) {
GroupPermissionDTO dto = new GroupPermissionDTO(); GroupPermissionDTO dto = new GroupPermissionDTO();
InputStream permission = getClass().getResourceAsStream("/permission.json"); InputStream permission = getClass().getResourceAsStream("/permission.json");
String type = g.getType();
String id = g.getId();
UserGroupPermissionExample userGroupPermissionExample = new UserGroupPermissionExample();
userGroupPermissionExample.createCriteria().andGroupIdEqualTo(id);
List<UserGroupPermission> userGroupPermissions = userGroupPermissionMapper.selectByExample(userGroupPermissionExample);
List<String> permissionList = userGroupPermissions.stream().map(UserGroupPermission::getPermissionId).collect(Collectors.toList());
if (permission == null) { if (permission == null) {
throw new RuntimeException("读取文件失败"); throw new RuntimeException("读取文件失败!");
} else { } else {
GroupJson group = null; GroupJson group = null;
try { try {
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();
dto.setSystem(getResourcePermission(resource, permissions, "SYSTEM")); getPermission(resource, permissions, type, dto, permissionList);
dto.setOrganization(getResourcePermission(resource, permissions, "ORGANIZATION"));
dto.setWorkspace(getResourcePermission(resource, permissions, "WORKSPACE"));
dto.setProject(getResourcePermission(resource, permissions, "PROJECT"));
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -100,9 +113,48 @@ public class GroupService {
return dto; return dto;
} }
private List<GroupResourceDTO> getResourcePermission(List<GroupResource> resource, List<GroupPermission> permissions, String type) { public void editGroupPermission(EditGroupRequest request) {
List<GroupPermission> permissions = request.getPermissions();
if (CollectionUtils.isEmpty(permissions)) {
return;
}
UserGroupPermissionExample userGroupPermissionExample = new UserGroupPermissionExample();
userGroupPermissionExample.createCriteria().andGroupIdEqualTo(request.getUserGroupId());
userGroupPermissionMapper.deleteByExample(userGroupPermissionExample);
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
UserGroupPermissionMapper mapper = sqlSession.getMapper(UserGroupPermissionMapper.class);
String groupId = request.getUserGroupId();
permissions.forEach(permission -> {
if (BooleanUtils.isTrue(permission.getChecked())) {
String permissionId = permission.getId();
String resourceId = permission.getResourceId();
UserGroupPermission groupPermission = new UserGroupPermission();
groupPermission.setId(UUID.randomUUID().toString());
groupPermission.setGroupId(groupId);
groupPermission.setPermissionId(permissionId);
groupPermission.setModuleId(resourceId);
mapper.insert(groupPermission);
}
});
sqlSession.flushStatements();
}
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());
permissions.forEach(p -> {
if (permissionList.contains(p.getId())) {
p.setChecked(true);
}
});
for (GroupResource r : resources) { for (GroupResource r : resources) {
GroupResourceDTO resourceDTO = new GroupResourceDTO(); GroupResourceDTO resourceDTO = new GroupResourceDTO();
resourceDTO.setResource(r); resourceDTO.setResource(r);
@ -135,6 +187,34 @@ 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) {

File diff suppressed because it is too large Load Diff

View File

@ -1,74 +1,111 @@
<template> <template>
<el-dialog :close-on-click-modal="false" :visible.sync="dialogVisible" width="50%" <el-dialog :close-on-click-modal="false" :visible.sync="dialogVisible" width="65%"
title="设置权限" title="设置权限"
:destroy-on-close="true"> :destroy-on-close="true"
<el-table v-loading="result.loading"
:data="tableData" >
style="width: 100%"> <div style="height: 500px;overflow: auto">
<el-table-column <el-table
type="selection" :data="tableData"
width="55"> style="width: 100%">
</el-table-column> <!-- <el-table-column-->
<el-table-column <!-- type="selection"-->
prop="func" <!-- width="55">-->
label="功能菜单" <!-- </el-table-column>-->
width="180"> <el-table-column
</el-table-column> prop="resource"
<!-- <el-table-column--> label="操作对象"
<!-- prop="operators"--> width="180">
<!-- label="操作对象"--> <template v-slot:default="scope">
<!-- width="180">--> {{scope.row.resource.name}}
<!-- <template v-slot:default="scope">--> </template>
<!-- <group-operator :operators="scope.row.operators"/>--> </el-table-column>
<!-- </template>--> <el-table-column
<!-- </el-table-column>--> prop="permissions"
<!-- <el-table-column--> label="权限">
<!-- prop="permission"--> <template v-slot:default="scope">
<!-- label="权限">--> <group-permission :permissions="scope.row.permissions" :selected.sync="tableData"/>
<!-- <template v-slot:default="scope">--> </template>
<!-- {{scope.row.permissions}}--> </el-table-column>
<!-- </template>--> </el-table>
<!-- </el-table-column>--> </div>
</el-table> <el-button type="primary" @click="onSubmit" size="small" style="margin-top: 10px;margin-left: 5px;">确定</el-button>
<el-button @click="cancel" size="small">取消</el-button>
</el-dialog> </el-dialog>
</template> </template>
<script> <script>
import GroupOperator from "@/business/components/settings/system/group/GroupOperator"; import GroupOperator from "@/business/components/settings/system/group/GroupOperator";
import GroupPermission from "@/business/components/settings/system/group/GroupPermission";
export default { export default {
name: "GroupPermission", name: "EditPermission",
component: {
GroupPermission,
},
data() { data() {
return { return {
dialogVisible: false, dialogVisible: false,
tableData: [] tableData: [],
selected: [],
group: {},
result: {}
}
},
computed: {
map() {
return new Map([
['system', ['system', 'organization', 'workspace', 'project']],
['organization', ['organization', 'workspace', 'project']],
['workspace', ['workspace', 'project']],
['project', ['project']],
])
} }
}, },
components: { components: {
GroupPermission,
GroupOperator GroupOperator
}, },
methods: { methods: {
open() { open(row) {
this.tableData = [];
this.dialogVisible = true; this.dialogVisible = true;
this.getGroupJson(); this.group = Object.assign({}, row);
this.getGroupJson(this.group.type);
}, },
getGroupJson() { getGroupJson(type) {
this.$get("/user/group/permission", result => { this.result = this.$post("/user/group/permission", this.group, result => {
let data = result.data; let data = result.data;
let arr = []; if (data) {
this._getData("系统", data.system, arr); this.tableData = this._getGroupPermission(data, type);
this._getData("组织", data.organization, arr); }
this._getData("工作空间", data.workspace, arr);
this._getData("项目", data.project, arr);
this.tableData = arr;
}) })
}, },
_getData(type, data, arr) { _getGroupPermission(data, type) {
let obj = {}; let arr = [];
obj.func = type; let group = this.map.get(type);
obj.operators = data.map(s => s.resource); if (group) {
obj.permissions = data.map(s => s.permissions); group.forEach(g => {
arr.push(obj); arr.push(...data[g]);
});
}
return arr;
},
onSubmit() {
let param = {};
let permissions = [];
this.tableData.forEach(td => {
permissions.push(...td.permissions);
})
param.userGroupId = this.group.id;
param.permissions = permissions;
this.result = this.$post('/user/group/permission/edit', param, () => {
this.$success(this.$t('commons.save_success'));
this.dialogVisible = false;
})
},
cancel() {
this.dialogVisible = false;
} }
} }
} }

View File

@ -100,7 +100,9 @@ export default {
this.dialogVisible = true; this.dialogVisible = true;
this.dialogType = type; this.dialogType = type;
this.form = Object.assign({}, row); this.form = Object.assign({}, row);
this.form.global = this.form.scopeId === "global"; if (type !== 'create') {
this.form.global = this.form.scopeId === "global";
}
}, },
cancel() { cancel() {
this.dialogVisible = false; this.dialogVisible = false;

View File

@ -1,10 +1,38 @@
<template> <template>
<div>
<span v-for="(permission, index) in permissions" :key="index" style="margin-left: 25px;">
<el-checkbox v-model="permission['checked']" @change="change($event, permission)">{{ permission.name }}</el-checkbox>
</span>
</div>
</template> </template>
<script> <script>
export default { export default {
name: "GroupPermission" name: "GroupPermission",
props: {
permissions: {
type: Array,
default() {
return {}
}
},
selected: {
type: Array,
default() {
return []
}
}
},
data() {
return {
}
},
methods: {
change(val, permission) {
permission.checked = val;
}
}
} }
</script> </script>

View File

@ -29,8 +29,8 @@
<el-table-column :label="$t('commons.operating')"> <el-table-column :label="$t('commons.operating')">
<template v-slot:default="scope"> <template v-slot:default="scope">
<ms-table-operator @editClick="edit(scope.row)" @deleteClick="del(scope.row)"> <ms-table-operator @editClick="edit(scope.row)" @deleteClick="del(scope.row)">
<template v-slot:behind> <template v-slot:middle>
<ms-table-operator-button tip="复制" icon="el-icon-document-copy" @exec="copy(scope.row)"/> <!-- <ms-table-operator-button tip="复制" icon="el-icon-document-copy" @exec="copy(scope.row)"/>-->
<ms-table-operator-button tip="设置权限" icon="el-icon-s-tools" @exec="setPermission(scope.row)"/> <ms-table-operator-button tip="设置权限" icon="el-icon-s-tools" @exec="setPermission(scope.row)"/>
</template> </template>
</ms-table-operator> </ms-table-operator>
@ -43,8 +43,8 @@
</el-card> </el-card>
<edit-user-group ref="editUserGroup" @refresh="initData"/> <edit-user-group ref="editUserGroup" @refresh="initData"/>
<edit-permission ref="editPermission"/> <edit-permission ref="editPermission"/>
<ms-delete-confirm title="删除用户组" @delete="_handleDel" ref="deleteConfirm"/>
</div> </div>
</template> </template>
@ -56,6 +56,7 @@ import {USER_GROUP_SCOPE} from "@/common/js/table-constants";
import EditUserGroup from "@/business/components/settings/system/group/EditUserGroup"; import EditUserGroup from "@/business/components/settings/system/group/EditUserGroup";
import MsTableOperatorButton from "@/business/components/common/components/MsTableOperatorButton"; import MsTableOperatorButton from "@/business/components/common/components/MsTableOperatorButton";
import EditPermission from "@/business/components/settings/system/group/EditPermission"; import EditPermission from "@/business/components/settings/system/group/EditPermission";
import MsDeleteConfirm from "@/business/components/common/components/MsDeleteConfirm";
export default { export default {
name: "UserGroup", name: "UserGroup",
@ -65,7 +66,8 @@ export default {
MsTableOperator, MsTableOperator,
MsTablePagination, MsTablePagination,
MsTableOperatorButton, MsTableOperatorButton,
EditPermission EditPermission,
MsDeleteConfirm
}, },
data() { data() {
return { return {
@ -77,9 +79,8 @@ export default {
groups: [] groups: []
} }
}, },
created() { activated() {
this.initData(); this.initData();
this.getGroupJson();
}, },
computed: { computed: {
userGroupType() { userGroupType() {
@ -102,23 +103,21 @@ export default {
edit(row) { edit(row) {
this.$refs.editUserGroup.open(row, 'edit'); this.$refs.editUserGroup.open(row, 'edit');
}, },
del(row) { _handleDel(row) {
this.result = this.$get("/user/group/delete/" + row.id, () => { this.result = this.$get("/user/group/delete/" + row.id, () => {
this.$success(this.$t('commons.delete_success')); this.$success(this.$t('commons.delete_success'));
this.initData(); this.initData();
}) })
}, },
del(row) {
this.$refs.deleteConfirm.open(row);
},
copy(row) { copy(row) {
console.log(row) console.log(row)
}, },
setPermission(row) { setPermission(row) {
this.$refs.editPermission.open(); this.$refs.editPermission.open(row);
}, },
getGroupJson() {
this.$get("/user/group/permission", result => {
let data = result.data;
})
}
} }
} }
</script> </script>