From eb120e6840436d06f94f2de5773e6f000b325819 Mon Sep 17 00:00:00 2001 From: tidusjar Date: Sun, 20 Nov 2016 19:16:22 +0000 Subject: [PATCH] Use the user alias everywhere if it is set #218 --- PlexRequests.Core/ISecurityExtensions.cs | 7 +++++ PlexRequests.Core/SecurityExtensions.cs | 37 ++++++++++++++++++++++++ PlexRequests.UI/Modules/BaseModule.cs | 7 ++++- 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/PlexRequests.Core/ISecurityExtensions.cs b/PlexRequests.Core/ISecurityExtensions.cs index b343e63f7..cc1b856fd 100644 --- a/PlexRequests.Core/ISecurityExtensions.cs +++ b/PlexRequests.Core/ISecurityExtensions.cs @@ -19,5 +19,12 @@ namespace PlexRequests.Core bool IsNormalUser(NancyContext context); bool IsPlexUser(NancyContext context); bool HasPermissions(string userName, Permissions perm); + + /// + /// Gets the username this could be the alias! We should always use this method when getting the username + /// + /// The username. + /// null if we cannot find a user + string GetUsername(string username); } } \ No newline at end of file diff --git a/PlexRequests.Core/SecurityExtensions.cs b/PlexRequests.Core/SecurityExtensions.cs index 3bb887cbd..bc99d7231 100644 --- a/PlexRequests.Core/SecurityExtensions.cs +++ b/PlexRequests.Core/SecurityExtensions.cs @@ -30,6 +30,7 @@ using Nancy; using Nancy.Linker; using Nancy.Responses; using Nancy.Security; +using PlexRequests.Core.Models; using PlexRequests.Helpers; using PlexRequests.Helpers.Permissions; using PlexRequests.Store.Repository; @@ -77,6 +78,42 @@ namespace PlexRequests.Core return dbUser != null; } + /// + /// Gets the username this could be the alias! We should always use this method when getting the username + /// + /// The username. + /// null if we cannot find a user + public string GetUsername(string username) + { + var plexUser = PlexUsers.GetUserByUsername(username); + if (plexUser != null) + { + if (!string.IsNullOrEmpty(plexUser.UserAlias)) + { + return plexUser.UserAlias; + } + else + { + return plexUser.Username; + } + } + + var dbUser = UserRepository.GetUserByUsername(username); + if (dbUser != null) + { + var userProps = ByteConverterHelper.ReturnObject(dbUser.UserProperties); + if (!string.IsNullOrEmpty(userProps.UserAlias)) + { + return userProps.UserAlias; + } + else + { + return dbUser.UserName; + } + } + return null; + } + /// /// Creates a hook to be used in a pipeline before a route handler to ensure diff --git a/PlexRequests.UI/Modules/BaseModule.cs b/PlexRequests.UI/Modules/BaseModule.cs index b8fcb63e4..e0ea40363 100644 --- a/PlexRequests.UI/Modules/BaseModule.cs +++ b/PlexRequests.UI/Modules/BaseModule.cs @@ -109,7 +109,12 @@ namespace PlexRequests.UI.Modules { try { - _username = User == null ? Session[SessionKeys.UsernameKey].ToString() : User.UserName; + var username = Security.GetUsername(User.UserName); + if (string.IsNullOrEmpty(username)) + { + return Session[SessionKeys.UsernameKey].ToString(); + } + _username = username; } catch (Exception) {