Use the user alias everywhere if it is set #218

pull/733/head
tidusjar 8 years ago
parent 4629301264
commit eb120e6840

@ -19,5 +19,12 @@ namespace PlexRequests.Core
bool IsNormalUser(NancyContext context); bool IsNormalUser(NancyContext context);
bool IsPlexUser(NancyContext context); bool IsPlexUser(NancyContext context);
bool HasPermissions(string userName, Permissions perm); bool HasPermissions(string userName, Permissions perm);
/// <summary>
/// Gets the username this could be the alias! We should always use this method when getting the username
/// </summary>
/// <param name="username">The username.</param>
/// <returns><c>null</c> if we cannot find a user</returns>
string GetUsername(string username);
} }
} }

@ -30,6 +30,7 @@ using Nancy;
using Nancy.Linker; using Nancy.Linker;
using Nancy.Responses; using Nancy.Responses;
using Nancy.Security; using Nancy.Security;
using PlexRequests.Core.Models;
using PlexRequests.Helpers; using PlexRequests.Helpers;
using PlexRequests.Helpers.Permissions; using PlexRequests.Helpers.Permissions;
using PlexRequests.Store.Repository; using PlexRequests.Store.Repository;
@ -77,6 +78,42 @@ namespace PlexRequests.Core
return dbUser != null; return dbUser != null;
} }
/// <summary>
/// Gets the username this could be the alias! We should always use this method when getting the username
/// </summary>
/// <param name="username">The username.</param>
/// <returns><c>null</c> if we cannot find a user</returns>
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<UserProperties>(dbUser.UserProperties);
if (!string.IsNullOrEmpty(userProps.UserAlias))
{
return userProps.UserAlias;
}
else
{
return dbUser.UserName;
}
}
return null;
}
/// <summary> /// <summary>
/// Creates a hook to be used in a pipeline before a route handler to ensure /// Creates a hook to be used in a pipeline before a route handler to ensure

@ -109,7 +109,12 @@ namespace PlexRequests.UI.Modules
{ {
try 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) catch (Exception)
{ {

Loading…
Cancel
Save