Beispiel #1
0
        /// <inheritdoc/>
        public async Task <IEnumerable <ApplicationDto> > Handle(GetApplicationsForApplicantQuery request, CancellationToken cancellationToken)
        {
            var userId = accessUserPrincipalService.GetUserId();
            var query  = dbContext.Applications.Where(a => a.CreatorUserId == userId);

            return(await mapper.ProjectTo <ApplicationDto>(query).ToListAsync(cancellationToken));
        }
Beispiel #2
0
        /// <inheritdoc/>
        public async Task <IEnumerable <AuthorityDto> > Handle(GetUsersAuthoritiesQuery request, CancellationToken cancellationToken)
        {
            var userId = accessUserPrincipalService.GetUserId();

            var query       = applicationDbContext.Authorities.Where(a => a.Users.Any(u => u.Id == userId));
            var authorities = await mapper.ProjectTo <AuthorityDto>(query).ToListAsync(cancellationToken);

            return(authorities);
        }
Beispiel #3
0
        /// <inheritdoc/>
        public async Task <IEnumerable <ApplicationDto> > Handle(GetApplicationsFromAllUsersAuthoritiesQuery request, CancellationToken cancellationToken)
        {
            var userId = accessUserPrincipalService.GetUserId();

            var query = dbContext.Applications
                        .Where(a => a.Status == ApplicationStatus.Sent);

            if (!await userManager.HasAnyRole(userId.GetValueOrDefault(), Domain.Entities.UserRoles.Admin))
            {
                var usersAuthorities = dbContext.Users.Where(u => u.Id == userId).SelectMany(u => u.Authorities);
                query = query.Where(a => usersAuthorities.Any(ua => ua.Id == a.AuthorityId));
            }

            return(await mapper.ProjectTo <ApplicationDto>(query).ToListAsync(cancellationToken));
        }
        /// <inheritdoc/>
        public async Task <TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate <TResponse> next)
        {
            var userId = accessUserPrincipalService.GetUserId();

            if (request is AuthorityRequestBase authorityRequest)
            {
                if (!await userManager.HasAnyRole(userId.GetValueOrDefault(), UserRoles.Admin))
                {
                    var authority = await applicationDbContext.Authorities.Where(a => a.Id == authorityRequest.AuthorityId).SingleAsync();

                    if (!authority.Users.Any(u => u.Id == userId))
                    {
                        throw new ForbiddenException("The user does not have access to the authority.");
                    }
                }
            }

            return(await next());
        }
        /// <inheritdoc/>
        public async Task <Unit> Handle(SendApplicationCommand request, CancellationToken cancellationToken)
        {
            var userId      = accessUserPrincipalService.GetUserId();
            var application = new Application()
            {
                Name          = request.Name,
                Description   = request.Description,
                CreatorUserId = userId,
                CreationDate  = DateTime.UtcNow
            };

            if (request.FormFiles != null)
            {
                var attachmentDtos = mapper.Map <IEnumerable <AttachmentDto> >(request.FormFiles);

                var attachments = await mediator.Send(
                    new SaveAttachmentCommand()
                {
                    Attachments = attachmentDtos
                },
                    cancellationToken);

                application.Attachments = attachments.ToList();
            }

            if (request.ApplicationTypeId.HasValue)
            {
                await SetAuthorityByApplicationTypeAsync(application, request.ApplicationTypeId.Value, cancellationToken);
            }

            dbContext.Applications.Add(application);
            await dbContext.SaveChangesAsync(cancellationToken);

            var editorialUsers = await userManager.GetUsersInRoleAsync(UserRoles.Editor.ToString());

            var emails = editorialUsers.Select(u => u.Email);

            await SendEmailsToEditorial(emails, mapper.Map <ApplicationDto>(application), CancellationToken.None);

            return(Unit.Value);
        }