diff --git a/backend/src/main/java/io/metersphere/service/WorkspaceService.java b/backend/src/main/java/io/metersphere/service/WorkspaceService.java index eab372637b..8741962410 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,10 +27,9 @@ 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())) { workspace.setId(UUID.randomUUID().toString()); // 设置ID @@ -47,7 +52,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"); + } } }