Add ability to ban users with a case

This commit is contained in:
jvyden 2022-08-05 21:12:10 -04:00
parent d540a54cd8
commit 842e161d7e
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
7 changed files with 18 additions and 92 deletions

View file

@ -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");

View file

@ -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 + "?";
}
<p>Are you sure you want to ban this user?</p>
<form method="post">
@Html.AntiForgeryToken()
<div class="ui left labeled input">
<label for="text" class="ui blue label">Reason: </label>
<input type="text" name="reason" id="text">
</div><br><br>
<div class="ui left labeled input">
<label for="modNotes" class="ui blue label">Moderation case notes: </label>
<input type="text" name="modNotes" id="modNotes">
</div><br><br>
<div class="ui left labeled input">
<label for="caseExpires" class="ui blue label">Expires on: </label>
<input type="datetime-local" name="caseExpires" id="caseExpires">
</div><br><br>
<br><input type="submit" value="Yes, ban @Model.TargetedUser.Username!" id="submit" class="ui red button"><br>
</form>

View file

@ -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<IActionResult> 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<IActionResult> 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}");
}
}

View file

@ -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<IActionResult> OnPost(CaseType? type, string reason, string modNotes, DateTime expires, int affectedId)
public async Task<IActionResult> 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);

View file

@ -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)
{
<div>
<a class="ui red button" href="/moderation/user/@Model.ProfileUser.UserId/ban">
<a class="ui red button" href="/moderation/newCase?type=@((int)CaseType.UserBan)&affectedId=@Model.ProfileUser.UserId">
<i class="ban icon"></i>
<span>Ban User</span>
</a>

View file

@ -43,10 +43,10 @@ public static class CaseTypeExtensions
};
}
public static Task<bool> IsIdValid(this CaseType type, int affectedId, Database database)
public static async Task<bool> 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();
}

View file

@ -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;