This commit is contained in:
tidusjar 2016-06-13 13:01:22 +01:00
commit 3e16d8acf0
18 changed files with 399 additions and 57 deletions

View file

@ -86,7 +86,9 @@ namespace PlexRequests.UI.Modules
private ICacheProvider Cache { get; }
private ISettingsService<SlackNotificationSettings> SlackSettings { get; }
private ISettingsService<LandingPageSettings> LandingSettings { get; }
private ISettingsService<ScheduledJobsSettings> ScheduledJobSettings { get; }
private ISlackApi SlackApi { get; }
private IJobRecord JobRecorder { get; }
private static Logger Log = LogManager.GetCurrentClassLogger();
public AdminModule(ISettingsService<PlexRequestSettings> prService,
@ -108,7 +110,8 @@ namespace PlexRequests.UI.Modules
ISettingsService<HeadphonesSettings> headphones,
ISettingsService<LogSettings> logs,
ICacheProvider cache, ISettingsService<SlackNotificationSettings> slackSettings,
ISlackApi slackApi, ISettingsService<LandingPageSettings> lp) : base("admin", prService)
ISlackApi slackApi, ISettingsService<LandingPageSettings> lp,
ISettingsService<ScheduledJobsSettings> scheduler, IJobRecord rec) : base("admin", prService)
{
PrService = prService;
CpService = cpService;
@ -132,6 +135,8 @@ namespace PlexRequests.UI.Modules
SlackSettings = slackSettings;
SlackApi = slackApi;
LandingSettings = lp;
ScheduledJobSettings = scheduler;
JobRecorder = rec;
this.RequiresClaims(UserClaims.Admin);
@ -193,6 +198,9 @@ namespace PlexRequests.UI.Modules
Get["/landingpage", true] = async (x, ct) => await LandingPage();
Post["/landingpage", true] = async (x, ct) => await SaveLandingPage();
Get["/scheduledjobs", true] = async (x, ct) => await GetScheduledJobs();
Post["/scheduledjobs", true] = async (x, ct) => await SaveScheduledJobs();
}
private async Task<Negotiator> Authentication()
@ -835,5 +843,34 @@ namespace PlexRequests.UI.Modules
? new JsonResponseModel { Result = true }
: new JsonResponseModel { Result = false, Message = "Could not save to Db Please check the logs" });
}
private async Task<Negotiator> GetScheduledJobs()
{
var s = await ScheduledJobSettings.GetSettingsAsync();
var allJobs = await JobRecorder.GetJobsAsync();
var jobsDict = allJobs.ToDictionary(k => k.Name, v => v.LastRun);
var model = new ScheduledJobsViewModel
{
CouchPotatoCacher = s.CouchPotatoCacher,
PlexAvailabilityChecker = s.PlexAvailabilityChecker,
SickRageCacher = s.SickRageCacher,
SonarrCacher = s.SonarrCacher,
StoreBackup = s.StoreBackup,
StoreCleanup = s.StoreCleanup,
JobRecorder = jobsDict
};
return View["SchedulerSettings", model];
}
private async Task<Response> SaveScheduledJobs()
{
var settings = this.Bind<ScheduledJobsSettings>();
var result = await ScheduledJobSettings.SaveSettingsAsync(settings);
return Response.AsJson(result
? new JsonResponseModel { Result = true }
: new JsonResponseModel { Result = false, Message = "Could not save to Db Please check the logs" });
}
}
}

View file

@ -23,64 +23,19 @@
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
using System.Linq;
#endregion
using Nancy;
using Nancy.Extensions;
using PlexRequests.UI.Models;
using System;
using PlexRequests.Core;
using PlexRequests.Core.SettingModels;
using PlexRequests.Helpers;
namespace PlexRequests.UI.Modules
{
public abstract 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 bool IsAdmin
{
get
{
if (Context.CurrentUser == null)
{
return false;
}
var claims = Context.CurrentUser.Claims.ToList();
return claims.Contains(UserClaims.Admin) || claims.Contains(UserClaims.PowerUser);
}
}
protected int DateTimeOffset
{
get
{
if (_dateTimeOffset == -1)
{
_dateTimeOffset = (int?)Session[SessionKeys.ClientDateTimeOffsetKey] ?? new DateTimeOffset().Offset.Minutes;
}
return _dateTimeOffset;
}
}
protected BaseAuthModule(ISettingsService<PlexRequestSettings> pr) : base(pr)
{
PlexRequestSettings = pr;
@ -101,7 +56,7 @@ namespace PlexRequests.UI.Modules
var baseUrl = settings.BaseUrl;
var redirectPath = string.IsNullOrEmpty(baseUrl) ? "~/userlogin" : $"~/{baseUrl}/userlogin";
return Session[SessionKeys.UsernameKey] == null
? Context.GetRedirect(redirectPath)
: null;

View file

@ -24,10 +24,15 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ************************************************************************/
#endregion
using System;
using System.Linq;
using Nancy;
using PlexRequests.Core;
using PlexRequests.Core.SettingModels;
using PlexRequests.Helpers;
using PlexRequests.UI.Models;
namespace PlexRequests.UI.Modules
{
@ -56,5 +61,45 @@ namespace PlexRequests.UI.Modules
ModulePath = settingModulePath;
}
private int _dateTimeOffset = -1;
protected int DateTimeOffset
{
get
{
if (_dateTimeOffset == -1)
{
_dateTimeOffset = (int?)Session[SessionKeys.ClientDateTimeOffsetKey] ?? new DateTimeOffset().Offset.Minutes;
}
return _dateTimeOffset;
}
}
private string _username;
protected string Username
{
get
{
if (string.IsNullOrEmpty(_username))
{
_username = Session[SessionKeys.UsernameKey].ToString();
}
return _username;
}
}
protected bool IsAdmin
{
get
{
if (Context.CurrentUser == null)
{
return false;
}
var claims = Context.CurrentUser.Claims.ToList();
return claims.Contains(UserClaims.Admin) || claims.Contains(UserClaims.PowerUser);
}
}
}
}