From f54f01494101f931b8987e64a65f7bc8f2314d09 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 12 Feb 2017 18:35:15 -0500 Subject: More admin panel work. --- ShiftOS.Server.WebAdmin/Program.cs | 370 +++++++++++++++++++++++++++++++++---- 1 file changed, 337 insertions(+), 33 deletions(-) (limited to 'ShiftOS.Server.WebAdmin/Program.cs') diff --git a/ShiftOS.Server.WebAdmin/Program.cs b/ShiftOS.Server.WebAdmin/Program.cs index 91e4831..b624ee7 100644 --- a/ShiftOS.Server.WebAdmin/Program.cs +++ b/ShiftOS.Server.WebAdmin/Program.cs @@ -45,6 +45,24 @@ namespace ShiftOS.Server.WebAdmin { templateParams.Add("{logout}", "
  • Log out
  • "); } + if (SystemManager.MudIsRunning()) + { + templateParams.Add("{mud_power}", "
  • Power off
  • "); + templateParams.Add("{mud_restart}", "
  • Restart
  • "); + } + else + { + templateParams.Add("{mud_power}", "
  • Power on
  • "); + templateParams.Add("{mud_restart}", ""); + } + + if(templateParams["{logout}"] == "") + { + templateParams["{mud_power}"] = ""; + templateParams["{mud_restart}"] = ""; + + } + switch (page) { case "status": @@ -97,23 +115,61 @@ namespace ShiftOS.Server.WebAdmin public static class SystemManager { + public static bool MudIsRunning() + { + var processes = System.Diagnostics.Process.GetProcessesByName("ShiftOS.Server"); + return processes.Length > 0; + } + + public static void KillMud() + { + var processes = System.Diagnostics.Process.GetProcessesByName("ShiftOS.Server"); + for(int i = 0; i < processes.Length; i++) + { + try + { + processes[i].Kill(); + } + catch + { + } + } + } + public static List GetClaims(string username) { - foreach (var user in JsonConvert.DeserializeObject>(ShiftOS.Server.Program.ReadEncFile("users.json"))) + foreach(var save in GetSaves()) { - if(user.Username == username) + if (save.IsMUDAdmin) { - return user.Claims; + return new List { "User", "Admin" }; } } return new List(new[] { "User" }); } + public static Save[] GetSaves() + { + List saves = new List(); + if (Directory.Exists("saves")) + { + foreach(var saveFile in Directory.GetFiles("saves")) + { + try + { + saves.Add(JsonConvert.DeserializeObject(Server.Program.ReadEncFile(saveFile))); + } + catch { } + } + } + return saves.ToArray(); + } + public static bool Login(string username, string password, out Guid id) { - foreach (var user in JsonConvert.DeserializeObject>(ShiftOS.Server.Program.ReadEncFile("users.json"))) + foreach (var user in GetSaves()) { - if (user.Username == username && user.Password == Encryption.Encrypt(password)) + if (user.Username == username && user.Password == password) { id = user.ID; return true; @@ -184,6 +240,40 @@ namespace ShiftOS.Server.WebAdmin return new Channel(); } + public static string BuildSaveListing(Save[] list) + { + StringBuilder sb = new StringBuilder(); + sb.AppendLine(""); + + sb.AppendLine(@" + + + + + + +"); + + foreach(var save in list) + { + sb.AppendLine($@" + + + + + + +"); + } + + sb.AppendLine("
    UsernameSystem NameCodepointsShiftorium UpgradesIs MUD AdminActions
    {save.Username}{save.SystemName}{save.Codepoints}{save.CountUpgrades()} installed, {save.Upgrades.Count} total{save.IsMUDAdmin} + Toggle admin + Delete save +
    "); + return sb.ToString(); + } + + public static string GetAllChats() { StringBuilder sb = new StringBuilder(); @@ -208,15 +298,40 @@ namespace ShiftOS.Server.WebAdmin {chat.DiscordBotToken} Edit - Delete + Delete "); + sb.AppendLine(CreateModal(chat.ID, "Delete " + chat.Name + "?", "Are you sure you want to delete this chat?", "/deletechat/" + chat.ID)); } } sb.AppendLine(""); return sb.ToString(); } + public static string CreateModal(string id, string title, string msg, string callbackUrl) + { + return $@"
    +
    + + +
    +
    + +

    {title}

    +
    +
    +

    {msg}

    +
    +
    + Yes + +
    +
    + +
    +
    "; + } + public static string GetCPWorth() { if (System.IO.Directory.Exists("saves")) @@ -253,7 +368,7 @@ namespace ShiftOS.Server.WebAdmin public static MudUserIdentity GetIdentity(Guid id) { - foreach (var user in JsonConvert.DeserializeObject>(ShiftOS.Server.Program.ReadEncFile("users.json"))) + foreach (var user in GetSaves()) { if (user.ID == id) { @@ -262,13 +377,50 @@ namespace ShiftOS.Server.WebAdmin } return null; } + + internal static void MakeAdmin(string username) + { + Save sav = null; + foreach(var save in GetSaves()) + { + if (save.Username == username) + sav = save; + } + if(sav != null) + { + sav.IsMUDAdmin = true; + Server.Program.WriteEncFile("saves/" + username + ".save", JsonConvert.SerializeObject(sav)); + } + } + + internal static Save[] GetAdmins() + { + var saves = new List(); + foreach(var save in GetSaves()) + { + if(save.IsMUDAdmin == true) + { + saves.Add(save); + } + } + return saves.ToArray(); + } } public class MudUser { + [FriendlyName("Username")] + [FriendlyDescription("The username you will appear as in-game.")] public string Username { get; set; } + + [FriendlyName("Password")] + [FriendlyDescription("A password that you will use to log in to the admin panel and the game.")] public string Password { get; set; } - public List Claims { get; set; } + + [FriendlyName("System name")] + [FriendlyDescription("An in-game hostname for your account. In ShiftOS, your user ID is always yourusername@yoursystemname. Be creative.")] + public string SystemName { get; set; } + public Guid ID { get; set; } } @@ -299,18 +451,30 @@ namespace ShiftOS.Server.WebAdmin { Get["/login"] = parameters => { - if (System.IO.File.Exists("users.json")) + if (SystemManager.GetSaves().Length > 0) { - return PageBuilder.Build("login", new Dictionary + if (SystemManager.GetAdmins().Length > 0) + { + return PageBuilder.Build("login", new Dictionary { {"{logout}", "" } }); + } + else + { + return PageBuilder.Build("initialsetup", new Dictionary + { + {"{logout}", "" }, + {"{savelist}", BuildSaveList() } + }); + } } else { - return PageBuilder.Build("initialsetup", new Dictionary + return PageBuilder.Build("bla", new Dictionary { - {"{logout}", "" } + {"{body}", Properties.Resources.NoUsersFound }, + {"{user_create_form}", SystemManager.BuildFormFromObject(new MudUser()) } }); } }; @@ -322,54 +486,169 @@ namespace ShiftOS.Server.WebAdmin Post["/login"] = parameters => { - var p = this.Bind(); - Guid id = new Guid(); - if (System.IO.File.Exists("users.json")) + if (SystemManager.GetSaves().Length > 0) { - if (SystemManager.Login(p.username, p.password, out id) == true) + if (SystemManager.GetAdmins().Length == 0) { - return this.Login(id); + var user = this.Bind(); + SystemManager.MakeAdmin(user.username); + Guid id = new Guid(); + if(SystemManager.Login(user.username, user.password, out id) == true) + { + return this.Login(id); + } + return new UserModule().Redirect("/login"); } else { - return PageBuilder.Build("loginFailed", new Dictionary - { - {"{logout}", "" } - }); + var user = this.Bind(); + Guid id = new Guid(); + if (SystemManager.Login(user.username, user.password, out id) == true) + { + return this.Login(id); + } + return new UserModule().Redirect("/login"); } } else { - var mudUser = new MudUser(); - mudUser.Username = p.username; - mudUser.Password = Encryption.Encrypt(p.password); - mudUser.Claims = new List(new[] { "Admin" }); - mudUser.ID = Guid.NewGuid(); - id = mudUser.ID; - List users = new List(new[] { mudUser }); - ShiftOS.Server.Program.WriteEncFile("users.json", JsonConvert.SerializeObject(users, Formatting.Indented)); - return this.Login(id); + var newUser = this.Bind(); + var save = new Save(); + save.Username = newUser.Username; + save.SystemName = newUser.SystemName; + save.Password = newUser.Password; + save.Codepoints = 0; + save.MyShop = ""; + save.Upgrades = new Dictionary(); + save.IsMUDAdmin = true; + save.StoryPosition = 1; + + if (!Directory.Exists("saves")) + Directory.CreateDirectory("saves"); + save.ID = Guid.NewGuid(); + + Server.Program.WriteEncFile("saves/" + save.Username + ".save", JsonConvert.SerializeObject(save)); + return this.Login(save.ID); } }; } + + private string BuildSaveList() + { + StringBuilder sb = new StringBuilder(); + sb.AppendLine(""); + sb.AppendLine($@" + + + + +"); + + foreach(var save in SystemManager.GetSaves()) + { + sb.AppendLine($@" + + + + +"); + } + + sb.AppendLine("
    UsernameSystem nameCodepointsActions
    {save.Username}{save.SystemName}{save.Codepoints}
    + + +
    "); + return sb.ToString(); + } } public class UserModule : NancyModule { + public string Redirect(string url) + { + return $@" + + + +"; + } + public UserModule() { this.RequiresAuthentication(); this.RequiresClaims("Admin"); Get["/"] = _ => { - return statusBuilder(); + return Redirect("/status"); + }; + + Get["/toggleadmin/{id}"] = parameters => + { + string id = parameters.id; + for (int i = 0; i < SystemManager.GetSaves().Length; i++) + { + var save = SystemManager.GetSaves()[i]; + if(save.ID.ToString() == id) + { + save.IsMUDAdmin = !save.IsMUDAdmin; + Server.Program.WriteEncFile("saves/" + save.Username + ".save", JsonConvert.SerializeObject(save)); + } + } + return Redirect("/saves"); + }; + + Get["/deletesave/{username}"] = parameters => + { + + + string id = parameters.username; + for (int i = 0; i < SystemManager.GetSaves().Length; i++) + { + if (SystemManager.GetSaves()[i].Username.ToString() == id) + { + File.Delete("saves/" + SystemManager.GetSaves()[i].Username + ".save"); + } + } + return Redirect("/saves"); + }; + + + Get["/saves"] = _ => + { + return PageBuilder.Build("bla", new Dictionary + { + { "{body}", Properties.Resources.GenericTableList }, + { "{listtitle}", "Test subjects" }, + { "{listdesc}", "Below is a list of test subjects (save files) on your multi-user domain. You can see their username, system name, Codepoints, amount of installed upgrades, and you can also perform basic actions on each save." }, + { "{list}", SystemManager.BuildSaveListing(SystemManager.GetSaves()) } + }); + }; + Get["/status"] = _ => { return statusBuilder(); }; + + Get["/deletechat/{id}"] = parameters => + { + string chatID = parameters.id; + var chats = JsonConvert.DeserializeObject>(File.ReadAllText("chats.json")); + for(int i = 0; i < chats.Count; i++) + { + try + { + if (chats[i].ID == chatID) + chats.RemoveAt(i); + } + catch { } + } + File.WriteAllText("chats.json", JsonConvert.SerializeObject(chats, Formatting.Indented)); + return Redirect("/chats"); + }; + Get["/chats"] = _ => { return chatsListBuilder(); @@ -410,7 +689,7 @@ namespace ShiftOS.Server.WebAdmin File.WriteAllText("chats.json", JsonConvert.SerializeObject(chats, Formatting.Indented)); - return chatsListBuilder(); + return Redirect("/chats"); }; Get["/editchat/{id}"] = parameters => @@ -447,9 +726,34 @@ namespace ShiftOS.Server.WebAdmin } File.WriteAllText("chats.json", JsonConvert.SerializeObject(chats, Formatting.Indented)); - return chatsListBuilder(); + return Redirect("/chats"); + }; + + Get["/poweron"] = _ => + { + if (!SystemManager.MudIsRunning()) + { + System.Diagnostics.Process.Start("ShiftOS.Server.exe"); + } + return Redirect("/"); }; + Get["/poweroff"] = _ => + { + if (SystemManager.MudIsRunning()) + { + SystemManager.KillMud(); + } + return Redirect("/"); + }; + Get["/restart"] = _ => + { + if (SystemManager.MudIsRunning()) + { + SystemManager.KillMud(); + } + return Redirect("/poweron"); + }; } private string statusBuilder() -- cgit v1.2.3