Пример #1
0
        /// <summary>
        /// Remove the groups where the user was joined at
        /// </summary>
        /// <param name="user">The user who wants to delete his account</param>
        /// <returns>True if the process was right, false otherwise</returns>
        private async Task <bool> removeGroups(User user)
        {
            _context.Entry(user).Collection("groups").Load();

            List <UserGroup> groups = user.groups.ToList();


            for (int i = 0; i < groups.Count(); i++)
            {
                using (var scope = _scopeFactory.CreateScope())
                {
                    try
                    {
                        var dbContext = scope.ServiceProvider.GetRequiredService <ApplicationDBContext>();
                        var hub       = scope.ServiceProvider.GetRequiredService <IHubContext <NotificationHub> >();

                        UserGroup delGroup = dbContext.UserGroup.Where(g =>
                                                                       g.groupid == groups.ElementAt(i).groupid&& g.userid == user.id).First();

                        if (!await QuitUserFromGroup.quitUser(delGroup, dbContext, hub))
                        {
                            return(false);
                        }
                    }
                    catch (Exception)
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
        /// <summary>
        /// Kick a user out from the group
        /// </summary>
        /// <param name="order">The info to kick a user from the group</param>
        /// See <see cref="Areas.GroupManage.Models.KickUser"/> to know the param structure
        /// <returns>The updated group page</returns>
        /// See <see cref="Areas.GroupManage.Models.GroupPage"/> to know the response structure
        public async System.Threading.Tasks.Task <IActionResult> removeUserAsync([FromBody] KickUser order)
        {
            User user = TokenUserManager.getUserFromToken(HttpContext, _context); //The user who tries to kick the user from the group

            if (!user.open)
            {
                return(BadRequest(new { error = "YoureBanned" }));
            }
            if (AdminPolicy.isAdmin(user, _context))
            {
                return(BadRequest("notAllowed"));
            }
            UserGroup targetUser = new UserGroup();
            Group     group      = new Group();

            if (!GroupAdminFuncionlities.checkFuncionality(user, ref group, order.groupName, ref targetUser, order.publicId, _context, GroupAdminFuncionality.REMOVE_USER, false))
            {
                return(BadRequest());
            }
            if (!group.open)
            {
                return(BadRequest(new { error = "GroupBanned" }));
            }

            try
            {
                _context.Entry(targetUser).Reference("User").Load();
                User sendNew      = targetUser.User;
                Guid targetUserid = targetUser.User.id;
                await QuitUserFromGroup.quitUser(targetUser, _context, _notificationHub);

                await KickChatNotification.sendKickMessageAsync(group.name, targetUser.User.publicid, _chatHub);

                InteractionManager.manageInteraction(targetUser.User, group, interactionType.KICKED, _context);

                using (var scope = _scopeFactory.CreateScope())
                {
                    var dbContext = scope.ServiceProvider.GetRequiredService <ApplicationDBContext>();
                    group = dbContext.Group.Where(g => g.name == order.groupName).First();
                    User recv = dbContext.User.Where(u => u.id == targetUserid).First();
                    user = dbContext.User.Where(u => u.id == user.id).First();

                    Home.Util.GroupNew.launch(sendNew, group, null, Home.Models.TypeGroupNew.KICK_USER_USER, false, dbContext);
                    Home.Util.GroupNew.launch(sendNew, group, null, Home.Models.TypeGroupNew.KICK_USER_GROUP, false, dbContext);
                    await SendNotification.send(_notificationHub, group.name, recv, NotificationType.KICKED_GROUP, dbContext);

                    return(Ok(GroupPageManager.GetPage(user, group, dbContext)));
                }
            }
            catch (Exception)
            {
                return(StatusCode(500));
            }
        }
Пример #3
0
        /// <summary>
        /// Get a user out of a group
        /// </summary>
        /// <param name="groupName">The name of the group that the user is gonna left</param>
        /// <returns>IActionResult of the leaving group action</returns>
        public async System.Threading.Tasks.Task <IActionResult> leaveGroup(string groupName)
        {
            User user = TokenUserManager.getUserFromToken(HttpContext, _context); //The user who tries to leave the group

            if (!user.open)
            {
                return(BadRequest(new { error = "YoureBanned" }));
            }
            if (AdminPolicy.isAdmin(user, _context))
            {
                return(BadRequest("notAllowed"));
            }
            UserGroup ugCaller = new UserGroup();
            Group     group    = new Group();

            if (!UserFromGroup.isOnIt(user.id, ref group, groupName, ref ugCaller, _context))
            {
                return(BadRequest());
            }
            if (!group.open)
            {
                return(BadRequest(new { error = "GroupBanned" }));
            }
            if (!await QuitUserFromGroup.quitUser(ugCaller, _context, _hub))
            {
                return(StatusCode(500));
            }
            InteractionManager.manageInteraction(user, group, interactionType.LEAVED, _context);

            using (var scope = _scopeFactory.CreateScope())
            {
                var dbContext = scope.ServiceProvider.GetRequiredService <ApplicationDBContext>();
                var groupE    = dbContext.Group.Where(g => g.name == groupName);

                if (groupE.Count() == 1)
                {
                    Home.Util.GroupNew.launch(user, group, null, Home.Models.TypeGroupNew.JOIN_LEFT_GROUP, false, _context);
                }
                if (groupE.Count() == 1)
                {
                    Home.Util.GroupNew.launch(user, group, null, Home.Models.TypeGroupNew.JOIN_LEFT_USER, false, _context);
                }
            }

            return(Ok(new { success = "SuccesfullGroupLeave" }));
        }