コード例 #1
0
        public async Task <List <BuildOnReturning> > GetReturningFromBuilder(string currentUserId, string projectId)
        {
            Builder builder = await _buildersService.GetBuilderFromAdminAsync(currentUserId);

            Project project = await _projectsService.GetProjectFromIdAsync(projectId);

            if (builder == null)
            {
                throw new UnauthorizedAccessException("You are not a builder");
            }
            if (project == null)
            {
                throw new Exception("The project doesn't exist");
            }
            if (builder.Id != project.BuilderId)
            {
                throw new UnauthorizedAccessException("You are not the owner of this project");
            }

            return(await(await _buildOnReturnings.FindAsync(databaseReturning =>
                                                            databaseReturning.ProjectId == projectId
                                                            )).ToListAsync());
        }
コード例 #2
0
        public async Task <ActionResult <Builder> > GetBuilder(string id)
        {
            var     currentUserId = User.Identity.Name;
            Builder builder;

            try
            {
                if (User.IsInRole(Role.Admin))
                {
                    builder = await _buildersService.GetBuilderFromAdminAsync(id);
                }
                else if (User.IsInRole(Role.Coach))
                {
                    builder = await _buildersService.GetBuilderFromCoachAsync(currentUserId, id);
                }
                else if (User.IsInRole(Role.Builder))
                {
                    builder = await _buildersService.GetBuilderFromBuilderAsync(currentUserId, id);
                }
                else
                {
                    return(Forbid("You must be part of the Buildup program"));
                }
            }
            catch (UnauthorizedAccessException e)
            {
                return(Forbid($"You are not allowed to view this builder: {e.Message}"));
            }
            catch (Exception e)
            {
                return(BadRequest($"Can't get the builder: {e.Message}"));
            }

            if (builder == null)
            {
                return(NotFound());
            }

            return(Ok(builder));
        }