Add users page to admin panel

This commit is contained in:
jvyden 2022-01-18 20:29:42 -05:00
commit ddd3863708
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
4 changed files with 103 additions and 27 deletions

View file

@ -8,6 +8,12 @@
Model.Title = "Admin Panel";
}
<a href="/admin/users">
<div class="ui blue button">
View Users
</div>
</a>
<h2>Commands</h2>
<div class="ui grid">
@foreach (ICommand command in MaintenanceHelper.Commands)

View file

@ -7,37 +7,35 @@ using LBPUnion.ProjectLighthouse.Pages.Layouts;
using LBPUnion.ProjectLighthouse.Types;
using Microsoft.AspNetCore.Mvc;
namespace LBPUnion.ProjectLighthouse.Pages.Admin
namespace LBPUnion.ProjectLighthouse.Pages.Admin;
public class AdminPanelPage : BaseLayout
{
public class AdminPanelPage : BaseLayout
public List<ICommand> Commands = MaintenanceHelper.Commands;
public AdminPanelPage(Database database) : base(database)
{}
public async Task<IActionResult> OnGet([FromQuery] string? args, [FromQuery] string? command, [FromQuery] string? maintenanceJob)
{
User? user = this.Database.UserFromWebRequest(this.Request);
if (user == null) return this.Redirect("~/login");
if (!user.IsAdmin) return this.NotFound();
public List<ICommand> Commands = MaintenanceHelper.Commands;
public AdminPanelPage(Database database) : base(database)
{}
public async Task<IActionResult> OnGet([FromQuery] string? args, [FromQuery] string? command, [FromQuery] string? maintenanceJob)
if (!string.IsNullOrEmpty(command))
{
User? user = this.Database.UserFromWebRequest(this.Request);
if (user == null) return this.Redirect("~/login");
if (!user.IsAdmin) return this.NotFound();
if (!string.IsNullOrEmpty(command))
{
args ??= "";
args = command + " " + args;
string[] split = args.Split(" ");
await MaintenanceHelper.RunCommand(split);
return this.Redirect("~/admin");
}
if (!string.IsNullOrEmpty(maintenanceJob))
{
await MaintenanceHelper.RunMaintenanceJob(maintenanceJob);
return this.Redirect("~/admin");
}
return this.Page();
args ??= "";
args = command + " " + args;
string[] split = args.Split(" ");
await MaintenanceHelper.RunCommand(split);
return this.Redirect("~/admin");
}
if (!string.IsNullOrEmpty(maintenanceJob))
{
await MaintenanceHelper.RunMaintenanceJob(maintenanceJob);
return this.Redirect("~/admin");
}
return this.Page();
}
}

View file

@ -0,0 +1,40 @@
@page "/admin/users"
@using LBPUnion.ProjectLighthouse.Types
@model LBPUnion.ProjectLighthouse.Pages.Admin.AdminPanelUsersPage
@{
Layout = "Layouts/BaseLayout";
Model.Title = "Users";
}
<p>There are currently @Model.UserCount users registered to your instance.</p>
<p><b>Note:</b> Users are ordered by most-recent-first.</p>
<div class="ui grid">
@foreach (User user in Model.Users)
{
string color = "blue";
string subtitle = "User";
if (user.IsAdmin)
{
color = "yellow";
subtitle = "Admin";
}
if (user.Banned)
{
color = "red";
subtitle = $"Banned user! Reason: {user.BannedReason}";
}
subtitle += $" (id: {user.UserId})";
<div class="eight wide column">
<div class="ui @color segment">
<h2>
<a href="/user/@user.UserId">@user.Username</a>
</h2>
<h3>@subtitle</h3>
</div>
</div>
}
</div>

View file

@ -0,0 +1,32 @@
#nullable enable
using System.Collections.Generic;
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 AdminPanelUsersPage : BaseLayout
{
public AdminPanelUsersPage(Database database) : base(database)
{}
public int UserCount;
public List<User> Users;
public async Task<IActionResult> OnGet()
{
User? user = this.Database.UserFromWebRequest(this.Request);
if (user == null) return this.Redirect("~/login");
if (!user.IsAdmin) return this.NotFound();
this.Users = await this.Database.Users.OrderByDescending(u => u.UserId).ToListAsync();
this.UserCount = this.Users.Count;
return this.Page();
}
}