From cdcc03fdc128b799a2146187c6622b6c929476b8 Mon Sep 17 00:00:00 2001 From: jvyden Date: Wed, 27 Jul 2022 17:34:59 -0400 Subject: [PATCH] Add case generator for bans, allow mods to ban --- .../Controllers/Admin/AdminUserController.cs | 6 +-- .../Pages/Admin/AdminBanUserPage.cshtml | 20 ------- .../Pages/Admin/ModerationBanUserPage.cshtml | 25 +++++++++ ...tml.cs => ModerationBanUserPage.cshtml.cs} | 13 +++-- .../Pages/UserPage.cshtml | 2 +- ProjectLighthouse/Administration/CaseType.cs | 4 +- .../SetUserPermissionLevelCommands.cs | 8 --- .../Administration/ModerationCase.cs | 54 ++++++++++++++++--- 8 files changed, 85 insertions(+), 47 deletions(-) delete mode 100644 ProjectLighthouse.Servers.Website/Pages/Admin/AdminBanUserPage.cshtml create mode 100644 ProjectLighthouse.Servers.Website/Pages/Admin/ModerationBanUserPage.cshtml rename ProjectLighthouse.Servers.Website/Pages/Admin/{AdminBanUserPage.cshtml.cs => ModerationBanUserPage.cshtml.cs} (77%) diff --git a/ProjectLighthouse.Servers.Website/Controllers/Admin/AdminUserController.cs b/ProjectLighthouse.Servers.Website/Controllers/Admin/AdminUserController.cs index c6a7da4c..949aeade 100644 --- a/ProjectLighthouse.Servers.Website/Controllers/Admin/AdminUserController.cs +++ b/ProjectLighthouse.Servers.Website/Controllers/Admin/AdminUserController.cs @@ -24,7 +24,7 @@ public class AdminUserController : ControllerBase public async Task UnbanUser([FromRoute] int id) { User? user = this.database.UserFromWebRequest(this.Request); - if (user == null || !user.IsAdmin) return this.NotFound(); + if (user == null || !user.IsModerator) return this.NotFound(); User? targetedUser = await this.database.Users.FirstOrDefaultAsync(u => u.UserId == id); if (targetedUser == null) return this.NotFound(); @@ -42,7 +42,7 @@ public class AdminUserController : ControllerBase [HttpGet("wipePlanets")] public async Task WipePlanets([FromRoute] int id) { User? user = this.database.UserFromWebRequest(this.Request); - if (user == null || !user.IsAdmin) return this.NotFound(); + if (user == null || !user.IsModerator) return this.NotFound(); User? targetedUser = await this.database.Users.FirstOrDefaultAsync(u => u.UserId == id); if (targetedUser == null) return this.NotFound(); @@ -118,7 +118,7 @@ public class AdminUserController : ControllerBase } else { - return this.Redirect($"/admin/user/{id}/ban"); + return this.Redirect($"/moderation/user/{id}/ban"); } return this.Redirect("/admin/users"); diff --git a/ProjectLighthouse.Servers.Website/Pages/Admin/AdminBanUserPage.cshtml b/ProjectLighthouse.Servers.Website/Pages/Admin/AdminBanUserPage.cshtml deleted file mode 100644 index e5138ee5..00000000 --- a/ProjectLighthouse.Servers.Website/Pages/Admin/AdminBanUserPage.cshtml +++ /dev/null @@ -1,20 +0,0 @@ -@page "/admin/user/{id:int}/ban" -@model LBPUnion.ProjectLighthouse.Servers.Website.Pages.Admin.AdminBanUserPage - -@{ - 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 b/ProjectLighthouse.Servers.Website/Pages/Admin/ModerationBanUserPage.cshtml new file mode 100644 index 00000000..71e882af --- /dev/null +++ b/ProjectLighthouse.Servers.Website/Pages/Admin/ModerationBanUserPage.cshtml @@ -0,0 +1,25 @@ +@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/AdminBanUserPage.cshtml.cs b/ProjectLighthouse.Servers.Website/Pages/Admin/ModerationBanUserPage.cshtml.cs similarity index 77% rename from ProjectLighthouse.Servers.Website/Pages/Admin/AdminBanUserPage.cshtml.cs rename to ProjectLighthouse.Servers.Website/Pages/Admin/ModerationBanUserPage.cshtml.cs index 5a7c99c3..80ef016c 100644 --- a/ProjectLighthouse.Servers.Website/Pages/Admin/AdminBanUserPage.cshtml.cs +++ b/ProjectLighthouse.Servers.Website/Pages/Admin/ModerationBanUserPage.cshtml.cs @@ -8,17 +8,17 @@ using Microsoft.EntityFrameworkCore; namespace LBPUnion.ProjectLighthouse.Servers.Website.Pages.Admin; -public class AdminBanUserPage : BaseLayout +public class ModeratorBanUserPage : BaseLayout { public User? TargetedUser; - public AdminBanUserPage(Database database) : base(database) + public ModeratorBanUserPage(Database database) : base(database) {} public async Task OnGet([FromRoute] int id) { User? user = this.Database.UserFromWebRequest(this.Request); - if (user == null || !user.IsAdmin) return this.NotFound(); + 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(); @@ -26,10 +26,10 @@ public class AdminBanUserPage : BaseLayout return this.Page(); } - public async Task OnPost([FromRoute] int id, string reason) + public async Task OnPost([FromRoute] int id, string reason, DateTime caseExpires) { User? user = this.Database.UserFromWebRequest(this.Request); - if (user == null || !user.IsAdmin) return this.NotFound(); + 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(); @@ -42,6 +42,9 @@ public class AdminBanUserPage : BaseLayout // 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, caseExpires)); await this.Database.SaveChangesAsync(); return this.Redirect($"/user/{this.TargetedUser.UserId}"); diff --git a/ProjectLighthouse.Servers.Website/Pages/UserPage.cshtml b/ProjectLighthouse.Servers.Website/Pages/UserPage.cshtml index fae97ecd..2f488bcc 100644 --- a/ProjectLighthouse.Servers.Website/Pages/UserPage.cshtml +++ b/ProjectLighthouse.Servers.Website/Pages/UserPage.cshtml @@ -126,7 +126,7 @@ @if (!Model.ProfileUser.IsBanned) {
- + Ban User diff --git a/ProjectLighthouse/Administration/CaseType.cs b/ProjectLighthouse/Administration/CaseType.cs index 929ea0eb..3b8eabb5 100644 --- a/ProjectLighthouse/Administration/CaseType.cs +++ b/ProjectLighthouse/Administration/CaseType.cs @@ -36,10 +36,10 @@ public static class CaseTypeExtensions { return type switch { + CaseType.UserDeletion => false, // you cant get a user from a deleted id CaseType.UserSilence => true, CaseType.UserRestriction => true, CaseType.UserBan => true, - CaseType.UserDeletion => true, CaseType.UserCommentsDisabled => true, CaseType.UserDetailsEdited => true, CaseType.UserEarthDeletion => true, @@ -51,7 +51,7 @@ public static class CaseTypeExtensions { return type switch { - CaseType.LevelDeletion => true, + CaseType.LevelDeletion => false, // you cant get a slot from a deleted id CaseType.LevelLock => true, CaseType.LevelCommentsDisabled => true, CaseType.LevelDetailsEdited => true, diff --git a/ProjectLighthouse/Administration/Maintenance/SetUserPermissionLevelCommands.cs b/ProjectLighthouse/Administration/Maintenance/SetUserPermissionLevelCommands.cs index cd39223c..d4309ceb 100644 --- a/ProjectLighthouse/Administration/Maintenance/SetUserPermissionLevelCommands.cs +++ b/ProjectLighthouse/Administration/Maintenance/SetUserPermissionLevelCommands.cs @@ -67,14 +67,6 @@ public class SetUserModeratorCommand : SetUserPermissionLevelCommand public override string[] Aliases() => new[] { "make-moderator", }; } -public class BanUserCommand : SetUserPermissionLevelCommand -{ - public BanUserCommand() : base(PermissionLevel.Banned) - {} - public override string Name() => "Ban User"; - public override string[] Aliases() => new[] { "ban", }; -} - public class DemoteUserCommand : SetUserPermissionLevelCommand { public DemoteUserCommand() : base(PermissionLevel.Default) diff --git a/ProjectLighthouse/Administration/ModerationCase.cs b/ProjectLighthouse/Administration/ModerationCase.cs index 52549c5a..d2714c8d 100644 --- a/ProjectLighthouse/Administration/ModerationCase.cs +++ b/ProjectLighthouse/Administration/ModerationCase.cs @@ -45,12 +45,50 @@ public class ModerationCase } #endregion - public static ModerationCase NewTeamPickCase(int caseCreator, int slotId, bool added) => new() - { - CaseType = added ? CaseType.LevelTeamPickAdded : CaseType.LevelTeamPickRemoved, - CaseDescription = "", - CaseCreatorId = caseCreator, - CaseCreated = DateTime.Now, - AffectedId = slotId, - }; + #region Case creators + #region Level + public static ModerationCase NewTeamPickCase(int caseCreator, int slotId, bool added) + => new() + { + CaseType = added ? CaseType.LevelTeamPickAdded : CaseType.LevelTeamPickRemoved, + CaseDescription = "", + CaseCreatorId = caseCreator, + CaseCreated = DateTime.Now, + AffectedId = slotId, + }; + + public static ModerationCase NewLevelDeletionCase(int caseCreator, int slotId) + => new() + { + CaseType = CaseType.LevelDeletion, + CaseDescription = "Deleted slot ID " + slotId, + CaseCreatorId = caseCreator, + CaseCreated = DateTime.Now, + }; + #endregion + + #region User + public static ModerationCase NewBanCase(int caseCreator, int userId, string reason, DateTime caseExpires) + => new() + { + CaseType = CaseType.UserBan, + CaseDescription = $"Banned for reason '{reason}'", + CaseCreatorId = caseCreator, + CaseCreated = DateTime.Now, + CaseExpires = caseExpires, + AffectedId = userId, + }; + + public static ModerationCase NewAccountDeletionCase(int caseCreator, int userId) + => new() + { + CaseType = CaseType.UserDeletion, + CaseDescription = "Deleted user ID " + userId, + CaseCreatorId = caseCreator, + CaseCreated = DateTime.Now, + }; + + #endregion + #endregion + } \ No newline at end of file