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);
}
@GetMapping("/permission")
public GroupPermissionDTO getGroupResource() {
return groupService.getGroupResource();
@PostMapping("/permission")
public GroupPermissionDTO getGroupResource(@RequestBody Group group) {
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;
import io.metersphere.base.domain.Group;
import io.metersphere.dto.GroupPermission;
import lombok.Getter;
import lombok.Setter;
@ -20,4 +21,8 @@ public class EditGroupRequest extends Group {
* 是否是全局用户组
*/
private Boolean global;
private List<GroupPermission> permissions;
private String userGroupId;
}

View File

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

View File

@ -1,11 +1,11 @@
package io.metersphere.service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.metersphere.base.domain.*;
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.ExtUserGroupMapper;
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.controller.request.group.EditGroupRequest;
import io.metersphere.dto.*;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
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.transaction.annotation.Transactional;
@ -38,6 +42,10 @@ public class GroupService {
private GroupMapper groupMapper;
@Resource
private ExtGroupMapper extGroupMapper;
@Resource
private SqlSessionFactory sqlSessionFactory;
@Resource
private UserGroupPermissionMapper userGroupPermissionMapper;
public Pager<List<GroupDTO>> getGroupList(EditGroupRequest request) {
SessionUser user = SessionUtils.getUser();
@ -75,24 +83,29 @@ public class GroupService {
public void deleteGroup(String 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();
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) {
throw new RuntimeException("读取文件失败");
throw new RuntimeException("读取文件失败!");
} else {
GroupJson group = null;
try {
group = JSON.parseObject(permission, GroupJson.class);
List<GroupResource> resource = group.getResource();
List<GroupPermission> permissions = group.getPermissions();
dto.setSystem(getResourcePermission(resource, permissions, "SYSTEM"));
dto.setOrganization(getResourcePermission(resource, permissions, "ORGANIZATION"));
dto.setWorkspace(getResourcePermission(resource, permissions, "WORKSPACE"));
dto.setProject(getResourcePermission(resource, permissions, "PROJECT"));
getPermission(resource, permissions, type, dto, permissionList);
} catch (IOException e) {
e.printStackTrace();
}
@ -100,9 +113,48 @@ public class GroupService {
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<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) {
GroupResourceDTO resourceDTO = new GroupResourceDTO();
resourceDTO.setResource(r);
@ -135,6 +187,34 @@ public class GroupService {
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) {

File diff suppressed because it is too large Load Diff

View File

@ -1,74 +1,111 @@
<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="设置权限"
:destroy-on-close="true">
<el-table
:data="tableData"
style="width: 100%">
<el-table-column
type="selection"
width="55">
</el-table-column>
<el-table-column
prop="func"
label="功能菜单"
width="180">
</el-table-column>
<!-- <el-table-column-->
<!-- prop="operators"-->
<!-- label="操作对象"-->
<!-- width="180">-->
<!-- <template v-slot:default="scope">-->
<!-- <group-operator :operators="scope.row.operators"/>-->
<!-- </template>-->
<!-- </el-table-column>-->
<!-- <el-table-column-->
<!-- prop="permission"-->
<!-- label="权限">-->
<!-- <template v-slot:default="scope">-->
<!-- {{scope.row.permissions}}-->
<!-- </template>-->
<!-- </el-table-column>-->
</el-table>
:destroy-on-close="true"
v-loading="result.loading"
>
<div style="height: 500px;overflow: auto">
<el-table
:data="tableData"
style="width: 100%">
<!-- <el-table-column-->
<!-- type="selection"-->
<!-- width="55">-->
<!-- </el-table-column>-->
<el-table-column
prop="resource"
label="操作对象"
width="180">
<template v-slot:default="scope">
{{scope.row.resource.name}}
</template>
</el-table-column>
<el-table-column
prop="permissions"
label="权限">
<template v-slot:default="scope">
<group-permission :permissions="scope.row.permissions" :selected.sync="tableData"/>
</template>
</el-table-column>
</el-table>
</div>
<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>
</template>
<script>
import GroupOperator from "@/business/components/settings/system/group/GroupOperator";
import GroupPermission from "@/business/components/settings/system/group/GroupPermission";
export default {
name: "GroupPermission",
name: "EditPermission",
component: {
GroupPermission,
},
data() {
return {
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: {
GroupPermission,
GroupOperator
},
methods: {
open() {
open(row) {
this.tableData = [];
this.dialogVisible = true;
this.getGroupJson();
this.group = Object.assign({}, row);
this.getGroupJson(this.group.type);
},
getGroupJson() {
this.$get("/user/group/permission", result => {
getGroupJson(type) {
this.result = this.$post("/user/group/permission", this.group, result => {
let data = result.data;
let arr = [];
this._getData("系统", data.system, arr);
this._getData("组织", data.organization, arr);
this._getData("工作空间", data.workspace, arr);
this._getData("项目", data.project, arr);
this.tableData = arr;
if (data) {
this.tableData = this._getGroupPermission(data, type);
}
})
},
_getData(type, data, arr) {
let obj = {};
obj.func = type;
obj.operators = data.map(s => s.resource);
obj.permissions = data.map(s => s.permissions);
arr.push(obj);
_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 = [];
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.dialogType = type;
this.form = Object.assign({}, row);
this.form.global = this.form.scopeId === "global";
if (type !== 'create') {
this.form.global = this.form.scopeId === "global";
}
},
cancel() {
this.dialogVisible = false;

View File

@ -1,10 +1,38 @@
<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>
<script>
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>

View File

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