private async Task <dynamic> AddSecurableItem()
        {
            this.RequiresClaims(AuthorizationWriteClaim);
            var securableItemApiModel = SecureBind();
            var incomingSecurableItem = securableItemApiModel.ToSecurableItemDomainModel();

            Validate(incomingSecurableItem);

            try
            {
                SecurableItem securableItem = await _securableItemService.AddSecurableItem(ClientId, incomingSecurableItem);

                return(CreateSuccessfulPostResponse(securableItem.ToSecurableItemApiModel()));
            }
            catch (NotFoundException <Client> ex)
            {
                Logger.Error(ex, ex.Message, ClientId);
                return(CreateFailureResponse($"The specified client with id: {ClientId} was not found",
                                             HttpStatusCode.Forbidden));
            }
            catch (AlreadyExistsException <SecurableItem> ex)
            {
                Logger.Error(ex, "The posted securable item {@securableItemApiModel} already exists.", securableItemApiModel);
                return(CreateFailureResponse(
                           ex.Message,
                           HttpStatusCode.BadRequest));
            }
        }
Пример #2
0
        private async Task <dynamic> AddSecurableItem(dynamic parameters)
        {
            if (!Guid.TryParse(parameters.securableItemId, out Guid securableItemId))
            {
                return(CreateFailureResponse("securableItemId must be a guid.", HttpStatusCode.BadRequest));
            }

            var securableItemApiModel = SecureBind();

            try
            {
                var parentSecurableItem = await _securableItemService.GetSecurableItem(ClientId, securableItemId);
                await CheckWriteAccess(_clientService, _grainService, parentSecurableItem.Grain, parentSecurableItem.Name);

                var incomingSecurableItem = securableItemApiModel.ToSecurableItemDomainModel();
                Validate(incomingSecurableItem);

                var securableItem =
                    await _securableItemService.AddSecurableItem(ClientId, securableItemId, incomingSecurableItem);

                return(CreateSuccessfulPostResponse(securableItem.ToSecurableItemApiModel()));
            }
            catch (NotFoundException <Client> ex)
            {
                Logger.Error(ex, ex.Message, ClientId);
                return(CreateFailureResponse($"The specified client with id: {ClientId} was not found",
                                             HttpStatusCode.Forbidden));
            }
            catch (AlreadyExistsException <SecurableItem> ex)
            {
                Logger.Error(ex, $"Securable item {securableItemApiModel.Name} already exists. Please provide a new name",
                             securableItemApiModel);
                return(CreateFailureResponse(
                           ex.Message,
                           HttpStatusCode.Conflict));
            }
            catch (NotFoundException <SecurableItem> ex)
            {
                Logger.Error(ex, ex.Message, parameters.securableItemId);
                return(CreateFailureResponse(
                           $"The specified securableItem with id: {parameters.securableItemId} was not found",
                           HttpStatusCode.NotFound));
            }
            catch (BadRequestException <SecurableItem> ex)
            {
                Logger.Error(ex, ex.Message, parameters.securableItemId);
                return(CreateFailureResponse(
                           ex.Message,
                           HttpStatusCode.BadRequest));
            }
        }