diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/AlertGroupController.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/AlertGroupController.java index 8bc4c7eba8..d78561991f 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/AlertGroupController.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/AlertGroupController.java @@ -109,7 +109,7 @@ public class AlertGroupController extends BaseController { @AccessLogAnnotation(ignoreRequestArgs = "loginUser") public Result list(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser) { - Map result = alertGroupService.queryAlertgroup(); + Map result = alertGroupService.queryAlertgroup(loginUser); return returnDataList(result); } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/EnvironmentController.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/EnvironmentController.java index 4a2a6aa08e..ed2ad9f1e6 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/EnvironmentController.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/EnvironmentController.java @@ -212,7 +212,7 @@ public class EnvironmentController extends BaseController { @ApiException(QUERY_ENVIRONMENT_ERROR) @AccessLogAnnotation(ignoreRequestArgs = "loginUser") public Result queryAllEnvironmentList(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser) { - Map result = environmentService.queryAllEnvironmentList(); + Map result = environmentService.queryAllEnvironmentList(loginUser); return returnDataList(result); } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/WorkerGroupController.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/WorkerGroupController.java index e48b492298..71a678f4e9 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/WorkerGroupController.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/WorkerGroupController.java @@ -135,7 +135,7 @@ public class WorkerGroupController extends BaseController { @ApiException(QUERY_WORKER_GROUP_FAIL) @AccessLogAnnotation(ignoreRequestArgs = "loginUser") public Result queryAllWorkerGroups(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser) { - Map result = workerGroupService.queryAllGroup(); + Map result = workerGroupService.queryAllGroup(loginUser); return returnDataList(result); } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/AlertGroupService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/AlertGroupService.java index faa93ee07a..8d7e49af51 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/AlertGroupService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/AlertGroupService.java @@ -30,9 +30,10 @@ public interface AlertGroupService { /** * query alert group list * + * @param loginUser * @return alert group list */ - Map queryAlertgroup(); + Map queryAlertgroup(User loginUser); /** * query alert group by id diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/EnvironmentService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/EnvironmentService.java index 219c02e2ba..617d2b26ac 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/EnvironmentService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/EnvironmentService.java @@ -86,9 +86,10 @@ public interface EnvironmentService { /** * query all environment * + * @param loginUser * @return all environment list */ - Map queryAllEnvironmentList(); + Map queryAllEnvironmentList(User loginUser); /** * verify environment name diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/WorkerGroupService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/WorkerGroupService.java index fabe6689f3..2de4134db5 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/WorkerGroupService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/WorkerGroupService.java @@ -52,9 +52,10 @@ public interface WorkerGroupService { /** * query all worker group * + * @param loginUser * @return all worker group list */ - Map queryAllGroup(); + Map queryAllGroup(User loginUser); /** * delete worker group by id diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AlertGroupServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AlertGroupServiceImpl.java index 05a8eddd8a..918fc7a680 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AlertGroupServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AlertGroupServiceImpl.java @@ -64,16 +64,26 @@ public class AlertGroupServiceImpl extends BaseServiceImpl implements AlertGroup /** * query alert group list * + * @param loginUser * @return alert group list */ @Override - public Map queryAlertgroup() { - + public Map queryAlertgroup(User loginUser) { HashMap result = new HashMap<>(); - List alertGroups = alertGroupMapper.queryAllGroupList(); + List alertGroups; + if (loginUser.getUserType().equals(UserType.ADMIN_USER)) { + alertGroups = alertGroupMapper.queryAllGroupList(); + } else { + Set ids = resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.ALERT_GROUP, loginUser.getId(), logger); + if (ids.isEmpty()) { + result.put(Constants.DATA_LIST, Collections.emptyList()); + putMsg(result, Status.SUCCESS); + return result; + } + alertGroups = alertGroupMapper.selectBatchIds(ids); + } result.put(Constants.DATA_LIST, alertGroups); putMsg(result, Status.SUCCESS); - return result; } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataSourceServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataSourceServiceImpl.java index e5dd5d65e8..0cea4cf5a6 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataSourceServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/DataSourceServiceImpl.java @@ -54,6 +54,7 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.Optional; +import java.util.stream.Collectors; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -305,14 +306,19 @@ public class DataSourceServiceImpl extends BaseServiceImpl implements DataSource Map result = new HashMap<>(); List datasourceList = null; - - if (canOperatorPermissions(loginUser,null,AuthorizationType.DATASOURCE,DATASOURCE_UPDATE)){ - datasourceList = dataSourceMapper.queryDataSourceByType(UserType.ADMIN_USER.equals(loginUser.getUserType()) ? 0 : loginUser.getId(), type); + if (loginUser.getUserType().equals(UserType.ADMIN_USER)) { + datasourceList = dataSourceMapper.queryDataSourceByType(0, type); + } else { + Set ids = resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.DATASOURCE, loginUser.getId(), logger); + if (ids.isEmpty()) { + result.put(Constants.DATA_LIST, Collections.emptyList()); + putMsg(result, Status.SUCCESS); + return result; + } + datasourceList = dataSourceMapper.selectBatchIds(ids).stream().filter(dataSource -> dataSource.getType().getCode() == type).collect(Collectors.toList()); } - result.put(Constants.DATA_LIST, datasourceList); putMsg(result, Status.SUCCESS); - return result; } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/EnvironmentServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/EnvironmentServiceImpl.java index 55196307b5..5ee992d16b 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/EnvironmentServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/EnvironmentServiceImpl.java @@ -211,13 +211,19 @@ public class EnvironmentServiceImpl extends BaseServiceImpl implements Environme /** * query all environment * + * @param loginUser * @return all environment list */ @Override - public Map queryAllEnvironmentList() { + public Map queryAllEnvironmentList(User loginUser) { Map result = new HashMap<>(); - List environmentList = environmentMapper.queryAllEnvironmentList(); - + Set ids = resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.ENVIRONMENT, loginUser.getId(), logger); + if (ids.isEmpty()) { + result.put(Constants.DATA_LIST, Collections.emptyList()); + putMsg(result,Status.SUCCESS); + return result; + } + List environmentList = environmentMapper.selectBatchIds(ids); if (CollectionUtils.isNotEmpty(environmentList)) { Map> relationMap = relationMapper.selectList(null).stream() .collect(Collectors.groupingBy(EnvironmentWorkerGroupRelation::getEnvironmentCode,Collectors.mapping(EnvironmentWorkerGroupRelation::getWorkerGroup,Collectors.toList()))); diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/TaskGroupQueueServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/TaskGroupQueueServiceImpl.java index e276ed0b30..c9c1eba5af 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/TaskGroupQueueServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/TaskGroupQueueServiceImpl.java @@ -17,9 +17,7 @@ package org.apache.dolphinscheduler.api.service.impl; -import org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant; import org.apache.dolphinscheduler.api.enums.Status; -import org.apache.dolphinscheduler.api.service.ProjectService; import org.apache.dolphinscheduler.api.service.TaskGroupQueueService; import org.apache.dolphinscheduler.api.utils.PageInfo; import org.apache.dolphinscheduler.common.Constants; @@ -27,8 +25,8 @@ import org.apache.dolphinscheduler.common.enums.AuthorizationType; import org.apache.dolphinscheduler.dao.entity.Project; import org.apache.dolphinscheduler.dao.entity.TaskGroupQueue; import org.apache.dolphinscheduler.dao.entity.User; +import org.apache.dolphinscheduler.dao.mapper.ProjectMapper; import org.apache.dolphinscheduler.dao.mapper.TaskGroupQueueMapper; -import org.apache.dolphinscheduler.dao.mapper.TaskInstanceMapper; import java.util.HashMap; import java.util.List; @@ -53,10 +51,7 @@ public class TaskGroupQueueServiceImpl extends BaseServiceImpl implements TaskGr TaskGroupQueueMapper taskGroupQueueMapper; @Autowired - private TaskInstanceMapper taskInstanceMapper; - - @Autowired - private ProjectService projectService; + private ProjectMapper projectMapper; private static final Logger logger = LoggerFactory.getLogger(TaskGroupQueueServiceImpl.class); @@ -73,18 +68,18 @@ public class TaskGroupQueueServiceImpl extends BaseServiceImpl implements TaskGr public Map queryTasksByGroupId(User loginUser, String taskName , String processName, Integer status, int groupId, int pageNo, int pageSize) { Map result = new HashMap<>(); - boolean canOperatorPermissions = canOperatorPermissions(loginUser, null, AuthorizationType.TASK_GROUP, ApiFuncIdentificationConstant.TASK_GROUP_QUEUE); - if (!canOperatorPermissions){ - result.put(Constants.STATUS, Status.NO_CURRENT_OPERATING_PERMISSION); + Page page = new Page<>(pageNo, pageSize); + PageInfo pageInfo = new PageInfo<>(pageNo, pageSize); + Set projectIds = resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.PROJECTS, loginUser.getId(), logger); + if (projectIds.isEmpty()) { + result.put(Constants.DATA_LIST, pageInfo); + putMsg(result, Status.SUCCESS); return result; } - Page page = new Page<>(pageNo, pageSize); - Map objectMap = this.projectService.queryAuthorizedProject(loginUser, loginUser.getId()); - List projects = (List)objectMap.get(Constants.DATA_LIST); + List projects = projectMapper.selectBatchIds(projectIds); IPage taskGroupQueue = taskGroupQueueMapper.queryTaskGroupQueueByTaskGroupIdPaging(page, taskName ,processName,status,groupId,projects); - PageInfo pageInfo = new PageInfo<>(pageNo, pageSize); pageInfo.setTotal((int) taskGroupQueue.getTotal()); pageInfo.setTotalList(taskGroupQueue.getRecords()); diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/WorkerGroupServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/WorkerGroupServiceImpl.java index 2f674470ad..bf678d3dcc 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/WorkerGroupServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/WorkerGroupServiceImpl.java @@ -236,12 +236,24 @@ public class WorkerGroupServiceImpl extends BaseServiceImpl implements WorkerGro /** * query all worker group * + * @param loginUser * @return all worker group list */ @Override - public Map queryAllGroup() { + public Map queryAllGroup(User loginUser) { Map result = new HashMap<>(); - List workerGroups = getWorkerGroups(false); + List workerGroups; + if (loginUser.getUserType().equals(UserType.ADMIN_USER)) { + workerGroups = getWorkerGroups(false); + } else { + Set ids = resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.WORKER_GROUP, loginUser.getId(), logger); + if (ids.isEmpty()) { + result.put(Constants.DATA_LIST, Collections.emptyList()); + putMsg(result, Status.SUCCESS); + return result; + } + workerGroups = workerGroupMapper.selectBatchIds(ids); + } List availableWorkerGroupList = workerGroups.stream() .map(WorkerGroup::getName) .collect(Collectors.toList()); diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AlertGroupServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AlertGroupServiceTest.java index 083676c8df..f86c905ecd 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AlertGroupServiceTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AlertGroupServiceTest.java @@ -21,6 +21,7 @@ import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationCon import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; +import org.apache.commons.compress.utils.Sets; import org.apache.dolphinscheduler.api.enums.Status; import org.apache.dolphinscheduler.api.service.impl.AlertGroupServiceImpl; import org.apache.dolphinscheduler.api.service.impl.BaseServiceImpl; @@ -64,6 +65,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page; public class AlertGroupServiceTest { private static final Logger baseServiceLogger = LoggerFactory.getLogger(BaseServiceImpl.class); private static final Logger logger = LoggerFactory.getLogger(AlertGroupServiceTest.class); + private static final Logger alertGroupServiceLogger = LoggerFactory.getLogger(AlertGroupServiceImpl.class); @InjectMocks private AlertGroupServiceImpl alertGroupService; @@ -80,7 +82,7 @@ public class AlertGroupServiceTest { public void testQueryAlertGroup() { Mockito.when(alertGroupMapper.queryAllGroupList()).thenReturn(getList()); - Map result = alertGroupService.queryAlertgroup(); + Map result = alertGroupService.queryAlertgroup(getLoginUser()); logger.info(result.toString()); List alertGroups = (List) result.get(Constants.DATA_LIST); Assert.assertTrue(CollectionUtils.isNotEmpty(alertGroups)); diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataSourceServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataSourceServiceTest.java index 9dcd5333de..3f4deddb49 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataSourceServiceTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/DataSourceServiceTest.java @@ -50,6 +50,7 @@ import org.apache.commons.collections.CollectionUtils; import java.sql.Connection; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -79,6 +80,7 @@ import org.slf4j.LoggerFactory; public class DataSourceServiceTest { private static final Logger baseServiceLogger = LoggerFactory.getLogger(BaseServiceImpl.class); private static final Logger logger = LoggerFactory.getLogger(DataSourceServiceTest.class); + private static final Logger dataSourceServiceLogger = LoggerFactory.getLogger(DataSourceServiceImpl.class); @InjectMocks private DataSourceServiceImpl dataSourceService; @@ -304,8 +306,15 @@ public class DataSourceServiceTest { public void queryDataSourceListTest() { User loginUser = new User(); loginUser.setUserType(UserType.GENERAL_USER); + Set dataSourceIds = new HashSet<>(); + dataSourceIds.add(1); Mockito.when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.DATASOURCE, loginUser.getId(), null, baseServiceLogger)).thenReturn(true); Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.DATASOURCE, null, 0, baseServiceLogger)).thenReturn(true); + Mockito.when(resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.DATASOURCE, loginUser.getId(), dataSourceServiceLogger)).thenReturn(dataSourceIds); + + DataSource dataSource = new DataSource(); + dataSource.setType(DbType.MYSQL); + Mockito.when(dataSourceMapper.selectBatchIds(dataSourceIds)).thenReturn(Collections.singletonList(dataSource)); Map map = dataSourceService.queryDataSourceList(loginUser, DbType.MYSQL.ordinal()); Assert.assertEquals(Status.SUCCESS, map.get(Constants.STATUS)); } diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/EnvironmentServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/EnvironmentServiceTest.java index d5ee44642c..9bfbcccef8 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/EnvironmentServiceTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/EnvironmentServiceTest.java @@ -35,8 +35,10 @@ import org.apache.dolphinscheduler.dao.mapper.TaskDefinitionMapper; import org.apache.commons.collections.CollectionUtils; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; import org.apache.dolphinscheduler.api.permission.ResourcePermissionCheckService; import org.assertj.core.util.Lists; @@ -67,6 +69,7 @@ public class EnvironmentServiceTest { public static final Logger logger = LoggerFactory.getLogger(EnvironmentServiceTest.class); private static final Logger baseServiceLogger = LoggerFactory.getLogger(BaseServiceImpl.class); + private static final Logger environmentServiceLogger = LoggerFactory.getLogger(EnvironmentServiceImpl.class); @InjectMocks private EnvironmentServiceImpl environmentService; @@ -166,8 +169,12 @@ public class EnvironmentServiceTest { @Test public void testQueryAllEnvironmentList() { - Mockito.when(environmentMapper.queryAllEnvironmentList()).thenReturn(Lists.newArrayList(getEnvironment())); - Map result = environmentService.queryAllEnvironmentList(); + Set ids = new HashSet<>(); + ids.add(1); + Mockito.when(resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.ENVIRONMENT, 1, environmentServiceLogger)).thenReturn(ids); + Mockito.when(environmentMapper.selectBatchIds(ids)).thenReturn(Lists.newArrayList(getEnvironment())); + + Map result = environmentService.queryAllEnvironmentList(getAdminUser()); logger.info(result.toString()); Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/WorkerGroupServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/WorkerGroupServiceTest.java index 110bb050e1..decd20bb89 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/WorkerGroupServiceTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/WorkerGroupServiceTest.java @@ -35,6 +35,7 @@ import java.util.List; import java.util.Map; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mockito; @@ -63,9 +64,17 @@ public class WorkerGroupServiceTest { private String groupName = "groupName000001"; + private User loginUSer; + + @Before + public void init(){ + loginUSer = new User(); + loginUSer.setUserType(UserType.ADMIN_USER); + } + @Test public void testQueryAllGroup() { - Map result = workerGroupService.queryAllGroup(); + Map result = workerGroupService.queryAllGroup(loginUSer); List workerGroups = (List) result.get(Constants.DATA_LIST); Assert.assertEquals(workerGroups.size(), 1); } @@ -104,7 +113,7 @@ public class WorkerGroupServiceTest { @Test public void testQueryAllGroupWithDefault() { - Map result = workerGroupService.queryAllGroup(); + Map result = workerGroupService.queryAllGroup(loginUSer); List workerGroups = (List) result.get(Constants.DATA_LIST); Assert.assertEquals(1, workerGroups.size()); Assert.assertEquals("default", workerGroups.toArray()[0]);