Fixed the plex friends. Added some unit tests, moved the plex auth into it's own page

This commit is contained in:
tidusjar 2016-03-04 12:45:42 +00:00
commit 07beddc26a
22 changed files with 602 additions and 151 deletions

View file

@ -56,8 +56,9 @@ namespace PlexRequests.UI
container.Register<ISettingsRepository, JsonRepository>();
container.Register<ICacheProvider, MemoryCacheProvider>();
container.Register<ISettingsService<RequestPlexSettings>, SettingsServiceV2<RequestPlexSettings>>();
container.Register<ISettingsService<PlexRequestSettings>, SettingsServiceV2<PlexRequestSettings>>();
container.Register<ISettingsService<CouchPotatoSettings>, SettingsServiceV2<CouchPotatoSettings>>();
container.Register<ISettingsService<AuthenticationSettings>, SettingsServiceV2<AuthenticationSettings>>();
container.Register<IRepository<RequestedModel>, GenericRepository<RequestedModel>>();
base.ConfigureRequestContainer(container, context);

View file

@ -42,17 +42,22 @@ namespace PlexRequests.UI.Modules
{
public class AdminModule : NancyModule
{
private ISettingsService<RequestPlexSettings> RpService { get; set; }
private ISettingsService<PlexRequestSettings> RpService { get; set; }
private ISettingsService<CouchPotatoSettings> CpService { get; set; }
public AdminModule(ISettingsService<RequestPlexSettings> rpService, ISettingsService<CouchPotatoSettings> cpService ) : base("admin")
private ISettingsService<AuthenticationSettings> AuthService { get; set; }
public AdminModule(ISettingsService<PlexRequestSettings> rpService, ISettingsService<CouchPotatoSettings> cpService, ISettingsService<AuthenticationSettings> auth) : base("admin")
{
RpService = rpService;
CpService = cpService;
AuthService = auth;
#if !DEBUG
this.RequiresAuthentication();
#endif
Get["/"] = _ => Admin();
Get["/authentication"] = _ => Authentication();
Post["/authentication"] = _ => SaveAuthentication();
Post["/"] = _ => SaveAdmin();
Post["/requestauth"] = _ => RequestAuthToken();
@ -63,6 +68,24 @@ namespace PlexRequests.UI.Modules
Post["/couchpotato"] = _ => SaveCouchPotato();
}
private Negotiator Authentication()
{
var settings = AuthService.GetSettings();
return View["/Authentication", settings];
}
private Response SaveAuthentication()
{
var model = this.Bind<AuthenticationSettings>();
var result = AuthService.SaveSettings(model);
if (result)
{
return Context.GetRedirect("~/admin/authentication");
}
return Context.GetRedirect("~/error"); //TODO create error page
}
private Negotiator Admin()
{
@ -70,12 +93,12 @@ namespace PlexRequests.UI.Modules
var settings = RpService.GetSettings();
model = settings;
return View["/Admin/Settings", model];
return View["/Settings", model];
}
private Response SaveAdmin()
{
var model = this.Bind<RequestPlexSettings>();
var model = this.Bind<PlexRequestSettings>();
RpService.SaveSettings(model);
@ -89,24 +112,24 @@ namespace PlexRequests.UI.Modules
if (string.IsNullOrEmpty(user.username) || string.IsNullOrEmpty(user.password))
{
return Context.GetRedirect("~/admin?error=true");
return Response.AsJson(new { Result = false, Message = "Please provide a valid username and password" });
}
var plex = new PlexApi();
var model = plex.GetToken(user.username, user.password);
var oldSettings = RpService.GetSettings();
var oldSettings = AuthService.GetSettings();
if (oldSettings != null)
{
oldSettings.PlexAuthToken = model.user.authentication_token;
RpService.SaveSettings(oldSettings);
AuthService.SaveSettings(oldSettings);
}
else
{
var newModel = new RequestPlexSettings
var newModel = new AuthenticationSettings
{
PlexAuthToken = model.user.authentication_token
};
RpService.SaveSettings(newModel);
AuthService.SaveSettings(newModel);
}
return Response.AsJson(new {Result = true, AuthToken = model.user.authentication_token});
@ -115,7 +138,7 @@ namespace PlexRequests.UI.Modules
private Response GetUsers()
{
var token = RpService.GetSettings().PlexAuthToken;
var token = AuthService.GetSettings().PlexAuthToken;
var api = new PlexApi();
var users = api.GetUsers(token);
var usernames = users.User.Select(x => x.Username);
@ -130,6 +153,7 @@ namespace PlexRequests.UI.Modules
return View["/Admin/CouchPotato", model];
}
private Response SaveCouchPotato()
{
var couchPotatoSettings = this.Bind<CouchPotatoSettings>();

View file

@ -78,7 +78,7 @@ namespace PlexRequests.UI.Modules
RequestedBy = tv.RequestedBy,
ReleaseYear = tv.ReleaseDate.Year.ToString()
}).ToList();
//TODO check if Available
//TODO check if Available in CP
return Response.AsJson(viewModel);
}
@ -101,7 +101,7 @@ namespace PlexRequests.UI.Modules
RequestedBy = tv.RequestedBy,
ReleaseYear = tv.ReleaseDate.Year.ToString()
}).ToList();
//TODO check if Available
//TODO check if Available in Sonarr
return Response.AsJson(viewModel);
}

View file

@ -244,6 +244,9 @@
<Content Include="Views\UserLogin\Index.cshtml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Views\Admin\Authentication.cshtml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<None Include="Web.Debug.config">
<DependentUpon>web.config</DependentUpon>
</None>

View file

@ -25,7 +25,6 @@
// ************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Data;
using Microsoft.Owin.Hosting;
@ -34,8 +33,6 @@ using Mono.Data.Sqlite;
using NLog;
using NLog.Config;
using NLog.LayoutRenderers;
using NLog.Layouts;
using NLog.Targets;
using PlexRequests.Core;
@ -77,7 +74,7 @@ namespace PlexRequests.UI
private static string GetStartupUri()
{
var uri = "http://localhost:3579/";
var service = new SettingsServiceV2<RequestPlexSettings>(new JsonRepository(new DbConfiguration(new SqliteFactory()), new MemoryCacheProvider()));
var service = new SettingsServiceV2<PlexRequestSettings>(new JsonRepository(new DbConfiguration(new SqliteFactory()), new MemoryCacheProvider()));
var settings = service.GetSettings();
if (settings.Port != 0)
@ -95,9 +92,13 @@ namespace PlexRequests.UI
var config = new LoggingConfiguration();
// Step 2. Create targets and add them to the configuration
var databaseTarget = new DatabaseTarget { CommandType = CommandType.Text,ConnectionString = connectionString,
var databaseTarget = new DatabaseTarget
{
CommandType = CommandType.Text,
ConnectionString = connectionString,
DBProvider = "Mono.Data.Sqlite, Version=4.0.0.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756",
Name = "database"};
Name = "database"
};
var messageParam = new DatabaseParameterInfo { Name = "@Message", Layout = "${message}" };
@ -131,7 +132,7 @@ namespace PlexRequests.UI
}
catch (Exception e)
{
throw;
}

View file

@ -0,0 +1,157 @@
@Html.Partial("/Admin/_Sidebar")
<div class="col-sm-8">
<form class="form-horizontal" method="POST" action="/admin/SaveAuthentication" id="mainForm">
<fieldset>
<legend>Authentication Settings</legend>
<div class="form-group">
<label for="userAuth" class="col-lg-2 control-label">Enable User Authentication</label>
<div class="col-lg-4 checkbox">
<label>
@if (Model.UserAuthentication)
{
<input type="checkbox" id="userAuth" name="UserAuthentication" checked="checked">
}
else
{
<input type="checkbox" id="userAuth" name="UserAuthentication">
}
</label>
</div>
</div>
<div class="form-group">
<label for="userAuth" class="col-lg-2 control-label">Require users to login with their passwords</label>
<div class="col-lg-4 checkbox">
<label>
@if (Model.UsePassword)
{
<input type="checkbox" id="UsePassword" name="UsePassword" checked="checked">
}
else
{
<input type="checkbox" id="UsePassword" name="UsePassword">
}
</label>
</div>
</div>
<div class="form-group">
<label for="authToken" class="col-lg-2 control-label">Plex Authorization Token</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="authToken" name="PlexAuthToken" placeholder="Plex Auth Token" value="@Model.PlexAuthToken">
</div>
</div>
<div class="form-group">
<label for="username" class="col-lg-2 control-label">Username and Password</label>
<div class="col-lg-4">
<input type="text" class="form-control" id="username" name="Username" placeholder="Username">
</div>
<div class="col-lg-4 col-lg-push-1">
<input type="password" class="form-control" id="password" name="Password" placeholder="Password">
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<button id="requestToken" class="btn btn-primary">Request Token <i class="fa fa-key"></i></button>
</div>
</div>
<br />
<br />
<small class="col-lg-offset-2">Current users that are allowed to authenticate: </small>
<br />
<br />
<div class="form-group">
<select id="users" multiple="" class="col-lg-10 col-lg-offset-2"></select>
</div>
<div class="form-group">
<br />
<br />
<div class="col-lg-10 col-lg-offset-2">
<button id="refreshUsers" class="btn btn-primary">Refresh Users</button>
</div>
</div>
<br />
<br />
<br />
<br />
<div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</fieldset>
</form>
</div>
<script>
$(function () {
if ($('#PlexAuthToken')) {
loadUserList();
}
$('#refreshUsers').click(function () {
e.preventDefault();
loadUserList();
});
$('#requestToken').click(function (e) {
e.preventDefault();
var $form = $("#mainForm");
$.ajax({
type: $form.prop("method"),
url: "requestauth",
data: $form.serialize(),
dataType: "json",
success: function (response) {
console.log(response);
if (response.result === true) {
generateNotify("Success!", "success");
$('#authToken').val(response.authToken);
} else {
generateNotify(response.message, "warning");
}
},
error: function (e) {
console.log(e);
generateNotify("Something went wrong!", "danger");
}
});
});
function loadUserList() {
$.ajax({
type: "Get",
url: "getusers",
dataType: "json",
success: function (response) {
if (response.length > 1) {
$(response).each(function(user) {
$('#users').append("<option>" + this + "</option>");
});
} else {
$('#users').append("<option>No Users!</option>");
}
},
error: function (e) {
console.log(e);
generateNotify("Something went wrong!", "danger");
}
});
}
});
</script>

View file

@ -65,63 +65,6 @@
</div>
</div>
<div class="form-group">
<label for="authToken" class="col-lg-2 control-label">Plex Authorization Token</label>
<div class="col-lg-10">
<input type="text" class="form-control" id="authToken" name="PlexAuthToken" placeholder="Plex Auth Token" value="@Model.PlexAuthToken">
</div>
</div>
<div class="form-group">
<label for="username" class="col-lg-2 control-label">Username and Password</label>
<div class="col-lg-4">
<input type="text" class="form-control" id="username" name="Username" placeholder="Username">
</div>
<div class="col-lg-4 col-lg-push-1">
<input type="password" class="form-control" id="password" name="Password" placeholder="Password">
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<button id="requestToken" class="btn btn-primary">Request Token</button>
</div>
</div>
<div class="form-group">
<label for="userAuth" class="col-lg-2 control-label">Enable User Authentication</label>
<div class="col-lg-4 checkbox">
<label>
@if (Model.UserAuthentication)
{
<input type="checkbox" id="userAuth" name="UserAuthentication" checked="checked">
}
else
{
<input type="checkbox" id="userAuth" name="UserAuthentication">
}
</label>
</div>
</div>
<br />
<br />
<small class="col-lg-offset-2">Current users that are allowed to authenticate: </small>
<br />
<br />
<div class="form-group">
<select id="users" multiple="" class="col-lg-10 col-lg-offset-2"></select>
</div>
<div class="form-group">
<br />
<br />
<div class="col-lg-10 col-lg-offset-2">
<button id="refreshUsers" class="btn btn-primary">Refresh Users</button>
</div>
</div>
<br />
<br />
<br />
<br />
<div>
</div>
<div class="form-group">
@ -131,68 +74,4 @@
</div>
</fieldset>
</form>
</div>
<script>
$(function () {
if ($('#PlexAuthToken')) {
loadUserList();
}
$('#refreshUsers').click(function () {
loadUserList();
});
$('#requestToken').click(function (e) {
e.preventDefault();
var $form = $("#mainForm");
$.ajax({
type: $form.prop("method"),
url: "admin/requestauth",
data: $form.serialize(),
dataType: "json",
success: function (response) {
console.log(response);
if (response.result === true) {
generateNotify("Success!", "success");
$('#authToken').val(response.authToken);
} else {
generateNotify(response.message, "warning");
}
},
error: function (e) {
console.log(e);
generateNotify("Something went wrong!", "danger");
}
});
});
function loadUserList() {
$.ajax({
type: "Get",
url: "admin/getusers",
dataType: "json",
success: function (response) {
if (response.length > 1) {
response.each(function(user) {
$('#users').append("<option>" + user + "</option>");
});
} else {
$('#users').append("<option>No Users!</option>");
}
},
error: function (e) {
console.log(e);
generateNotify("Something went wrong!", "danger");
}
});
}
});
</script>
</div>

View file

@ -1,6 +1,7 @@
<div class="col-lg-3 col-md-3 col-sm-4">
<div class="list-group table-of-contents">
<a class="list-group-item" href="/admin">Request Plex Settings</a>
<a class="list-group-item" href="/admin/authentication">Authentication</a>
<a class="list-group-item" href="/admin/couchpotato">CouchPotato Settings</a>
<a class="list-group-item" href="/admin/sonarr">Sonarr Settings</a>
<a class="list-group-item" href="/admin/sickbeard">Sickbeard Settings</a>