mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-08-06 11:58:38 +00:00
Add ban confirmation page
This commit is contained in:
parent
1f2d9f3303
commit
960f26b95c
6 changed files with 73 additions and 28 deletions
|
@ -1,5 +1,4 @@
|
|||
#nullable enable
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using LBPUnion.ProjectLighthouse.Types;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
@ -18,34 +17,11 @@ namespace LBPUnion.ProjectLighthouse.Controllers.Website.Admin
|
|||
this.database = database;
|
||||
}
|
||||
|
||||
[HttpGet("ban")]
|
||||
public async Task<IActionResult> BanUser([FromRoute] int id)
|
||||
{
|
||||
User? user = this.database.UserFromWebRequest(this.Request);
|
||||
if (user == null || !user.IsAdmin) return this.StatusCode(403, "");
|
||||
|
||||
User? targetedUser = await this.database.Users.FirstOrDefaultAsync(u => u.UserId == id);
|
||||
;
|
||||
if (targetedUser == null) return this.NotFound();
|
||||
|
||||
targetedUser.Banned = true;
|
||||
targetedUser.BannedReason = $"Banned by admin {user.Username} (id: {user.UserId})";
|
||||
|
||||
// invalidate all currently active gametokens
|
||||
this.database.GameTokens.RemoveRange(this.database.GameTokens.Where(t => t.UserId == targetedUser.UserId));
|
||||
|
||||
// invalidate all currently active webtokens
|
||||
this.database.WebTokens.RemoveRange(this.database.WebTokens.Where(t => t.UserId == targetedUser.UserId));
|
||||
|
||||
await this.database.SaveChangesAsync();
|
||||
return this.Redirect($"/user/{targetedUser.UserId}");
|
||||
}
|
||||
|
||||
[HttpGet("unban")]
|
||||
public async Task<IActionResult> UnbanUser([FromRoute] int id)
|
||||
{
|
||||
User? user = this.database.UserFromWebRequest(this.Request);
|
||||
if (user == null || !user.IsAdmin) return this.StatusCode(403, "");
|
||||
if (user == null || !user.IsAdmin) return this.NotFound();
|
||||
|
||||
User? targetedUser = await this.database.Users.FirstOrDefaultAsync(u => u.UserId == id);
|
||||
;
|
||||
|
|
20
ProjectLighthouse/Pages/Admin/AdminBanUserPage.cshtml
Normal file
20
ProjectLighthouse/Pages/Admin/AdminBanUserPage.cshtml
Normal file
|
@ -0,0 +1,20 @@
|
|||
@page "/admin/user/{id:int}/ban"
|
||||
@model LBPUnion.ProjectLighthouse.Pages.Admin.AdminBanUserPage
|
||||
|
||||
@{
|
||||
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>
|
||||
|
||||
<input type="submit" value="Yes, ban @Model.TargetedUser.Username!" id="submit" class="ui red button"><br>
|
||||
</form>
|
49
ProjectLighthouse/Pages/Admin/AdminBanUserPage.cshtml.cs
Normal file
49
ProjectLighthouse/Pages/Admin/AdminBanUserPage.cshtml.cs
Normal file
|
@ -0,0 +1,49 @@
|
|||
#nullable enable
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using LBPUnion.ProjectLighthouse.Pages.Layouts;
|
||||
using LBPUnion.ProjectLighthouse.Types;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace LBPUnion.ProjectLighthouse.Pages.Admin;
|
||||
|
||||
public class AdminBanUserPage : BaseLayout
|
||||
{
|
||||
public AdminBanUserPage(Database database) : base(database)
|
||||
{}
|
||||
|
||||
public User? TargetedUser;
|
||||
|
||||
public async Task<IActionResult> OnGet([FromRoute] int id)
|
||||
{
|
||||
User? user = this.Database.UserFromWebRequest(this.Request);
|
||||
if (user == null || !user.IsAdmin) 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)
|
||||
{
|
||||
User? user = this.Database.UserFromWebRequest(this.Request);
|
||||
if (user == null || !user.IsAdmin) return this.NotFound();
|
||||
|
||||
this.TargetedUser = await this.Database.Users.FirstOrDefaultAsync(u => u.UserId == id);
|
||||
if (this.TargetedUser == null) return this.NotFound();
|
||||
|
||||
this.TargetedUser.Banned = true;
|
||||
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));
|
||||
|
||||
await this.Database.SaveChangesAsync();
|
||||
return this.Redirect($"/user/{this.TargetedUser.UserId}");
|
||||
}
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
@page "/admin"
|
||||
@using LBPUnion.ProjectLighthouse.Helpers
|
||||
@using LBPUnion.ProjectLighthouse.Maintenance
|
||||
@model LBPUnion.ProjectLighthouse.Pages.AdminPanelPage
|
||||
@model LBPUnion.ProjectLighthouse.Pages.Admin.AdminPanelPage
|
||||
|
||||
@{
|
||||
Layout = "Layouts/BaseLayout";
|
|
@ -7,7 +7,7 @@ using LBPUnion.ProjectLighthouse.Pages.Layouts;
|
|||
using LBPUnion.ProjectLighthouse.Types;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace LBPUnion.ProjectLighthouse.Pages
|
||||
namespace LBPUnion.ProjectLighthouse.Pages.Admin
|
||||
{
|
||||
public class AdminPanelPage : BaseLayout
|
||||
{
|
|
@ -6,7 +6,7 @@
|
|||
Model.Title = "Password Reset Required";
|
||||
}
|
||||
|
||||
<p>An admin has deemed it necessary that you reset your password. Please do so.</p>
|
||||
<p>An administrator has deemed it necessary that you reset your password. Please do so.</p>
|
||||
|
||||
<a href="/passwordReset">
|
||||
<div class="ui blue button">Reset Password</div>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue