From 1ffb5d6e8d6512ee0be958069e8bbd1c105b2989 Mon Sep 17 00:00:00 2001 From: Sunny Lei <56599784+SunnyZ-L@users.noreply.github.com> Date: Thu, 24 Feb 2022 18:17:25 +0800 Subject: [PATCH] [DS-7896][fix] Fixed when a non-admin user creates a udf function, resources other than .jar will be displayed in the UDF resource (#8458) This closes #7892 --- .../service/impl/ResourcesServiceImpl.java | 5 +++- .../api/service/ResourcesServiceTest.java | 27 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ResourcesServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ResourcesServiceImpl.java index 9892dd527b..22621a50b2 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ResourcesServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ResourcesServiceImpl.java @@ -1346,9 +1346,12 @@ public class ResourcesServiceImpl extends BaseServiceImpl implements ResourcesSe // query resource relation relationResources = queryResourceList(userId, 0); } + // filter by resource type + List relationTypeResources = + relationResources.stream().filter(rs -> rs.getType() == type).collect(Collectors.toList()); List ownResourceList = resourcesMapper.queryResourceListAuthored(userId, type.ordinal()); - ownResourceList.addAll(relationResources); + ownResourceList.addAll(relationTypeResources); return ownResourceList; } diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ResourcesServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ResourcesServiceTest.java index f438f63e0f..6f78772ac0 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ResourcesServiceTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ResourcesServiceTest.java @@ -42,6 +42,7 @@ import org.apache.commons.collections.CollectionUtils; import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -293,6 +294,19 @@ public class ResourcesServiceTest { Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); List resourceList = (List) result.get(Constants.DATA_LIST); Assert.assertTrue(CollectionUtils.isNotEmpty(resourceList)); + + // test udf + loginUser.setUserType(UserType.GENERAL_USER); + Mockito.when(resourceUserMapper.queryResourcesIdListByUserIdAndPerm(0, 0)) + .thenReturn(Arrays.asList(Integer.valueOf(10), Integer.valueOf(11))); + Mockito.when(resourcesMapper.queryResourceListById(Arrays.asList(Integer.valueOf(10), Integer.valueOf(11)))) + .thenReturn(Arrays.asList(getResource(10, ResourceType.FILE), getResource(11, ResourceType.UDF))); + Mockito.when(resourcesMapper.queryResourceListAuthored(0, 1)).thenReturn(getResourceList()); + result = resourcesService.queryResourceList(loginUser, ResourceType.UDF); + logger.info(result.toString()); + Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); + resourceList = (List) result.get(Constants.DATA_LIST); + Assert.assertTrue(resourceList.size() == 4); } @Test @@ -755,6 +769,19 @@ public class ResourcesServiceTest { return resource; } + private Resource getResource(int resourceId,ResourceType type) { + + Resource resource = new Resource(); + resource.setId(resourceId); + resource.setPid(-1); + resource.setUserId(1); + resource.setDescription("ResourcesServiceTest.jar"); + resource.setAlias("ResourcesServiceTest.jar"); + resource.setFullName("/ResourcesServiceTest.jar"); + resource.setType(type); + return resource; + } + private Resource getUdfResource() { Resource resource = new Resource();