Skip to content

Commit

Permalink
Fix mass ban system
Browse files Browse the repository at this point in the history
Previous code didn't work with exempt flags properly.
  • Loading branch information
PJB3005 committed Aug 5, 2024
1 parent cf20c33 commit 8c7ed08
Showing 1 changed file with 30 additions and 33 deletions.
63 changes: 30 additions & 33 deletions SS14.Admin/Pages/Bans/CreateMassBan.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ namespace SS14.Admin.Pages.Bans
[ValidateAntiForgeryToken]
public class CreateMassBanModel : PageModel
{
private static readonly CsvHelper.Configuration.CsvConfiguration CsvConfig = new(CultureInfo.InvariantCulture)
{
Delimiter = "\t", // Specify tab as the delimiter
HasHeaderRecord = true, // TSV files have a header row
MissingFieldFound = null // Ignore missing fields
};

private readonly PostgresServerDbContext _dbContext;
private readonly BanHelper _banHelper;

Expand All @@ -24,9 +31,9 @@ public CreateMassBanModel(PostgresServerDbContext dbContext, BanHelper banHelper
public int BanCount { get; private set; }

public record TsvEntry(
string UserId,
string Address,
string Hwid,
string? UserId,
string? Address,
string? Hwid,
string Reason,
bool Datacenter,
bool BlacklistedRange
Expand All @@ -53,17 +60,15 @@ public async Task<IActionResult> OnPostAsync(IFormFile file)

foreach (var entry in entries)
{
var ExemptFlags = BanExemptions.GetExemptionFromForm(Request.Form);

var ban = new ServerBan();

var ipAddr = entry.Address;
var hwid = entry.Hwid;

ban.ExemptFlags = ExemptFlags;
// ban.AutoDelete = Input.AutoDelete; // Uncomment and use if necessary
//ban.Hidden = Input.Hidden;
//ban.Severity = Input.Severity;
if (entry.Datacenter)
ban.ExemptFlags |= ServerBanExemptFlags.Datacenter;
if (entry.BlacklistedRange)
ban.ExemptFlags |= ServerBanExemptFlags.BlacklistedRange;

var error = await _banHelper.FillBanCommon(
ban,
Expand Down Expand Up @@ -98,33 +103,25 @@ private List<TsvEntry> ParseTsv(StreamReader reader)
{
var records = new List<TsvEntry>();

var config = new CsvHelper.Configuration.CsvConfiguration(CultureInfo.InvariantCulture)
{
Delimiter = "\t", // Specify tab as the delimiter
HasHeaderRecord = true, // TSV files have a header row
MissingFieldFound = null // Ignore missing fields
};
using var csvReader = new CsvReader(reader, CsvConfig);

using (var csvReader = new CsvReader(reader, config))
if (!csvReader.Read() || !csvReader.ReadHeader())
{
if (!csvReader.Read() || !csvReader.ReadHeader())
{
throw new InvalidDataException("The TSV file is missing a header.");
}
throw new InvalidDataException("The TSV file is missing a header.");
}

while (csvReader.Read())
{
var record = new TsvEntry(
csvReader.GetField<string>("user_id"),
csvReader.GetField<string>("address"),
csvReader.GetField<string>("hwid"),
csvReader.GetField<string>("reason"),
csvReader.GetField<bool>("datacenter"),
csvReader.GetField<bool>("blacklisted_range")
);
records.Add(record);
BanCount += 1;
}
while (csvReader.Read())
{
var record = new TsvEntry(
csvReader.GetField<string>("user_id"),
csvReader.GetField<string>("address"),
csvReader.GetField<string>("hwid"),
csvReader.GetField<string>("reason") ?? "",
csvReader.GetField<bool>("datacenter"),
csvReader.GetField<bool>("blacklisted_range")
);
records.Add(record);
BanCount += 1;
}

return records;
Expand Down

1 comment on commit 8c7ed08

@Geekyhobo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code is perfect

Please sign in to comment.