diff --git a/application/src/main/java/org/thingsboard/server/controller/BaseController.java b/application/src/main/java/org/thingsboard/server/controller/BaseController.java index d1c282508b..3294d5fc59 100644 --- a/application/src/main/java/org/thingsboard/server/controller/BaseController.java +++ b/application/src/main/java/org/thingsboard/server/controller/BaseController.java @@ -57,6 +57,7 @@ import org.thingsboard.server.dao.plugin.PluginService; import org.thingsboard.server.dao.relation.RelationService; import org.thingsboard.server.dao.rule.RuleChainService; import org.thingsboard.server.dao.rule.RuleService; +import org.thingsboard.server.dao.tenant.TenantService; import org.thingsboard.server.dao.user.UserService; import org.thingsboard.server.dao.widget.WidgetTypeService; import org.thingsboard.server.dao.widget.WidgetsBundleService; @@ -85,6 +86,9 @@ public abstract class BaseController { @Autowired private ThingsboardErrorResponseHandler errorResponseHandler; + @Autowired + protected TenantService tenantService; + @Autowired protected CustomerService customerService; @@ -602,5 +606,8 @@ public abstract class BaseController { auditLogService.logEntityAction(user.getTenantId(), customerId, user.getId(), user.getName(), entityId, entity, actionType, e, additionalInfo); } + protected static Exception toException(Throwable error) { + return Exception.class.isInstance(error) ? (Exception) error : new Exception(error); + } } diff --git a/application/src/main/java/org/thingsboard/server/controller/TelemetryController.java b/application/src/main/java/org/thingsboard/server/controller/TelemetryController.java index dcc4a5cf54..1944fa2e68 100644 --- a/application/src/main/java/org/thingsboard/server/controller/TelemetryController.java +++ b/application/src/main/java/org/thingsboard/server/controller/TelemetryController.java @@ -9,33 +9,49 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.context.request.async.DeferredResult; import org.thingsboard.server.actors.plugin.ValidationResult; +import org.thingsboard.server.common.data.Customer; import org.thingsboard.server.common.data.DataConstants; import org.thingsboard.server.common.data.Device; +import org.thingsboard.server.common.data.Tenant; +import org.thingsboard.server.common.data.asset.Asset; +import org.thingsboard.server.common.data.audit.ActionType; +import org.thingsboard.server.common.data.id.AssetId; +import org.thingsboard.server.common.data.id.CustomerId; import org.thingsboard.server.common.data.id.DeviceId; import org.thingsboard.server.common.data.id.EntityId; import org.thingsboard.server.common.data.id.EntityIdFactory; +import org.thingsboard.server.common.data.id.RuleChainId; +import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.common.data.id.UUIDBased; import org.thingsboard.server.common.data.kv.AttributeKvEntry; import org.thingsboard.server.common.data.kv.KvEntry; +import org.thingsboard.server.common.data.kv.TsKvEntry; +import org.thingsboard.server.common.data.rule.RuleChain; import org.thingsboard.server.dao.attributes.AttributesService; import org.thingsboard.server.dao.timeseries.TimeseriesService; import org.thingsboard.server.exception.ThingsboardException; import org.thingsboard.server.extensions.api.exception.ToErrorResponseEntity; import org.thingsboard.server.extensions.api.plugins.PluginConstants; +import org.thingsboard.server.extensions.core.plugin.telemetry.AttributeData; import org.thingsboard.server.service.security.model.SecurityUser; import javax.annotation.Nullable; import javax.annotation.PreDestroy; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.function.BiConsumer; import java.util.stream.Collectors; /** @@ -74,61 +90,169 @@ public class TelemetryController extends BaseController { @ResponseStatus(value = HttpStatus.OK) public DeferredResult getAttributeKeys( @PathVariable("entityType") String entityType, @PathVariable("entityId") String entityIdStr) throws ThingsboardException { - DeferredResult response = new DeferredResult(); + return validateEntityAndCallback(entityType, entityIdStr, + this::getAttributeKeysCallback, + (result, t) -> handleError(t, result, HttpStatus.INTERNAL_SERVER_ERROR)); + } + + @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')") + @RequestMapping(value = "/{entityType}/{entityId}/keys/ATTRIBUTES/{scope}", method = RequestMethod.GET) + @ResponseStatus(value = HttpStatus.OK) + public DeferredResult getAttributeKeysByScope( + @PathVariable("entityType") String entityType, @PathVariable("entityId") String entityIdStr + , @PathVariable("scope") String scope) throws ThingsboardException { + return validateEntityAndCallback(entityType, entityIdStr, + (result, entityId) -> getAttributeKeysCallback(result, entityId, scope), + (result, t) -> handleError(t, result, HttpStatus.INTERNAL_SERVER_ERROR)); + } + + @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')") + @RequestMapping(value = "/{entityType}/{entityId}/values/ATTRIBUTES", method = RequestMethod.GET) + @ResponseStatus(value = HttpStatus.OK) + public DeferredResult getAttributes( + @PathVariable("entityType") String entityType, @PathVariable("entityId") String entityIdStr, + @RequestParam(name = "keys", required = false) String keysStr) throws ThingsboardException { + SecurityUser user = getCurrentUser(); + return validateEntityAndCallback(entityType, entityIdStr, + (result, entityId) -> getAttributeValuesCallback(result, user, entityId, null, keysStr), + (result, t) -> handleError(t, result, HttpStatus.INTERNAL_SERVER_ERROR)); + } + + @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')") + @RequestMapping(value = "/{entityType}/{entityId}/values/ATTRIBUTES/{scope}", method = RequestMethod.GET) + @ResponseStatus(value = HttpStatus.OK) + public DeferredResult getAttributesByScope( + @PathVariable("entityType") String entityType, @PathVariable("entityId") String entityIdStr, + @PathVariable("scope") String scope, + @RequestParam(name = "keys", required = false) String keysStr) throws ThingsboardException { + SecurityUser user = getCurrentUser(); + return validateEntityAndCallback(entityType, entityIdStr, + (result, entityId) -> getAttributeValuesCallback(result, user, entityId, scope, keysStr), + (result, t) -> handleError(t, result, HttpStatus.INTERNAL_SERVER_ERROR)); + } + + @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')") + @RequestMapping(value = "/{entityType}/{entityId}/keys/TIMESERIES", method = RequestMethod.GET) + @ResponseStatus(value = HttpStatus.OK) + public DeferredResult getTimeseriesKeys( + @PathVariable("entityType") String entityType, @PathVariable("entityId") String entityIdStr) throws ThingsboardException { + return validateEntityAndCallback(entityType, entityIdStr, + (result, entityId) -> { + Futures.addCallback(tsService.findAllLatest(entityId), getTsKeysToResponseCallback(result)); + }, + (result, t) -> handleError(t, result, HttpStatus.INTERNAL_SERVER_ERROR)); + } + + @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')") + @RequestMapping(value = "/{entityType}/{entityId}/values/TIMESERIES", method = RequestMethod.GET) + @ResponseStatus(value = HttpStatus.OK) + public DeferredResult getLatestTimeseries( + @PathVariable("entityType") String entityType, @PathVariable("entityId") String entityIdStr, + @PathVariable("scope") String scope, + @RequestParam(name = "keys", required = false) String keysStr) throws ThingsboardException { + SecurityUser user = getCurrentUser(); + + return validateEntityAndCallback(entityType, entityIdStr, + (result, entityId) -> getAttributeValuesCallback(result, user, entityId, scope, keysStr), + (result, t) -> handleError(t, result, HttpStatus.INTERNAL_SERVER_ERROR)); + } + + + @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')") + @RequestMapping(value = "/{entityType}/{entityId}/values/TIMESERIES", method = RequestMethod.GET) + @ResponseStatus(value = HttpStatus.OK) + public DeferredResult getLatestTimeseries( + @PathVariable("entityType") String entityType, @PathVariable("entityId") String entityIdStr, + @PathVariable("scope") String scope, + @RequestParam(name = "keys", required = false) String keysStr) throws ThingsboardException { + SecurityUser user = getCurrentUser(); + + return validateEntityAndCallback(entityType, entityIdStr, + (result, entityId) -> getAttributeValuesCallback(result, user, entityId, scope, keysStr), + (result, t) -> handleError(t, result, HttpStatus.INTERNAL_SERVER_ERROR)); + } + + private DeferredResult validateEntityAndCallback(String entityType, String entityIdStr, + BiConsumer, EntityId> onSuccess, BiConsumer, Throwable> onFailure) throws ThingsboardException { + final DeferredResult response = new DeferredResult<>(); EntityId entityId = EntityIdFactory.getByTypeAndId(entityType, entityIdStr); validate(getCurrentUser(), entityId, new ValidationCallback(response, new FutureCallback>() { @Override public void onSuccess(@Nullable DeferredResult result) { - List>> futures = new ArrayList<>(); - for (String scope : DataConstants.allScopes()) { - futures.add(attributesService.findAll(entityId, scope)); - } - - ListenableFuture> future = Futures.transform(Futures.successfulAsList(futures), - (Function>, ? extends List>) input -> { - List tmp = new ArrayList<>(); - if (input != null) { - input.forEach(tmp::addAll); - } - return tmp; - }, executor); - - Futures.addCallback(future, getAttributeKeysPluginCallback(result)); + onSuccess.accept(response, entityId); } @Override public void onFailure(Throwable t) { - handleError(t, response, HttpStatus.INTERNAL_SERVER_ERROR); + onFailure.accept(response, t); } })); return response; } - @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')") - @RequestMapping(value = "/{entityType}/{entityId}/keys/ATTRIBUTES/{scope}", method = RequestMethod.GET) - @ResponseStatus(value = HttpStatus.OK) - public DeferredResult getAttributeKeysByScope() { - return null; + private void getAttributeValuesCallback(@Nullable DeferredResult result, SecurityUser user, EntityId entityId, String scope, String keys) { + List keyList = null; + if (!StringUtils.isEmpty(keys)) { + keyList = Arrays.asList(keys.split(",")); + } + FutureCallback> callback = getAttributeValuesToResponseCallback(result, user, scope, entityId, keyList); + if (!StringUtils.isEmpty(scope)) { + if (keyList != null && !keyList.isEmpty()) { + Futures.addCallback(attributesService.find(entityId, scope, keyList), callback); + } else { + Futures.addCallback(attributesService.findAll(entityId, scope), callback); + } + } else { + List>> futures = new ArrayList<>(); + for (String tmpScope : DataConstants.allScopes()) { + if (keyList != null && !keyList.isEmpty()) { + futures.add(attributesService.find(entityId, tmpScope, keyList)); + } else { + futures.add(attributesService.findAll(entityId, tmpScope)); + } + } + + ListenableFuture> future = mergeAllAttributesFutures(futures); + + Futures.addCallback(future, callback); + } } - @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')") - @RequestMapping(value = "/{entityType}/{entityId}/values/ATTRIBUTES", method = RequestMethod.GET) - @ResponseStatus(value = HttpStatus.OK) - public DeferredResult getAttributeValues() { - return null; + private void getAttributeKeysCallback(@Nullable DeferredResult result, EntityId entityId, String scope) { + Futures.addCallback(attributesService.findAll(entityId, scope), getAttributeKeysToResponseCallback(result)); } - @PreAuthorize("hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')") - @RequestMapping(value = "/{entityType}/{entityId}/values/ATTRIBUTES", method = RequestMethod.GET) - @ResponseStatus(value = HttpStatus.OK) - public DeferredResult getAttributeValuesByScope() { - return null; + private void getAttributeKeysCallback(@Nullable DeferredResult result, EntityId entityId) { + List>> futures = new ArrayList<>(); + for (String scope : DataConstants.allScopes()) { + futures.add(attributesService.findAll(entityId, scope)); + } + + ListenableFuture> future = mergeAllAttributesFutures(futures); + + Futures.addCallback(future, getAttributeKeysToResponseCallback(result)); } - private FutureCallback> getAttributeKeysPluginCallback(final DeferredResult response) { + private FutureCallback> getTsKeysToResponseCallback(final DeferredResult response) { + return new FutureCallback>() { + @Override + public void onSuccess(List values) { + List keys = values.stream().map(KvEntry::getKey).collect(Collectors.toList()); + response.setResult(new ResponseEntity<>(keys, HttpStatus.OK)); + } + + @Override + public void onFailure(Throwable e) { + log.error("Failed to fetch attributes", e); + handleError(e, response, HttpStatus.INTERNAL_SERVER_ERROR); + } + }; + } + + private FutureCallback> getAttributeKeysToResponseCallback(final DeferredResult response) { return new FutureCallback>() { @Override @@ -145,6 +269,40 @@ public class TelemetryController extends BaseController { }; } + private FutureCallback> getAttributeValuesToResponseCallback(final DeferredResult response, final SecurityUser user, final String scope, + final EntityId entityId, final List keyList) { + return new FutureCallback>() { + @Override + public void onSuccess(List attributes) { + List values = attributes.stream().map(attribute -> new AttributeData(attribute.getLastUpdateTs(), + attribute.getKey(), attribute.getValue())).collect(Collectors.toList()); + logAttributesRead(user, entityId, scope, keyList, null); + response.setResult(new ResponseEntity<>(values, HttpStatus.OK)); + } + + @Override + public void onFailure(Throwable e) { + log.error("Failed to fetch attributes", e); + logAttributesRead(user, entityId, scope, keyList, e); + handleError(e, response, HttpStatus.INTERNAL_SERVER_ERROR); + } + }; + } + + private void logAttributesRead(SecurityUser user, EntityId entityId, String scope, List keys, Throwable e) { + auditLogService.logEntityAction( + user.getTenantId(), + user.getCustomerId(), + user.getId(), + user.getName(), + (UUIDBased & EntityId) entityId, + null, + ActionType.ATTRIBUTES_READ, + toException(e), + scope, + keys); + } + private void handleError(Throwable e, final DeferredResult response, HttpStatus defaultErrorStatus) { ResponseEntity responseEntity; if (e != null && e instanceof ToErrorResponseEntity) { @@ -162,24 +320,18 @@ public class TelemetryController extends BaseController { case DEVICE: validateDevice(currentUser, entityId, callback); return; -// case ASSET: -// validateAsset(ctx, entityId, callback); -// return; -// case RULE: -// validateRule(ctx, entityId, callback); -// return; -// case RULE_CHAIN: -// validateRuleChain(ctx, entityId, callback); -// return; -// case PLUGIN: -// validatePlugin(ctx, entityId, callback); -// return; -// case CUSTOMER: -// validateCustomer(ctx, entityId, callback); -// return; -// case TENANT: -// validateTenant(ctx, entityId, callback); -// return; + case ASSET: + validateAsset(currentUser, entityId, callback); + return; + case RULE_CHAIN: + validateRuleChain(currentUser, entityId, callback); + return; + case CUSTOMER: + validateCustomer(currentUser, entityId, callback); + return; + case TENANT: + validateTenant(currentUser, entityId, callback); + return; default: //TODO: add support of other entities throw new IllegalStateException("Not Implemented!"); @@ -207,6 +359,89 @@ public class TelemetryController extends BaseController { } } + private void validateAsset(final SecurityUser currentUser, EntityId entityId, ValidationCallback callback) { + if (currentUser.isSystemAdmin()) { + callback.onSuccess(ValidationResult.accessDenied(SYSTEM_ADMINISTRATOR_IS_NOT_ALLOWED_TO_PERFORM_THIS_OPERATION)); + } else { + ListenableFuture assetFuture = assetService.findAssetByIdAsync(new AssetId(entityId.getId())); + Futures.addCallback(assetFuture, getCallback(callback, asset -> { + if (asset == null) { + return ValidationResult.entityNotFound("Asset with requested id wasn't found!"); + } else { + if (!asset.getTenantId().equals(currentUser.getTenantId())) { + return ValidationResult.accessDenied("Asset doesn't belong to the current Tenant!"); + } else if (currentUser.isCustomerUser() && !asset.getCustomerId().equals(currentUser.getCustomerId())) { + return ValidationResult.accessDenied("Asset doesn't belong to the current Customer!"); + } else { + return ValidationResult.ok(); + } + } + })); + } + } + + + private void validateRuleChain(final SecurityUser currentUser, EntityId entityId, ValidationCallback callback) { + if (currentUser.isCustomerUser()) { + callback.onSuccess(ValidationResult.accessDenied(CUSTOMER_USER_IS_NOT_ALLOWED_TO_PERFORM_THIS_OPERATION)); + } else { + ListenableFuture ruleChainFuture = ruleChainService.findRuleChainByIdAsync(new RuleChainId(entityId.getId())); + Futures.addCallback(ruleChainFuture, getCallback(callback, ruleChain -> { + if (ruleChain == null) { + return ValidationResult.entityNotFound("Rule chain with requested id wasn't found!"); + } else { + if (currentUser.isTenantAdmin() && !ruleChain.getTenantId().equals(currentUser.getTenantId())) { + return ValidationResult.accessDenied("Rule chain doesn't belong to the current Tenant!"); + } else if (currentUser.isSystemAdmin() && !ruleChain.getTenantId().isNullUid()) { + return ValidationResult.accessDenied("Rule chain is not in system scope!"); + } else { + return ValidationResult.ok(); + } + } + })); + } + } + + private void validateCustomer(final SecurityUser currentUser, EntityId entityId, ValidationCallback callback) { + if (currentUser.isSystemAdmin()) { + callback.onSuccess(ValidationResult.accessDenied(SYSTEM_ADMINISTRATOR_IS_NOT_ALLOWED_TO_PERFORM_THIS_OPERATION)); + } else { + ListenableFuture customerFuture = customerService.findCustomerByIdAsync(new CustomerId(entityId.getId())); + Futures.addCallback(customerFuture, getCallback(callback, customer -> { + if (customer == null) { + return ValidationResult.entityNotFound("Customer with requested id wasn't found!"); + } else { + if (!customer.getTenantId().equals(currentUser.getTenantId())) { + return ValidationResult.accessDenied("Customer doesn't belong to the current Tenant!"); + } else if (currentUser.isCustomerUser() && !customer.getId().equals(currentUser.getCustomerId())) { + return ValidationResult.accessDenied("Customer doesn't relate to the currently authorized customer user!"); + } else { + return ValidationResult.ok(); + } + } + })); + } + } + + private void validateTenant(final SecurityUser currentUser, EntityId entityId, ValidationCallback callback) { + if (currentUser.isCustomerUser()) { + callback.onSuccess(ValidationResult.accessDenied(CUSTOMER_USER_IS_NOT_ALLOWED_TO_PERFORM_THIS_OPERATION)); + } else if (currentUser.isSystemAdmin()) { + callback.onSuccess(ValidationResult.ok()); + } else { + ListenableFuture tenantFuture = tenantService.findTenantByIdAsync(new TenantId(entityId.getId())); + Futures.addCallback(tenantFuture, getCallback(callback, tenant -> { + if (tenant == null) { + return ValidationResult.entityNotFound("Tenant with requested id wasn't found!"); + } else if (!tenant.getId().equals(currentUser.getTenantId())) { + return ValidationResult.accessDenied("Tenant doesn't relate to the currently authorized user!"); + } else { + return ValidationResult.ok(); + } + })); + } + } + private FutureCallback getCallback(ValidationCallback callback, Function transformer) { return new FutureCallback() { @Override @@ -220,131 +455,16 @@ public class TelemetryController extends BaseController { } }; } -// -// private void validateAsset(final PluginApiCallSecurityContext ctx, EntityId entityId, ValidationCallback callback) { -// if (ctx.isSystemAdmin()) { -// callback.onSuccess(this, ValidationResult.accessDenied(SYSTEM_ADMINISTRATOR_IS_NOT_ALLOWED_TO_PERFORM_THIS_OPERATION)); -// } else { -// ListenableFuture assetFuture = pluginCtx.assetService.findAssetByIdAsync(new AssetId(entityId.getId())); -// Futures.addCallback(assetFuture, getCallback(callback, asset -> { -// if (asset == null) { -// return ValidationResult.entityNotFound("Asset with requested id wasn't found!"); -// } else { -// if (!asset.getTenantId().equals(ctx.getTenantId())) { -// return ValidationResult.accessDenied("Asset doesn't belong to the current Tenant!"); -// } else if (ctx.isCustomerUser() && !asset.getCustomerId().equals(ctx.getCustomerId())) { -// return ValidationResult.accessDenied("Asset doesn't belong to the current Customer!"); -// } else { -// return ValidationResult.ok(); -// } -// } -// })); -// } -// } -// -// private void validateRule(final PluginApiCallSecurityContext ctx, EntityId entityId, ValidationCallback callback) { -// if (ctx.isCustomerUser()) { -// callback.onSuccess(this, ValidationResult.accessDenied(CUSTOMER_USER_IS_NOT_ALLOWED_TO_PERFORM_THIS_OPERATION)); -// } else { -// ListenableFuture ruleFuture = pluginCtx.ruleService.findRuleByIdAsync(new RuleId(entityId.getId())); -// Futures.addCallback(ruleFuture, getCallback(callback, rule -> { -// if (rule == null) { -// return ValidationResult.entityNotFound("Rule with requested id wasn't found!"); -// } else { -// if (ctx.isTenantAdmin() && !rule.getTenantId().equals(ctx.getTenantId())) { -// return ValidationResult.accessDenied("Rule doesn't belong to the current Tenant!"); -// } else if (ctx.isSystemAdmin() && !rule.getTenantId().isNullUid()) { -// return ValidationResult.accessDenied("Rule is not in system scope!"); -// } else { -// return ValidationResult.ok(); -// } -// } -// })); -// } -// } -// -// private void validateRuleChain(final PluginApiCallSecurityContext ctx, EntityId entityId, ValidationCallback callback) { -// if (ctx.isCustomerUser()) { -// callback.onSuccess(this, ValidationResult.accessDenied(CUSTOMER_USER_IS_NOT_ALLOWED_TO_PERFORM_THIS_OPERATION)); -// } else { -// ListenableFuture ruleChainFuture = pluginCtx.ruleChainService.findRuleChainByIdAsync(new RuleChainId(entityId.getId())); -// Futures.addCallback(ruleChainFuture, getCallback(callback, ruleChain -> { -// if (ruleChain == null) { -// return ValidationResult.entityNotFound("Rule chain with requested id wasn't found!"); -// } else { -// if (ctx.isTenantAdmin() && !ruleChain.getTenantId().equals(ctx.getTenantId())) { -// return ValidationResult.accessDenied("Rule chain doesn't belong to the current Tenant!"); -// } else if (ctx.isSystemAdmin() && !ruleChain.getTenantId().isNullUid()) { -// return ValidationResult.accessDenied("Rule chain is not in system scope!"); -// } else { -// return ValidationResult.ok(); -// } -// } -// })); -// } -// } -// -// -// private void validatePlugin(final PluginApiCallSecurityContext ctx, EntityId entityId, ValidationCallback callback) { -// if (ctx.isCustomerUser()) { -// callback.onSuccess(this, ValidationResult.accessDenied(CUSTOMER_USER_IS_NOT_ALLOWED_TO_PERFORM_THIS_OPERATION)); -// } else { -// ListenableFuture pluginFuture = pluginCtx.pluginService.findPluginByIdAsync(new PluginId(entityId.getId())); -// Futures.addCallback(pluginFuture, getCallback(callback, plugin -> { -// if (plugin == null) { -// return ValidationResult.entityNotFound("Plugin with requested id wasn't found!"); -// } else { -// if (ctx.isTenantAdmin() && !plugin.getTenantId().equals(ctx.getTenantId())) { -// return ValidationResult.accessDenied("Plugin doesn't belong to the current Tenant!"); -// } else if (ctx.isSystemAdmin() && !plugin.getTenantId().isNullUid()) { -// return ValidationResult.accessDenied("Plugin is not in system scope!"); -// } else { -// return ValidationResult.ok(); -// } -// } -// })); -// } -// } -// -// private void validateCustomer(final PluginApiCallSecurityContext ctx, EntityId entityId, ValidationCallback callback) { -// if (ctx.isSystemAdmin()) { -// callback.onSuccess(this, ValidationResult.accessDenied(SYSTEM_ADMINISTRATOR_IS_NOT_ALLOWED_TO_PERFORM_THIS_OPERATION)); -// } else { -// ListenableFuture customerFuture = pluginCtx.customerService.findCustomerByIdAsync(new CustomerId(entityId.getId())); -// Futures.addCallback(customerFuture, getCallback(callback, customer -> { -// if (customer == null) { -// return ValidationResult.entityNotFound("Customer with requested id wasn't found!"); -// } else { -// if (!customer.getTenantId().equals(ctx.getTenantId())) { -// return ValidationResult.accessDenied("Customer doesn't belong to the current Tenant!"); -// } else if (ctx.isCustomerUser() && !customer.getId().equals(ctx.getCustomerId())) { -// return ValidationResult.accessDenied("Customer doesn't relate to the currently authorized customer user!"); -// } else { -// return ValidationResult.ok(); -// } -// } -// })); -// } -// } -// -// private void validateTenant(final PluginApiCallSecurityContext ctx, EntityId entityId, ValidationCallback callback) { -// if (ctx.isCustomerUser()) { -// callback.onSuccess(this, ValidationResult.accessDenied(CUSTOMER_USER_IS_NOT_ALLOWED_TO_PERFORM_THIS_OPERATION)); -// } else if (ctx.isSystemAdmin()) { -// callback.onSuccess(this, ValidationResult.ok()); -// } else { -// ListenableFuture tenantFuture = pluginCtx.tenantService.findTenantByIdAsync(new TenantId(entityId.getId())); -// Futures.addCallback(tenantFuture, getCallback(callback, tenant -> { -// if (tenant == null) { -// return ValidationResult.entityNotFound("Tenant with requested id wasn't found!"); -// } else if (!tenant.getId().equals(ctx.getTenantId())) { -// return ValidationResult.accessDenied("Tenant doesn't relate to the currently authorized user!"); -// } else { -// return ValidationResult.ok(); -// } -// })); -// } -// } + private ListenableFuture> mergeAllAttributesFutures(List>> futures) { + return Futures.transform(Futures.successfulAsList(futures), + (Function>, ? extends List>) input -> { + List tmp = new ArrayList<>(); + if (input != null) { + input.forEach(tmp::addAll); + } + return tmp; + }, executor); + } }