From 55b9a4c3c69864084bf7a2ebb1a6147b4863316e Mon Sep 17 00:00:00 2001 From: shiziyuan9527 Date: Wed, 23 Jun 2021 17:48:07 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E7=94=A8=E6=88=B7=E7=BB=84=E5=92=8C?= =?UTF-8?q?=E6=9D=83=E9=99=90):=20=E5=90=8E=E5=8F=B0=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E6=9D=83=E9=99=90=E8=AE=A4=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/metersphere/controller/GroupController.java | 11 ++++++++++- .../java/io/metersphere/service/GroupService.java | 10 +++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/backend/src/main/java/io/metersphere/controller/GroupController.java b/backend/src/main/java/io/metersphere/controller/GroupController.java index 897a5fdb56..96922cd070 100644 --- a/backend/src/main/java/io/metersphere/controller/GroupController.java +++ b/backend/src/main/java/io/metersphere/controller/GroupController.java @@ -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> 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 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 getGroupsByType(@RequestBody GroupRequest request) { return groupService.getGroupsByType(request); } diff --git a/backend/src/main/java/io/metersphere/service/GroupService.java b/backend/src/main/java/io/metersphere/service/GroupService.java index 89a1e4332e..d19e60e4a7 100644 --- a/backend/src/main/java/io/metersphere/service/GroupService.java +++ b/backend/src/main/java/io/metersphere/service/GroupService.java @@ -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> getGroupList(EditGroupRequest request) { SessionUser user = SessionUtils.getUser(); - List userGroup = extUserGroupMapper.getUserGroup(user.getId()); + List userGroup = extUserGroupMapper.getUserGroup(Objects.requireNonNull(user).getId()); List 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 resource = group.getResource(); @@ -209,8 +210,7 @@ public class GroupService { userGroupExample.createCriteria().andUserIdEqualTo(userId); List userGroups = userGroupMapper.selectByExample(userGroupExample); List 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 map = new HashMap<>(2);