Esempio n. 1
0
        public async Task <IActionResult> SetCollectionOptions([FromBody] CollectionOptions collectionOptions)
        {
            if (collectionOptions == null)
            {
                return(BadRequest("Collection options not set"));
            }
            if (!NamingConventions.IsValidDataType(collectionOptions.CollectionName))
            {
                return(BadRequest($"Data type '{collectionOptions.CollectionName}' is not a valid name for a collection"));
            }

            IRdDataStorage rdDataStorage;

            try
            {
                rdDataStorage = await dataRouter.GetSourceSystemAsync(collectionOptions.CollectionName);
            }
            catch (KeyNotFoundException e)
            {
                return(BadRequest(e.Message));
            }
            if (collectionOptions.IdGeneratorType.HasValue && !rdDataStorage.IsIdGeneratorTypeSupported(collectionOptions.IdGeneratorType.Value))
            {
                return(BadRequest($"The backing storage for collection '{collectionOptions.CollectionName}' doesn't support ID generator type '{collectionOptions.IdGeneratorType}'"));
            }

            // Authorize
            var loggedInUsername    = UsernameNormalizer.Normalize(HttpContext.User.Identity.Name);
            var resourceDescription = new SetCollectionOptionsResourceDescription();
            var authorizationResult = await authorizationModule.AuthorizeAsync(resourceDescription, loggedInUsername);

            if (!authorizationResult.IsAuthorized)
            {
                return(StatusCode((int)HttpStatusCode.Unauthorized, "Not authorized"));
            }

            // Provide
            try
            {
                await authorizationModule.AddOrUpdateCollectionMetadata(collectionOptions);

                var logEntries = LoggingHelpers.WriteCollectionMetadataLog(collectionOptions, authorizationResult.User.UserName);
                logEntries.ForEach(apiEventLogger.Log);
                return(Ok());
            }
            catch (Exception e)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError, e.InnermostException().Message));
            }
        }