Skip to content

Commit

Permalink
Ban template system
Browse files Browse the repository at this point in the history
  • Loading branch information
PJB3005 committed Jun 23, 2024
1 parent 5418e1a commit 4103020
Show file tree
Hide file tree
Showing 9 changed files with 483 additions and 75 deletions.
31 changes: 31 additions & 0 deletions SS14.Admin/Helpers/BanExemptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Content.Server.Database;

namespace SS14.Admin.Helpers;

public static class BanExemptions
{
public static (ServerBanExemptFlags Value, string DisplayName)[] GetExemptions()
{
return
[
(ServerBanExemptFlags.Datacenter, "Datacenter"),
(ServerBanExemptFlags.IP, "Only matches IP"),
(ServerBanExemptFlags.BlacklistedRange, "Blacklisted range"),
];
}

public static ServerBanExemptFlags GetExemptionFromForm(IFormCollection formCollection)
{
var flags = ServerBanExemptFlags.None;

foreach (var (value, _) in GetExemptions())
{
if (formCollection.TryGetValue($"exemption_{value}", out var checkValue) && checkValue == "on")
{
flags |= value;
}
}

return flags;
}
}
15 changes: 15 additions & 0 deletions SS14.Admin/Helpers/NoteSeverityHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Content.Shared.Database;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace SS14.Admin.Helpers;

public static class NoteSeverityHelper
{
public static SelectListItem[] SeverityItems =>
[
new SelectListItem("None", NoteSeverity.None.ToString()),
new SelectListItem("Minor", NoteSeverity.Minor.ToString()),
new SelectListItem("Medium", NoteSeverity.Medium.ToString()),
new SelectListItem("High", NoteSeverity.High.ToString()),
];
}
23 changes: 23 additions & 0 deletions SS14.Admin/Pages/Bans/BanTemplates/Create.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
@page
@model SS14.Admin.Pages.Bans.BanTemplates.Create


@{
ViewData["Title"] = "Create new ban template";
}

<partial name="Shared/StatusMessageAlert" />

<form method="post" asp-page-handler="Create" class="container">
<h1>@ViewData["Title"]</h1>

<div asp-validation-summary="All" class="text-danger"></div>

<div class="form-group row">
<label asp-for="Title" class="col-sm-3 col-form-label">Title</label>
<div class="col-sm-9">
<input asp-for="Title" class="form-control">
</div>
</div>
<input type="submit" value="Create" class="btn btn-primary">
</form>
38 changes: 38 additions & 0 deletions SS14.Admin/Pages/Bans/BanTemplates/Create.cshtml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Content.Server.Database;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace SS14.Admin.Pages.Bans.BanTemplates;

[Authorize(Roles = "BAN")]
[ValidateAntiForgeryToken]
public class Create(PostgresServerDbContext dbContext) : PageModel
{
[BindProperty] public string Title { get; set; } = "";

public void OnGet()
{

}

public async Task<IActionResult> OnPostCreateAsync()
{
if (string.IsNullOrWhiteSpace(Title))
{
TempData["StatusMessage"] = "Error: name must not be empty";
return Page();
}

var template = new BanTemplate
{
Title = Title,
};

dbContext.BanTemplate.Add(template);

await dbContext.SaveChangesAsync();

return RedirectToPage("View", new { id = template.Id });
}
}
87 changes: 87 additions & 0 deletions SS14.Admin/Pages/Bans/BanTemplates/View.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
@page
@using SS14.Admin.Helpers
@model SS14.Admin.Pages.Bans.BanTemplates.View

@{
ViewData["Title"] = $"Ban template: {Model.Template.Title}";
}

<partial name="Shared/StatusMessageAlert" />

<form method="post" asp-page-handler="Edit" class="container">
<h1>@ViewData["Title"]</h1>

<div asp-validation-summary="All" class="text-danger"></div>

<div class="form-group row">
<label asp-for="Input.Title" class="col-sm-3 col-form-label">Name</label>
<div class="col-sm-9">
<input asp-for="Input.Title" class="form-control">
</div>
</div>

<div class="form-group row">
<label asp-for="Input.LengthMinutes" class="col-sm-3 col-form-label">Minutes (0: permanent)</label>
<div class="col-sm-9">
<div class="row">
<div class="col-lg-6">
<input asp-for="Input.LengthMinutes" id="durationInput" class="form-control">
</div>
<div class="col-lg-6">
<div class="row g-0">
<button data-duration="60" data-suffix="h" class="durationButton btn btn-secondary col"></button>
<button data-duration="1440" data-suffix="d" class="durationButton btn btn-secondary col"></button>
<button data-duration="10080" data-suffix="w" class="durationButton btn btn-secondary col"></button>
<button data-duration="43200" data-suffix="M" class="durationButton btn btn-secondary col"></button>
</div>
</div>
</div>
</div>
</div>

<div class="form-group row">
<label asp-for="Input.Reason" class="col-sm-3 col-form-label">Reason</label>
<div class="col-sm-9">
<textarea asp-for="Input.Reason" class="form-control"></textarea>
</div>
</div>

<div class="form-group row">
<label asp-for="Input.Reason" class="col-sm-3 col-form-label">Severity</label>
<div class="col-sm-9">
<select asp-for="Input.Severity" class="form-control" asp-items="@NoteSeverityHelper.SeverityItems">
</select>
</div>
</div>

<div class="form-group row">
<legend class="col-form-label col-sm-3 float-sm-left pt-0">Settings</legend>
<div class="col-sm-9 mb-3">
<div class="form-check">
<input asp-for="Input.AutoDelete" class="form-check-input"/>
<label asp-for="Input.AutoDelete" class="form-check-label"></label>
</div>
<div class="form-check">
<input asp-for="Input.Hidden" class="form-check-input"/>
<label asp-for="Input.Hidden" class="form-check-label"></label>
</div>
</div>
</div>

<div class="form-group row">
<legend class="col-form-label col-sm-3 float-sm-left pt-0">Exemption flags</legend>
<div class="col-sm-9 mb-3">
@foreach (var (value, display) in BanExemptions.GetExemptions())
{
<div class="form-check">
<input id="exemption_@value" name="exemption_@value" class="form-check-input" type="checkbox" checked="@((Model.Template.ExemptFlags & value) != 0)" />
<label for="exemption_@value" class="form-check-label">@display</label>
</div>
}
</div>
</div>

<input type="submit" asp-route-id="@Model.Template.Id" asp-page-handler="Edit" value="Save" class="btn btn-primary">
</form>

<partial name="Shared/BanTimeButtonScript" />
88 changes: 88 additions & 0 deletions SS14.Admin/Pages/Bans/BanTemplates/View.cshtml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System.ComponentModel.DataAnnotations;
using Content.Server.Database;
using Content.Shared.Database;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using SS14.Admin.Helpers;

namespace SS14.Admin.Pages.Bans.BanTemplates;

[Authorize(Roles = "BAN")]
[ValidateAntiForgeryToken]
public class View(PostgresServerDbContext dbContext) : PageModel
{
public BanTemplate Template = default!;

[BindProperty] public InputModel Input { get; set; } = new();

public sealed class InputModel
{
[Required] public string Title { get; set; } = "";
public int LengthMinutes { get; set; }
public string? Reason { get; set; }

[Display(Name = "Delete ban when expired")]
public bool AutoDelete { get; set; }

[Display(Name = "Hidden from player")] public bool Hidden { get; set; }
public NoteSeverity Severity { get; set; }
}

public async Task<IActionResult> OnGetAsync(int id)
{
var template = await dbContext.BanTemplate.SingleOrDefaultAsync(t => t.Id == id);
if (template == null)
return NotFound();

Input.Hidden = template.Hidden;
Input.Severity = template.Severity;
Input.Reason = template.Reason;
Input.Title = template.Title;
Input.LengthMinutes = (int)template.Length.TotalMinutes;
Input.AutoDelete = template.AutoDelete;

Template = template;
return Page();
}

public async Task<IActionResult> OnPostEditAsync(int id)
{
if (!ModelState.IsValid)
return RedirectToPage(new { id });

var template = await dbContext.BanTemplate.SingleOrDefaultAsync(t => t.Id == id);
if (template == null)
return NotFound();

if (string.IsNullOrWhiteSpace(Input.Title))
{
TempData["StatusMessage"] = "Error: title is empty";
return RedirectToPage(new { id });
}

template.Title = Input.Title;
template.Length = TimeSpan.FromMinutes(Input.LengthMinutes);
template.Reason = Input.Reason ?? "";
template.AutoDelete = Input.AutoDelete;
template.Hidden = Input.Hidden;
template.Severity = Input.Severity;

var flags = ServerBanExemptFlags.None;
foreach (var (value, _) in BanExemptions.GetExemptions())
{
if (Request.Form.TryGetValue($"exemption_{value}", out var checkValue) && checkValue == "on")
{
flags |= value;
}
}

template.ExemptFlags = flags;

await dbContext.SaveChangesAsync();

return RedirectToPage(new { id });
}
}
Loading

0 comments on commit 4103020

Please sign in to comment.