Fixed an issue where there were some JS errors on the landing page settings and stopped us being redirected to the login sometimes as an admin

This commit is contained in:
tidusjar 2016-08-25 21:43:49 +01:00
parent c235018e98
commit 2aebbe0259
3 changed files with 159 additions and 151 deletions

View file

@ -118,9 +118,10 @@ namespace PlexRequests.UI.Helpers
public static IHtmlString LoadDateTimePickerAsset(this HtmlHelpers helper) public static IHtmlString LoadDateTimePickerAsset(this HtmlHelpers helper)
{ {
var startUrl = GetBaseUrl(); var content = GetBaseUrl();
var sb = new StringBuilder(); var sb = new StringBuilder();
var startUrl = $"{content}/Content";
sb.AppendLine($"<link rel=\"stylesheet\" href=\"{startUrl}/datepicker.min.css\" type=\"text/css\"/>"); sb.AppendLine($"<link rel=\"stylesheet\" href=\"{startUrl}/datepicker.min.css\" type=\"text/css\"/>");
sb.AppendLine($"<script src=\"{startUrl}/bootstrap-datetimepicker.min.js\"></script>"); sb.AppendLine($"<script src=\"{startUrl}/bootstrap-datetimepicker.min.js\"></script>");

View file

@ -122,7 +122,7 @@ namespace PlexRequests.UI.Modules
{ {
get get
{ {
if (Context?.CurrentUser == null) if (!LoggedIn)
{ {
return false; return false;
} }
@ -130,6 +130,9 @@ namespace PlexRequests.UI.Modules
return claims.Contains(UserClaims.Admin) || claims.Contains(UserClaims.PowerUser); return claims.Contains(UserClaims.Admin) || claims.Contains(UserClaims.PowerUser);
} }
} }
protected bool LoggedIn => Context?.CurrentUser != null;
protected string Culture { get; set; } protected string Culture { get; set; }
protected const string CultureCookieName = "_culture"; protected const string CultureCookieName = "_culture";
protected Response SetCookie() protected Response SetCookie()

View file

@ -1,150 +1,154 @@
#region Copyright #region Copyright
// /************************************************************************ // /************************************************************************
// Copyright (c) 2016 Jamie Rees // Copyright (c) 2016 Jamie Rees
// File: LoginModule.cs // File: LoginModule.cs
// Created By: Jamie Rees // Created By: Jamie Rees
// //
// Permission is hereby granted, free of charge, to any person obtaining // Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the // a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including // "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish, // without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to // distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to // permit persons to whom the Software is furnished to do so, subject to
// the following conditions: // the following conditions:
// //
// The above copyright notice and this permission notice shall be // The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software. // included in all copies or substantial portions of the Software.
// //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/ // ************************************************************************/
#endregion #endregion
using System; using System;
using System.Dynamic; using System.Dynamic;
using Nancy; using Nancy;
using Nancy.Authentication.Forms; using Nancy.Authentication.Forms;
using Nancy.Extensions; using Nancy.Extensions;
using Nancy.Responses.Negotiation; using Nancy.Linker;
using Nancy.Security; using Nancy.Responses.Negotiation;
using Nancy.Security;
using PlexRequests.Core;
using PlexRequests.Core.SettingModels; using PlexRequests.Core;
using PlexRequests.Helpers; using PlexRequests.Core.SettingModels;
using PlexRequests.UI.Models; using PlexRequests.Helpers;
using PlexRequests.UI.Models;
namespace PlexRequests.UI.Modules
{ namespace PlexRequests.UI.Modules
public class LoginModule : BaseModule {
{ public class LoginModule : BaseModule
public LoginModule(ISettingsService<PlexRequestSettings> pr, ICustomUserMapper m) : base(pr) {
{ public LoginModule(ISettingsService<PlexRequestSettings> pr, ICustomUserMapper m, IResourceLinker linker) : base(pr)
UserMapper = m; {
Get["/login"] = _ => UserMapper = m;
{ Get["/login"] = _ =>
{ {
dynamic model = new ExpandoObject(); if (LoggedIn)
model.Redirect = Request.Query.redirect.Value ?? string.Empty; {
model.Errored = Request.Query.error.HasValue; var url = linker.BuildRelativeUri(Context, "SearchIndex");
var adminCreated = UserMapper.DoUsersExist(); return Response.AsRedirect(url.ToString());
model.AdminExists = adminCreated; }
return View["Index", model]; dynamic model = new ExpandoObject();
} model.Redirect = Request.Query.redirect.Value ?? string.Empty;
model.Errored = Request.Query.error.HasValue;
}; var adminCreated = UserMapper.DoUsersExist();
model.AdminExists = adminCreated;
Get["/logout"] = x => this.LogoutAndRedirect(!string.IsNullOrEmpty(BaseUrl) ? $"~/{BaseUrl}/" : "~/"); return View["Index", model];
};
Post["/login"] = x =>
{ Get["/logout"] = x => this.LogoutAndRedirect(!string.IsNullOrEmpty(BaseUrl) ? $"~/{BaseUrl}/" : "~/");
var username = (string)Request.Form.Username;
var password = (string)Request.Form.Password; Post["/login"] = x =>
var dtOffset = (int)Request.Form.DateTimeOffset; {
var redirect = (string)Request.Form.Redirect; var username = (string)Request.Form.Username;
var password = (string)Request.Form.Password;
var userId = UserMapper.ValidateUser(username, password); var dtOffset = (int)Request.Form.DateTimeOffset;
var redirect = (string)Request.Form.Redirect;
if (userId == null)
{ var userId = UserMapper.ValidateUser(username, password);
return Context.GetRedirect(!string.IsNullOrEmpty(BaseUrl) ? $"~/{BaseUrl}/login?error=true&username=" + username : "~/login?error=true&username=" + username);
} if (userId == null)
DateTime? expiry = null; {
if (Request.Form.RememberMe.HasValue) return Context.GetRedirect(!string.IsNullOrEmpty(BaseUrl) ? $"~/{BaseUrl}/login?error=true&username=" + username : "~/login?error=true&username=" + username);
{ }
expiry = DateTime.Now.AddDays(7); DateTime? expiry = null;
} if (Request.Form.RememberMe.HasValue)
Session[SessionKeys.UsernameKey] = username; {
Session[SessionKeys.ClientDateTimeOffsetKey] = dtOffset; expiry = DateTime.Now.AddDays(7);
if(redirect.Contains("userlogin")){ }
redirect = !string.IsNullOrEmpty(BaseUrl) ? $"/{BaseUrl}/search" : "/search"; Session[SessionKeys.UsernameKey] = username;
} Session[SessionKeys.ClientDateTimeOffsetKey] = dtOffset;
return this.LoginAndRedirect(userId.Value, expiry, redirect); if (redirect.Contains("userlogin"))
}; {
redirect = !string.IsNullOrEmpty(BaseUrl) ? $"/{BaseUrl}/search" : "/search";
Get["/register"] = x => }
{ return this.LoginAndRedirect(userId.Value, expiry, redirect);
{ };
dynamic model = new ExpandoObject();
model.Errored = Request.Query.error.HasValue; Get["/register"] = x =>
{
return View["Register", model]; {
} dynamic model = new ExpandoObject();
}; model.Errored = Request.Query.error.HasValue;
Post["/register"] = x => return View["Register", model];
{ }
var username = (string)Request.Form.Username; };
var exists = UserMapper.DoUsersExist();
if (exists) Post["/register"] = x =>
{ {
return Context.GetRedirect(!string.IsNullOrEmpty(BaseUrl) ? $"~/{BaseUrl}/register?error=true" : "~/register?error=true"); var username = (string)Request.Form.Username;
} var exists = UserMapper.DoUsersExist();
var userId = UserMapper.CreateAdmin(username, Request.Form.Password); if (exists)
Session[SessionKeys.UsernameKey] = username; {
return this.LoginAndRedirect((Guid)userId); return Context.GetRedirect(!string.IsNullOrEmpty(BaseUrl) ? $"~/{BaseUrl}/register?error=true" : "~/register?error=true");
}; }
var userId = UserMapper.CreateAdmin(username, Request.Form.Password);
Get["/changepassword"] = _ => ChangePassword(); Session[SessionKeys.UsernameKey] = username;
Post["/changepassword"] = _ => ChangePasswordPost(); return this.LoginAndRedirect((Guid)userId);
} };
private ICustomUserMapper UserMapper { get; }
Get["/changepassword"] = _ => ChangePassword();
private Negotiator ChangePassword() Post["/changepassword"] = _ => ChangePasswordPost();
{ }
this.RequiresAuthentication(); private ICustomUserMapper UserMapper { get; }
return View["ChangePassword"];
} private Negotiator ChangePassword()
{
private Response ChangePasswordPost() this.RequiresAuthentication();
{ return View["ChangePassword"];
var username = Context.CurrentUser.UserName; }
var oldPass = Request.Form.OldPassword;
var newPassword = Request.Form.NewPassword; private Response ChangePasswordPost()
var newPasswordAgain = Request.Form.NewPasswordAgain; {
var username = Context.CurrentUser.UserName;
if (string.IsNullOrEmpty(oldPass) || string.IsNullOrEmpty(newPassword) || var oldPass = Request.Form.OldPassword;
string.IsNullOrEmpty(newPasswordAgain)) var newPassword = Request.Form.NewPassword;
{ var newPasswordAgain = Request.Form.NewPasswordAgain;
return Response.AsJson(new JsonResponseModel { Message = "Please fill in all fields", Result = false });
} if (string.IsNullOrEmpty(oldPass) || string.IsNullOrEmpty(newPassword) ||
string.IsNullOrEmpty(newPasswordAgain))
if (!newPassword.Equals(newPasswordAgain)) {
{ return Response.AsJson(new JsonResponseModel { Message = "Please fill in all fields", Result = false });
return Response.AsJson(new JsonResponseModel { Message = "The passwords do not match", Result = false }); }
}
if (!newPassword.Equals(newPasswordAgain))
var result = UserMapper.UpdatePassword(username, oldPass, newPassword); {
if (result) return Response.AsJson(new JsonResponseModel { Message = "The passwords do not match", Result = false });
{ }
return Response.AsJson(new JsonResponseModel { Message = "Password has been changed!", Result = true });
} var result = UserMapper.UpdatePassword(username, oldPass, newPassword);
if (result)
return Response.AsJson(new JsonResponseModel { Message = "Could not update the password in the database", Result = false }); {
} return Response.AsJson(new JsonResponseModel { Message = "Password has been changed!", Result = true });
} }
return Response.AsJson(new JsonResponseModel { Message = "Could not update the password in the database", Result = false });
}
}
} }