From dac30f47a5d1134691962b1d0af85399f0c59587 Mon Sep 17 00:00:00 2001 From: shiziyuan9527 Date: Mon, 17 Feb 2020 23:58:09 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=92=E8=89=B2=E5=88=87=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../base/mapper/ext/ExtUserRoleMapper.java | 11 ++ .../base/mapper/ext/ExtUserRoleMapper.xml | 30 +++++ .../controller/UserController.java | 20 ++- .../main/java/io/metersphere/dto/UserDTO.java | 10 ++ .../java/io/metersphere/dto/UserRoleDTO.java | 68 +++++++++++ .../io/metersphere/dto/UserRoleHelpDTO.java | 50 ++++++++ .../io/metersphere/service/UserService.java | 112 ++++++++++++++++- frontend/src/login/Login.vue | 2 + frontend/src/performance/App.vue | 4 +- .../components/HeaderSwitchUser.vue | 114 ++++++++++++++++++ .../components/settings/SettingMenu.vue | 6 +- frontend/src/performance/permission.js | 15 +-- frontend/src/performance/store.js | 12 +- 13 files changed, 426 insertions(+), 28 deletions(-) create mode 100644 backend/src/main/java/io/metersphere/base/mapper/ext/ExtUserRoleMapper.java create mode 100644 backend/src/main/java/io/metersphere/base/mapper/ext/ExtUserRoleMapper.xml create mode 100644 backend/src/main/java/io/metersphere/dto/UserRoleDTO.java create mode 100644 backend/src/main/java/io/metersphere/dto/UserRoleHelpDTO.java create mode 100644 frontend/src/performance/components/HeaderSwitchUser.vue diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtUserRoleMapper.java b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtUserRoleMapper.java new file mode 100644 index 0000000000..3cbe99f3b0 --- /dev/null +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtUserRoleMapper.java @@ -0,0 +1,11 @@ +package io.metersphere.base.mapper.ext; + +import io.metersphere.dto.UserRoleHelpDTO; +import org.apache.ibatis.annotations.Param; + +import java.util.List; + +public interface ExtUserRoleMapper { + + List getUserRoleHelpList(@Param("userId") String userId); +} diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtUserRoleMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtUserRoleMapper.xml new file mode 100644 index 0000000000..ed798731f7 --- /dev/null +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtUserRoleMapper.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/backend/src/main/java/io/metersphere/controller/UserController.java b/backend/src/main/java/io/metersphere/controller/UserController.java index 15b801b4be..5eeed703bb 100644 --- a/backend/src/main/java/io/metersphere/controller/UserController.java +++ b/backend/src/main/java/io/metersphere/controller/UserController.java @@ -3,10 +3,10 @@ package io.metersphere.controller; import io.metersphere.base.domain.Role; import io.metersphere.base.domain.User; import io.metersphere.dto.UserDTO; -import io.metersphere.dto.UserOperateDTO; +import io.metersphere.dto.UserRoleDTO; import io.metersphere.service.UserService; +import io.metersphere.user.SessionUtils; import org.springframework.web.bind.annotation.*; - import javax.annotation.Resource; import java.util.List; @@ -35,4 +35,20 @@ public class UserController { public List getUserRolesList(@PathVariable(value = "userId") String userId) { return userService.getUserRolesList(userId); } + + @GetMapping("/rolelist/{userId}") + public List convertUserRoleDTO(@PathVariable(value = "userId") String userId) { + return userService.getUserRoleList(userId); + } + + @PostMapping("/switch/source/{sourceId}") + public void switchUserRole(@PathVariable (value = "sourceId") String sourceId) { + UserDTO user = SessionUtils.getUser(); + userService.switchUserRole(user, sourceId); + } + + @GetMapping("/info/{userId}") + public User getUserInfo(@PathVariable(value = "userId") String userId) { + return userService.getUserInfo(userId); + } } diff --git a/backend/src/main/java/io/metersphere/dto/UserDTO.java b/backend/src/main/java/io/metersphere/dto/UserDTO.java index 54a38270ca..b6e684af51 100644 --- a/backend/src/main/java/io/metersphere/dto/UserDTO.java +++ b/backend/src/main/java/io/metersphere/dto/UserDTO.java @@ -20,6 +20,16 @@ public class UserDTO { private Long updateTime; + private String lastSourceId; + + public String getLastSourceId() { + return lastSourceId; + } + + public void setLastSourceId(String lastSourceId) { + this.lastSourceId = lastSourceId; + } + private List roles = new ArrayList<>(); public List getRoles() { diff --git a/backend/src/main/java/io/metersphere/dto/UserRoleDTO.java b/backend/src/main/java/io/metersphere/dto/UserRoleDTO.java new file mode 100644 index 0000000000..602fa101db --- /dev/null +++ b/backend/src/main/java/io/metersphere/dto/UserRoleDTO.java @@ -0,0 +1,68 @@ +package io.metersphere.dto; + +public class UserRoleDTO { + + private String id; + private String roleId; + private String type; + private String name; + private String desc; + private String parentId; + private Boolean switchable = true; + + public String getRoleId() { + return roleId; + } + + public void setRoleId(String roleId) { + this.roleId = roleId; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public Boolean getSwitchable() { + return switchable; + } + + public void setSwitchable(Boolean switchable) { + this.switchable = switchable; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDesc() { + return desc; + } + + public void setDesc(String desc) { + this.desc = desc; + } + + public String getParentId() { + return parentId; + } + + public void setParentId(String parentId) { + this.parentId = parentId; + } +} diff --git a/backend/src/main/java/io/metersphere/dto/UserRoleHelpDTO.java b/backend/src/main/java/io/metersphere/dto/UserRoleHelpDTO.java new file mode 100644 index 0000000000..f36e94ea96 --- /dev/null +++ b/backend/src/main/java/io/metersphere/dto/UserRoleHelpDTO.java @@ -0,0 +1,50 @@ +package io.metersphere.dto; + +public class UserRoleHelpDTO { + + private String roleId; + private String roleName; + private String sourceId; + private String sourceName; + private String parentId; + + public String getParentId() { + return parentId; + } + + public void setParentId(String parentId) { + this.parentId = parentId; + } + + public String getRoleId() { + return roleId; + } + + public void setRoleId(String roleId) { + this.roleId = roleId; + } + + public String getRoleName() { + return roleName; + } + + public void setRoleName(String roleName) { + this.roleName = roleName; + } + + public String getSourceId() { + return sourceId; + } + + public void setSourceId(String sourceId) { + this.sourceId = sourceId; + } + + public String getSourceName() { + return sourceName; + } + + public void setSourceName(String sourceName) { + this.sourceName = sourceName; + } +} diff --git a/backend/src/main/java/io/metersphere/service/UserService.java b/backend/src/main/java/io/metersphere/service/UserService.java index 50589d6232..66b84ad20d 100644 --- a/backend/src/main/java/io/metersphere/service/UserService.java +++ b/backend/src/main/java/io/metersphere/service/UserService.java @@ -1,12 +1,16 @@ package io.metersphere.service; import io.metersphere.base.domain.*; +import io.metersphere.base.mapper.OrganizationMapper; import io.metersphere.base.mapper.RoleMapper; import io.metersphere.base.mapper.UserMapper; import io.metersphere.base.mapper.UserRoleMapper; +import io.metersphere.base.mapper.ext.ExtUserRoleMapper; import io.metersphere.commons.exception.MSException; import io.metersphere.dto.UserDTO; import io.metersphere.dto.UserOperateDTO; +import io.metersphere.dto.UserRoleDTO; +import io.metersphere.dto.UserRoleHelpDTO; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; @@ -14,7 +18,7 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; import javax.annotation.Resource; -import java.util.List; +import java.util.*; import java.util.stream.Collectors; @Service @@ -27,6 +31,10 @@ public class UserService { private RoleMapper roleMapper; @Resource private UserRoleMapper userRoleMapper; + @Resource + private ExtUserRoleMapper extUserRoleMapper; + @Resource + private OrganizationMapper organizationMapper; public UserDTO insert(User user) { checkUserParam(user); @@ -113,4 +121,106 @@ public class UserService { roleExample.createCriteria().andIdIn(roleIds); return roleMapper.selectByExample(roleExample); } + + public List getUserRoleList(String userId) { + if (StringUtils.isEmpty(userId)) { + return new ArrayList<>(); + } + return convertUserRoleDTO(extUserRoleMapper.getUserRoleHelpList(userId)); + } + + private List convertUserRoleDTO(List helpDTOList) { + StringBuilder buffer = new StringBuilder(); + + Map roleMap = new HashMap<>(); + + List resultList = new ArrayList<>(); + + List otherList = new ArrayList<>(); + + Set orgSet = new HashSet<>(); + + Set workspaceSet = new HashSet<>(); + + for (UserRoleHelpDTO helpDTO : helpDTOList) { + UserRoleDTO userRoleDTO = roleMap.get(helpDTO.getSourceId()); + + if (userRoleDTO == null) { + userRoleDTO = new UserRoleDTO(); + + if (!StringUtils.isEmpty(helpDTO.getParentId())) { + workspaceSet.add(helpDTO.getParentId()); + userRoleDTO.setType("workspace"); + } else { + orgSet.add(helpDTO.getSourceId()); + userRoleDTO.setType("organization"); + } + + userRoleDTO.setId(helpDTO.getSourceId()); + userRoleDTO.setRoleId(helpDTO.getRoleId()); + userRoleDTO.setName(helpDTO.getSourceName()); + userRoleDTO.setParentId(helpDTO.getParentId()); + userRoleDTO.setDesc(helpDTO.getRoleName()); + + } else { + userRoleDTO.setDesc(userRoleDTO.getDesc() + "," + helpDTO.getRoleName()); + } + roleMap.put(helpDTO.getSourceId(), userRoleDTO); + } + + if (!StringUtils.isEmpty(buffer.toString())) { + UserRoleDTO dto = new UserRoleDTO(); + dto.setId("admin"); + dto.setType("admin"); + dto.setDesc(buffer.toString()); + resultList.add(dto); + } + + for (String org : orgSet) { + workspaceSet.remove(org); + } + + List orgWorkSpace = new ArrayList<>(roleMap.values()); + + if (!CollectionUtils.isEmpty(workspaceSet)) { + for (String orgId : workspaceSet) { + Organization organization = organizationMapper.selectByPrimaryKey(orgId); + if (organization != null) { + UserRoleDTO dto = new UserRoleDTO(); + dto.setId(orgId); + dto.setName(organization.getName()); + dto.setSwitchable(false); + dto.setType("organization"); + orgWorkSpace.add(dto); + } + } + } + + orgWorkSpace.sort((o1, o2) -> { + if (o1.getParentId() == null) { + return -1; + } + + if (o2.getParentId() == null) { + return 1; + } + + return o1.getParentId().compareTo(o2.getParentId()); + }); + resultList.addAll(orgWorkSpace); + resultList.addAll(otherList); + + return resultList; + } + + public void switchUserRole(UserDTO user, String sourceId) { + User newUser = new User(); + BeanUtils.copyProperties(user, newUser); + newUser.setLastSourceId(sourceId); + userMapper.updateByPrimaryKeySelective(newUser); + } + + public User getUserInfo(String userId) { + return userMapper.selectByPrimaryKey(userId); + } } diff --git a/frontend/src/login/Login.vue b/frontend/src/login/Login.vue index 7bb1c87e90..a0ddc5ac2b 100644 --- a/frontend/src/login/Login.vue +++ b/frontend/src/login/Login.vue @@ -109,6 +109,8 @@ this.$post("signin", this.form, (response) => { // 登录信息保存 cookie Cookies.set(TokenKey, response.data); + // 保存上次角色 + localStorage.setItem("lastSourceId", JSON.parse(Cookies.get(TokenKey)).lastSourceId); window.location.href = "/" }); } else { diff --git a/frontend/src/performance/App.vue b/frontend/src/performance/App.vue index 15d39aaa49..de6a245a25 100644 --- a/frontend/src/performance/App.vue +++ b/frontend/src/performance/App.vue @@ -2,6 +2,7 @@