Ejemplo n.º 1
0
        /// <summary>
        /// Get all definitions
        /// </summary>
        /// <param name="ct"></param>
        /// <returns>Definitions</returns>
        public async Task <IEnumerable <ViewModels.Definition> > GetAsync(CancellationToken ct)
        {
            var user = await _claimsService.GetClaimsPrincipal(_user.GetId(), true);

            if (!(await _authorizationService.AuthorizeAsync(user, null, new BasicRightsRequirement())).Succeeded)
            {
                throw new ForbiddenException();
            }

            var items = await _context.Definitions
                        .ToListAsync(ct);

            return(_mapper.Map <IEnumerable <Definition> >(items));
        }
Ejemplo n.º 2
0
        public async Task <IEnumerable <Team> > GetByExerciseIdForUserAsync(Guid exerciseId, Guid userId, CancellationToken ct)
        {
            if (!(await _authorizationService.AuthorizeAsync(_user, null, new SameUserOrExerciseAdminRequirement(exerciseId, userId))).Succeeded)
            {
                throw new ForbiddenException();
            }

            var exerciseExists = _context.Exercises
                                 .Where(e => e.Id == exerciseId)
                                 .DeferredAny()
                                 .FutureValue();

            var userExists = _context.Users
                             .Where(u => u.Id == userId)
                             .DeferredAny()
                             .FutureValue();

            //QueryFutureEnumerable<TeamDTO> teamQuery;
            IQueryable <TeamDTO> teamQuery;

            if ((await _authorizationService.AuthorizeAsync(await _claimsService.GetClaimsPrincipal(userId, true), null, new ExerciseAdminRequirement(exerciseId))).Succeeded)
            {
                teamQuery = _context.Teams
                            .Where(t => t.ExerciseId == exerciseId)
                            .ProjectTo <TeamDTO>();
                //.Future();
            }
            else
            {
                teamQuery = _context.TeamMemberships
                            .Where(x => x.UserId == userId && x.Team.ExerciseId == exerciseId)
                            .Select(x => x.Team)
                            .Distinct()
                            .ProjectTo <TeamDTO>();
                //.Future();
            }

            var teams = await teamQuery.ToListAsync();

            if (!(await userExists.ValueAsync()))
            {
                throw new EntityNotFoundException <User>();
            }

            if (!(await exerciseExists.ValueAsync()))
            {
                throw new EntityNotFoundException <Exercise>();
            }

            return(_mapper.Map <IEnumerable <Team> >(teams));
        }
Ejemplo n.º 3
0
        public async Task <IEnumerable <string> > GetUserClaimValuesAsync(CancellationToken ct)
        {
            var claimValues = new List <string>();
            var user        = await _claimsService.GetClaimsPrincipal(_user.GetId(), true);

            if ((await _authorizationService.AuthorizeAsync(user, null, new SystemAdminRightsRequirement())).Succeeded)
            {
                claimValues.Add("SystemAdmin");
            }
            if ((await _authorizationService.AuthorizeAsync(user, null, new ContentDeveloperRightsRequirement())).Succeeded)
            {
                claimValues.Add("ContentDeveloper");
            }

            return(claimValues);
        }
Ejemplo n.º 4
0
        public async Task <IEnumerable <Team> > GetByViewIdForUserAsync(Guid viewId, Guid userId, CancellationToken ct)
        {
            if (!(await _authorizationService.AuthorizeAsync(_user, null, new SameUserOrViewAdminRequirement(viewId, userId))).Succeeded)
            {
                throw new ForbiddenException();
            }

            var viewExists = await _context.Views
                             .Where(e => e.Id == viewId)
                             .AnyAsync();

            if (!viewExists)
            {
                throw new EntityNotFoundException <View>();
            }

            var userExists = await _context.Users
                             .Where(u => u.Id == userId)
                             .AnyAsync();

            if (!userExists)
            {
                throw new EntityNotFoundException <User>();
            }

            IQueryable <TeamDTO> teamQuery;

            if ((await _authorizationService.AuthorizeAsync(await _claimsService.GetClaimsPrincipal(userId, true), null, new ViewAdminRequirement(viewId))).Succeeded)
            {
                teamQuery = _context.Teams
                            .Where(t => t.ViewId == viewId)
                            .ProjectTo <TeamDTO>(_mapper.ConfigurationProvider);
            }
            else
            {
                teamQuery = _context.TeamMemberships
                            .Where(x => x.UserId == userId && x.Team.ViewId == viewId)
                            .Select(x => x.Team)
                            .Distinct()
                            .ProjectTo <TeamDTO>(_mapper.ConfigurationProvider);
            }

            var teams = await teamQuery.ToListAsync();

            return(_mapper.Map <IEnumerable <Team> >(teams));
        }
Ejemplo n.º 5
0
        public async Task <IEnumerable <Implementation> > GetAsync(CancellationToken ct)
        {
            var user = await _claimsService.GetClaimsPrincipal(_user.GetId(), true);

            if (!(await _authorizationService.AuthorizeAsync(user, null, new SystemAdminRightsRequirement())).Succeeded &&
                !(await _authorizationService.AuthorizeAsync(user, null, new ContentDeveloperRightsRequirement())).Succeeded)
            {
                throw new ForbiddenException();
            }

            IEnumerable <ImplementationEntity> items = null;

            if ((await _authorizationService.AuthorizeAsync(user, null, new SystemAdminRightsRequirement())).Succeeded)
            {
                items = await _context.Implementations.ToListAsync(ct);
            }
            else
            {
                items = await _context.Implementations.Where(x => x.CreatedBy == user.GetId()).ToListAsync(ct);
            }

            return(_mapper.Map <IEnumerable <Implementation> >(items));
        }