diff options
| author | Michael <[email protected]> | 2017-02-11 20:40:40 -0500 |
|---|---|---|
| committer | Michael <[email protected]> | 2017-02-11 20:40:40 -0500 |
| commit | 118174ec137a07c376f08ac02f995b99dc8facd9 (patch) | |
| tree | 3fa75bc7c9128f8ad9b40b8689a255b9156cc54f /ShiftOS.Server.WebAdmin/Program.cs | |
| parent | 263ee283de817c68bad1b2c7ff55a6f370a0c2c9 (diff) | |
| download | shiftos_thereturn-118174ec137a07c376f08ac02f995b99dc8facd9.tar.gz shiftos_thereturn-118174ec137a07c376f08ac02f995b99dc8facd9.tar.bz2 shiftos_thereturn-118174ec137a07c376f08ac02f995b99dc8facd9.zip | |
Admin panel login system is working.
Diffstat (limited to 'ShiftOS.Server.WebAdmin/Program.cs')
| -rw-r--r-- | ShiftOS.Server.WebAdmin/Program.cs | 118 |
1 files changed, 110 insertions, 8 deletions
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}] <AdminPanel/NancyInit> 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<string, string> templateParams = null) { string templatehtml = Properties.Resources.HtmlTemplate; + if (templateParams == null) + { + templateParams = new Dictionary<string, string>(); + } + if (!templateParams.ContainsKey("{logout}")) + { + templateParams.Add("{logout}", "<li><a href=\"/mudadmin/logout\">Log out</a></li>"); + } 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<Save>(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<List<MudUser>>(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<string, string> + { + {"{logout}", "" } + }); + } + else + { + return PageBuilder.Build("initialsetup", new Dictionary<string, string> + { + {"{logout}", "" } + }); + } }; Get["/logout"] = parameters => @@ -156,28 +227,53 @@ namespace ShiftOS.Server.WebAdmin Post["/login"] = parameters => { + var p = this.Bind<LoginRequest>(); 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<string, string> + { + {"{logout}", "" } + }); + } } else { - return PageBuilder.Build("loginFailed"); + var mudUser = new MudUser(); + mudUser.Username = p.username; + mudUser.Password = Encryption.Encrypt(p.password); + mudUser.Claims = new List<string>(new[] { "Admin" }); + mudUser.ID = Guid.NewGuid(); + id = mudUser.ID; + List<MudUser> users = new List<MudUser>(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<string, string>{ + { "{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; } + } } |
