Exemplo n.º 1
0
        public IActionResult UpdateApiKey(Guid id, [FromBody] ApiKeyUpdateRequest apiKeyUpdateRequest)
        {
            if (apiKeyUpdateRequest == null ||
                apiKeyUpdateRequest.Rights == null ||
                string.IsNullOrWhiteSpace(apiKeyUpdateRequest.Name))
            {
                return(HandleBadRequest("A valid key name and rights list have to be supplied."));
            }

            // Validate access rights
            List <Right> rights = new List <Right>();

            foreach (string right in apiKeyUpdateRequest.Rights)
            {
                Right parsedRight = RightsService.GetRight(right);
                if (parsedRight == null)
                {
                    return(HandleBadRequest($"'{right}' is not a valid access right value."));
                }
                rights.Add(parsedRight);
            }

            // Attempt to update the key data.
            try
            {
                ApiKey key = ApiKeyService.UpdateApiKey(id, apiKeyUpdateRequest.Name, rights);
                return(Ok(new ApiKeyResponse(key)));
            }
            catch (ApiKeyNotFoundException exception)
            {
                return(HandleResourceNotFoundException(exception));
            }
            catch (Exception exception)
            {
                return(HandleUnexpectedException(exception));
            }
        }