Merge pull request #2271 from DeathCradle/h/fix-typo

Fix ban system
This commit is contained in:
Lucas Nicodemus 2021-04-12 11:02:48 -07:00 committed by GitHub
commit 93e13c167b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 58 additions and 23 deletions

View file

@ -16,7 +16,7 @@ This is the rolling changelog for TShock for Terraria. Use past tense when addin
* This could be you! * This could be you!
## TShock 4.5.0.1 ## TShock 4.5.0.1
* Fixed typo in conversion from old to new ban system for MySQL hosted ban databases. (@DeathCradle) * Fixed conversion from old to new ban system for MySQL hosted ban databases. (@DeathCradle)
## TShock 4.5.0 ## TShock 4.5.0
* Updated OTAPI and TSAPI to Terraria 1.4.2.1. (@Stealownz, @DeathCradle) * Updated OTAPI and TSAPI to Terraria 1.4.2.1. (@Stealownz, @DeathCradle)

View file

@ -37,18 +37,7 @@ namespace TShockAPI.DB
/// <summary> /// <summary>
/// Readonly dictionary of Bans, keyed on ban ticket number. /// Readonly dictionary of Bans, keyed on ban ticket number.
/// </summary> /// </summary>
public ReadOnlyDictionary<int, Ban> Bans public ReadOnlyDictionary<int, Ban> Bans => new ReadOnlyDictionary<int, Ban>(_bans);
{
get
{
if (_bans == null)
{
_bans = RetrieveAllBans().ToDictionary(b => b.TicketNumber);
}
return new ReadOnlyDictionary<int, Ban>(_bans);
}
}
/// <summary> /// <summary>
/// Event invoked when a ban is checked for validity /// Event invoked when a ban is checked for validity
@ -93,12 +82,24 @@ namespace TShockAPI.DB
throw new Exception("Could not find a database library (probably Sqlite3.dll)"); throw new Exception("Could not find a database library (probably Sqlite3.dll)");
} }
EnsureBansCollection();
TryConvertBans(); TryConvertBans();
OnBanValidate += BanValidateCheck; OnBanValidate += BanValidateCheck;
OnBanPreAdd += BanAddedCheck; OnBanPreAdd += BanAddedCheck;
} }
/// <summary>
/// Ensures the <see cref="_bans"/> collection is ready to use.
/// </summary>
private void EnsureBansCollection()
{
if (_bans == null)
{
_bans = RetrieveAllBans().ToDictionary(b => b.TicketNumber);
}
}
/// <summary> /// <summary>
/// Converts bans from the old ban system to the new. /// Converts bans from the old ban system to the new.
/// </summary> /// </summary>
@ -116,6 +117,7 @@ namespace TShockAPI.DB
if (res != 0) if (res != 0)
{ {
var bans = new List<BanPreAddEventArgs>();
using (var reader = database.QueryReader("SELECT * FROM Bans")) using (var reader = database.QueryReader("SELECT * FROM Bans"))
{ {
while (reader.Read()) while (reader.Read())
@ -140,22 +142,46 @@ namespace TShockAPI.DB
if (!string.IsNullOrWhiteSpace(ip)) if (!string.IsNullOrWhiteSpace(ip))
{ {
InsertBan($"{Identifier.IP}{ip}", reason, banningUser, start, end); bans.Add(new BanPreAddEventArgs
{
Identifier = $"{Identifier.IP}{ip}",
Reason = reason,
BanningUser = banningUser,
BanDateTime = start,
ExpirationDateTime = end
});
} }
if (!string.IsNullOrWhiteSpace(account)) if (!string.IsNullOrWhiteSpace(account))
{ {
InsertBan($"{Identifier.Account}{account}", reason, banningUser, start, end); bans.Add(new BanPreAddEventArgs
{
Identifier = $"{Identifier.Account}{account}",
Reason = reason,
BanningUser = banningUser,
BanDateTime = start,
ExpirationDateTime = end
});
} }
if (!string.IsNullOrWhiteSpace(uuid)) if (!string.IsNullOrWhiteSpace(uuid))
{ {
InsertBan($"{Identifier.UUID}{uuid}", reason, banningUser, start, end); bans.Add(new BanPreAddEventArgs
{
Identifier = $"{Identifier.UUID}{uuid}",
Reason = reason,
BanningUser = banningUser,
BanDateTime = start,
ExpirationDateTime = end
});
} }
} }
} }
database.Query("DROP TABLE 'Bans'"); foreach (var ban in bans)
InsertBan(ban);
database.Query("DROP TABLE Bans");
} }
} }
@ -232,7 +258,7 @@ namespace TShockAPI.DB
args.Message = args.Valid ? null : "a current ban for this identifier already exists."; args.Message = args.Valid ? null : "a current ban for this identifier already exists.";
} }
} }
/// <summary> /// <summary>
/// Adds a new ban for the given identifier. Returns a Ban object if the ban was added, else null /// Adds a new ban for the given identifier. Returns a Ban object if the ban was added, else null
/// </summary> /// </summary>
@ -252,7 +278,16 @@ namespace TShockAPI.DB
BanDateTime = fromDate, BanDateTime = fromDate,
ExpirationDateTime = toDate ExpirationDateTime = toDate
}; };
return InsertBan(args);
}
/// <summary>
/// Adds a new ban for the given data. Returns a Ban object if the ban was added, else null
/// </summary>
/// <param name="args">A predefined instance of <see cref="BanPreAddEventArgs"/></param>
/// <returns></returns>
public AddBanResult InsertBan(BanPreAddEventArgs args)
{
OnBanPreAdd?.Invoke(this, args); OnBanPreAdd?.Invoke(this, args);
if (!args.Valid) if (!args.Valid)
@ -265,21 +300,21 @@ namespace TShockAPI.DB
if (database.GetSqlType() == SqlType.Mysql) if (database.GetSqlType() == SqlType.Mysql)
{ {
query += "SELECT CAST(LAST_INSERT_ID() as INT);"; query += "SELECT LAST_INSERT_ID();";
} }
else else
{ {
query += "SELECT CAST(last_insert_rowid() as INT);"; query += "SELECT CAST(last_insert_rowid() as INT);";
} }
int ticketId = database.QueryScalar<int>(query, identifier, reason, banningUser, fromDate.Ticks, toDate.Ticks); int ticketId = database.QueryScalar<int>(query, args.Identifier, args.Reason, args.BanningUser, args.BanDateTime.Ticks, args.ExpirationDateTime.Ticks);
if (ticketId == 0) if (ticketId == 0)
{ {
return new AddBanResult { Message = "Database insert failed." }; return new AddBanResult { Message = "Database insert failed." };
} }
Ban b = new Ban(ticketId, identifier, reason, banningUser, fromDate, toDate); Ban b = new Ban(ticketId, args.Identifier, args.Reason, args.BanningUser, args.BanDateTime, args.ExpirationDateTime);
_bans.Add(ticketId, b); _bans.Add(ticketId, b);
OnBanPostAdd?.Invoke(this, new BanEventArgs { Ban = b }); OnBanPostAdd?.Invoke(this, new BanEventArgs { Ban = b });

View file

@ -1637,7 +1637,7 @@ namespace TShockAPI
if (force) if (force)
{ {
TShock.Bans.InsertBan($"{Identifier.IP}{IP}", reason, adminUserName, DateTime.UtcNow, DateTime.MaxValue); TShock.Bans.InsertBan($"{Identifier.IP}{IP}", reason, adminUserName, DateTime.UtcNow, DateTime.MaxValue);
TShock.Bans.InsertBan($"{Identifier.IP}{UUID}", reason, adminUserName, DateTime.UtcNow, DateTime.MaxValue); TShock.Bans.InsertBan($"{Identifier.UUID}{UUID}", reason, adminUserName, DateTime.UtcNow, DateTime.MaxValue);
if (Account != null) if (Account != null)
{ {
TShock.Bans.InsertBan($"{Identifier.Account}{Account.Name}", reason, adminUserName, DateTime.UtcNow, DateTime.MaxValue); TShock.Bans.InsertBan($"{Identifier.Account}{Account.Name}", reason, adminUserName, DateTime.UtcNow, DateTime.MaxValue);

View file

@ -70,7 +70,7 @@
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL"> <Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath> <HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference> </Reference>
<Reference Include="OTAPI="> <Reference Include="OTAPI">
<SpecificVersion>False</SpecificVersion> <SpecificVersion>False</SpecificVersion>
<HintPath Condition="Exists('..\TerrariaServerAPI\TerrariaServerAPI\bin\$(ConfigurationName)\OTAPI.dll')">..\TerrariaServerAPI\TerrariaServerAPI\bin\$(ConfigurationName)\OTAPI.dll</HintPath> <HintPath Condition="Exists('..\TerrariaServerAPI\TerrariaServerAPI\bin\$(ConfigurationName)\OTAPI.dll')">..\TerrariaServerAPI\TerrariaServerAPI\bin\$(ConfigurationName)\OTAPI.dll</HintPath>
</Reference> </Reference>