|
|
|
@ -6,15 +6,16 @@ using System.Threading.Tasks;
|
|
|
|
|
using Nancy;
|
|
|
|
|
using Nancy.Extensions;
|
|
|
|
|
using Nancy.Responses.Negotiation;
|
|
|
|
|
using Nancy.Security;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using PlexRequests.Api.Interfaces;
|
|
|
|
|
using PlexRequests.Api.Models.Plex;
|
|
|
|
|
using PlexRequests.Core;
|
|
|
|
|
using PlexRequests.Core.Models;
|
|
|
|
|
using PlexRequests.Core.SettingModels;
|
|
|
|
|
using PlexRequests.Helpers;
|
|
|
|
|
using PlexRequests.Helpers.Permissions;
|
|
|
|
|
using PlexRequests.Store;
|
|
|
|
|
using PlexRequests.Store.Models;
|
|
|
|
|
using PlexRequests.Store.Repository;
|
|
|
|
|
using PlexRequests.UI.Models;
|
|
|
|
|
|
|
|
|
@ -22,15 +23,16 @@ namespace PlexRequests.UI.Modules
|
|
|
|
|
{
|
|
|
|
|
public class UserManagementModule : BaseModule
|
|
|
|
|
{
|
|
|
|
|
public UserManagementModule(ISettingsService<PlexRequestSettings> pr, ICustomUserMapper m, IPlexApi plexApi, ISettingsService<PlexSettings> plex, IRepository<UserLogins> userLogins) : base("usermanagement", pr)
|
|
|
|
|
public UserManagementModule(ISettingsService<PlexRequestSettings> pr, ICustomUserMapper m, IPlexApi plexApi, ISettingsService<PlexSettings> plex, IRepository<UserLogins> userLogins, IRepository<PlexUsers> plexRepo) : base("usermanagement", pr)
|
|
|
|
|
{
|
|
|
|
|
#if !DEBUG
|
|
|
|
|
this.RequiresAnyClaim(UserClaims.Admin);
|
|
|
|
|
Before += (ctx) => Security.AdminLoginRedirect(Permissions.Administrator, ctx);
|
|
|
|
|
#endif
|
|
|
|
|
UserMapper = m;
|
|
|
|
|
PlexApi = plexApi;
|
|
|
|
|
PlexSettings = plex;
|
|
|
|
|
UserLoginsRepo = userLogins;
|
|
|
|
|
PlexUsersRepository = plexRepo;
|
|
|
|
|
|
|
|
|
|
Get["/"] = x => Load();
|
|
|
|
|
|
|
|
|
@ -40,7 +42,7 @@ namespace PlexRequests.UI.Modules
|
|
|
|
|
Get["/plex/{id}", true] = async (x, ct) => await PlexDetails(x.id);
|
|
|
|
|
Get["/permissions"] = x => GetEnum<Permissions>();
|
|
|
|
|
Get["/features"] = x => GetEnum<Features>();
|
|
|
|
|
Post["/updateuser"] = x => UpdateUser();
|
|
|
|
|
Post["/updateuser", true] = async (x, ct) => await UpdateUser();
|
|
|
|
|
Post["/deleteuser"] = x => DeleteUser();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -48,6 +50,7 @@ namespace PlexRequests.UI.Modules
|
|
|
|
|
private IPlexApi PlexApi { get; }
|
|
|
|
|
private ISettingsService<PlexSettings> PlexSettings { get; }
|
|
|
|
|
private IRepository<UserLogins> UserLoginsRepo { get; }
|
|
|
|
|
private IRepository<PlexUsers> PlexUsersRepository { get; }
|
|
|
|
|
|
|
|
|
|
private Negotiator Load()
|
|
|
|
|
{
|
|
|
|
@ -57,13 +60,14 @@ namespace PlexRequests.UI.Modules
|
|
|
|
|
private async Task<Response> LoadUsers()
|
|
|
|
|
{
|
|
|
|
|
var localUsers = await UserMapper.GetUsersAsync();
|
|
|
|
|
var plexDbUsers = await PlexUsersRepository.GetAllAsync();
|
|
|
|
|
var model = new List<UserManagementUsersViewModel>();
|
|
|
|
|
|
|
|
|
|
var usersDb = UserLoginsRepo.GetAll().ToList();
|
|
|
|
|
var userLogins = UserLoginsRepo.GetAll().ToList();
|
|
|
|
|
|
|
|
|
|
foreach (var user in localUsers)
|
|
|
|
|
{
|
|
|
|
|
var userDb = usersDb.FirstOrDefault(x => x.UserId == user.UserGuid);
|
|
|
|
|
var userDb = userLogins.FirstOrDefault(x => x.UserId == user.UserGuid);
|
|
|
|
|
model.Add(MapLocalUser(user, userDb?.LastLoggedIn ?? DateTime.MinValue));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -75,20 +79,18 @@ namespace PlexRequests.UI.Modules
|
|
|
|
|
|
|
|
|
|
foreach (var u in plexUsers.User)
|
|
|
|
|
{
|
|
|
|
|
var userDb = usersDb.FirstOrDefault(x => x.UserId == u.Id);
|
|
|
|
|
model.Add(new UserManagementUsersViewModel
|
|
|
|
|
var dbUser = plexDbUsers.FirstOrDefault(x => x.PlexUserId == u.Id);
|
|
|
|
|
var userDb = userLogins.FirstOrDefault(x => x.UserId == u.Id);
|
|
|
|
|
// We don't have the user in the database yet
|
|
|
|
|
if (dbUser == null)
|
|
|
|
|
{
|
|
|
|
|
Username = u.Username,
|
|
|
|
|
Type = UserType.PlexUser,
|
|
|
|
|
Id = u.Id,
|
|
|
|
|
FeaturesFormattedString = "Requestor",
|
|
|
|
|
EmailAddress = u.Email,
|
|
|
|
|
PlexInfo = new UserManagementPlexInformation
|
|
|
|
|
{
|
|
|
|
|
Thumb = u.Thumb
|
|
|
|
|
},
|
|
|
|
|
LastLoggedIn = userDb?.LastLoggedIn ?? DateTime.MinValue,
|
|
|
|
|
});
|
|
|
|
|
model.Add(MapPlexUser(u, null, userDb?.LastLoggedIn ?? DateTime.MinValue));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// The Plex User is in the database
|
|
|
|
|
model.Add(MapPlexUser(u, dbUser, userDb?.LastLoggedIn ?? DateTime.MinValue));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Response.AsJson(model);
|
|
|
|
@ -128,7 +130,7 @@ namespace PlexRequests.UI.Modules
|
|
|
|
|
permissionsVal += f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var user = UserMapper.CreateUser(model.Username, model.Password, featuresVal, permissionsVal, new UserProperties { EmailAddress = model.EmailAddress });
|
|
|
|
|
var user = UserMapper.CreateUser(model.Username, model.Password, permissionsVal, featuresVal, new UserProperties { EmailAddress = model.EmailAddress });
|
|
|
|
|
if (user.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return Response.AsJson(MapLocalUser(UserMapper.GetUser(user.Value), DateTime.MinValue));
|
|
|
|
@ -137,7 +139,7 @@ namespace PlexRequests.UI.Modules
|
|
|
|
|
return Response.AsJson(new JsonResponseModel { Result = false, Message = "Could not save user" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Response UpdateUser()
|
|
|
|
|
private async Task<Response> UpdateUser()
|
|
|
|
|
{
|
|
|
|
|
var body = Request.Body.AsString();
|
|
|
|
|
if (string.IsNullOrEmpty(body))
|
|
|
|
@ -156,22 +158,68 @@ namespace PlexRequests.UI.Modules
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var val = model.Permissions.Where(c => c.Selected).Sum(c => c.Value);
|
|
|
|
|
var permissionsValue = model.Permissions.Where(c => c.Selected).Sum(c => c.Value);
|
|
|
|
|
var featuresValue = model.Features.Where(c => c.Selected).Sum(c => c.Value);
|
|
|
|
|
|
|
|
|
|
Guid outId;
|
|
|
|
|
Guid.TryParse(model.Id, out outId);
|
|
|
|
|
var localUser = UserMapper.GetUser(outId);
|
|
|
|
|
|
|
|
|
|
// Update Local User
|
|
|
|
|
if (localUser != null)
|
|
|
|
|
{
|
|
|
|
|
localUser.Permissions = permissionsValue;
|
|
|
|
|
localUser.Features = featuresValue;
|
|
|
|
|
|
|
|
|
|
var currentProps = ByteConverterHelper.ReturnObject<UserProperties>(localUser.UserProperties);
|
|
|
|
|
currentProps.UserAlias = model.Alias;
|
|
|
|
|
currentProps.EmailAddress = model.EmailAddress;
|
|
|
|
|
|
|
|
|
|
localUser.UserProperties = ByteConverterHelper.ReturnBytes(currentProps);
|
|
|
|
|
|
|
|
|
|
var user = UserMapper.EditUser(localUser);
|
|
|
|
|
var dbUser = UserLoginsRepo.GetAll().FirstOrDefault(x => x.UserId == user.UserGuid);
|
|
|
|
|
var retUser = MapLocalUser(user, dbUser?.LastLoggedIn ?? DateTime.MinValue);
|
|
|
|
|
return Response.AsJson(retUser);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var plexSettings = await PlexSettings.GetSettingsAsync();
|
|
|
|
|
var plexDbUsers = await PlexUsersRepository.GetAllAsync();
|
|
|
|
|
var plexUsers = PlexApi.GetUsers(plexSettings.PlexAuthToken);
|
|
|
|
|
var plexDbUser = plexDbUsers.FirstOrDefault(x => x.PlexUserId == model.Id);
|
|
|
|
|
var plexUser = plexUsers.User.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
var userLogin = UserLoginsRepo.GetAll().FirstOrDefault(x => x.UserId == model.Id);
|
|
|
|
|
if (plexDbUser != null && plexUser != null)
|
|
|
|
|
{
|
|
|
|
|
// We have a user in the DB for this Plex Account
|
|
|
|
|
plexDbUser.Permissions = permissionsValue;
|
|
|
|
|
plexDbUser.Features = featuresValue;
|
|
|
|
|
|
|
|
|
|
var userFound = UserMapper.GetUser(new Guid(model.Id));
|
|
|
|
|
plexDbUser.UserAlias = model.Alias;
|
|
|
|
|
|
|
|
|
|
userFound.Permissions = val;
|
|
|
|
|
await PlexUsersRepository.UpdateAsync(plexDbUser);
|
|
|
|
|
|
|
|
|
|
var retUser = MapPlexUser(plexUser, plexDbUser, userLogin?.LastLoggedIn ?? DateTime.MinValue);
|
|
|
|
|
return Response.AsJson(retUser);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var currentProps = ByteConverterHelper.ReturnObject<UserProperties>(userFound.UserProperties);
|
|
|
|
|
currentProps.UserAlias = model.Alias;
|
|
|
|
|
currentProps.EmailAddress = model.EmailAddress;
|
|
|
|
|
// We have a Plex Account but he's not in the DB
|
|
|
|
|
if (plexUser != null)
|
|
|
|
|
{
|
|
|
|
|
var user = new PlexUsers
|
|
|
|
|
{
|
|
|
|
|
Permissions = permissionsValue,
|
|
|
|
|
Features = featuresValue,
|
|
|
|
|
UserAlias = model.Alias,
|
|
|
|
|
PlexUserId = plexUser.Id
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
userFound.UserProperties = ByteConverterHelper.ReturnBytes(currentProps);
|
|
|
|
|
await PlexUsersRepository.InsertAsync(user);
|
|
|
|
|
|
|
|
|
|
var user = UserMapper.EditUser(userFound);
|
|
|
|
|
var dbUser = UserLoginsRepo.GetAll().FirstOrDefault(x => x.UserId == user.UserGuid);
|
|
|
|
|
var retUser = MapLocalUser(user, dbUser?.LastLoggedIn ?? DateTime.MinValue);
|
|
|
|
|
return Response.AsJson(retUser);
|
|
|
|
|
var retUser = MapPlexUser(plexUser, user, userLogin?.LastLoggedIn ?? DateTime.MinValue);
|
|
|
|
|
return Response.AsJson(retUser);
|
|
|
|
|
}
|
|
|
|
|
return null; // We should never end up here.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Response DeleteUser()
|
|
|
|
@ -248,8 +296,8 @@ namespace PlexRequests.UI.Modules
|
|
|
|
|
|
|
|
|
|
private UserManagementUsersViewModel MapLocalUser(UsersModel user, DateTime lastLoggedIn)
|
|
|
|
|
{
|
|
|
|
|
var features = (Features) user.Features;
|
|
|
|
|
var permissions = (Permissions) user.Permissions;
|
|
|
|
|
var features = (Features)user.Features;
|
|
|
|
|
var permissions = (Permissions)user.Permissions;
|
|
|
|
|
|
|
|
|
|
var userProps = ByteConverterHelper.ReturnObject<UserProperties>(user.UserProperties);
|
|
|
|
|
|
|
|
|
@ -297,6 +345,64 @@ namespace PlexRequests.UI.Modules
|
|
|
|
|
|
|
|
|
|
return m;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private UserManagementUsersViewModel MapPlexUser(UserFriends plexInfo, PlexUsers dbUser, DateTime lastLoggedIn)
|
|
|
|
|
{
|
|
|
|
|
if (dbUser == null)
|
|
|
|
|
{
|
|
|
|
|
dbUser = new PlexUsers();
|
|
|
|
|
}
|
|
|
|
|
var features = (Features)dbUser?.Features;
|
|
|
|
|
var permissions = (Permissions)dbUser?.Permissions;
|
|
|
|
|
|
|
|
|
|
var m = new UserManagementUsersViewModel
|
|
|
|
|
{
|
|
|
|
|
Id = plexInfo.Id,
|
|
|
|
|
PermissionsFormattedString = permissions == 0 ? "None" : permissions.ToString(),
|
|
|
|
|
FeaturesFormattedString = features.ToString(),
|
|
|
|
|
Username = plexInfo.Username,
|
|
|
|
|
Type = UserType.PlexUser,
|
|
|
|
|
EmailAddress = plexInfo.Email,
|
|
|
|
|
Alias = dbUser?.UserAlias ?? string.Empty,
|
|
|
|
|
LastLoggedIn = lastLoggedIn,
|
|
|
|
|
PlexInfo = new UserManagementPlexInformation
|
|
|
|
|
{
|
|
|
|
|
Thumb = plexInfo.Thumb
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Add permissions
|
|
|
|
|
foreach (var p in Enum.GetValues(typeof(Permissions)))
|
|
|
|
|
{
|
|
|
|
|
var perm = (Permissions)p;
|
|
|
|
|
var displayValue = EnumHelper<Permissions>.GetDisplayValue(perm);
|
|
|
|
|
var pm = new CheckBox
|
|
|
|
|
{
|
|
|
|
|
Name = displayValue,
|
|
|
|
|
Selected = permissions.HasFlag(perm),
|
|
|
|
|
Value = (int)perm
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
m.Permissions.Add(pm);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add features
|
|
|
|
|
foreach (var p in Enum.GetValues(typeof(Features)))
|
|
|
|
|
{
|
|
|
|
|
var perm = (Features)p;
|
|
|
|
|
var displayValue = EnumHelper<Features>.GetDisplayValue(perm);
|
|
|
|
|
var pm = new CheckBox
|
|
|
|
|
{
|
|
|
|
|
Name = displayValue,
|
|
|
|
|
Selected = features.HasFlag(perm),
|
|
|
|
|
Value = (int)perm
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
m.Features.Add(pm);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return m;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|