Finally fixed #72

This commit is contained in:
tidusjar 2016-04-14 14:50:40 +01:00
commit 6190eceb60
22 changed files with 320 additions and 94 deletions

View file

@ -39,7 +39,7 @@ using PlexRequests.UI.Models;
namespace PlexRequests.UI.Modules
{
public class ApplicationTesterModule : BaseModule
public class ApplicationTesterModule : BaseAuthModule
{
public ApplicationTesterModule(ICouchPotatoApi cpApi, ISonarrApi sonarrApi, IPlexApi plexApi,

View file

@ -43,7 +43,7 @@ using PlexRequests.UI.Models;
namespace PlexRequests.UI.Modules
{
public class ApprovalModule : BaseModule
public class ApprovalModule : BaseAuthModule
{
public ApprovalModule(IRequestService service, ISettingsService<CouchPotatoSettings> cpService, ICouchPotatoApi cpApi, ISonarrApi sonarrApi,

View file

@ -0,0 +1,85 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: BaseAuthModule.cs
// Created By: Jamie Rees
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
#endregion
using Nancy;
using Nancy.Extensions;
using PlexRequests.UI.Models;
using System;
namespace PlexRequests.UI.Modules
{
public class BaseAuthModule : BaseModule
{
private string _username;
private int _dateTimeOffset = -1;
protected string Username
{
get
{
if (string.IsNullOrEmpty(_username))
{
_username = Session[SessionKeys.UsernameKey].ToString();
}
return _username;
}
}
protected int DateTimeOffset
{
get
{
if (_dateTimeOffset == -1)
{
_dateTimeOffset = (int?)Session[SessionKeys.ClientDateTimeOffsetKey] ?? new DateTimeOffset().Offset.Minutes;
}
return _dateTimeOffset;
}
}
public BaseAuthModule()
{
Before += (ctx) => CheckAuth();
}
public BaseAuthModule(string modulePath) : base(modulePath)
{
Before += (ctx) => CheckAuth();
}
private Response CheckAuth()
{
if (Session[SessionKeys.UsernameKey] == null)
{
return Context.GetRedirect("~/test/userlogin");
}
return null;
}
}
}

View file

@ -24,63 +24,35 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
#endregion
using Nancy;
using Nancy.Extensions;
using PlexRequests.UI.Models;
using System;
using PlexRequests.Core;
using PlexRequests.Core.SettingModels;
using PlexRequests.UI.Helpers;
namespace PlexRequests.UI.Modules
{
public class BaseModule : NancyModule
{
private string _username;
private int _dateTimeOffset = -1;
protected string Username
{
get
{
if (string.IsNullOrEmpty(_username))
{
_username = Session[SessionKeys.UsernameKey].ToString();
}
return _username;
}
}
protected int DateTimeOffset
{
get
{
if (_dateTimeOffset == -1)
{
_dateTimeOffset = Session[SessionKeys.ClientDateTimeOffsetKey] != null ?
(int)Session[SessionKeys.ClientDateTimeOffsetKey] : (new DateTimeOffset().Offset).Minutes;
}
return _dateTimeOffset;
}
}
private ServiceLocator Locator => ServiceLocator.Instance;
public BaseModule()
{
Before += (ctx) => CheckAuth();
var settings = Locator.Resolve<ISettingsService<PlexRequestSettings>>().GetSettings();
var baseUrl = settings.BaseUrl;
var modulePath = string.IsNullOrEmpty(baseUrl) ? string.Empty : baseUrl;
ModulePath = modulePath;
}
public BaseModule(string modulePath) : base(modulePath)
public BaseModule(string modulePath)
{
Before += (ctx) => CheckAuth();
var settings = Locator.Resolve<ISettingsService<PlexRequestSettings>>().GetSettings();
var baseUrl = settings.BaseUrl;
var settingModulePath = string.IsNullOrEmpty(baseUrl) ? modulePath : $"{baseUrl}/{modulePath}";
ModulePath = settingModulePath;
}
private Response CheckAuth()
{
if (Session[SessionKeys.UsernameKey] == null)
{
return Context.GetRedirect("~/userlogin");
}
return null;
}
}
}

View file

@ -29,7 +29,7 @@ using Nancy.Extensions;
namespace PlexRequests.UI.Modules
{
public class IndexModule : BaseModule
public class IndexModule : BaseAuthModule
{
public IndexModule()
{

View file

@ -46,7 +46,7 @@ using System.Threading.Tasks;
namespace PlexRequests.UI.Modules
{
public class RequestsModule : BaseModule
public class RequestsModule : BaseAuthModule
{
public RequestsModule(
IRequestService service,

View file

@ -55,7 +55,7 @@ using TMDbLib.Objects.General;
namespace PlexRequests.UI.Modules
{
public class SearchModule : BaseModule
public class SearchModule : BaseAuthModule
{
public SearchModule(ICacheProvider cache, ISettingsService<CouchPotatoSettings> cpSettings,
ISettingsService<PlexRequestSettings> prSettings, IAvailabilityChecker checker,

View file

@ -44,7 +44,7 @@ using PlexRequests.UI.Models;
namespace PlexRequests.UI.Modules
{
public class UserLoginModule : NancyModule
public class UserLoginModule : BaseModule
{
public UserLoginModule(ISettingsService<AuthenticationSettings> auth, IPlexApi api) : base("userlogin")
{