|
|
|
@ -4,6 +4,7 @@ using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Dynamic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Web.UI;
|
|
|
|
|
using Nancy;
|
|
|
|
|
using Nancy.Extensions;
|
|
|
|
|
using Nancy.ModelBinding;
|
|
|
|
@ -23,73 +24,93 @@ namespace RequestPlex.UI.Modules
|
|
|
|
|
#if !DEBUG
|
|
|
|
|
this.RequiresAuthentication();
|
|
|
|
|
#endif
|
|
|
|
|
Get["admin/"] = _ =>
|
|
|
|
|
{
|
|
|
|
|
dynamic model = new ExpandoObject();
|
|
|
|
|
model.Errored = Request.Query.error.HasValue;
|
|
|
|
|
model.Port = null;
|
|
|
|
|
var s = new SettingsService();
|
|
|
|
|
var settings = s.GetSettings();
|
|
|
|
|
if (settings != null)
|
|
|
|
|
{
|
|
|
|
|
model.Port = settings.Port;
|
|
|
|
|
model.PlexAuthToken = settings.PlexAuthToken;
|
|
|
|
|
}
|
|
|
|
|
Get["admin/"] = _ => Admin();
|
|
|
|
|
|
|
|
|
|
Post["admin/"] = _ => SaveAdmin();
|
|
|
|
|
|
|
|
|
|
Post["admin/requestauth"] = _ => RequestAuthToken();
|
|
|
|
|
|
|
|
|
|
Get["admin/getusers"] = _ => GetUsers();
|
|
|
|
|
|
|
|
|
|
Get["admin/couchpotato"] = _ => CouchPotato();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return View["/Admin/Settings", model];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Post["admin/"] = _ =>
|
|
|
|
|
private Response Admin()
|
|
|
|
|
{
|
|
|
|
|
dynamic model = new ExpandoObject();
|
|
|
|
|
model.Errored = Request.Query.error.HasValue;
|
|
|
|
|
model.Port = null;
|
|
|
|
|
var s = new SettingsService();
|
|
|
|
|
var settings = s.GetSettings();
|
|
|
|
|
if (settings != null)
|
|
|
|
|
{
|
|
|
|
|
var model = this.Bind<SettingsModel>();
|
|
|
|
|
model.Port = settings.Port;
|
|
|
|
|
model.PlexAuthToken = settings.PlexAuthToken;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return View["/Admin/Settings", model];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var s = new SettingsService();
|
|
|
|
|
s.SaveSettings(model);
|
|
|
|
|
private Response SaveAdmin()
|
|
|
|
|
{
|
|
|
|
|
var model = this.Bind<SettingsModel>();
|
|
|
|
|
|
|
|
|
|
var s = new SettingsService();
|
|
|
|
|
s.SaveSettings(model);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return Context.GetRedirect("~/admin");
|
|
|
|
|
};
|
|
|
|
|
return Context.GetRedirect("~/admin");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Post["admin/requestauth"] = _ =>
|
|
|
|
|
private Response RequestAuthToken()
|
|
|
|
|
{
|
|
|
|
|
var user = this.Bind<PlexAuth>();
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(user.username) || string.IsNullOrEmpty(user.password))
|
|
|
|
|
{
|
|
|
|
|
var user = this.Bind<PlexAuth>();
|
|
|
|
|
return Context.GetRedirect("~/admin?error=true");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(user.username) || string.IsNullOrEmpty(user.password))
|
|
|
|
|
{
|
|
|
|
|
return Context.GetRedirect("~/admin?error=true");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var plex = new PlexApi();
|
|
|
|
|
var model = plex.GetToken(user.username, user.password);
|
|
|
|
|
var s = new SettingsService();
|
|
|
|
|
var oldSettings = s.GetSettings();
|
|
|
|
|
if (oldSettings != null)
|
|
|
|
|
{
|
|
|
|
|
oldSettings.PlexAuthToken = model.user.authentication_token;
|
|
|
|
|
s.SaveSettings(oldSettings);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
var plex = new PlexApi();
|
|
|
|
|
var model = plex.GetToken(user.username, user.password);
|
|
|
|
|
var s = new SettingsService();
|
|
|
|
|
var oldSettings = s.GetSettings();
|
|
|
|
|
if (oldSettings != null)
|
|
|
|
|
{
|
|
|
|
|
oldSettings.PlexAuthToken = model.user.authentication_token;
|
|
|
|
|
s.SaveSettings(oldSettings);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var newModel = new SettingsModel
|
|
|
|
|
{
|
|
|
|
|
var newModel = new SettingsModel
|
|
|
|
|
{
|
|
|
|
|
PlexAuthToken = model.user.authentication_token
|
|
|
|
|
};
|
|
|
|
|
s.SaveSettings(newModel);
|
|
|
|
|
}
|
|
|
|
|
PlexAuthToken = model.user.authentication_token
|
|
|
|
|
};
|
|
|
|
|
s.SaveSettings(newModel);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Context.GetRedirect("~/admin");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Response GetUsers()
|
|
|
|
|
{
|
|
|
|
|
var s = new SettingsService();
|
|
|
|
|
var token = s.GetSettings().PlexAuthToken;
|
|
|
|
|
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.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Context.GetRedirect("~/admin");
|
|
|
|
|
};
|
|
|
|
|
private Response CouchPotato()
|
|
|
|
|
{
|
|
|
|
|
dynamic model = new ExpandoObject();
|
|
|
|
|
|
|
|
|
|
Get["admin/getusers"] = _ =>
|
|
|
|
|
{
|
|
|
|
|
var api = new PlexApi();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return View["/Admin/Settings"];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return View["/Admin/CouchPotato", model];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|