diff --git a/server/openblocks-domain/src/main/java/com/openblocks/domain/permission/service/DatasourcePermissionHandler.java b/server/openblocks-domain/src/main/java/com/openblocks/domain/permission/service/DatasourcePermissionHandler.java index b0059851..ac1a3693 100644 --- a/server/openblocks-domain/src/main/java/com/openblocks/domain/permission/service/DatasourcePermissionHandler.java +++ b/server/openblocks-domain/src/main/java/com/openblocks/domain/permission/service/DatasourcePermissionHandler.java @@ -1,18 +1,27 @@ package com.openblocks.domain.permission.service; +import static com.openblocks.domain.permission.model.ResourceHolder.USER; +import static com.openblocks.sdk.constants.Authentication.ANONYMOUS_USER_ID; + import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collectors; +import org.apache.commons.collections4.CollectionUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Component; +import com.google.common.collect.Maps; import com.openblocks.domain.datasource.model.Datasource; import com.openblocks.domain.datasource.service.DatasourceService; import com.openblocks.domain.permission.model.ResourceAction; import com.openblocks.domain.permission.model.ResourcePermission; +import com.openblocks.domain.permission.model.ResourceRole; +import com.openblocks.domain.permission.model.ResourceType; import reactor.core.publisher.Mono; @@ -20,6 +29,8 @@ @Component class DatasourcePermissionHandler extends ResourcePermissionHandler { + private static final ResourceRole SYSTEM_STATIC_DATASOURCE_USER_ROLE = ResourceRole.OWNER; + @Autowired private DatasourceService datasourceService; @@ -33,4 +44,39 @@ protected Mono getOrgId(String resourceId) { return datasourceService.getById(resourceId) .map(Datasource::getOrganizationId); } + + @Override + public Mono>> getAllMatchingPermissions(String userId, Collection resourceIds, + ResourceAction resourceAction) { + + List systemStaticDatasourceIds = resourceIds.stream() + .filter(Datasource::isSystemStaticId) + .toList(); + List nonSystemStaticDatasourceIds = resourceIds.stream() + .filter(Datasource::isNotSystemStaticId) + .toList(); + + if (CollectionUtils.isEmpty(systemStaticDatasourceIds)) { + return super.getAllMatchingPermissions(userId, nonSystemStaticDatasourceIds, resourceAction); + } + return super.getAllMatchingPermissions(userId, nonSystemStaticDatasourceIds, resourceAction) + .map(allMatchingPermissions -> { + Map> result = Maps.newHashMap(); + Map> systemStaticDatasourcePermissions = systemStaticDatasourceIds.stream() + .collect(Collectors.toMap(Function.identity(), id -> getSystemStaticDatasourcePermission(userId, id))); + result.putAll(systemStaticDatasourcePermissions); + result.putAll(allMatchingPermissions); + return result; + }); + } + + private List getSystemStaticDatasourcePermission(String userId, String datasourceId) { + return Collections.singletonList(ResourcePermission.builder() + .resourceId(datasourceId) + .resourceType(ResourceType.DATASOURCE) + .resourceHolder(USER) + .resourceHolderId(userId) + .resourceRole(SYSTEM_STATIC_DATASOURCE_USER_ROLE) + .build()); + } } diff --git a/server/openblocks-server/src/main/java/com/openblocks/api/authentication/request/AuthRequestFactoryFacade.java b/server/openblocks-server/src/main/java/com/openblocks/api/authentication/request/AuthRequestFactoryFacade.java index 545aed25..c4bf8a2c 100644 --- a/server/openblocks-server/src/main/java/com/openblocks/api/authentication/request/AuthRequestFactoryFacade.java +++ b/server/openblocks-server/src/main/java/com/openblocks/api/authentication/request/AuthRequestFactoryFacade.java @@ -22,10 +22,11 @@ @Slf4j @Primary @Component +@SuppressWarnings({"rawtypes", "unchecked"}) public class AuthRequestFactoryFacade implements AuthRequestFactory { @Autowired - private List> authRequestFactories; + private List authRequestFactories; private final Map> authRequestFactoryMap = new HashMap<>();