Add admin panel page

This commit is contained in:
jvyden 2021-11-23 19:25:01 -05:00
commit f83325dc66
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
4 changed files with 72 additions and 3 deletions

View file

@ -9,11 +9,11 @@ namespace LBPUnion.ProjectLighthouse.Helpers
{
public static class CommandHelper
{
private static List<ICommand> commands;
public static List<ICommand> Commands { get; }
static CommandHelper()
{
commands = Assembly.GetExecutingAssembly()
Commands = Assembly.GetExecutingAssembly()
.GetTypes()
.Where(t => t.GetInterfaces().Contains(typeof(ICommand)) && t.GetConstructor(Type.EmptyTypes) != null)
.Select(t => Activator.CreateInstance(t) as ICommand)
@ -34,7 +34,7 @@ namespace LBPUnion.ProjectLighthouse.Helpers
string baseCmd = args[0];
args = args.Skip(1).ToArray();
IEnumerable<ICommand> suitableCommands = commands.Where
IEnumerable<ICommand> suitableCommands = Commands.Where
(command => command.Aliases().Any(a => a.ToLower() == baseCmd.ToLower()))
.Where(command => args.Length >= command.RequiredArgs());
foreach (ICommand command in suitableCommands)

View file

@ -0,0 +1,27 @@
@page "/admin"
@using LBPUnion.ProjectLighthouse.CommandLine
@model LBPUnion.ProjectLighthouse.Pages.AdminPanelPage
@{
Layout = "Layouts/BaseLayout";
}
<h1>Admin Panel</h1>
<div class="ui grid">
@foreach (ICommand command in Model!.Commands)
{
<div class="four wide column">
<div class="ui black segment">
<h3>@command.Name()</h3>
<form>
<div class="ui input" style="width: 100%;">
<input type="text" name="args" placeholder="Arguments">
</div><br><br>
<input type="text" name="command" style="display: none;" value="@command.FirstAlias">
<input type="submit" value="Execute Command" class="ui green button" style="width: 100%;">
</form>
</div>
</div>
}
</div>

View file

@ -0,0 +1,37 @@
#nullable enable
using System.Collections.Generic;
using System.Threading.Tasks;
using LBPUnion.ProjectLighthouse.CommandLine;
using LBPUnion.ProjectLighthouse.Helpers;
using LBPUnion.ProjectLighthouse.Pages.Layouts;
using LBPUnion.ProjectLighthouse.Types;
using Microsoft.AspNetCore.Mvc;
namespace LBPUnion.ProjectLighthouse.Pages
{
public class AdminPanelPage : BaseLayout
{
public AdminPanelPage(Database database) : base(database)
{}
public List<ICommand> Commands = CommandHelper.Commands;
public async Task<IActionResult> OnGet([FromQuery] string? args, [FromQuery] string? 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 CommandHelper.RunCommand(split);
return this.Redirect("~/admin");
}
return this.Page();
}
}
}

View file

@ -16,6 +16,11 @@
Model.NavigationItems.Add(new PageNavigationItem("Authentication", "/authentication", "key"));
}
Model.NavigationItemsRight.Add(new PageNavigationItem("Profile", "/user/" + Model.User.UserId, "user alternate"));
@if (Model.User.IsAdmin)
{
Model.NavigationItemsRight.Add(new PageNavigationItem("Admin Panel", "/admin", "cogs"));
}
Model.NavigationItemsRight.Add(new PageNavigationItem("Log out", "/logout", "user alternate slash")); // should always be last
}
}