diff --git a/backend/src/main/java/io/metersphere/commons/constants/SessionConstants.java b/backend/src/main/java/io/metersphere/commons/constants/SessionConstants.java new file mode 100644 index 0000000000..85e4ff1935 --- /dev/null +++ b/backend/src/main/java/io/metersphere/commons/constants/SessionConstants.java @@ -0,0 +1,5 @@ +package io.metersphere.commons.constants; + +public class SessionConstants { + public static final String ATTR_USER = "user"; +} diff --git a/backend/src/main/java/io/metersphere/controller/WorkspaceController.java b/backend/src/main/java/io/metersphere/controller/WorkspaceController.java index 52efa2cff9..3d0f98bc8f 100644 --- a/backend/src/main/java/io/metersphere/controller/WorkspaceController.java +++ b/backend/src/main/java/io/metersphere/controller/WorkspaceController.java @@ -34,7 +34,7 @@ public class WorkspaceController { @GetMapping("delete/{workspaceId}") @RequiresRoles(RoleConstants.ORG_ADMIN) - public void saveWorkspace(@PathVariable String workspaceId) { + public void deleteWorkspace(@PathVariable String workspaceId) { workspaceService.checkOwner(workspaceId); workspaceService.deleteWorkspace(workspaceId); } diff --git a/backend/src/main/java/io/metersphere/dto/UserDTO.java b/backend/src/main/java/io/metersphere/dto/UserDTO.java index b6e684af51..555e1feff1 100644 --- a/backend/src/main/java/io/metersphere/dto/UserDTO.java +++ b/backend/src/main/java/io/metersphere/dto/UserDTO.java @@ -1,6 +1,7 @@ package io.metersphere.dto; import io.metersphere.base.domain.Role; +import io.metersphere.base.domain.UserRole; import java.util.ArrayList; import java.util.List; @@ -22,6 +23,10 @@ public class UserDTO { private String lastSourceId; + private List roles = new ArrayList<>(); + + private List userRoles = new ArrayList<>(); + public String getLastSourceId() { return lastSourceId; } @@ -30,8 +35,6 @@ public class UserDTO { this.lastSourceId = lastSourceId; } - private List roles = new ArrayList<>(); - public List getRoles() { return roles; } @@ -97,4 +100,12 @@ public class UserDTO { public void setUpdateTime(Long updateTime) { this.updateTime = updateTime; } + + public List getUserRoles() { + return userRoles; + } + + public void setUserRoles(List userRoles) { + this.userRoles = userRoles; + } } diff --git a/backend/src/main/java/io/metersphere/security/ShiroDBRealm.java b/backend/src/main/java/io/metersphere/security/ShiroDBRealm.java index 7f03597ec9..76f1bb5739 100644 --- a/backend/src/main/java/io/metersphere/security/ShiroDBRealm.java +++ b/backend/src/main/java/io/metersphere/security/ShiroDBRealm.java @@ -5,7 +5,7 @@ import io.metersphere.base.domain.Role; import io.metersphere.dto.UserDTO; import io.metersphere.service.UserService; import io.metersphere.user.SessionUser; -import org.apache.shiro.SecurityUtils; +import io.metersphere.user.SessionUtils; import org.apache.shiro.authc.*; import org.apache.shiro.authz.AuthorizationInfo; import org.apache.shiro.authz.SimpleAuthorizationInfo; @@ -68,7 +68,7 @@ public class ShiroDBRealm extends AuthorizingRealm { // TODO 密码验证 SessionUser sessionUser = SessionUser.fromUser(user); - SecurityUtils.getSubject().getSession().setAttribute("user", sessionUser); + SessionUtils.putUser(sessionUser); return new SimpleAuthenticationInfo(userId, password, getName()); } diff --git a/backend/src/main/java/io/metersphere/service/ProjectService.java b/backend/src/main/java/io/metersphere/service/ProjectService.java index 2a9f1867cf..428bc4a641 100644 --- a/backend/src/main/java/io/metersphere/service/ProjectService.java +++ b/backend/src/main/java/io/metersphere/service/ProjectService.java @@ -1,7 +1,11 @@ package io.metersphere.service; import io.metersphere.base.domain.Project; +import io.metersphere.base.domain.ProjectExample; import io.metersphere.base.mapper.ProjectMapper; +import io.metersphere.commons.exception.MSException; +import io.metersphere.user.SessionUtils; +import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -16,12 +20,22 @@ public class ProjectService { private ProjectMapper projectMapper; public Project addProject(Project project) { + if (StringUtils.isBlank(project.getName())) { + MSException.throwException("Project name cannot be null"); + } + ProjectExample example = new ProjectExample(); + example.createCriteria() + .andWorkspaceIdEqualTo(SessionUtils.getCurrentWorkspaceId()) + .andNameEqualTo(project.getName()); + if (projectMapper.countByExample(example) > 0) { + MSException.throwException("The project name already exists"); + } project.setId(UUID.randomUUID().toString()); long createTime = System.currentTimeMillis(); project.setCreateTime(createTime); project.setUpdateTime(createTime); - // todo set workspace id -// project.setWorkspaceId(); + // set workspace id + project.setWorkspaceId(SessionUtils.getCurrentWorkspaceId()); projectMapper.insertSelective(project); return project; } diff --git a/backend/src/main/java/io/metersphere/service/UserService.java b/backend/src/main/java/io/metersphere/service/UserService.java index 2f37a2aab3..901447a9c8 100644 --- a/backend/src/main/java/io/metersphere/service/UserService.java +++ b/backend/src/main/java/io/metersphere/service/UserService.java @@ -89,6 +89,8 @@ public class UserService { if (CollectionUtils.isEmpty(userRoleList)) { return userDTO; } + // 设置 user_role + userDTO.setUserRoles(userRoleList); List roleIds = userRoleList.stream().map(UserRole::getRoleId).collect(Collectors.toList()); diff --git a/backend/src/main/java/io/metersphere/service/WorkspaceService.java b/backend/src/main/java/io/metersphere/service/WorkspaceService.java index eab372637b..a5788652c3 100644 --- a/backend/src/main/java/io/metersphere/service/WorkspaceService.java +++ b/backend/src/main/java/io/metersphere/service/WorkspaceService.java @@ -1,8 +1,13 @@ package io.metersphere.service; +import io.metersphere.base.domain.UserRole; import io.metersphere.base.domain.Workspace; +import io.metersphere.base.domain.WorkspaceExample; import io.metersphere.base.mapper.WorkspaceMapper; +import io.metersphere.commons.constants.RoleConstants; import io.metersphere.commons.exception.MSException; +import io.metersphere.user.SessionUser; +import io.metersphere.user.SessionUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -10,6 +15,7 @@ import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.List; import java.util.UUID; +import java.util.stream.Collectors; @Service @Transactional(rollbackFor = Exception.class) @@ -21,12 +27,18 @@ public class WorkspaceService { if (StringUtils.isBlank(workspace.getName())) { MSException.throwException("Workspace name cannot be null."); } - // TODO 组织ID 暂无 - if (StringUtils.isBlank(workspace.getOrganizationId())) { - workspace.setOrganizationId("root"); - } + // set organization id + workspace.setOrganizationId(SessionUtils.getCurrentOrganizationId()); + long currentTime = System.currentTimeMillis(); if (StringUtils.isBlank(workspace.getId())) { + WorkspaceExample example = new WorkspaceExample(); + example.createCriteria() + .andOrganizationIdEqualTo(SessionUtils.getCurrentOrganizationId()) + .andNameEqualTo(workspace.getName()); + if (workspaceMapper.countByExample(example) > 0) { + MSException.throwException("The workspace name already exists"); + } workspace.setId(UUID.randomUUID().toString()); // 设置ID workspace.setCreateTime(currentTime); workspace.setUpdateTime(currentTime); // 首次 update time @@ -47,7 +59,18 @@ public class WorkspaceService { } public void checkOwner(String workspaceId) { - // TODO 验证当前用户是否拥有当前此空间权限 + SessionUser user = SessionUtils.getUser(); + List orgIds = user.getUserRoles().stream() + .filter(ur -> RoleConstants.ORG_ADMIN.equals(ur.getRoleId())) + .map(UserRole::getSourceId) + .collect(Collectors.toList()); + WorkspaceExample example = new WorkspaceExample(); + example.createCriteria() + .andOrganizationIdIn(orgIds) + .andIdEqualTo(workspaceId); + if (workspaceMapper.countByExample(example) == 0) { + MSException.throwException("The current workspace does not belong to the current user"); + } } } diff --git a/backend/src/main/java/io/metersphere/user/SessionUser.java b/backend/src/main/java/io/metersphere/user/SessionUser.java index 187a39621f..a9066df24a 100644 --- a/backend/src/main/java/io/metersphere/user/SessionUser.java +++ b/backend/src/main/java/io/metersphere/user/SessionUser.java @@ -1,17 +1,44 @@ package io.metersphere.user; import io.metersphere.dto.UserDTO; +import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; import java.io.Serializable; +import static io.metersphere.commons.constants.RoleConstants.*; + public class SessionUser extends UserDTO implements Serializable { private static final long serialVersionUID = -7149638440406959033L; + private String workspaceId; + private String organizationId; + + public String getWorkspaceId() { + return workspaceId; + } + + public String getOrganizationId() { + return organizationId; + } + public static SessionUser fromUser(UserDTO user) { SessionUser sessionUser = new SessionUser(); BeanUtils.copyProperties(user, sessionUser); + String lastSourceId = sessionUser.getLastSourceId(); + user.getUserRoles().forEach(ur -> { + if (StringUtils.equals(ur.getSourceId(), lastSourceId)) { + if (StringUtils.equals(ur.getRoleId(), ORG_ADMIN)) { + sessionUser.organizationId = lastSourceId; + return; + } + if (StringUtils.equalsAny(ur.getRoleId(), TEST_MANAGER, TEST_USER, TEST_VIEWER)) { + sessionUser.workspaceId = lastSourceId; + } + } + }); + return sessionUser; } diff --git a/backend/src/main/java/io/metersphere/user/SessionUtils.java b/backend/src/main/java/io/metersphere/user/SessionUtils.java index 0de933ebe8..ff569833f0 100644 --- a/backend/src/main/java/io/metersphere/user/SessionUtils.java +++ b/backend/src/main/java/io/metersphere/user/SessionUtils.java @@ -4,15 +4,32 @@ import org.apache.shiro.SecurityUtils; import org.apache.shiro.session.Session; import org.apache.shiro.subject.Subject; +import java.util.Optional; + +import static io.metersphere.commons.constants.SessionConstants.ATTR_USER; + public class SessionUtils { public static SessionUser getUser() { try { Subject subject = SecurityUtils.getSubject(); Session session = subject.getSession(); - return (SessionUser) session.getAttribute("user"); + return (SessionUser) session.getAttribute(ATTR_USER); } catch (Exception e) { return null; } } + + // + public static void putUser(SessionUser sessionUser) { + SecurityUtils.getSubject().getSession().setAttribute(ATTR_USER, sessionUser); + } + + public static String getCurrentWorkspaceId() { + return Optional.ofNullable(getUser()).orElse(new SessionUser()).getWorkspaceId(); + } + + public static String getCurrentOrganizationId() { + return Optional.ofNullable(getUser()).orElse(new SessionUser()).getOrganizationId(); + } } diff --git a/frontend/src/login/Login.vue b/frontend/src/login/Login.vue index a0ddc5ac2b..6c612a882f 100644 --- a/frontend/src/login/Login.vue +++ b/frontend/src/login/Login.vue @@ -16,7 +16,7 @@
- @@ -109,8 +109,10 @@ this.$post("signin", this.form, (response) => { // 登录信息保存 cookie Cookies.set(TokenKey, response.data); - // 保存上次角色 - localStorage.setItem("lastSourceId", JSON.parse(Cookies.get(TokenKey)).lastSourceId); + let rolesArray = response.data.roles; + let roles = rolesArray.map(r => r.id); + // 保存角色 + localStorage.setItem("roles", roles); window.location.href = "/" }); } else { diff --git a/frontend/src/performance/App.vue b/frontend/src/performance/App.vue index de6a245a25..45dadf7905 100644 --- a/frontend/src/performance/App.vue +++ b/frontend/src/performance/App.vue @@ -2,7 +2,6 @@ {{ $t("i18n.home") }} - + 工作空间1 工作空间2 显示全部 - + 项目1 项目2 @@ -21,7 +21,7 @@ 创建项目 - + @@ -33,7 +33,7 @@ 创建测试 - + 报告1 报告2 diff --git a/frontend/src/performance/components/HeaderSwitchUser.vue b/frontend/src/performance/components/HeaderSwitchUser.vue deleted file mode 100644 index 292390eaad..0000000000 --- a/frontend/src/performance/components/HeaderSwitchUser.vue +++ /dev/null @@ -1,114 +0,0 @@ - - - - - diff --git a/frontend/src/performance/components/project/MsProject.vue b/frontend/src/performance/components/project/MsProject.vue index eae9ec5c24..d3272fdcd6 100644 --- a/frontend/src/performance/components/project/MsProject.vue +++ b/frontend/src/performance/components/project/MsProject.vue @@ -1,7 +1,7 @@