You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Ombi/RequestPlex.UI/Modules/AdminModule.cs

131 lines
4.3 KiB

#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: AdminModule.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
9 years ago
using System.Dynamic;
9 years ago
using System.Linq;
9 years ago
using System.Web.UI;
using Nancy;
9 years ago
using Nancy.Extensions;
9 years ago
using Nancy.ModelBinding;
using Nancy.Responses.Negotiation;
9 years ago
using Nancy.Security;
9 years ago
using RequestPlex.Api;
9 years ago
using RequestPlex.Core;
using RequestPlex.Core.SettingModels;
9 years ago
using RequestPlex.UI.Models;
namespace RequestPlex.UI.Modules
{
public class AdminModule : NancyModule
{
9 years ago
private ISettingsService<RequestPlexSettings> Service { get; set; }
public AdminModule(ISettingsService<RequestPlexSettings> service)
{
9 years ago
Service = service;
9 years ago
#if !DEBUG
9 years ago
this.RequiresAuthentication();
9 years ago
#endif
9 years ago
Get["admin/"] = _ => Admin();
Post["admin/"] = _ => SaveAdmin();
Post["admin/requestauth"] = _ => RequestAuthToken();
Get["admin/getusers"] = _ => GetUsers();
Get["admin/couchpotato"] = _ => CouchPotato();
}
9 years ago
private Negotiator Admin()
9 years ago
{
dynamic model = new ExpandoObject();
9 years ago
var settings = Service.GetSettings();
9 years ago
model = settings;
9 years ago
return View["/Admin/Settings", model];
}
9 years ago
9 years ago
private Response SaveAdmin()
{
9 years ago
var model = this.Bind<RequestPlexSettings>();
9 years ago
9 years ago
Service.SaveSettings(model);
9 years ago
9 years ago
return Context.GetRedirect("~/admin");
}
9 years ago
9 years ago
private Response RequestAuthToken()
{
var user = this.Bind<PlexAuth>();
if (string.IsNullOrEmpty(user.username) || string.IsNullOrEmpty(user.password))
9 years ago
{
9 years ago
return Context.GetRedirect("~/admin?error=true");
}
9 years ago
9 years ago
var plex = new PlexApi();
var model = plex.GetToken(user.username, user.password);
9 years ago
var oldSettings = Service.GetSettings();
9 years ago
if (oldSettings != null)
{
oldSettings.PlexAuthToken = model.user.authentication_token;
9 years ago
Service.SaveSettings(oldSettings);
9 years ago
}
else
{
9 years ago
var newModel = new RequestPlexSettings
9 years ago
{
9 years ago
PlexAuthToken = model.user.authentication_token
};
9 years ago
Service.SaveSettings(newModel);
9 years ago
}
return Response.AsJson(new {Result = true, AuthToken = model.user.authentication_token});
9 years ago
}
9 years ago
9 years ago
9 years ago
private Response GetUsers()
{
9 years ago
var token = Service.GetSettings().PlexAuthToken;
9 years ago
var api = new PlexApi();
var users = api.GetUsers(token);
var usernames = users.User.Select(x => x.Username);
return Response.AsJson(usernames); //TODO usernames are not populated.
}
9 years ago
9 years ago
private Response CouchPotato()
{
dynamic model = new ExpandoObject();
9 years ago
9 years ago
9 years ago
return View["/Admin/CouchPotato", model];
}
}
}