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

105 lines
3.5 KiB

using System.Collections.Generic;
8 years ago
using System.Linq;
using System.Threading.Tasks;
using Nancy;
using Nancy.Responses.Negotiation;
using Nancy.Security;
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
{
8 years ago
public UserManagementModule(ISettingsService<PlexRequestSettings> pr, ICustomUserMapper m, IPlexApi plexApi, ISettingsService<AuthenticationSettings> auth) : base("usermanagement", pr)
9 years ago
{
8 years ago
//this.RequiresClaims(UserClaims.Admin);
UserMapper = m;
8 years ago
PlexApi = plexApi;
AuthSettings = auth;
Get["/"] = x => Load();
Get["/users", true] = async (x, ct) => await LoadUsers();
Post["/createuser"] = x => CreateUser(Request.Form["userName"].ToString(), Request.Form["password"].ToString());
9 years ago
}
8 years ago
private ICustomUserMapper UserMapper { get; }
8 years ago
private IPlexApi PlexApi { get; }
private ISettingsService<AuthenticationSettings> AuthSettings { 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
{
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 authSettings = await AuthSettings.GetSettingsAsync();
if (!string.IsNullOrEmpty(authSettings.PlexAuthToken))
{
//Get Plex Users
var plexUsers = PlexApi.GetUsers(authSettings.PlexAuthToken);
foreach (var u in plexUsers.User)
{
model.Add(new UserManagementUsersViewModel
{
Username = u.Username,
Type = UserType.PlexUser,
//Alias =
Claims = "Requestor",
EmailAddress = u.Email
});
}
}
return Response.AsJson(model);
9 years ago
}
8 years ago
private Response CreateUser(string username, string password)
{
if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password))
{
return Response.AsJson(new JsonResponseModel
{
Result = true,
Message = "Please enter in a valid Username and Password"
});
}
var user = UserMapper.CreateRegularUser(username, password);
if (user.HasValue)
{
return Response.AsJson(user);
}
return Response.AsJson(new JsonResponseModel { Result = false, Message = "Could not save user" });
}
9 years ago
}
}