From 118174ec137a07c376f08ac02f995b99dc8facd9 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 11 Feb 2017 20:40:40 -0500 Subject: [PATCH] Admin panel login system is working. --- ShiftOS.Server.WebAdmin/Program.cs | 118 ++++++++++++++++-- .../Properties/Resources.Designer.cs | 49 +++++++- .../Properties/Resources.resx | 6 + .../Resources/HtmlTemplate.txt | 34 ++--- .../Resources/LoginView.txt | 15 ++- .../Resources/SetupView.txt | 22 ++++ ShiftOS.Server.WebAdmin/Resources/Status.txt | 19 +++ .../ShiftOS.Server.WebAdmin.csproj | 10 ++ 8 files changed, 244 insertions(+), 29 deletions(-) create mode 100644 ShiftOS.Server.WebAdmin/Resources/SetupView.txt create mode 100644 ShiftOS.Server.WebAdmin/Resources/Status.txt diff --git a/ShiftOS.Server.WebAdmin/Program.cs b/ShiftOS.Server.WebAdmin/Program.cs index 970f236..6e3661d 100644 --- a/ShiftOS.Server.WebAdmin/Program.cs +++ b/ShiftOS.Server.WebAdmin/Program.cs @@ -7,9 +7,11 @@ using Nancy; using Nancy.Authentication.Forms; using Nancy.Bootstrapper; using Nancy.Hosting.Self; +using Nancy.ModelBinding; using Nancy.Security; using Nancy.TinyIoc; using Newtonsoft.Json; +using ShiftOS.Objects; namespace ShiftOS.Server.WebAdmin { @@ -20,7 +22,7 @@ namespace ShiftOS.Server.WebAdmin var HostConf = new HostConfiguration(); HostConf.UrlReservations.CreateAutomatically = true; - using(var nancy = new NancyHost(HostConf, new Uri("http://localhost:13371/mudadmin"))) + using(var nancy = new NancyHost(HostConf, new Uri("http://localhost:13371/mudadmin/"))) { nancy.Start(); Console.WriteLine($"[{DateTime.Now}] Initiating on localhost:13371..."); @@ -31,15 +33,37 @@ namespace ShiftOS.Server.WebAdmin public static class PageBuilder { - public static string Build(string page) + public static string Build(string page, Dictionary templateParams = null) { string templatehtml = Properties.Resources.HtmlTemplate; + if (templateParams == null) + { + templateParams = new Dictionary(); + } + if (!templateParams.ContainsKey("{logout}")) + { + templateParams.Add("{logout}", "
  • Log out
  • "); + } switch (page) { + case "status": + templatehtml = templatehtml.Replace("{body}", Properties.Resources.Status); + break; case "login": templatehtml = templatehtml.Replace("{body}", Properties.Resources.LoginView); break; + case "initialsetup": + templatehtml = templatehtml.Replace("{body}", Properties.Resources.SetupView); + break; } + try + { + foreach (var param in templateParams) + { + templatehtml = templatehtml.Replace(param.Key, param.Value); + } + } + catch { } return templatehtml; } } @@ -98,6 +122,40 @@ namespace ShiftOS.Server.WebAdmin return false; } + public static string GetCPWorth() + { + if (System.IO.Directory.Exists("saves")) + { + int cp = 0; + + foreach(var file in System.IO.Directory.GetFiles("saves")) + { + if (file.EndsWith(".save")) + { + var save = JsonConvert.DeserializeObject(Server.Program.ReadEncFile(file)); + cp += save.Codepoints; + } + } + return cp.ToString(); + } + else + { + return "0"; + } + } + + public static string GetUserCount() + { + if (System.IO.Directory.Exists("saves")) + { + return System.IO.Directory.GetFiles("saves").Length.ToString(); + } + else + { + return "0"; + } + } + public static MudUserIdentity GetIdentity(Guid id) { foreach (var user in JsonConvert.DeserializeObject>(ShiftOS.Server.Program.ReadEncFile("users.json"))) @@ -146,7 +204,20 @@ namespace ShiftOS.Server.WebAdmin { Get["/login"] = parameters => { - return PageBuilder.Build("login"); + if (System.IO.File.Exists("users.json")) + { + return PageBuilder.Build("login", new Dictionary + { + {"{logout}", "" } + }); + } + else + { + return PageBuilder.Build("initialsetup", new Dictionary + { + {"{logout}", "" } + }); + } }; Get["/logout"] = parameters => @@ -156,28 +227,53 @@ namespace ShiftOS.Server.WebAdmin Post["/login"] = parameters => { + var p = this.Bind(); Guid id = new Guid(); - if (SystemManager.Login(parameters.username, parameters.password, out id) == true) + if (System.IO.File.Exists("users.json")) { - return this.Login(id); + if (SystemManager.Login(p.username, p.password, out id) == true) + { + return this.Login(id); + } + else + { + return PageBuilder.Build("loginFailed", new Dictionary + { + {"{logout}", "" } + }); + } } else { - return PageBuilder.Build("loginFailed"); + 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); } }; } } + + public class UserModule : NancyModule { public UserModule() { this.RequiresAuthentication(); - this.RequiresClaims("User"); + this.RequiresClaims("Admin"); Get["/"] = _ => { - return PageBuilder.Build("status"); + return PageBuilder.Build("status", new Dictionary{ + { "{cp_worth}", SystemManager.GetCPWorth() }, + { "{user_count}", SystemManager.GetUserCount() }, + { "{system_time}", DateTime.Now.ToString() }, + }); }; Get["/status"] = _ => { @@ -185,4 +281,10 @@ namespace ShiftOS.Server.WebAdmin }; } } + + public class LoginRequest + { + public string username { get; set; } + public string password { get; set; } + } } diff --git a/ShiftOS.Server.WebAdmin/Properties/Resources.Designer.cs b/ShiftOS.Server.WebAdmin/Properties/Resources.Designer.cs index 00f939c..cf60006 100644 --- a/ShiftOS.Server.WebAdmin/Properties/Resources.Designer.cs +++ b/ShiftOS.Server.WebAdmin/Properties/Resources.Designer.cs @@ -66,10 +66,10 @@ namespace ShiftOS.Server.WebAdmin.Properties { /// <title>Multi-user domain &bull; ShiftOS</title> /// <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> /// - /// <link rel="stylesheet" href="css/theme.css"/> + /// <link rel="stylesheet" href="http://getshiftos.ml/css/theme.css"/> /// /// <!-- Latest compiled and minified JavaScript --> - /// <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkf [rest of string was truncated]";. + /// <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5I [rest of string was truncated]";. /// internal static string HtmlTemplate { get { @@ -98,5 +98,50 @@ namespace ShiftOS.Server.WebAdmin.Properties { return ResourceManager.GetString("LoginView", resourceCulture); } } + + /// + /// Looks up a localized string similar to <h1>Initial setup</h1> + /// + ///<p>Welcome to your multi-user domain. If you are seeing this screen, it means that the web administration panel is successfully running and listening for requests. Before you can start using the admin panel you must create an Admin account. This account will let you log in and manage other authorized users, manage saves, Shiftnet pages and other information.</p> + /// + ///<p>To continue, type a username and password.</p> + /// + ///<form method="post" action=""> + /// <table class="table"> + /// <tr> + /// [rest of string was truncated]";. + /// + internal static string SetupView { + get { + return ResourceManager.GetString("SetupView", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to <h3>System status</h3> + /// + ///<p>Below is a summary of this multi-user domain's status.</p> + /// + ///<div class="row"> + /// <div class="col-xs-6"> + /// <h4>MUD stats</h4> + /// <ul> + /// <li>This server is worth <strong>{cp_worth}</strong> Codepoints.</li> + /// <li>This server has <strong>{user_count}</strong> players registered.</li> + /// </ul> + /// </div> + /// <div class="col-xs-6"> + /// <h4>System environment</h4> + /// <ul> + /// <li><strong>Current system time:</strong> {system_time}</li> + /// </ul> + /// </div> + ///</div>. + /// + internal static string Status { + get { + return ResourceManager.GetString("Status", resourceCulture); + } + } } } diff --git a/ShiftOS.Server.WebAdmin/Properties/Resources.resx b/ShiftOS.Server.WebAdmin/Properties/Resources.resx index 5a705f4..a6e7f9b 100644 --- a/ShiftOS.Server.WebAdmin/Properties/Resources.resx +++ b/ShiftOS.Server.WebAdmin/Properties/Resources.resx @@ -124,4 +124,10 @@ ..\Resources\LoginView.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252 + + ..\Resources\SetupView.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252 + + + ..\Resources\Status.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252 + \ No newline at end of file diff --git a/ShiftOS.Server.WebAdmin/Resources/HtmlTemplate.txt b/ShiftOS.Server.WebAdmin/Resources/HtmlTemplate.txt index 7f10c59..fa0fcc6 100644 --- a/ShiftOS.Server.WebAdmin/Resources/HtmlTemplate.txt +++ b/ShiftOS.Server.WebAdmin/Resources/HtmlTemplate.txt @@ -3,42 +3,42 @@ Multi-user domain • ShiftOS - + - -