Esempio n. 1
0
    public IActionResult GetClientError(ActionContext actionContext, IClientErrorActionResult clientError)
    {
        if (clientError is null)
        {
            throw new System.ArgumentNullException(nameof(clientError));
        }

        if (clientError.StatusCode.HasValue)
        {
            switch (clientError.StatusCode.Value)
            {
            case StatusCodes.Status400BadRequest:
                return(ErrorResult.BadRequest().ToActionResult());

            case StatusCodes.Status401Unauthorized:
                return(ErrorResult.Unauthorized().ToActionResult());

            case StatusCodes.Status403Forbidden:
                return(ErrorResult.Forbidden().ToActionResult());

            case StatusCodes.Status404NotFound:
                return(ErrorResult.NotFound("Not Found").ToActionResult());

            case StatusCodes.Status409Conflict:
                return(ErrorResult.Conflict("Conflict").ToActionResult());

            case StatusCodes.Status500InternalServerError:
                return(ErrorResult.ServerError().ToActionResult());
            }
        }

        return(ErrorResult.Unknown(clientError.StatusCode).ToActionResult());
    }
Esempio n. 2
0
        public async Task <IActionResult> EnsureProjectAndProviderAsync(Func <ProjectDocument, ProviderDocument, Task <IActionResult> > callback)
        {
            try
            {
                if (callback is null)
                {
                    throw new ArgumentNullException(nameof(callback));
                }

                if (string.IsNullOrEmpty(ProjectIdentifier))
                {
                    return(ErrorResult
                           .BadRequest($"Project name or id provided in the url path is invalid.  Must be a valid project name or id (guid).", ResultErrorCode.ValidationError)
                           .ToActionResult());
                }

                if (string.IsNullOrEmpty(ProviderId))
                {
                    return(ErrorResult
                           .BadRequest($"Provider id provided in the url path is invalid.  Must be a valid non-empty string.", ResultErrorCode.ValidationError)
                           .ToActionResult());
                }

                var project = await ProjectRepository
                              .GetAsync(ProjectIdentifier)
                              .ConfigureAwait(false);

                if (project is null)
                {
                    return(ErrorResult
                           .NotFound($"A Project with the name or id '{ProjectIdentifier}' could not be found.")
                           .ToActionResult());
                }

                var provider = await ProviderRepository
                               .GetAsync(ProviderId)
                               .ConfigureAwait(false);

                if (provider is null)
                {
                    return(ErrorResult
                           .NotFound($"A Provider with the id '{ProviderId}' could not be found..")
                           .ToActionResult());
                }

                return(await callback(project, provider)
                       .ConfigureAwait(false));
            }
            catch (Exception exc)
            {
                return(ErrorResult
                       .ServerError(exc)
                       .ToActionResult());
            }
        }
Esempio n. 3
0
        private async Task <IActionResult> EnsureProviderInternalAsync(Func <ProviderDocument, Task <IActionResult> > asyncCallback = null, Func <ProviderDocument, IActionResult> callback = null)
        {
            try
            {
                if (asyncCallback is null && callback is null)
                {
                    throw new InvalidOperationException("asyncCallback or callback must have a value");
                }

                if (!(asyncCallback is null || callback is null))
                {
                    throw new InvalidOperationException("Only one of asyncCallback or callback can hava a value");
                }

                if (string.IsNullOrEmpty(ProviderId))
                {
                    return(ErrorResult
                           .BadRequest($"Provider id provided in the url path is invalid.  Must be a valid non-empty string.", ResultErrorCode.ValidationError)
                           .ToActionResult());
                }

                var provider = await ProviderRepository
                               .GetAsync(ProviderId)
                               .ConfigureAwait(false);

                if (provider is null)
                {
                    return(ErrorResult
                           .NotFound($"A Provider with the id '{ProviderId}' could not be found..")
                           .ToActionResult());
                }

                if (!(callback is null))
                {
                    return(callback(provider));
                }

                if (!(asyncCallback is null))
                {
                    return(await asyncCallback(provider)
                           .ConfigureAwait(false));
                }

                throw new InvalidOperationException("asyncCallback or callback must have a value");
            }
            catch (Exception exc)
            {
                return(ErrorResult
                       .ServerError(exc)
                       .ToActionResult());
            }
        }
Esempio n. 4
0
        private async Task <IActionResult> EnsureCurrentUserInternalAsync(Func <UserDocument, Task <IActionResult> > asyncCallback = null, Func <UserDocument, IActionResult> callback = null)
        {
            try
            {
                if (asyncCallback is null && callback is null)
                {
                    throw new InvalidOperationException("asyncCallback or callback must have a value");
                }

                if (!(asyncCallback is null || callback is null))
                {
                    throw new InvalidOperationException("Only one of asyncCallback or callback can hava a value");
                }

                var user = await UserService
                           .CurrentUserAsync()
                           .ConfigureAwait(false);

                if (user is null)
                {
                    return(ErrorResult
                           .NotFound($"A User matching the current authenticated user was not found..")
                           .ToActionResult());
                }

                if (!(callback is null))
                {
                    return(callback(user));
                }

                if (!(asyncCallback is null))
                {
                    return(await asyncCallback(user)
                           .ConfigureAwait(false));
                }

                throw new InvalidOperationException("asyncCallback or callback must have a value");
            }
            catch (Exception exc)
            {
                return(ErrorResult
                       .ServerError(exc)
                       .ToActionResult());
            }
        }
Esempio n. 5
0
        private async Task <IActionResult> ProcessAsync(Func <Task <IActionResult> > callback)
        {
            try
            {
                if (callback is null)
                {
                    throw new ArgumentNullException(nameof(callback));
                }

                if (string.IsNullOrEmpty(ProjectId))
                {
                    return(ErrorResult
                           .BadRequest($"Project Id provided in the url path is invalid.  Must be a valid GUID.", ResultErrorCode.ValidationError)
                           .ToActionResult());
                }

                var project = await projectRepository
                              .GetAsync(ProjectId)
                              .ConfigureAwait(false);

                if (project is null)
                {
                    return(ErrorResult
                           .NotFound($"A Project with the ID '{ProjectId}' could not be found in this TeamCloud Instance.")
                           .ToActionResult());
                }

                return(await callback()
                       .ConfigureAwait(false));
            }
            catch (Exception exc)
            {
                return(ErrorResult
                       .ServerError(exc)
                       .ToActionResult());
            }
        }
Esempio n. 6
0
        private async Task <IActionResult> EnsureProjectAndCurrentUserInternalAsync(Func <ProjectDocument, UserDocument, Task <IActionResult> > asyncCallback = null, Func <ProjectDocument, UserDocument, IActionResult> callback = null)
        {
            try
            {
                if (asyncCallback is null && callback is null)
                {
                    throw new InvalidOperationException("asyncCallback or callback must have a value");
                }

                if (!(asyncCallback is null || callback is null))
                {
                    throw new InvalidOperationException("Only one of asyncCallback or callback can hava a value");
                }

                if (string.IsNullOrEmpty(ProjectIdentifier))
                {
                    return(ErrorResult
                           .BadRequest($"Project name or id provided in the url path is invalid.  Must be a valid project name or id (guid).", ResultErrorCode.ValidationError)
                           .ToActionResult());
                }

                var project = await ProjectRepository
                              .GetAsync(ProjectIdentifier)
                              .ConfigureAwait(false);

                if (project is null)
                {
                    return(ErrorResult
                           .NotFound($"A Project with the name or id '{ProjectIdentifier}' could not be found.")
                           .ToActionResult());
                }

                var user = await UserService
                           .CurrentUserAsync()
                           .ConfigureAwait(false);

                if (user is null)
                {
                    return(ErrorResult
                           .NotFound($"A User matching the current authenticated user was not found.")
                           .ToActionResult());
                }

                if (!(callback is null))
                {
                    return(callback(project, user));
                }

                if (!(asyncCallback is null))
                {
                    return(await asyncCallback(project, user)
                           .ConfigureAwait(false));
                }

                throw new InvalidOperationException("asyncCallback or callback must have a value");
            }
            catch (Exception exc)
            {
                return(ErrorResult
                       .ServerError(exc)
                       .ToActionResult());
            }
        }
Esempio n. 7
0
        private async Task <IActionResult> EnsureUserInternalAsync(Func <UserDocument, Task <IActionResult> > asyncCallback = null, Func <UserDocument, IActionResult> callback = null)
        {
            try
            {
                if (asyncCallback is null && callback is null)
                {
                    throw new InvalidOperationException("asyncCallback or callback must have a value");
                }

                if (!(asyncCallback is null || callback is null))
                {
                    throw new InvalidOperationException("Only one of asyncCallback or callback can hava a value");
                }

                if (string.IsNullOrEmpty(UserIdentifier))
                {
                    return(ErrorResult
                           .BadRequest($"User name or id provided in the url path is invalid.  Must be a valid email address, service pricipal name, or GUID.", ResultErrorCode.ValidationError)
                           .ToActionResult());
                }

                var userId = await UserService
                             .GetUserIdAsync(UserIdentifier)
                             .ConfigureAwait(false);

                if (string.IsNullOrEmpty(userId))
                {
                    return(ErrorResult
                           .NotFound($"A User with the name or id '{UserIdentifier}' could not be found.")
                           .ToActionResult());
                }

                var user = await UserRepository
                           .GetAsync(userId)
                           .ConfigureAwait(false);

                if (user is null)
                {
                    return(ErrorResult
                           .NotFound($"A User with the Id '{UserIdentifier}' could not be found..")
                           .ToActionResult());
                }

                if (!(callback is null))
                {
                    return(callback(user));
                }

                if (!(asyncCallback is null))
                {
                    return(await asyncCallback(user)
                           .ConfigureAwait(false));
                }

                throw new InvalidOperationException("asyncCallback or callback must have a value");
            }
            catch (Exception exc)
            {
                return(ErrorResult
                       .ServerError(exc)
                       .ToActionResult());
            }
        }