コード例 #1
0
        public async Task CreateAdminAction(AdminActionDto adminActionDto)
        {
            if (adminActionDto == null)
            {
                throw new ArgumentNullException(nameof(adminActionDto));
            }

            var player = await _legacyContext.Player2.SingleOrDefaultAsync(p => p.PlayerId == adminActionDto.PlayerId);

            if (player == null)
            {
                throw new NullReferenceException(nameof(player));
            }

            AspNetUsers admin = null;

            if (!string.IsNullOrWhiteSpace(adminActionDto.AdminId))
            {
                admin = await _legacyContext.AspNetUsers.SingleOrDefaultAsync(u => u.XtremeIdiotsId == adminActionDto.AdminId);
            }

            var adminAction = new AdminActions
            {
                PlayerPlayer = player,
                Admin        = admin,
                Type         = adminActionDto.Type,
                Text         = adminActionDto.Text,
                Created      = adminActionDto.Created,
                Expires      = adminActionDto.Expires,
                ForumTopicId = adminActionDto.ForumTopicId
            };

            _legacyContext.AdminActions.Add(adminAction);
            await _legacyContext.SaveChangesAsync();
        }
コード例 #2
0
        public static AdminActionDto WithPlayerDto(this AdminActionDto adminActionDto, PlayerDto playerDto)
        {
            adminActionDto.PlayerId = playerDto.PlayerId;
            adminActionDto.GameType = playerDto.GameType;
            adminActionDto.Username = playerDto.Username;
            adminActionDto.Guid     = playerDto.Guid;

            return(adminActionDto);
        }
コード例 #3
0
 private string PostContent(AdminActionDto model)
 {
     return("<p>" +
            $"   Username: {model.Username}<br>" +
            $"   Player Link: <a href=\"https://portal.xtremeidiots.com/Players/Details/{model.PlayerId}\">Portal</a><br>" +
            $"   {model.Type} Created: {model.Created.ToString(CultureInfo.InvariantCulture)}" +
            "</p>" +
            "<p>" +
            $"   {model.Text}" +
            "</p>" +
            "<p>" +
            "   <small>Do not edit this post directly as it will be overwritten by the Portal. Add comments on posts below or edit the record in the Portal.</small>" +
            "</p>");
 }
コード例 #4
0
        public async Task <IActionResult> Edit(AdminActionDto model)
        {
            var adminActionDto = await _adminActionsRepository.GetAdminAction(model.AdminActionId);

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

            if (!ModelState.IsValid)
            {
                model.Username = adminActionDto.Username;
                return(View(model));
            }

            var canEditAdminAction = await _authorizationService.AuthorizeAsync(User, adminActionDto, AuthPolicies.EditAdminAction);

            if (!canEditAdminAction.Succeeded)
            {
                return(Unauthorized());
            }

            adminActionDto.Text = model.Text;

            if (model.Type == AdminActionType.TempBan)
            {
                adminActionDto.Expires = model.Expires;
            }

            var canChangeAdminActionAdmin = await _authorizationService.AuthorizeAsync(User, adminActionDto, AuthPolicies.ChangeAdminActionAdmin);

            if (canChangeAdminActionAdmin.Succeeded)
            {
                adminActionDto.AdminId = model.AdminId;
            }

            await _adminActionsRepository.UpdateAdminAction(adminActionDto);

            if (adminActionDto.ForumTopicId != 0)
            {
                await _playersForumsClient.UpdateTopicForAdminAction(adminActionDto);
            }

            _logger.LogInformation(EventIds.AdminAction, "User {User} has updated {AdminActionId} against {PlayerId}", User.Username(), model.AdminActionId, model.PlayerId);
            this.AddAlertSuccess($"The {model.Type} has been successfully updated for {adminActionDto.Username}");

            return(RedirectToAction("Details", "Players", new { id = model.PlayerId }));
        }
コード例 #5
0
        public async Task UpdateTopicForAdminAction(AdminActionDto model)
        {
            if (model.ForumTopicId == 0)
            {
                return;
            }

            var userId = 21145; // Admin

            if (model.AdminId != null)
            {
                userId = Convert.ToInt32(model.AdminId);
            }

            await _forumsClient.UpdateTopic(model.ForumTopicId, userId, PostContent(model));
        }
コード例 #6
0
        public async Task <int> CreateTopicForAdminAction(AdminActionDto model)
        {
            try
            {
                var userId = 21145; // Admin
                if (model.AdminId != null)
                {
                    userId = Convert.ToInt32(model.AdminId);
                }

                var forumId = 28;
                switch (model.Type)
                {
                case AdminActionType.Observation:
                    forumId = model.GameType.ForumIdForObservations();
                    break;

                case AdminActionType.Warning:
                    forumId = model.GameType.ForumIdForWarnings();
                    break;

                case AdminActionType.Kick:
                    forumId = model.GameType.ForumIdForKicks();
                    break;

                case AdminActionType.TempBan:
                    forumId = model.GameType.ForumIdForTempBans();
                    break;

                case AdminActionType.Ban:
                    forumId = model.GameType.ForumIdForBans();
                    break;
                }

                var postTopicResult = await _forumsClient.PostTopic(forumId, userId, $"{model.Username} - {model.Type}", PostContent(model), model.Type.ToString());

                return(postTopicResult.TopicId);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error creating admin action topic");
                return(0);
            }
        }
コード例 #7
0
        public async Task UpdateAdminAction(AdminActionDto adminActionDto)
        {
            if (adminActionDto == null)
            {
                throw new ArgumentNullException(nameof(adminActionDto));
            }

            var adminAction = await _legacyContext.AdminActions.SingleOrDefaultAsync(aa => aa.AdminActionId == adminActionDto.AdminActionId);

            if (adminAction == null)
            {
                throw new NullReferenceException(nameof(adminAction));
            }

            adminAction.Text    = adminActionDto.Text;
            adminAction.Expires = adminActionDto.Expires;

            if (adminAction.AdminId != adminActionDto.AdminId)
            {
                if (string.IsNullOrWhiteSpace(adminActionDto.AdminId))
                {
                    adminAction.Admin = null;
                }
                else
                {
                    var admin = await _legacyContext.AspNetUsers.SingleOrDefaultAsync(u => u.XtremeIdiotsId == adminActionDto.AdminId);

                    if (admin == null)
                    {
                        throw new NullReferenceException(nameof(admin));
                    }

                    adminAction.Admin = admin;
                }
            }

            if (adminActionDto.ForumTopicId != 0)
            {
                adminAction.ForumTopicId = adminActionDto.ForumTopicId;
            }

            await _legacyContext.SaveChangesAsync();
        }
コード例 #8
0
        public async Task <IActionResult> Create(AdminActionDto model)
        {
            var playerDto = await _playersRepository.GetPlayer(model.PlayerId);

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

            if (!ModelState.IsValid)
            {
                model = model.WithPlayerDto(playerDto);
                return(View(model));
            }

            var adminActionDto       = new AdminActionDto().OfType(model.Type).WithPlayerDto(playerDto);
            var canCreateAdminAction = await _authorizationService.AuthorizeAsync(User, adminActionDto, AuthPolicies.CreateAdminAction);

            if (!canCreateAdminAction.Succeeded)
            {
                return(Unauthorized());
            }

            adminActionDto.AdminId = User.XtremeIdiotsId();
            adminActionDto.Text    = model.Text;

            if (model.Type == AdminActionType.TempBan)
            {
                adminActionDto.Expires = model.Expires;
            }

            adminActionDto.ForumTopicId = await _playersForumsClient.CreateTopicForAdminAction(adminActionDto);

            await _adminActionsRepository.CreateAdminAction(adminActionDto);

            _logger.LogInformation(EventIds.AdminAction, "User {User} has created a new {AdminActionType} against {PlayerId}", User.Username(), model.Type, model.PlayerId);
            this.AddAlertSuccess($"The {model.Type} has been successfully against {playerDto.Username} with a <a target=\"_blank\" href=\"https://www.xtremeidiots.com/forums/topic/{adminActionDto.ForumTopicId}-topic/\" class=\"alert-link\">topic</a>");

            return(RedirectToAction("Details", "Players", new { id = model.PlayerId }));
        }
コード例 #9
0
        public async Task <IActionResult> Create(Guid id, AdminActionType adminActionType)
        {
            var playerDto = await _playersRepository.GetPlayer(id);

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

            var adminActionDto       = new AdminActionDto().OfType(adminActionType).WithPlayerDto(playerDto);
            var canCreateAdminAction = await _authorizationService.AuthorizeAsync(User, adminActionDto, AuthPolicies.CreateAdminAction);

            if (!canCreateAdminAction.Succeeded)
            {
                return(Unauthorized());
            }

            if (adminActionType == AdminActionType.TempBan)
            {
                adminActionDto.Expires = DateTime.UtcNow.AddDays(7);
            }

            return(View(adminActionDto));
        }
コード例 #10
0
        public async Task IngestBanFileDataForGame(GameType gameType, string remoteBanFileData)
        {
            var skipTags = new[] { "[PBBAN]", "[B3BAN]", "[BANSYNC]", "[EXTERNAL]" };

            foreach (var line in remoteBanFileData.Split('\n'))
            {
                if (string.IsNullOrWhiteSpace(line) || skipTags.Any(skipTag => line.Contains(skipTag)))
                {
                    continue;
                }

                ParseLine(line, out var guid, out var name);

                if (string.IsNullOrWhiteSpace(guid) || string.IsNullOrWhiteSpace(name))
                {
                    continue;
                }

                if (!_guidValidator.IsValid(gameType, guid))
                {
                    _logger.LogWarning($"Could not validate guid {guid} for {gameType}");
                    continue;
                }

                var player = await _playersRepository.GetPlayer(gameType, guid);

                if (player == null)
                {
                    _logger.LogInformation($"BanFileIngest - creating new player {name} with guid {guid} with import ban");

                    await _playersRepository.CreatePlayer(new PlayerDto
                    {
                        GameType = gameType,
                        Username = name,
                        Guid     = guid
                    });

                    player = await _playersRepository.GetPlayer(gameType, guid);

                    var adminActionDto = new AdminActionDto()
                                         .OfType(AdminActionType.Ban)
                                         .WithPlayerDto(player);

                    adminActionDto.Text         = "Imported from server";
                    adminActionDto.ForumTopicId = await _playersForumsClient.CreateTopicForAdminAction(adminActionDto);

                    await _adminActionsRepository.CreateAdminAction(adminActionDto);
                }
                else
                {
                    var adminActions = await _adminActionsRepository.GetAdminActions(new AdminActionsFilterModel
                    {
                        PlayerId = player.PlayerId,
                        Filter   = AdminActionsFilterModel.FilterType.ActiveBans
                    });

                    if (adminActions.Count(aa => aa.Type == AdminActionType.Ban) == 0)
                    {
                        _logger.LogInformation($"BanFileImport - adding import ban to existing player {player.Username} - {player.Guid} ({player.GameType})");

                        var adminActionDto = new AdminActionDto()
                                             .OfType(AdminActionType.Ban)
                                             .WithPlayerDto(player);

                        adminActionDto.Text         = "Imported from server";
                        adminActionDto.ForumTopicId = await _playersForumsClient.CreateTopicForAdminAction(adminActionDto);

                        await _adminActionsRepository.CreateAdminAction(adminActionDto);
                    }
                }
            }
        }
コード例 #11
0
        public static AdminActionDto OfType(this AdminActionDto adminActionDto, AdminActionType adminActionType)
        {
            adminActionDto.Type = adminActionType;

            return(adminActionDto);
        }