示例#1
0
        public async Task <IActionResult> ManageUser(string groupid, string userid, string operation)
        {
            User user = await _userManager.GetUserAsync(User);

            User targetuser = await _userManager.FindByIdAsync(userid);

            Group group = await _context.Groups.FindAsync(groupid);

            // Null checks
            if (user == null)
            {
                StatusMessage = $"Error: Please log in!";
                return(RedirectToAction("Edit"));
            }
            if (group == null)
            {
                StatusMessage = $"Error: Can't find group {groupid}!";
                return(RedirectToAction("Edit"));
            }
            if (targetuser == null)
            {
                StatusMessage = $"Error: Can't find user {userid}!";
                return(RedirectToAction("Edit"));
            }
            if (string.IsNullOrWhiteSpace(operation))
            {
                StatusMessage = $"Error: Operation is null!";
                return(RedirectToAction("Edit"));
            }


            string[] split  = operation.Split('|');
            string   action = split[0];

            TaskResult result = new TaskResult(false, "The action was unknown.");

            if (action == "kick")
            {
                result = await GroupManager.KickFromGroup(group, user, targetuser);
            }
            else if (action == "ban")
            {
                result = await GroupManager.BanFromGroup(group, user, targetuser);
            }
            else if (action == "addrole")
            {
                if (split.Length < 2)
                {
                    StatusMessage = $"Error: Operation is missing an argument!";
                    return(RedirectToAction("Edit", new { groupid = groupid }));
                }

                GroupRole role = _context.GroupRoles.FirstOrDefault(r => r.RoleId.ToLower() == split[1].ToLower());

                result = await GroupManager.AddToRole(group, user, targetuser, role);

                StatusMessage = result.Info;

                return(Redirect(Request.Headers["Referer"].ToString()));
            }
            else if (action == "removerole")
            {
                if (split.Length < 2)
                {
                    StatusMessage = $"Error: Operation is missing an argument!";
                    return(RedirectToAction("Edit", new { groupid = groupid }));
                }

                GroupRole role = _context.GroupRoles.FirstOrDefault(r => r.RoleId.ToLower() == split[1].ToLower());

                result = await GroupManager.RemoveFromRole(group, user, targetuser, role);

                StatusMessage = result.Info;

                return(Redirect(Request.Headers["Referer"].ToString()));
            }
            else if (action == "invite")
            {
                result = await GroupManager.AddInvite(group, user, targetuser);
            }
            else if (action == "uninvite")
            {
                result = await GroupManager.RemoveInvite(group, user, targetuser);
            }

            StatusMessage = result.Info;

            return(Redirect(Request.Headers["Referer"].ToString()));
        }