refactor(用户组和权限): 后台添加权限认证

This commit is contained in:
shiziyuan9527 2021-06-23 17:48:07 +08:00 committed by 刘瑞斌
parent d0c9ca9c33
commit 55b9a4c3c6
2 changed files with 15 additions and 6 deletions

View File

@ -2,12 +2,14 @@ package io.metersphere.controller;
import io.metersphere.base.domain.Group;
import io.metersphere.base.domain.Organization;
import io.metersphere.commons.constants.PermissionConstants;
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.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
@ -22,6 +24,7 @@ public class GroupController {
private GroupService groupService;
@PostMapping("/get/{goPage}/{pageSize}")
@RequiresPermissions(PermissionConstants.SYSTEM_GROUP_READ)
public Pager<List<GroupDTO>> getGroupList(@PathVariable int goPage, @PathVariable int pageSize, @RequestBody EditGroupRequest request) {
request.setGoPage(goPage);
request.setPageSize(pageSize);
@ -29,21 +32,25 @@ public class GroupController {
}
@PostMapping("/get")
@RequiresPermissions(PermissionConstants.SYSTEM_GROUP_READ)
public List<Group> getGroupByType(@RequestBody EditGroupRequest request) {
return groupService.getGroupByType(request);
}
@PostMapping("/add")
@RequiresPermissions(PermissionConstants.SYSTEM_GROUP_READ_CREATE)
public Group addGroup(@RequestBody EditGroupRequest request) {
return groupService.addGroup(request);
}
@PostMapping("/edit")
@RequiresPermissions(PermissionConstants.SYSTEM_GROUP_READ_EDIT)
public void editGroup(@RequestBody EditGroupRequest request) {
groupService.editGroup(request);
}
@GetMapping("/delete/{id}")
@RequiresPermissions(PermissionConstants.SYSTEM_GROUP_READ_DELETE)
public void deleteGroup(@PathVariable String id) {
groupService.deleteGroup(id);
}
@ -54,7 +61,8 @@ public class GroupController {
}
@PostMapping("/permission/edit")
public void EditGroupPermission(@RequestBody EditGroupRequest editGroupRequest) {
@RequiresPermissions(PermissionConstants.SYSTEM_GROUP_READ_SETTING_PERMISSION)
public void editGroupPermission(@RequestBody EditGroupRequest editGroupRequest) {
groupService.editGroupPermission(editGroupRequest);
}
@ -64,6 +72,7 @@ public class GroupController {
}
@PostMapping("/list")
@RequiresPermissions(PermissionConstants.SYSTEM_GROUP_READ)
public List<Group> getGroupsByType(@RequestBody GroupRequest request) {
return groupService.getGroupsByType(request);
}

View File

@ -10,6 +10,7 @@ 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.UserGroupConstants;
import io.metersphere.commons.constants.UserGroupType;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.user.SessionUser;
@ -63,7 +64,7 @@ public class GroupService {
public Pager<List<GroupDTO>> getGroupList(EditGroupRequest request) {
SessionUser user = SessionUtils.getUser();
List<UserGroupDTO> userGroup = extUserGroupMapper.getUserGroup(user.getId());
List<UserGroupDTO> userGroup = extUserGroupMapper.getUserGroup(Objects.requireNonNull(user).getId());
List<String> groupTypeList = userGroup.stream().map(UserGroupDTO::getType).collect(Collectors.toList());
return getGroups(groupTypeList, request);
}
@ -104,7 +105,7 @@ public class GroupService {
}
public void editGroup(EditGroupRequest request) {
if (StringUtils.equals(request.getId(), "admin")) {
if (StringUtils.equals(request.getId(), UserGroupConstants.ADMIN)) {
MSException.throwException("系统管理员无法编辑!");
}
checkGroupExist(request);
@ -145,7 +146,7 @@ public class GroupService {
if (permission == null) {
throw new RuntimeException("读取文件失败!");
} else {
GroupJson group = null;
GroupJson group;
try {
group = JSON.parseObject(permission, GroupJson.class);
List<GroupResource> resource = group.getResource();
@ -209,8 +210,7 @@ public class GroupService {
userGroupExample.createCriteria().andUserIdEqualTo(userId);
List<UserGroup> userGroups = userGroupMapper.selectByExample(userGroupExample);
List<String> groupsIds = userGroups.stream().map(UserGroup::getGroupId).distinct().collect(Collectors.toList());
for (int i = 0; i < groupsIds.size(); i++) {
String id = groupsIds.get(i);
for (String id : groupsIds) {
Group group = groupMapper.selectByPrimaryKey(id);
String type = group.getType();
Map<String, Object> map = new HashMap<>(2);