diff --git a/ProjectLighthouse/Helpers/CommandHelper.cs b/ProjectLighthouse/Helpers/CommandHelper.cs index ed47d3b3..cf824029 100644 --- a/ProjectLighthouse/Helpers/CommandHelper.cs +++ b/ProjectLighthouse/Helpers/CommandHelper.cs @@ -9,11 +9,11 @@ namespace LBPUnion.ProjectLighthouse.Helpers { public static class CommandHelper { - private static List commands; + public static List 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 suitableCommands = commands.Where + IEnumerable suitableCommands = Commands.Where (command => command.Aliases().Any(a => a.ToLower() == baseCmd.ToLower())) .Where(command => args.Length >= command.RequiredArgs()); foreach (ICommand command in suitableCommands) diff --git a/ProjectLighthouse/Pages/AdminPanelPage.cshtml b/ProjectLighthouse/Pages/AdminPanelPage.cshtml new file mode 100644 index 00000000..41d80740 --- /dev/null +++ b/ProjectLighthouse/Pages/AdminPanelPage.cshtml @@ -0,0 +1,27 @@ +@page "/admin" +@using LBPUnion.ProjectLighthouse.CommandLine +@model LBPUnion.ProjectLighthouse.Pages.AdminPanelPage + +@{ + Layout = "Layouts/BaseLayout"; +} + +

Admin Panel

+ +
+ @foreach (ICommand command in Model!.Commands) + { +
+
+

@command.Name()

+
+
+ +


+ + +
+
+
+ } +
\ No newline at end of file diff --git a/ProjectLighthouse/Pages/AdminPanelPage.cshtml.cs b/ProjectLighthouse/Pages/AdminPanelPage.cshtml.cs new file mode 100644 index 00000000..6101a981 --- /dev/null +++ b/ProjectLighthouse/Pages/AdminPanelPage.cshtml.cs @@ -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 Commands = CommandHelper.Commands; + + public async Task 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(); + } + } +} \ No newline at end of file diff --git a/ProjectLighthouse/Pages/Layouts/BaseLayout.cshtml b/ProjectLighthouse/Pages/Layouts/BaseLayout.cshtml index a03632a3..2a5c4b56 100644 --- a/ProjectLighthouse/Pages/Layouts/BaseLayout.cshtml +++ b/ProjectLighthouse/Pages/Layouts/BaseLayout.cshtml @@ -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 } }