Exemplo n.º 1
0
        public async Task AddModeratorAsync(Community community, AddModeratorDto addModeratorDto)
        {
            string roleName = $"{nameof(Community)}/{community.Id}/{Roles.Moderator}";

            if (!await _roleManager.RoleExistsAsync(roleName))
            {
                throw new HttpStatusCodeException(HttpStatusCode.BadRequest);
            }

            User newModerator = await _userManager.FindByIdAsync(addModeratorDto.NewModeratorId);

            await _userManager.AddToRoleAsync(newModerator, roleName);
        }
Exemplo n.º 2
0
        public async Task <IActionResult> AddModerator([FromRoute] string communityId,
                                                       [FromBody] AddModeratorDto addModeratorDto)
        {
            Community community = await _mediator.Send(new GetCommunityEntityQuery(communityId));

            AuthorizationResult authorizationResult =
                await _authorizationService.AuthorizeAsync(User, community, PolicyConstants.AddModeratorRolePolicy);

            if (!authorizationResult.Succeeded)
            {
                return(ActionResults.UnauthorizedResult(User.Identity.IsAuthenticated));
            }

            await _mediator.Send(new AddModeratorCommand(community, addModeratorDto));

            return(Ok());
        }
Exemplo n.º 3
0
        public async Task <IActionResult> AddModerator(AddModeratorDto input)
        {
            var token  = GetToken();
            var userId = LoginHelper.GetClaim(token, "UserId");

            if (userId == null || token == null || String.IsNullOrEmpty(token))
            {
                return(Unauthorized());
            }

            var isModerator = await _communityUserRepository.GetAll()
                              .Where(x => x.UserId == Guid.Parse(userId) && x.IsAdmin && x.Community.Slug == input.CommunitySlug && !x.IsDeleted)
                              .FirstOrDefaultAsync();

            if (isModerator == null)
            {
                return(Unauthorized());
            }

            input.RequesterModeratorId = Guid.Parse(userId);
            var result = await _communityAppService.AddModerator(input);

            return(Ok(result));
        }
Exemplo n.º 4
0
 public AddModeratorCommand(Domain.Entities.Community community, AddModeratorDto addModeratorDto)
 {
     Community       = community;
     AddModeratorDto = addModeratorDto;
 }
        public async Task <Response> AddModerator(AddModeratorDto input)
        {
            var response  = new Response();
            var community = await _communityRepository.GetAll()
                            .FirstOrDefaultAsync(x => !x.IsDeleted && x.Slug == input.CommunitySlug);

            var requester = await _userRepository.GetByIdAsync(input.RequesterModeratorId);

            var user = await _userRepository.GetByIdAsync(input.UserId);

            var isAlreadyExist = await _communityUserRepository.GetAll()
                                 .Where(x => x.UserId == input.UserId && x.Community.Slug == input.CommunitySlug &&
                                        x.IsDeleted == false && x.IsAdmin).FirstOrDefaultAsync();

            if (isAlreadyExist != null)
            {
                response.Message = "Bu kullanıcı zaten moderatör";
                response.Status  = false;
                return(response);
            }

            var isWaiting = await _communityUserRepository.GetAll()
                            .Where(x => x.UserId == input.UserId && x.Community.Slug == input.CommunitySlug &&
                                   !x.IsDeleted && x.ModeratorRequest == ModeratorRequest.Waiting && !x.IsAdmin).FirstOrDefaultAsync();

            if (isWaiting != null)
            {
                response.Message = "Bu kullanıcıya zaten istek gönderilmiş";
                response.Status  = false;
                return(response);
            }

            var isJoined = await _communityUserRepository.GetAll()
                           .Where(x => x.UserId == input.UserId && x.Community.Slug == input.CommunitySlug && !x.IsDeleted)
                           .FirstOrDefaultAsync();

            if (isJoined != null)
            {
                isJoined.ModeratorRequest = ModeratorRequest.Waiting;
                await _communityUserRepository.UpdateAsync(isJoined);

                response.Message = "Kullanıcıya istek gönderildi.";
                response.Status  = true;
                var content = requester.Username + " " + "seni" + " " + community.Name +
                              " topluluğuna moderatör olarak eklemek istiyor.";
                var notify = new Notification
                {
                    TargetId    = community.Id,
                    OwnerUserId = input.UserId,
                    TargetName  = "/t/" + community.Slug,
                    Type        = NotifyContentType.AddModerator,
                    Content     = content,
                    ImgPath     = requester.ProfileImagePath
                };
                await _notificationRepository.AddAsync(notify);

                await _emailSender.SendEmail(user.EmailAddress, "Moderatörlük isteği", content + " https://saalla.com");

                return(response);
            }

            var newRelation = new CommunityUser
            {
                UserId           = input.UserId,
                CommunityId      = community.Id,
                ModeratorRequest = ModeratorRequest.Waiting
            };
            await _communityUserRepository.AddAsync(newRelation);

            response.Message = "Kullanıcıya istek gönderildi.";
            response.Status  = true;
            var content2 = requester.Username + " seni" + community.Name +
                           " topluluğuna moderatör olarak eklemek istiyor.";
            var notify2 = new Notification
            {
                TargetId    = community.Id,
                OwnerUserId = input.UserId,
                TargetName  = "/t/" + community.Slug,
                Type        = NotifyContentType.AddModerator,
                Content     = requester.Username + " seni" + community.Name + " topluluğuna moderatör olarak eklemek istiyor.",
                ImgPath     = community.LogoPath
            };
            await _notificationRepository.AddAsync(notify2);

            await _emailSender.SendEmail(user.EmailAddress, "Moderatörlük isteği", content2 + " https://saalla.com");

            return(response);
        }