From 3188a720fae41481018bad3eeadddcb1fa97cf78 Mon Sep 17 00:00:00 2001 From: tidusjar Date: Mon, 14 Mar 2016 14:14:08 +0000 Subject: [PATCH] Added a bunch of logging --- PlexRequests.UI/Modules/SearchModule.cs | 26 ++++++++++++++-------- PlexRequests.UI/Modules/UserLoginModule.cs | 24 ++++++++++++++++---- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/PlexRequests.UI/Modules/SearchModule.cs b/PlexRequests.UI/Modules/SearchModule.cs index c3c6e9fbd..3a681c338 100644 --- a/PlexRequests.UI/Modules/SearchModule.cs +++ b/PlexRequests.UI/Modules/SearchModule.cs @@ -105,6 +105,7 @@ namespace PlexRequests.UI.Modules if (tvShow?.data == null) { + Log.Trace("TV Show data is null"); return Response.AsJson(""); } var model = new List(); @@ -137,32 +138,39 @@ namespace PlexRequests.UI.Modules Zap2ItId = t.zap2itId }); } + + Log.Trace("Returning TV Show results: "); + Log.Trace(model.DumpJson()); return Response.AsJson(model); } - private Response UpcomingMovies() + private Response UpcomingMovies() // TODO : Not used { var movies = MovieApi.GetUpcomingMovies(); var result = movies.Result; + Log.Trace("Movie Upcoming Results: "); + Log.Trace(result.DumpJson()); return Response.AsJson(result); } - private Response CurrentlyPlayingMovies() + private Response CurrentlyPlayingMovies() // TODO : Not used { var movies = MovieApi.GetCurrentPlayingMovies(); var result = movies.Result; + Log.Trace("Movie Currently Playing Results: "); + Log.Trace(result.DumpJson()); return Response.AsJson(result); } private Response RequestMovie(int movieId) { - Log.Trace("Requesting movie with id {0}", movieId); + Log.Info("Requesting movie with id {0}", movieId); if (RequestService.CheckRequest(movieId)) { Log.Trace("movie with id {0} exists", movieId); return Response.AsJson(new { Result = false, Message = "Movie has already been requested!" }); } - Log.Trace("movie with id {0} doesnt exists", movieId); + Log.Debug("movie with id {0} doesnt exists", movieId); var cpSettings = CpService.GetSettings(); if (cpSettings.ApiKey == null) { @@ -195,16 +203,17 @@ namespace PlexRequests.UI.Modules var settings = PrService.GetSettings(); + Log.Trace(settings.DumpJson()); if (!settings.RequireApproval) { var cp = new CouchPotatoApi(); - Log.Trace("Adding movie to CP (No approval required)"); + Log.Info("Adding movie to CP (No approval required)"); var result = cp.AddMovie(model.ImdbId, cpSettings.ApiKey, model.Title, cpSettings.FullUri); - Log.Trace("Adding movie to CP result {0}", result); + Log.Debug("Adding movie to CP result {0}", result); if (result) { model.Approved = true; - Log.Trace("Adding movie to database requests (No approval required)"); + Log.Debug("Adding movie to database requests (No approval required)"); RequestService.AddRequest(movieId, model); return Response.AsJson(new { Result = true }); @@ -214,7 +223,7 @@ namespace PlexRequests.UI.Modules try { - Log.Trace("Adding movie to database requests"); + Log.Debug("Adding movie to database requests"); var id = RequestService.AddRequest(movieId, model); //BackgroundJob.Enqueue(() => Checker.CheckAndUpdate(model.Title, (int)id)); @@ -236,7 +245,6 @@ namespace PlexRequests.UI.Modules /// private Response RequestTvShow(int showId, bool latest) { - // Latest send to Sonarr and no need to store in DB if (RequestService.CheckRequest(showId)) { return Response.AsJson(new { Result = false, Message = "TV Show has already been requested!" }); diff --git a/PlexRequests.UI/Modules/UserLoginModule.cs b/PlexRequests.UI/Modules/UserLoginModule.cs index e8f6d214e..f4901061e 100644 --- a/PlexRequests.UI/Modules/UserLoginModule.cs +++ b/PlexRequests.UI/Modules/UserLoginModule.cs @@ -30,16 +30,17 @@ using Nancy; using Nancy.Extensions; using Nancy.Responses.Negotiation; +using NLog; + using PlexRequests.Api.Interfaces; -using PlexRequests.Api.Models; using PlexRequests.Api.Models.Plex; using PlexRequests.Core; using PlexRequests.Core.SettingModels; +using PlexRequests.Helpers; using PlexRequests.UI.Models; namespace PlexRequests.UI.Modules { - // TODO: Add ability to logout public class UserLoginModule : NancyModule { public UserLoginModule(ISettingsService auth, IPlexApi api) : base("userlogin") @@ -54,6 +55,8 @@ namespace PlexRequests.UI.Modules private ISettingsService AuthService { get; } private IPlexApi Api { get; } + private static Logger Log = LogManager.GetCurrentClassLogger(); + public Negotiator Index() { var settings = AuthService.GetSettings(); @@ -63,7 +66,7 @@ namespace PlexRequests.UI.Modules private Response LoginUser() { var username = Request.Form.username.Value; - + Log.Debug("Username \"{0}\" attempting to login",username); if (string.IsNullOrWhiteSpace(username)) { return Response.AsJson(new JsonResponseModel { Result = false, Message = "Incorrect User or Password" }); @@ -72,39 +75,49 @@ namespace PlexRequests.UI.Modules var authenticated = false; var settings = AuthService.GetSettings(); - + Log.Debug("Settings: "); + Log.Debug(settings.DumpJson()); if (IsUserInDeniedList(username, settings)) { + Log.Debug("User is in denied list, not allowing them to authenticate"); return Response.AsJson(new JsonResponseModel { Result = false, Message = "Incorrect User or Password" }); } var password = string.Empty; if (settings.UsePassword) { + Log.Debug("Using password"); password = Request.Form.password.Value; } if (settings.UserAuthentication && settings.UsePassword) // Authenticate with Plex { + Log.Debug("Need to auth and also provide pass"); var signedIn = (PlexAuthentication)Api.SignIn(username, password); if (signedIn.user?.authentication_token != null) { + Log.Debug("Correct credentials, checking if the user is in the friends list"); authenticated = CheckIfUserIsInPlexFriends(username, settings.PlexAuthToken); + Log.Debug("Friends list result = {0}", authenticated); } } else if(settings.UserAuthentication) // Check against the users in Plex { + Log.Debug("Need to auth"); authenticated = CheckIfUserIsInPlexFriends(username, settings.PlexAuthToken); + Log.Debug("Friends list result = {0}", authenticated); } else if(!settings.UserAuthentication) // No auth, let them pass! { + Log.Debug("No need to auth"); authenticated = true; } if (authenticated) { + Log.Debug("We are authenticated! Setting session."); // Add to the session (Used in the BaseModules) Session[SessionKeys.UsernameKey] = (string)username; } @@ -116,6 +129,7 @@ namespace PlexRequests.UI.Modules private Response Logout() { + Log.Debug("Logging Out"); if (Session[SessionKeys.UsernameKey] != null) { Session.Delete(SessionKeys.UsernameKey); @@ -126,6 +140,8 @@ namespace PlexRequests.UI.Modules private bool CheckIfUserIsInPlexFriends(string username, string authToken) { var users = Api.GetUsers(authToken); + Log.Debug("Plex Users: "); + Log.Debug(users.DumpJson()); return users.User.Any(x => x.Username == username); }