Offload ban checking to BanManager

Remove ban immunity
This commit is contained in:
Chris 2020-11-29 18:10:47 +10:30
parent c334e00042
commit 24a4ab885c
2 changed files with 36 additions and 41 deletions

View file

@ -158,6 +158,38 @@ namespace TShockAPI.DB
}
}
internal bool CheckBan(TSPlayer player)
{
List<string> identifiers = new List<string>
{
$"{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;
}
/// <summary>
/// Determines whether or not a ban is valid
/// </summary>
@ -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);
}
}

View file

@ -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);
}
/// <summary>OnAccountDelete - Internal hook fired on account delete.</summary>
@ -1204,29 +1189,7 @@ namespace TShockAPI
return;
}
List<string> identifiers = new List<string>
{
$"{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);
}
/// <summary>OnLeave - Called when a player leaves the server.</summary>