From 842e161d7e110db636cec8ebdcb89bb8c0a4c08b Mon Sep 17 00:00:00 2001 From: jvyden Date: Fri, 5 Aug 2022 21:12:10 -0400 Subject: [PATCH] Add ability to ban users with a case --- .../Controllers/Admin/AdminUserController.cs | 2 +- .../Pages/Admin/ModerationBanUserPage.cshtml | 30 ----------- .../Admin/ModerationBanUserPage.cshtml.cs | 52 ------------------- .../Pages/Admin/NewCasePage.cshtml.cs | 13 +++-- .../Pages/UserPage.cshtml | 3 +- ProjectLighthouse/Administration/CaseType.cs | 6 +-- .../RepeatingTasks/PerformCaseActionsTask.cs | 4 ++ 7 files changed, 18 insertions(+), 92 deletions(-) delete mode 100644 ProjectLighthouse.Servers.Website/Pages/Admin/ModerationBanUserPage.cshtml delete mode 100644 ProjectLighthouse.Servers.Website/Pages/Admin/ModerationBanUserPage.cshtml.cs diff --git a/ProjectLighthouse.Servers.Website/Controllers/Admin/AdminUserController.cs b/ProjectLighthouse.Servers.Website/Controllers/Admin/AdminUserController.cs index ec8f8481..18bf4566 100644 --- a/ProjectLighthouse.Servers.Website/Controllers/Admin/AdminUserController.cs +++ b/ProjectLighthouse.Servers.Website/Controllers/Admin/AdminUserController.cs @@ -118,7 +118,7 @@ public class AdminUserController : ControllerBase } else { - return this.Redirect($"/moderation/user/{id}/ban"); + return this.Redirect($"/moderation/newCase?type={(int)CaseType.UserBan}&affectedId={id}"); } return this.Redirect("/admin/users"); diff --git a/ProjectLighthouse.Servers.Website/Pages/Admin/ModerationBanUserPage.cshtml b/ProjectLighthouse.Servers.Website/Pages/Admin/ModerationBanUserPage.cshtml deleted file mode 100644 index 8777d36b..00000000 --- a/ProjectLighthouse.Servers.Website/Pages/Admin/ModerationBanUserPage.cshtml +++ /dev/null @@ -1,30 +0,0 @@ -@page "/moderation/user/{id:int}/ban" -@model LBPUnion.ProjectLighthouse.Servers.Website.Pages.Admin.ModeratorBanUserPage - -@{ - Layout = "Layouts/BaseLayout"; - Model.Title = "Ban " + Model.TargetedUser!.Username + "?"; -} - -

Are you sure you want to ban this user?

- -
- @Html.AntiForgeryToken() - -
- - -


- -
- - -


- -
- - -


- -

-
\ No newline at end of file diff --git a/ProjectLighthouse.Servers.Website/Pages/Admin/ModerationBanUserPage.cshtml.cs b/ProjectLighthouse.Servers.Website/Pages/Admin/ModerationBanUserPage.cshtml.cs deleted file mode 100644 index 8f5af0e2..00000000 --- a/ProjectLighthouse.Servers.Website/Pages/Admin/ModerationBanUserPage.cshtml.cs +++ /dev/null @@ -1,52 +0,0 @@ -#nullable enable -using LBPUnion.ProjectLighthouse.Administration; -using LBPUnion.ProjectLighthouse.PlayerData.Profiles; -using LBPUnion.ProjectLighthouse.Servers.Website.Pages.Layouts; -using LBPUnion.ProjectLighthouse.Types; -using Microsoft.AspNetCore.Mvc; -using Microsoft.EntityFrameworkCore; - -namespace LBPUnion.ProjectLighthouse.Servers.Website.Pages.Admin; - -public class ModeratorBanUserPage : BaseLayout -{ - - public User? TargetedUser; - public ModeratorBanUserPage(Database database) : base(database) - {} - - public async Task OnGet([FromRoute] int id) - { - User? user = this.Database.UserFromWebRequest(this.Request); - if (user == null || !user.IsModerator) return this.NotFound(); - - this.TargetedUser = await this.Database.Users.FirstOrDefaultAsync(u => u.UserId == id); - if (this.TargetedUser == null) return this.NotFound(); - - return this.Page(); - } - - public async Task OnPost([FromRoute] int id, string reason, string modNotes, DateTime caseExpires) - { - User? user = this.Database.UserFromWebRequest(this.Request); - if (user == null || !user.IsModerator) return this.NotFound(); - - this.TargetedUser = await this.Database.Users.FirstOrDefaultAsync(u => u.UserId == id); - if (this.TargetedUser == null) return this.NotFound(); - - this.TargetedUser.PermissionLevel = PermissionLevel.Banned; - this.TargetedUser.BannedReason = reason; - - // invalidate all currently active gametokens - this.Database.GameTokens.RemoveRange(this.Database.GameTokens.Where(t => t.UserId == this.TargetedUser.UserId)); - - // invalidate all currently active webtokens - this.Database.WebTokens.RemoveRange(this.Database.WebTokens.Where(t => t.UserId == this.TargetedUser.UserId)); - - // generate & add moderation case - this.Database.Add(ModerationCase.NewBanCase(user.UserId, this.TargetedUser.UserId, reason, modNotes, caseExpires)); - - await this.Database.SaveChangesAsync(); - return this.Redirect($"/user/{this.TargetedUser.UserId}"); - } -} \ No newline at end of file diff --git a/ProjectLighthouse.Servers.Website/Pages/Admin/NewCasePage.cshtml.cs b/ProjectLighthouse.Servers.Website/Pages/Admin/NewCasePage.cshtml.cs index 01253742..b8727a16 100644 --- a/ProjectLighthouse.Servers.Website/Pages/Admin/NewCasePage.cshtml.cs +++ b/ProjectLighthouse.Servers.Website/Pages/Admin/NewCasePage.cshtml.cs @@ -14,28 +14,31 @@ public class NewCasePage : BaseLayout public CaseType Type { get; set; } public int AffectedId { get; set; } - public IActionResult OnGet([FromQuery] CaseType? type) + public IActionResult OnGet([FromQuery] CaseType? type, [FromQuery] int? affectedId) { User? user = this.Database.UserFromWebRequest(this.Request); if (user == null || !user.IsModerator) return this.Redirect("/login"); if (type == null) return this.BadRequest(); + if (affectedId == null) return this.BadRequest(); this.Type = (CaseType)type; + this.AffectedId = (int)affectedId; return this.Page(); } - public async Task OnPost(CaseType? type, string reason, string modNotes, DateTime expires, int affectedId) + public async Task OnPost(CaseType? type, string reason, string modNotes, DateTime expires, int? affectedId) { User? user = this.Database.UserFromWebRequest(this.Request); if (user == null || !user.IsModerator) return this.Redirect("/login"); if (type == null) return this.BadRequest(); + if (affectedId == null) return this.BadRequest(); - // this is fucking ugly and so is the person who wrote it + // this is fucking ugly // if id is invalid then return bad request - if (!(await ((CaseType)type).IsIdValid(affectedId, this.Database))) return this.BadRequest(); + if (!(await ((CaseType)type).IsIdValid((int)affectedId, this.Database))) return this.BadRequest(); ModerationCase @case = new() { @@ -45,7 +48,7 @@ public class NewCasePage : BaseLayout ExpiresAt = expires, CreatedAt = DateTime.Now, CreatorId = user.UserId, - AffectedId = affectedId, + AffectedId = (int)affectedId, }; this.Database.Cases.Add(@case); diff --git a/ProjectLighthouse.Servers.Website/Pages/UserPage.cshtml b/ProjectLighthouse.Servers.Website/Pages/UserPage.cshtml index c0013525..f3d82017 100644 --- a/ProjectLighthouse.Servers.Website/Pages/UserPage.cshtml +++ b/ProjectLighthouse.Servers.Website/Pages/UserPage.cshtml @@ -1,5 +1,6 @@ @page "/user/{userId:int}" @using System.Web +@using LBPUnion.ProjectLighthouse.Administration @using LBPUnion.ProjectLighthouse.Extensions @using LBPUnion.ProjectLighthouse.Localization.StringLists @using LBPUnion.ProjectLighthouse.PlayerData @@ -127,7 +128,7 @@ @if (!Model.ProfileUser.IsBanned) {
- + Ban User diff --git a/ProjectLighthouse/Administration/CaseType.cs b/ProjectLighthouse/Administration/CaseType.cs index c8a4cba1..eac1c574 100644 --- a/ProjectLighthouse/Administration/CaseType.cs +++ b/ProjectLighthouse/Administration/CaseType.cs @@ -43,10 +43,10 @@ public static class CaseTypeExtensions }; } - public static Task IsIdValid(this CaseType type, int affectedId, Database database) + public static async Task IsIdValid(this CaseType type, int affectedId, Database database) { - if (type.AffectsUser()) return database.Users.Has(u => u.UserId == affectedId); - if (type.AffectsLevel()) return database.Slots.Has(u => u.SlotId == affectedId); + if (type.AffectsUser()) return await database.Users.Has(u => u.UserId == affectedId); + if (type.AffectsLevel()) return await database.Slots.Has(u => u.SlotId == affectedId); throw new ArgumentOutOfRangeException(); } diff --git a/ProjectLighthouse/Administration/Maintenance/RepeatingTasks/PerformCaseActionsTask.cs b/ProjectLighthouse/Administration/Maintenance/RepeatingTasks/PerformCaseActionsTask.cs index 8ac529cf..3bd86f13 100644 --- a/ProjectLighthouse/Administration/Maintenance/RepeatingTasks/PerformCaseActionsTask.cs +++ b/ProjectLighthouse/Administration/Maintenance/RepeatingTasks/PerformCaseActionsTask.cs @@ -63,6 +63,10 @@ public class PerformCaseActionsTask : IRepeatingTask case CaseType.UserBan: { user!.PermissionLevel = PermissionLevel.Banned; + user.BannedReason = @case.Reason; + + database.GameTokens.RemoveRange(database.GameTokens.Where(t => t.UserId == user.UserId)); + database.WebTokens.RemoveRange(database.WebTokens.Where(t => t.UserId == user.UserId)); break; } case CaseType.UserCommentsDisabled: break;