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

This commit is contained in:
shiziyuan9527 2021-05-21 14:04:10 +08:00 committed by 刘瑞斌
parent 2f74b4bb56
commit 6ab68ee48d
3 changed files with 117 additions and 24 deletions

View File

@ -1,18 +1,14 @@
package io.metersphere.controller;
import io.metersphere.base.domain.Group;
import io.metersphere.base.domain.Role;
import io.metersphere.commons.constants.RoleConstants;
import io.metersphere.base.domain.Organization;
import io.metersphere.commons.utils.Pager;
import io.metersphere.controller.request.GroupRequest;
import io.metersphere.controller.request.group.EditGroupRequest;
import io.metersphere.dto.GroupDTO;
import io.metersphere.dto.GroupPermissionDTO;
import io.metersphere.service.GroupService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
@ -78,10 +74,12 @@ public class GroupController {
}
@GetMapping("/list/ws/{workspaceId}/{userId}")
@RequiresRoles(value = {RoleConstants.ADMIN, RoleConstants.ORG_ADMIN, RoleConstants.TEST_MANAGER}, logical = Logical.OR)
public List<Group> getWorkspaceMemberGroups(@PathVariable String workspaceId, @PathVariable String userId) {
return groupService.getWorkspaceMemberGroups(workspaceId, userId);
}
@GetMapping("/org/{userId}")
public List<Organization> getOrganization(@PathVariable String userId) {
return groupService.getOrganization(userId);
}
}

View File

@ -5,11 +5,13 @@ 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.OrganizationMapper;
import io.metersphere.base.mapper.UserGroupMapper;
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;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.user.SessionUser;
import io.metersphere.commons.utils.BeanUtils;
import io.metersphere.commons.utils.PageUtils;
@ -52,6 +54,8 @@ public class GroupService {
private UserGroupMapper userGroupMapper;
@Resource
private OrganizationService organizationService;
@Resource
private OrganizationMapper organizationMapper;
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));
@ -69,6 +73,7 @@ public class GroupService {
public Group addGroup(EditGroupRequest request) {
Group group = new Group();
checkGroupExist(request);
group.setId(UUID.randomUUID().toString());
group.setName(request.getName());
group.setCreator(SessionUtils.getUserId());
@ -86,7 +91,23 @@ public class GroupService {
return group;
}
private void checkGroupExist(EditGroupRequest request) {
String name = request.getName();
String id = request.getId();
GroupExample groupExample = new GroupExample();
GroupExample.Criteria criteria = groupExample.createCriteria();
criteria.andNameEqualTo(name);
if (StringUtils.isNotBlank(id)) {
criteria.andIdNotEqualTo(id);
}
List<Group> groups = groupMapper.selectByExample(groupExample);
if (CollectionUtils.isNotEmpty(groups)) {
MSException.throwException("用户组名称已存在!");
}
}
public void editGroup(EditGroupRequest request) {
checkGroupExist(request);
Group group = new Group();
request.setScopeId(null);
BeanUtils.copyBean(group, request);
@ -280,4 +301,28 @@ public class GroupService {
return PageUtils.setPageInfo(page, groups);
}
public List<Organization> getOrganization(String userId) {
List<Organization> list = new ArrayList<>();
GroupExample groupExample = new GroupExample();
groupExample.createCriteria().andTypeEqualTo(UserGroupType.ORGANIZATION);
List<Group> groups = groupMapper.selectByExample(groupExample);
List<String> groupIds = groups.stream().map(Group::getId).collect(Collectors.toList());
if (CollectionUtils.isEmpty(groups)) {
return list;
}
UserGroupExample userGroupExample = new UserGroupExample();
userGroupExample.createCriteria().andUserIdEqualTo(userId).andGroupIdIn(groupIds);
List<UserGroup> userGroups = userGroupMapper.selectByExample(userGroupExample);
List<String> orgIds = userGroups.stream().map(UserGroup::getSourceId).collect(Collectors.toList());
if (CollectionUtils.isEmpty(orgIds)) {
return list;
}
OrganizationExample organizationExample = new OrganizationExample();
organizationExample.createCriteria().andIdIn(orgIds);
list = organizationMapper.selectByExample(organizationExample);
return list;
}
}

View File

@ -5,13 +5,13 @@
<el-form ref="form" :model="form" label-width="auto" size="small" :rules="rules">
<el-row>
<el-col :span="11">
<el-form-item label="名称">
<el-form-item label="名称" prop="name">
<el-input v-model="form.name"></el-input>
</el-form-item>
</el-col>
<el-col :span="11" :offset="2">
<el-form-item label="所属类型">
<el-select v-model="form.type" placeholder="请选择所属类型" style="width: 100%">
<el-form-item label="所属类型" prop="type">
<el-select v-model="form.type" placeholder="请选择所属类型" style="width: 100%" @change="changeGroup">
<el-option label="系统" value="SYSTEM"></el-option>
<el-option label="组织" value="ORGANIZATION"></el-option>
<el-option label="工作空间" value="WORKSPACE"></el-option>
@ -20,19 +20,16 @@
</el-form-item>
</el-col>
</el-row>
<el-form-item label="描述">
<el-form-item label="描述" prop="description">
<el-input type="textarea" v-model="form.description"></el-input>
</el-form-item>
<el-form-item label="全局用户组">
<el-switch v-model="form.global" :disabled="dialogType === 'edit'"></el-switch>
<el-switch v-model="form.global" :disabled="dialogType === 'edit'" @change="change(form.global)"></el-switch>
</el-form-item>
<el-form-item label="所属组织" v-if="!form.global">
<el-select v-model="form.scopeId" placeholder="请选择所属组织" style="width: 100%;" :disabled="dialogType === 'edit'">
<el-option label="区域一" value="shanghai"></el-option>
<el-option label="区域二" value="beijing"></el-option>
<el-form-item label="所属组织" v-if="show" prop="scopeId">
<el-select v-model="form.scopeId" placeholder="请选择所属组织" style="width: 100%;" :disabled="dialogType === 'edit'" clearable>
<el-option v-for="item in organizations" :key="item.id" :label="item.name" :value="item.id"/>
</el-select>
</el-form-item>
<el-form-item>
@ -44,6 +41,9 @@
</template>
<script>
import {GROUP_SYSTEM} from "@/common/js/constants";
import {getCurrentUserId} from "@/common/js/utils";
export default {
name: "EditUserGroup",
data() {
@ -53,9 +53,24 @@ export default {
global: false
},
rules: {
name: [
{required: true, message: '请输入名称', trigger: 'blur'},
{min: 2, max: 50, message: this.$t('commons.input_limit', [2, 50]), trigger: 'blur'},
],
type: [
{required: true, message: '请选择所属类型', trigger: 'blur'},
],
description: [
{min: 2, max: 90, message: this.$t('commons.input_limit', [2, 90]), trigger: 'blur'},
],
scopeId: [
{required: true, message: '请选择所属组织', trigger: 'blur'},
]
},
dialogType: '',
isSystem: false,
show: true,
organizations: []
}
},
props: {
@ -83,10 +98,16 @@ export default {
},
create() {
this.$post("/user/group/add", this.form, () => {
this.$success(this.$t('commons.save_success'));
this.$emit("refresh")
this.dialogVisible = false;
this.$refs['form'].validate(valid => {
if (valid) {
this.$post("/user/group/add", this.form, () => {
this.$success(this.$t('commons.save_success'));
this.$emit("refresh")
this.dialogVisible = false;
})
} else {
return false;
}
})
},
edit() {
@ -97,15 +118,44 @@ export default {
})
},
open(row, type) {
this.isSystem = false;
this.show = true;
this.dialogVisible = true;
this.dialogType = type;
this.form = Object.assign({}, row);
if (type !== 'create') {
this.form.global = this.form.scopeId === "global";
if (this.form.type === GROUP_SYSTEM) {
this.form.global = true;
this.show = false;
} else {
this.form.global = this.form.scopeId === "global";
this.show = !this.form.global;
}
}
this.getOrganization();
},
cancel() {
this.dialogVisible = false;
},
change(global) {
this.show = this.isSystem ? false : !global;
},
changeGroup(val) {
if (val === GROUP_SYSTEM) {
this.isSystem = true;
this.$set(this.form, "global", true);
this.change(true);
} else {
this.isSystem = false;
}
},
getOrganization() {
this.$get("/user/group/org/" + getCurrentUserId(), res => {
let data = res.data;
if (data) {
this.organizations = data;
}
})
}
}
}