You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Ombi/PlexRequests.UI/Modules/UserManagementModule.cs

170 lines
5.6 KiB

using System;
using System.Collections.Generic;
8 years ago
using System.Linq;
using System.Threading.Tasks;
using Nancy;
using Nancy.Extensions;
using Nancy.Responses.Negotiation;
using Nancy.Security;
using Newtonsoft.Json;
8 years ago
using PlexRequests.Api.Interfaces;
using PlexRequests.Core;
8 years ago
using PlexRequests.Core.Models;
using PlexRequests.Core.SettingModels;
using PlexRequests.Helpers;
using PlexRequests.UI.Models;
namespace PlexRequests.UI.Modules
{
9 years ago
public class UserManagementModule : BaseModule
{
public UserManagementModule(ISettingsService<PlexRequestSettings> pr, ICustomUserMapper m, IPlexApi plexApi, ISettingsService<PlexSettings> plex) : base("usermanagement", pr)
9 years ago
{
#if !DEBUG
this.RequiresClaims(UserClaims.Admin);
#endif
UserMapper = m;
8 years ago
PlexApi = plexApi;
PlexSettings = plex;
8 years ago
Get["/"] = x => Load();
Get["/users", true] = async (x, ct) => await LoadUsers();
Post["/createuser"] = x => CreateUser();
Get["/local/{id}"] = x => LocalDetails((Guid)x.id);
Get["/plex/{id}", true] = async (x, ct) => await PlexDetails(x.id);
8 years ago
Get["/claims"] = x => GetClaims();
9 years ago
}
8 years ago
private ICustomUserMapper UserMapper { get; }
8 years ago
private IPlexApi PlexApi { get; }
private ISettingsService<PlexSettings> PlexSettings { get; }
9 years ago
private Negotiator Load()
{
return View["Index"];
}
8 years ago
private async Task<Response> LoadUsers()
9 years ago
{
8 years ago
var localUsers = await UserMapper.GetUsersAsync();
9 years ago
var model = new List<UserManagementUsersViewModel>();
8 years ago
foreach (var user in localUsers)
9 years ago
{
8 years ago
var claims = ByteConverterHelper.ReturnObject<string[]>(user.Claims);
var claimsString = string.Join(", ", claims);
var userProps = ByteConverterHelper.ReturnObject<UserProperties>(user.UserProperties);
9 years ago
model.Add(new UserManagementUsersViewModel
{
Id = user.UserGuid,
8 years ago
Claims = claimsString,
9 years ago
Username = user.UserName,
8 years ago
Type = UserType.LocalUser,
EmailAddress = userProps.EmailAddress
9 years ago
});
}
8 years ago
var plexSettings = await PlexSettings.GetSettingsAsync();
if (!string.IsNullOrEmpty(plexSettings.PlexAuthToken))
8 years ago
{
//Get Plex Users
var plexUsers = PlexApi.GetUsers(plexSettings.PlexAuthToken);
8 years ago
foreach (var u in plexUsers.User)
{
8 years ago
8 years ago
model.Add(new UserManagementUsersViewModel
{
Username = u.Username,
Type = UserType.PlexUser,
Id = u.Id,
8 years ago
Claims = "Requestor",
8 years ago
EmailAddress = u.Email,
PlexInfo = new UserManagementPlexInformation
{
Thumb = u.Thumb
}
8 years ago
});
}
}
return Response.AsJson(model);
9 years ago
}
private Response CreateUser()
8 years ago
{
var body = Request.Body.AsString();
if (string.IsNullOrEmpty(body))
{
return Response.AsJson(new JsonResponseModel { Result = false, Message = "Could not save user, invalid JSON body" });
}
var model = JsonConvert.DeserializeObject<UserManagementCreateModel>(body);
if (string.IsNullOrWhiteSpace(model.Username) || string.IsNullOrWhiteSpace(model.Password))
8 years ago
{
return Response.AsJson(new JsonResponseModel
{
Result = true,
Message = "Please enter in a valid Username and Password"
});
}
var user = UserMapper.CreateUser(model.Username, model.Password, model.Claims, new UserProperties { EmailAddress = model.EmailAddress });
8 years ago
if (user.HasValue)
{
return Response.AsJson(user);
}
return Response.AsJson(new JsonResponseModel { Result = false, Message = "Could not save user" });
}
private Response LocalDetails(Guid id)
{
var localUser = UserMapper.GetUser(id);
if (localUser != null)
{
return Response.AsJson(localUser);
}
return Nancy.Response.NoBody;
}
8 years ago
private async Task<Response> PlexDetails(string id)
{
var plexSettings = await PlexSettings.GetSettingsAsync();
if (!string.IsNullOrEmpty(plexSettings.PlexAuthToken))
{
//Get Plex Users
var plexUsers = PlexApi.GetUsers(plexSettings.PlexAuthToken);
8 years ago
var selectedUser = plexUsers.User?.FirstOrDefault(x => x.Id.ToString() == id);
if (selectedUser != null)
{
return Response.AsJson(selectedUser);
}
}
return Nancy.Response.NoBody;
}
8 years ago
/// <summary>
/// Returns all claims for the users.
/// </summary>
/// <returns></returns>
private Response GetClaims()
{
var retVal = new List<dynamic>();
var claims = UserMapper.GetAllClaims();
foreach (var c in claims)
{
retVal.Add(new { Name = c, Selected = false });
8 years ago
}
return Response.AsJson(retVal);
}
9 years ago
}
}