Ban rewrite and various other adjustments

This commit is contained in:
Chris 2020-11-26 17:20:19 +10:30
parent ce523e1436
commit cde4cc5f04
11 changed files with 742 additions and 276 deletions

View file

@ -638,25 +638,25 @@ namespace TShockAPI
if (TShock.Bans.InsertBan(identifier, reason, args.TokenData.Username, startDate, endDate) != null)
{
TSPlayer player = null;
if (identifier.StartsWith(Ban.Identifiers.IP))
if (identifier.StartsWith(Identifiers.IP))
{
player = TShock.Players.FirstOrDefault(p => p.IP == identifier.Substring(Ban.Identifiers.IP.Length));
player = TShock.Players.FirstOrDefault(p => p.IP == identifier.Substring(Identifiers.IP.Length));
}
else if (identifier.StartsWith(Ban.Identifiers.Name))
else if (identifier.StartsWith(Identifiers.Name))
{
//Character names may not necessarily be unique, so kick all matches
foreach (var ply in TShock.Players.Where(p => p.Name == identifier.Substring(Ban.Identifiers.Name.Length)))
foreach (var ply in TShock.Players.Where(p => p.Name == identifier.Substring(Identifiers.Name.Length)))
{
ply.Kick(reason, true);
}
}
else if (identifier.StartsWith(Ban.Identifiers.Account))
else if (identifier.StartsWith(Identifiers.Account))
{
player = TShock.Players.FirstOrDefault(p => p.Account?.Name == identifier.Substring(Ban.Identifiers.Account.Length));
player = TShock.Players.FirstOrDefault(p => p.Account?.Name == identifier.Substring(Identifiers.Account.Length));
}
else if (identifier.StartsWith(Ban.Identifiers.UUID))
else if (identifier.StartsWith(Identifiers.UUID))
{
player = TShock.Players.FirstOrDefault(p => p.UUID == identifier.Substring(Ban.Identifiers.UUID.Length));
player = TShock.Players.FirstOrDefault(p => p.UUID == identifier.Substring(Identifiers.UUID.Length));
}
if (player != null)
@ -673,18 +673,23 @@ namespace TShockAPI
[Description("Delete an existing ban entry.")]
[Route("/v3/bans/destroy")]
[Permission(RestPermissions.restmanagebans)]
[Noun("identifier", true, "The identifier of the ban to delete.", typeof(String))]
[Noun("uniqueId", true, "The unique ID of the ban to delete.", typeof(String))]
[Noun("fullDelete", false, "Whether or not to completely remove the ban from the system.", typeof(bool))]
[Token]
private object BanDestroyV3(RestRequestArgs args)
{
string identifier = args.Parameters["identifier"];
if (string.IsNullOrWhiteSpace(identifier))
return RestMissingParam("identifier");
string id = args.Parameters["uniqueId"];
if (string.IsNullOrWhiteSpace(id))
return RestMissingParam("uniqueId");
if (!int.TryParse(id, out int uniqueId))
{
return RestInvalidParam("uniqueId");
}
bool.TryParse(args.Parameters["fullDelete"], out bool fullDelete);
if (TShock.Bans.RemoveBan(identifier, fullDelete))
if (TShock.Bans.RemoveBan(uniqueId, fullDelete))
{
return RestResponse("Ban removed.");
}
@ -695,15 +700,20 @@ namespace TShockAPI
[Description("View the details of a specific ban.")]
[Route("/v3/bans/read")]
[Permission(RestPermissions.restviewbans)]
[Noun("identifier", true, "The identifier to search for.", typeof(String))]
[Noun("uniqueId", true, "The unique ID to search for.", typeof(String))]
[Token]
private object BanInfoV3(RestRequestArgs args)
{
string identifier = args.Parameters["identifier"];
if (string.IsNullOrWhiteSpace(identifier))
return RestMissingParam("identifier");
string id = args.Parameters["uniqueId"];
if (string.IsNullOrWhiteSpace(id))
return RestMissingParam("uniqueId");
Ban ban = TShock.Bans.GetBanByIdentifier(identifier);
if (!int.TryParse(id, out int uniqueId))
{
return RestInvalidParam("uniqueId");
}
Ban ban = TShock.Bans.GetBanById(uniqueId);
if (ban == null)
{
@ -726,7 +736,7 @@ namespace TShockAPI
[Token]
private object BanListV3(RestRequestArgs args)
{
IEnumerable<Ban> bans = TShock.Bans.GetAllBans();
IEnumerable<Ban> bans = TShock.Bans.Bans.Select(kvp => kvp.Value);
var banList = new ArrayList();
foreach (var ban in bans)