tidusjar 8 years ago
commit 36b7c91dfb

@ -134,6 +134,17 @@ namespace PlexRequests.Core
return CreateUser(username, password, new[] { UserClaims.User }, properties); return CreateUser(username, password, new[] { UserClaims.User }, properties);
} }
public Guid? CreateUser(string username, string password, string[] claims)
{
return CreateUser(username, password, claims, null);
}
public IEnumerable<string> GetAllClaims()
{
var properties = typeof(UserClaims).GetConstantsValues<string>();
return properties;
}
public bool UpdatePassword(string username, string oldPassword, string newPassword) public bool UpdatePassword(string username, string oldPassword, string newPassword)
{ {
var users = Repo.GetAll(); var users = Repo.GetAll();
@ -175,6 +186,8 @@ namespace PlexRequests.Core
public interface ICustomUserMapper public interface ICustomUserMapper
{ {
Guid? CreateUser(string username, string password, string[] claims);
IEnumerable<string> GetAllClaims();
IEnumerable<UsersModel> GetUsers(); IEnumerable<UsersModel> GetUsers();
Task<IEnumerable<UsersModel>> GetUsersAsync(); Task<IEnumerable<UsersModel>> GetUsersAsync();
UsersModel GetUser(Guid userId); UsersModel GetUser(Guid userId);

@ -26,7 +26,7 @@
#endregion #endregion
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using NUnit.Framework; using NUnit.Framework;
using PlexRequests.Store; using PlexRequests.Store;
@ -42,6 +42,14 @@ namespace PlexRequests.Helpers.Tests
return input.GetPropertyNames(); return input.GetPropertyNames();
} }
[Test]
public void GetConstantsTest()
{
var consts = typeof(UserClaims).GetConstantsValues<string>();
Assert.That(consts.Contains("Admin"),Is.True);
Assert.That(consts.Contains("PowerUser"),Is.True);
Assert.That(consts.Contains("User"),Is.True);
}
private static IEnumerable<TestCaseData> TypeData private static IEnumerable<TestCaseData> TypeData
{ {

@ -25,7 +25,9 @@
// ************************************************************************/ // ************************************************************************/
#endregion #endregion
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection;
namespace PlexRequests.Helpers namespace PlexRequests.Helpers
{ {
@ -35,5 +37,19 @@ namespace PlexRequests.Helpers
{ {
return t.GetProperties().Select(x => x.Name).ToArray(); return t.GetProperties().Select(x => x.Name).ToArray();
} }
public static IEnumerable<FieldInfo> GetConstants(this Type type)
{
var fieldInfos = type.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
return fieldInfos.Where(fi => fi.IsLiteral && !fi.IsInitOnly);
}
public static IEnumerable<T> GetConstantsValues<T>(this Type type) where T : class
{
var fieldInfos = GetConstants(type);
return fieldInfos.Select(fi => fi.GetRawConstantValue() as T);
}
} }
} }

@ -4,23 +4,27 @@
$scope.user = {}; // The local user $scope.user = {}; // The local user
$scope.users = []; // list of users $scope.users = []; // list of users
$scope.claims = []; // List of claims
$scope.selectedUser = {}; $scope.selectedUser = {}; // User on the right side
$scope.selectedClaims = {};
$scope.sortType = 'username'; $scope.sortType = "username";
$scope.sortReverse = false; $scope.sortReverse = false;
$scope.searchTerm = ''; $scope.searchTerm = "";
$scope.error = { $scope.error = {
error: false, error: false,
errorMessage: "" errorMessage: ""
}; };
// Select a user to populate on the right side
$scope.selectUser = function (id) { $scope.selectUser = function (id) {
$scope.selectedUser = {};
$scope.selectedUser = $scope.users.find(x => x.id === id); $scope.selectedUser = $scope.users.find(x => x.id === id);
} }
// Get all users in the system
$scope.getUsers = function () { $scope.getUsers = function () {
$scope.users = userManagementService.getUsers() $scope.users = userManagementService.getUsers()
.then(function (data) { .then(function (data) {
@ -28,25 +32,50 @@
}); });
}; };
// Get the claims and populate the create dropdown
$scope.getClaims = function () {
userManagementService.getClaims()
.then(function (data) {
$scope.claims = data.data;
});
}
// Create a user, do some validation too
$scope.addUser = function () { $scope.addUser = function () {
if (!$scope.user.username || !$scope.user.password) { if (!$scope.user.username || !$scope.user.password) {
$scope.error.error = true; $scope.error.error = true;
$scope.error.errorMessage = "Please provide a correct username and password"; $scope.error.errorMessage = "Please provide a correct username and password";
generateNotify($scope.error.errorMessage, 'warning'); generateNotify($scope.error.errorMessage, 'warning');
return; return;
} }
userManagementService.addUser($scope.user).then(function (data) {
userManagementService.addUser($scope.user, $scope.selectedClaims).then(function (data) {
if (data.message) { if (data.message) {
$scope.error.error = true; $scope.error.error = true;
$scope.error.errorMessage = data.message; $scope.error.errorMessage = data.message;
} else { } else {
$scope.users.push(data); $scope.users.push(data); // Push the new user into the array to update the DOM
$scope.user = {}; $scope.user = {};
$scope.selectedClaims = {};
} }
}); });
}; };
$scope.$watch('claims|filter:{selected:true}', function (nv) {
$scope.selectedClaims = nv.map(function (claim) {
return claim.name;
});
}, true);
// On page load
$scope.init = function () {
$scope.getUsers();
$scope.getClaims();
return;
}
} }
angular.module('PlexRequests').controller('userManagementController', ["$scope", "userManagementService", controller]); angular.module('PlexRequests').controller('userManagementController', ["$scope", "userManagementService", controller]);
}()); }());

@ -2,27 +2,35 @@
var userManagementService = function ($http) { var userManagementService = function ($http) {
$http.defaults.headers.common['Content-Type'] = 'application/x-www-form-urlencoded'; // Set default headers //$http.defaults.headers.common['Content-Type'] = 'application/x-www-form-urlencoded'; // Set default headers
var getUsers = function () { var getUsers = function () {
return $http.get('/usermanagement/users'); return $http.get('/usermanagement/users');
}; };
var addUser = function (user) { var addUser = function (user, claims) {
if (!user) { if (!user || claims.length === 0) {
return null; return null;
} }
var claimJson = angular.toJson(claims);
var objectToSerialize = { 'claims': claimJson };
var data = $.param(user) +"&"+ $.param(objectToSerialize);
return $http({ return $http({
url: '/usermanagement/createuser', url: '/usermanagement/createuser',
method: "POST", method: "POST",
data: $.param(user) data: data
}); });
} }
var getClaims = function() {
return $http.get('/usermanagement/claims');
}
return { return {
getUsers: getUsers, getUsers: getUsers,
addUser: addUser addUser: addUser,
getClaims: getClaims
}; };
} }

@ -30,9 +30,10 @@ namespace PlexRequests.UI.Modules
Get["/"] = x => Load(); Get["/"] = x => Load();
Get["/users", true] = async (x, ct) => await LoadUsers(); Get["/users", true] = async (x, ct) => await LoadUsers();
Post["/createuser"] = x => CreateUser(Request.Form["username"].ToString(), Request.Form["password"].ToString()); Post["/createuser"] = x => CreateUser(Request.Form["username"].ToString(), Request.Form["password"].ToString(), (string[])Request.Form["claims"]);
Get["/local/{id}"] = x => LocalDetails((Guid)x.id); Get["/local/{id}"] = x => LocalDetails((Guid)x.id);
Get["/plex/{id}", true] = async (x,ct) => await PlexDetails(x.id); Get["/plex/{id}", true] = async (x,ct) => await PlexDetails(x.id);
Get["/claims"] = x => GetClaims();
} }
private ICustomUserMapper UserMapper { get; } private ICustomUserMapper UserMapper { get; }
@ -91,7 +92,7 @@ namespace PlexRequests.UI.Modules
return Response.AsJson(model); return Response.AsJson(model);
} }
private Response CreateUser(string username, string password) private Response CreateUser(string username, string password, string[] claims)
{ {
if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password)) if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password))
{ {
@ -101,7 +102,7 @@ namespace PlexRequests.UI.Modules
Message = "Please enter in a valid Username and Password" Message = "Please enter in a valid Username and Password"
}); });
} }
var user = UserMapper.CreateRegularUser(username, password); var user = UserMapper.Ce(username, password);
if (user.HasValue) if (user.HasValue)
{ {
return Response.AsJson(user); return Response.AsJson(user);
@ -139,6 +140,21 @@ namespace PlexRequests.UI.Modules
return Nancy.Response.NoBody; return Nancy.Response.NoBody;
} }
/// <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});
}
return Response.AsJson(retVal);
}
} }
} }

@ -3,7 +3,7 @@
<script src="~/Content/app/userManagement/userManagementController.js"></script> <script src="~/Content/app/userManagement/userManagementController.js"></script>
<script src="~/Content/app/userManagement/userManagementService.js"></script> <script src="~/Content/app/userManagement/userManagementService.js"></script>
<div ng-controller="userManagementController" ng-init="getUsers()"> <div ng-controller="userManagementController" ng-init="init()">
<br /> <br />
<br /> <br />
@ -12,15 +12,23 @@
<br> <br>
<br> <br>
<div ng-show="error.error" ng-bind="error.errorMessage"></div> <div ng-show="error.error" ng-bind="error.errorMessage"></div>
<form ng-submit="addUser()"> <form name="userform" ng-submit="addUser()" novalidate>
<div class="form-group"> <div class="form-group">
<input id="username" type="text" placeholder="user" ng-model="user.username" class="form-control form-control-custom" /> <input id="username" type="text" placeholder="user" ng-model="user.username" class="form-control form-control-custom" />
</div> </div>
<div class="form-group"> <div class="form-group">
<input id="password" type="password" placeholder="password" ng-model="user.password" class="form-control form-control-custom" /> <input id="password" type="password" placeholder="password" ng-model="user.password" class="form-control form-control-custom" />
</div> </div>
<div class="checkbox" ng-repeat="claim in claims">
<input id="claimCheckbox_{{$id}}" class="checkbox-custom" name="selectedClaims[]"
ng-checked="claim.selected" ng-model="claim.selected" type="checkbox" value="claim" />
<label for="claimCheckbox_{{$id}}">{{claim.name}}</label>
</div>
<input type="submit" class="btn btn-success-outline" value="Add" /> <input type="submit" class="btn btn-success-outline" value="Add" />
</form> </form>
<form> <form>
<div class="form-group"> <div class="form-group">
<div class="input-group"> <div class="input-group">
@ -33,6 +41,7 @@
</div> </div>
</div> </div>
</form> </form>
<table class="table table-striped table-hover table-responsive table-condensed"> <table class="table table-striped table-hover table-responsive table-condensed">
<thead> <thead>
<tr> <tr>

Loading…
Cancel
Save