From 24a4ab885cdbf55397640adbbda1dbc6eebc520e Mon Sep 17 00:00:00 2001 From: Chris <2648373+QuiCM@users.noreply.github.com> Date: Sun, 29 Nov 2020 18:10:47 +1030 Subject: [PATCH] Offload ban checking to BanManager Remove ban immunity --- TShockAPI/DB/BanManager.cs | 36 +++++++++++++++++++++++++++++++-- TShockAPI/TShock.cs | 41 ++------------------------------------ 2 files changed, 36 insertions(+), 41 deletions(-) diff --git a/TShockAPI/DB/BanManager.cs b/TShockAPI/DB/BanManager.cs index d70dfbc9..6122b133 100644 --- a/TShockAPI/DB/BanManager.cs +++ b/TShockAPI/DB/BanManager.cs @@ -158,6 +158,38 @@ namespace TShockAPI.DB } } + internal bool CheckBan(TSPlayer player) + { + List identifiers = new List + { + $"{Identifier.UUID}{player.UUID}", + $"{Identifier.Name}{player.Name}", + $"{Identifier.IP}{player.IP}" + }; + + if (player.Account != null) + { + identifiers.Add($"{Identifier.Account}{player.Account.Name}"); + } + + Ban ban = TShock.Bans.Bans.FirstOrDefault(b => identifiers.Contains(b.Value.Identifier) && TShock.Bans.IsValidBan(b.Value, player)).Value; + + if (ban != null) + { + if (ban.ExpirationDateTime == DateTime.MaxValue) + { + player.Disconnect("You are banned: " + ban.Reason); + return true; + } + + TimeSpan ts = ban.ExpirationDateTime - DateTime.UtcNow; + player.Disconnect($"You are banned: {ban.Reason} ({ban.GetPrettyExpirationString()} remaining)"); + return true; + } + + return false; + } + /// /// Determines whether or not a ban is valid /// @@ -182,8 +214,8 @@ namespace TShockAPI.DB //Only perform validation if the event has not been cancelled before we got here if (args.Valid) { - //We consider a ban to be valid if the start time is before now and the end time is after now, and the player is not immune to bans - args.Valid = (DateTime.UtcNow > args.Ban.BanDateTime && DateTime.UtcNow < args.Ban.ExpirationDateTime) && !args.Player.HasPermission(Permissions.immunetoban); + //We consider a ban to be valid if the start time is before now and the end time is after now + args.Valid = (DateTime.UtcNow > args.Ban.BanDateTime && DateTime.UtcNow < args.Ban.ExpirationDateTime); } } diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index ac7b774f..117a93b9 100644 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -478,22 +478,7 @@ namespace TShockAPI args.Player.Account.KnownIps = JsonConvert.SerializeObject(KnownIps, Formatting.Indented); UserAccounts.UpdateLogin(args.Player.Account); - //Check if this user has a recorded ban on their account - var ban = Bans.Bans.FirstOrDefault(b => b.Value.Identifier == $"{Identifier.Account}{args.Player.Account.Name}" && Bans.IsValidBan(b.Value, args.Player)).Value; - - //If they do and the ban is still valid, kick them - if (ban != null && !args.Player.HasPermission(Permissions.immunetoban)) - { - if (ban.ExpirationDateTime == DateTime.MaxValue) - { - args.Player.Disconnect("You are banned: " + ban.Reason); - } - else - { - TimeSpan ts = ban.ExpirationDateTime - DateTime.UtcNow; - args.Player.Disconnect($"You are banned: {ban.Reason} ({ban.GetPrettyExpirationString()} remaining)"); - } - } + Bans.CheckBan(args.Player); } /// OnAccountDelete - Internal hook fired on account delete. @@ -1204,29 +1189,7 @@ namespace TShockAPI return; } - List identifiers = new List - { - $"{Identifier.UUID}{player.UUID}", - $"{Identifier.Name}{player.Name}", - $"{Identifier.IP}{player.IP}" - }; - - Ban ban = Bans.Bans.FirstOrDefault(b => identifiers.Contains(b.Value.Identifier) && Bans.IsValidBan(b.Value, player)).Value; - - if (ban != null) - { - if (ban.ExpirationDateTime == DateTime.MaxValue) - { - player.Disconnect("You are banned: " + ban.Reason); - } - else - { - TimeSpan ts = ban.ExpirationDateTime - DateTime.UtcNow; - player.Disconnect($"You are banned: {ban.Reason} ({ban.GetPrettyExpirationString()} remaining)"); - } - - args.Handled = true; - } + Bans.CheckBan(player); } /// OnLeave - Called when a player leaves the server.