Exemple #1
0
        public async Task <IActionResult> OnPostAcceptJoinRequestAsync(string returnUrl = null)
        {
            returnUrl ??= Url.Content("~/");

            var request = await _context.GroupJoinRequest
                          .FirstOrDefaultAsync(r => r.GroupJoinRequestID == JoinRequestInput.RequestID);

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

            var membership = new MultiuserGroupMembership()
            {
                ChatUserID          = request.ChatUserID,
                GroupID             = request.GroupID,
                IsActiveInGroup     = false,
                IsOnlineInGroup     = false,
                Rank                = PermissionRank.USER.Ordinal,
                LastViewedMessageID = null,
                NumNew              = 0
            };

            await _context.Membership.AddAsync(membership);

            _context.GroupJoinRequest.Remove(request);

            var group = await _context.Group
                        .OfType <MultiuserGroup>()
                        .FirstOrDefaultAsync(g => g.GroupID == request.GroupID);

            var notif = new Notification()
            {
                ChatUserID = request.ChatUserID,
                Date       = DateTime.Now,
                Title      = "Join request accepted",
                Text       = $"Your join request for {group.Name} was accepted.",
                ViewURL    = $"/Group?groupId={group.GroupID}"
            };

            await _context.Notification.AddAsync(notif);

            await _context.SaveChangesAsync();

            await _hubContext.SendNotificationAsync(notif);

            return(LocalRedirect(returnUrl));
        }
Exemple #2
0
        public async Task <IActionResult> OnPostAcceptJoinInvitationAsync(string returnUrl = null)
        {
            returnUrl ??= Url.Content("~/");
            var currentUser = await _userManager.GetUserAsync(User);

            var chatUser = await _context.Users
                           .FirstOrDefaultAsync(g => g.Id == JoinInvitationInput.ChatUserID);

            if (chatUser is null)
            {
                return(BadRequest());
            }

            if (currentUser.Id != chatUser.Id)
            {
                return(BadRequest());
            }

            var invitation = await _context.GroupJoinInvitation
                             .FirstOrDefaultAsync(i => i.GroupJoinInvitationID == JoinInvitationInput.InvitationID);

            if (invitation is null)
            {
                return(BadRequest());
            }

            var membership = new MultiuserGroupMembership()
            {
                ChatUserID          = invitation.ChatUserID,
                GroupID             = invitation.GroupID,
                IsActiveInGroup     = false,
                IsOnlineInGroup     = false,
                Rank                = PermissionRank.USER.Ordinal,
                LastViewedMessageID = null,
                NumNew              = 0
            };

            await _context.Membership.AddAsync(membership);

            _context.GroupJoinInvitation.Remove(invitation);
            await _context.SaveChangesAsync();

            return(LocalRedirect(returnUrl));
        }
        public async Task <IActionResult> OnPostAsync()
        {
            if (_signInManager.IsSignedIn(User))
            {
                var chatUser = await _userManager.GetUserAsync(User);

                var group = new MultiuserGroup()
                {
                    Name        = Input.Name,
                    Description = Input.Description,
                    DateCreated = DateTime.Now,
                    IsArchived  = false,
                    IsPrivate   = !Input.IsPublic,
                    GroupImage  = _fileConfiguration.SaveFileFromDefault(_fileConfiguration.DefaultGroupImage)
                };

                await _context.Group.AddAsync(group);

                await _context.SaveChangesAsync();

                var membership = new MultiuserGroupMembership()
                {
                    ChatUserID      = chatUser.Id,
                    GroupID         = group.GroupID,
                    IsActiveInGroup = false,
                    IsOnlineInGroup = false,
                    Rank            = PermissionRank.OWNER.Ordinal
                };

                await _context.Membership.AddAsync(membership);

                await _context.SaveChangesAsync();

                return(LocalRedirect("/group?groupId=" + group.GroupID));
            }
            else
            {
                return(LocalRedirect("/Identity/Account/Login"));
            }
        }
Exemple #4
0
        public async Task <IActionResult> OnGetAsync(int?userId, int?groupId)
        {
            if (userId == null || groupId == null)
            {
                return(BadRequest());
            }

            var user = await _context.Users.FirstOrDefaultAsync(u => u.Id == userId);

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

            var currentUser = await _userManager.GetUserAsync(User);

            if (currentUser.Id == userId.Value)
            {
                return(BadRequest());
            }

            var group = await _context.Group
                        .OfType <MultiuserGroup>()
                        .Include(g => g.Memberships)
                        .FirstOrDefaultAsync(g => g.GroupID == groupId);

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

            var memberships = group.Memberships
                              .OfType <MultiuserGroupMembership>()
                              .Where(m => m.ChatUserID == user.Id || m.ChatUserID == currentUser.Id);

            MultiuserGroupMembership other   = null;
            MultiuserGroupMembership current = null;

            foreach (var m in memberships)
            {
                if (m.ChatUserID == user.Id)
                {
                    other = m;
                }
                else
                {
                    current = m;
                }
            }

            if (current is null || current.Rank < PermissionRank.OFFICER.Ordinal)
            {
                return(BadRequest());
            }

            if (other is null)
            {
                MembershipID = null;
            }
            else if (other.Rank >= current.Rank)
            {
                return(BadRequest());
            }
            else
            {
                MembershipID = other.MembershipID;
            }

            UserID   = user.Id;
            UserName = user.UserName;
            GroupID  = group.GroupID;

            return(Page());
        }
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl ??= Url.Content("~/");
            if (ModelState.IsValid)
            {
                var user = new ChatUser()
                {
                    UserName           = Input.Username,
                    Email              = Input.Email,
                    FirstName          = Input.FirstName,
                    LastName           = Input.LastName,
                    ProfileDescription = "",
                    ProfileImage       = _fileConfiguration.SaveFileFromDefault(_fileConfiguration.DefaultUserProfileImage),
                    IsEnabled          = true,
                    NumOnline          = 0,
                };

                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)
                {
                    Group group = await _context.Group.FirstAsync();

                    var membership = new MultiuserGroupMembership()
                    {
                        GroupID         = group.GroupID,
                        ChatUserID      = user.Id,
                        Rank            = PermissionRank.USER.Ordinal,
                        IsOnlineInGroup = false,
                        IsActiveInGroup = false,
                        Group           = group,
                        ChatUser        = user
                    };
                    _context.Membership.Add(membership);
                    _context.SaveChanges();
                    _logger.LogInformation("User created a new account with password.");

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var callbackUrl = Url.Page(
                        "/Account/ConfirmEmail",
                        pageHandler: null,
                        values: new { userId = user.Id, code = code },
                        protocol: Request.Scheme);

                    await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
                                                      $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

                    await _signInManager.SignInAsync(user, isPersistent : false);

                    return(LocalRedirect(returnUrl));
                }

                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            // If we got this far, something failed, redisplay form
            return(Page());
        }