Exemple #1
0
        /// <summary>
        /// Accepts a single xls file that contains operation configuration.
        /// </summary>
        /// <param name="fileMetadata">Metadata associated with the file upload request.</param>
        /// <param name="authenticationHeader">Authentication header for the request.</param>
        /// <param name="requestTenantId">The selected Tenant Id from the request import the Operation Config to</param>
        /// <returns>A task that returns the result of the upload option.</returns>
        public async Task <CommandResultNoDto> Upload(FileUploadMetadataDto fileMetadata, string authenticationHeader, Guid requestTenantId)
        {
            var errors      = new List <FFErrorCode>();
            var odataHelper = new Core.Api.OData.ODataHelper();

            var userId = Thread.CurrentPrincipal == null ? null : Thread.CurrentPrincipal.GetUserIdFromPrincipal();

            if (userId == null)
            {
                errors.Add(GeneralErrorCodes.TokenInvalid("UserId"));
            }

            if (errors.Count > 0)
            {
                return(NoDtoHelpers.CreateCommandResult(errors));
            }

            // ReSharper disable once AssignNullToNotNullAttribute
            var userIdGuid = Guid.Parse(userId);

            // Check that the Tenant Id in the request body is in the user's claim tenants
            var tenants = odataHelper.GetTenantIds(Thread.CurrentPrincipal) as List <Guid>;

            // Check user has no tenants in their claim or if the tenantid in the request body is not in the claim
            if (tenants == null || tenants.All(x => x != requestTenantId))
            {
                errors.Add(ValidationErrorCode.ForeignKeyValueDoesNotExist("TenantId"));
            }

            if (errors.Count > 0)
            {
                return(NoDtoHelpers.CreateCommandResult(errors));
            }


            // Store file in blob storage.
            var result = await _blobManager.StoreAsync(_blobStorageConnectionString, _blobStorageContainerName, fileMetadata.SavedFileName);

            // Add file metadata to documentDB to later be retrieved by request
            // An Id property is created by documentDB and in populated in the result object
            await _documentDb.CreateItemAsync(
                new UploadTransaction
            {
                OriginalFileName      = fileMetadata.OriginalFileName,
                TenantIds             = tenants,
                UploadTransactionType = fileMetadata.TransactionType,
                UserId       = userIdGuid,
                UtcTimestamp = DateTime.UtcNow
            });

            var queueMessage = new BlobQueueMessage
            {
                BlobName            = result.BlobName,
                BlobSize            = result.BlobSize,
                BlobUrl             = result.BlobUrl,
                BlobTransactionType = fileMetadata.TransactionType,
                UserId = userIdGuid,
                AuthenticationHeader = authenticationHeader,
                // TenantId should be checked by the blob processor that it matches the tenant in the Operation Config to be processed
                TenantId = requestTenantId
            };

            var msg = JsonConvert.SerializeObject(queueMessage);
            // Add message to queue.
            await _queueManager.AddAsync(_blobStorageConnectionString, _queueStorageContainerName, msg);

            return(NoDtoHelpers.CreateCommandResult(errors));
        }
Exemple #2
0
        /// <summary>
        /// Creates an operation configuration file and saves it to blob storage. When
        /// the file is ready to be downloaded, a signalr notification is sent to the user who made the
        /// requst.
        /// </summary>
        /// <param name="tenantId">Identifies the tenant that the operation belongs to.</param>
        /// <param name="operationId">Identifies the operation to create the configuration for or
        /// null to create a configuration file template with no operation related data.</param>
        /// <param name="authenticationHeader">Authentication header for the request.</param>
        /// <returns>A task that returns the result of the request.</returns>
        public async Task <CommandResultNoDto> Get(Guid tenantId, Guid?operationId, string authenticationHeader)
        {
            var errors = new List <FFErrorCode>();

            var userId = Thread.CurrentPrincipal == null ? null : Thread.CurrentPrincipal.GetUserIdFromPrincipal();

            if (userId == null)
            {
                errors.Add(GeneralErrorCodes.TokenInvalid("UserId"));
            }

            if (errors.Count > 0)
            {
                return(NoDtoHelpers.CreateCommandResult(errors));
            }

            // ReSharper disable once AssignNullToNotNullAttribute
            var userIdGuid = Guid.Parse(userId);

            // Check that the user has access to the requested tenant.
            var odataHelper = new Core.Api.OData.ODataHelper();
            var tenants     = odataHelper.GetTenantIds(Thread.CurrentPrincipal) as List <Guid>;

            if (tenants == null || tenants.All(x => x != tenantId))
            {
                errors.Add(ValidationErrorCode.ForeignKeyValueDoesNotExist("TenantId"));
            }

            if (operationId != null)
            {
                var operation = await _context.Locations.FirstOrDefaultAsync(x => x.Id == operationId).ConfigureAwait(false);

                if (operation == null)
                {
                    errors.Add(ValidationErrorCode.ForeignKeyValueDoesNotExist("OperationId"));
                }
                else if (operation.ProductOfferingTenantLocations.All(x => x.TenantId != tenantId))
                {
                    errors.Add(ValidationErrorCode.ForeignKeyValueDoesNotExist("TenantId"));
                }
            }

            if (errors.Count > 0)
            {
                return(NoDtoHelpers.CreateCommandResult(errors));
            }

            var transactionType = operationId == null
                ? UploadTransactionTypes.ExportOperationTemplate : UploadTransactionTypes.ExportOperationConfig;

            var queueMessage = new BlobQueueMessage
            {
                BlobTransactionType = transactionType,
                UserId               = userIdGuid,
                TenantId             = tenantId,
                OperationId          = operationId,
                AuthenticationHeader = authenticationHeader
            };

            // Add message to queue.
            await _queueManager.AddAsync(_blobStorageConnectionString, _queueStorageContainerName, JsonConvert.SerializeObject(queueMessage));

            return(NoDtoHelpers.CreateCommandResult(errors));
        }