/// <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));
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Block/unblock a user from the group
        /// </summary>
        /// <param name="order">The info to block/unblock the user</param>
        /// See <see cref="Areas.GroupManage.Models.MakeAdmin_blockUser"/> 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 Task <IActionResult> blockUser([FromBody] MakeAdmin_blockUser 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.BLOCK_USER, order.make_unmake))
            {
                return(BadRequest());
            }
            if (!group.open)
            {
                return(BadRequest(new { error = "GroupBanned" }));
            }

            try
            {
                _context.Entry(user).Collection("groups").Load();
                UserGroup callerUG = user.groups.Where(g => g.groupid == group.id).First();

                targetUser.blocked   = !targetUser.blocked;
                targetUser.role      = RoleManager.getGroupNormal(_context);
                targetUser.blockedBy = callerUG.role;
                _context.Update(targetUser);
                _context.SaveChanges();

                _context.Entry(targetUser).Reference("User").Load();
                if (order.make_unmake)
                {
                    await KickChatNotification.sendKickMessageAsync(group.name, targetUser.User.publicid, _chatHub);
                }
                await sendMessages(targetUser, group, order.make_unmake);

                return(Ok(GroupPageManager.GetPage(user, group, _context)));
            }
            catch (Exception)
            {
                return(StatusCode(500));
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Gives the group-admin to a member of a group
        /// </summary>
        /// <param name="order">The infoto make a user admin of a group</param>
        /// See <see cref="Areas.GroupManage.Models.MakeAdmin_blockUser"/> 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 Task <IActionResult> makeAdmin([FromBody] MakeAdmin_blockUser order)
        {
            User user = TokenUserManager.getUserFromToken(HttpContext, _context); //The user who tries to make admin to another user

            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.MAKE_ADMIN, order.make_unmake))
            {
                return(BadRequest());
            }
            if (!group.open)
            {
                return(BadRequest(new { error = "GroupBanned" }));
            }

            try
            {
                targetUser.role = order.make_unmake ? RoleManager.getGroupAdmin(_context) : RoleManager.getNormalUser(_context);
                _context.Update(targetUser);
                _context.SaveChanges();

                await sendNews(targetUser, group, order.make_unmake);

                return(Ok(GroupPageManager.GetPage(user, group, _context)));
            }
            catch (Exception)
            {
                return(StatusCode(500));
            }
        }