From 632ce75fa062d2a7378e164ebe37bf55416298d7 Mon Sep 17 00:00:00 2001 From: tidusjar Date: Tue, 9 Aug 2016 14:03:48 +0100 Subject: [PATCH] Finished #459 --- .../SettingModels/PlexRequestSettings.cs | 1 + PlexRequests.Core/Setup.cs | 14 +++++- PlexRequests.UI/Content/wizard.js | 3 +- PlexRequests.UI/Modules/BaseAuthModule.cs | 6 +++ PlexRequests.UI/Modules/BaseModule.cs | 4 +- PlexRequests.UI/Modules/UserWizardModule.cs | 50 ++++++++++++++----- PlexRequests.UI/Views/UserWizard/Index.cshtml | 28 +++++++++-- 7 files changed, 84 insertions(+), 22 deletions(-) diff --git a/PlexRequests.Core/SettingModels/PlexRequestSettings.cs b/PlexRequests.Core/SettingModels/PlexRequestSettings.cs index 95bf3988c..8def76efd 100644 --- a/PlexRequests.Core/SettingModels/PlexRequestSettings.cs +++ b/PlexRequests.Core/SettingModels/PlexRequestSettings.cs @@ -55,6 +55,7 @@ namespace PlexRequests.Core.SettingModels public string NoApprovalUsers { get; set; } public bool CollectAnalyticData { get; set; } public bool IgnoreNotifyForAutoApprovedRequests { get; set; } + public bool Wizard { get; set; } /// /// The CSS name of the theme we want diff --git a/PlexRequests.Core/Setup.cs b/PlexRequests.Core/Setup.cs index 0ef67ce52..33ad9a69c 100644 --- a/PlexRequests.Core/Setup.cs +++ b/PlexRequests.Core/Setup.cs @@ -184,15 +184,25 @@ namespace PlexRequests.Core var plexSettings = new SettingsServiceV2(new SettingsJsonRepository(Db, new MemoryCacheProvider())); var currentSettings = plexSettings.GetSettings(); - if (!string.IsNullOrEmpty(auth.OldPlexAuthToken)) + if (!string.IsNullOrEmpty(auth?.OldPlexAuthToken)) { - currentSettings.PlexAuthToken = auth.OldPlexAuthToken; + currentSettings.PlexAuthToken = auth?.OldPlexAuthToken; plexSettings.SaveSettings(currentSettings); // Clear out the old value auth.OldPlexAuthToken = string.Empty; authSettings.SaveSettings(auth); } + + + //If we have an authToken we do not need to go through the setup + if (!string.IsNullOrEmpty(auth?.OldPlexAuthToken)) + { + var prServuce = new SettingsServiceV2(new SettingsJsonRepository(Db, new MemoryCacheProvider())); + var settings = prServuce.GetSettings(); + settings.Wizard = true; + prServuce.SaveSettings(settings); + } } /// diff --git a/PlexRequests.UI/Content/wizard.js b/PlexRequests.UI/Content/wizard.js index 88369b2b3..877a26ee8 100644 --- a/PlexRequests.UI/Content/wizard.js +++ b/PlexRequests.UI/Content/wizard.js @@ -94,7 +94,7 @@ success: function (response) { if (response.result === true) { //Next - loadArea("createAdminArea"); + loadArea("adminArea"); } else { generateNotify(response.message, "warning"); } @@ -105,6 +105,7 @@ }); }); + $('#contentBody').on('click', '#SearchForMovies', function () { var checked = this.checked; changeDisabledStatus($('#RequireMovieApproval'), checked, $('#RequireMovieApprovalLabel')); diff --git a/PlexRequests.UI/Modules/BaseAuthModule.cs b/PlexRequests.UI/Modules/BaseAuthModule.cs index 4fbc0e096..d5dde01c9 100644 --- a/PlexRequests.UI/Modules/BaseAuthModule.cs +++ b/PlexRequests.UI/Modules/BaseAuthModule.cs @@ -53,6 +53,12 @@ namespace PlexRequests.UI.Modules private Response CheckAuth() { var settings = PlexRequestSettings.GetSettings(); + // Have we been through the wizard? + if (!settings.Wizard) + { + return Context.GetRedirect("~/wizard"); + } + var baseUrl = settings.BaseUrl; var redirectPath = string.IsNullOrEmpty(baseUrl) ? "~/userlogin" : $"~/{baseUrl}/userlogin"; diff --git a/PlexRequests.UI/Modules/BaseModule.cs b/PlexRequests.UI/Modules/BaseModule.cs index 88e252daa..1a205202b 100644 --- a/PlexRequests.UI/Modules/BaseModule.cs +++ b/PlexRequests.UI/Modules/BaseModule.cs @@ -42,7 +42,7 @@ namespace PlexRequests.UI.Modules public abstract class BaseModule : NancyModule { protected string BaseUrl { get; set; } - + protected BaseModule(ISettingsService settingsService) { @@ -66,7 +66,7 @@ namespace PlexRequests.UI.Modules BaseUrl = baseUrl; var settingModulePath = string.IsNullOrEmpty(baseUrl) ? modulePath : $"{baseUrl}/{modulePath}"; - + ModulePath = settingModulePath; Before += (ctx) => diff --git a/PlexRequests.UI/Modules/UserWizardModule.cs b/PlexRequests.UI/Modules/UserWizardModule.cs index e08f3fee3..9736337f2 100644 --- a/PlexRequests.UI/Modules/UserWizardModule.cs +++ b/PlexRequests.UI/Modules/UserWizardModule.cs @@ -24,10 +24,13 @@ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // ************************************************************************/ #endregion +using System; using System.Linq; using System.Threading.Tasks; using Nancy; +using Nancy.Authentication.Forms; +using Nancy.Extensions; using Nancy.ModelBinding; using Nancy.Responses.Negotiation; using Nancy.Validation; @@ -43,33 +46,37 @@ namespace PlexRequests.UI.Modules public class UserWizardModule : BaseModule { public UserWizardModule(ISettingsService pr, ISettingsService plex, IPlexApi plexApi, - ISettingsService auth) : base("wizard", pr) + ISettingsService auth, ICustomUserMapper m) : base("wizard", pr) { PlexSettings = plex; PlexApi = plexApi; PlexRequestSettings = pr; Auth = auth; + Mapper = m; - Get["/"] = x => Index(); + Get["/", true] = async (x, ct) => + { + var settings = await PlexRequestSettings.GetSettingsAsync(); + if (settings.Wizard) + { + return Context.GetRedirect("~/search"); + } + return View["Index"]; + }; Post["/plexAuth"] = x => PlexAuth(); - Post["/plex", true] = async (x,ct) => await Plex(); - Post["/plexrequest", true] = async (x,ct) => await PlexRequest(); - Post["/auth", true] = async (x,ct) => await Authentication(); + Post["/plex", true] = async (x, ct) => await Plex(); + Post["/plexrequest", true] = async (x, ct) => await PlexRequest(); + Post["/auth", true] = async (x, ct) => await Authentication(); + Post["/createuser",true] = async (x,ct) => await CreateUser(); } private ISettingsService PlexSettings { get; } private IPlexApi PlexApi { get; } private ISettingsService PlexRequestSettings { get; } private ISettingsService Auth { get; } + private ICustomUserMapper Mapper { get; } - private Negotiator Index() - { - return View["Index"]; - } - - - - + private Response PlexAuth() { var user = this.Bind(); @@ -141,5 +148,22 @@ namespace PlexRequests.UI.Modules } return Response.AsJson(new JsonResponseModel { Result = false, Message = "Could not save the settings to the database, please try again." }); } + + private async Task CreateUser() + { + var username = (string)Request.Form.Username; + var userId = Mapper.CreateAdmin(username, Request.Form.Password); + Session[SessionKeys.UsernameKey] = username; + + // Destroy the Plex Auth Token + Session.Delete(SessionKeys.UserWizardPlexAuth); + + // Update the settings so we know we have been through the wizard + var settings = await PlexRequestSettings.GetSettingsAsync(); + settings.Wizard = true; + await PlexRequestSettings.SaveSettingsAsync(settings); + + return this.LoginAndRedirect((Guid)userId, fallbackRedirectUrl: "/search"); + } } } \ No newline at end of file diff --git a/PlexRequests.UI/Views/UserWizard/Index.cshtml b/PlexRequests.UI/Views/UserWizard/Index.cshtml index 2ec4a6610..665af33db 100644 --- a/PlexRequests.UI/Views/UserWizard/Index.cshtml +++ b/PlexRequests.UI/Views/UserWizard/Index.cshtml @@ -32,7 +32,7 @@ + + \ No newline at end of file