From 068f75f5141280819b32c45ced82bf11617a4b5e Mon Sep 17 00:00:00 2001 From: tidusjar Date: Fri, 19 Aug 2016 11:22:17 +0100 Subject: [PATCH] User work --- PlexRequests.Core/UserMapper.cs | 13 ++++++ PlexRequests.Helpers.Tests/TypeHelperTests.cs | 10 ++++- PlexRequests.Helpers/TypeHelper.cs | 16 +++++++ .../userManagementController.js | 44 ++++++++++++++++--- .../userManagement/userManagementService.js | 18 +++++--- .../Modules/UserManagementModule.cs | 22 ++++++++-- .../Views/UserManagement/Index.cshtml | 13 +++++- 7 files changed, 118 insertions(+), 18 deletions(-) diff --git a/PlexRequests.Core/UserMapper.cs b/PlexRequests.Core/UserMapper.cs index 5be4a868a..d4eca3e2a 100644 --- a/PlexRequests.Core/UserMapper.cs +++ b/PlexRequests.Core/UserMapper.cs @@ -134,6 +134,17 @@ namespace PlexRequests.Core 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 GetAllClaims() + { + var properties = typeof(UserClaims).GetConstantsValues(); + return properties; + } + public bool UpdatePassword(string username, string oldPassword, string newPassword) { var users = Repo.GetAll(); @@ -175,6 +186,8 @@ namespace PlexRequests.Core public interface ICustomUserMapper { + Guid? CreateUser(string username, string password, string[] claims); + IEnumerable GetAllClaims(); IEnumerable GetUsers(); Task> GetUsersAsync(); UsersModel GetUser(Guid userId); diff --git a/PlexRequests.Helpers.Tests/TypeHelperTests.cs b/PlexRequests.Helpers.Tests/TypeHelperTests.cs index 295b955ad..1d93c375f 100644 --- a/PlexRequests.Helpers.Tests/TypeHelperTests.cs +++ b/PlexRequests.Helpers.Tests/TypeHelperTests.cs @@ -26,7 +26,7 @@ #endregion using System; using System.Collections.Generic; - +using System.Linq; using NUnit.Framework; using PlexRequests.Store; @@ -42,6 +42,14 @@ namespace PlexRequests.Helpers.Tests return input.GetPropertyNames(); } + [Test] + public void GetConstantsTest() + { + var consts = typeof(UserClaims).GetConstantsValues(); + Assert.That(consts.Contains("Admin"),Is.True); + Assert.That(consts.Contains("PowerUser"),Is.True); + Assert.That(consts.Contains("User"),Is.True); + } private static IEnumerable TypeData { diff --git a/PlexRequests.Helpers/TypeHelper.cs b/PlexRequests.Helpers/TypeHelper.cs index 27a108bef..77eae6360 100644 --- a/PlexRequests.Helpers/TypeHelper.cs +++ b/PlexRequests.Helpers/TypeHelper.cs @@ -25,7 +25,9 @@ // ************************************************************************/ #endregion using System; +using System.Collections.Generic; using System.Linq; +using System.Reflection; namespace PlexRequests.Helpers { @@ -35,5 +37,19 @@ namespace PlexRequests.Helpers { return t.GetProperties().Select(x => x.Name).ToArray(); } + + public static IEnumerable 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 GetConstantsValues(this Type type) where T : class + { + var fieldInfos = GetConstants(type); + + return fieldInfos.Select(fi => fi.GetRawConstantValue() as T); + } } } \ No newline at end of file diff --git a/PlexRequests.UI/Content/app/userManagement/userManagementController.js b/PlexRequests.UI/Content/app/userManagement/userManagementController.js index 8e74d3c2d..a577b1380 100644 --- a/PlexRequests.UI/Content/app/userManagement/userManagementController.js +++ b/PlexRequests.UI/Content/app/userManagement/userManagementController.js @@ -4,22 +4,27 @@ $scope.user = {}; // The local user $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.searchTerm = ''; + $scope.searchTerm = ""; + $scope.error = { error: false, errorMessage: "" }; - $scope.selectUser = function(id) { + // Select a user to populate on the right side + $scope.selectUser = function (id) { $scope.selectedUser = $scope.users.find(x => x.id === id); } + // Get all users in the system $scope.getUsers = function () { $scope.users = userManagementService.getUsers() .then(function (data) { @@ -27,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 () { + if (!$scope.user.username || !$scope.user.password) { $scope.error.error = true; $scope.error.errorMessage = "Please provide a correct username and password"; generateNotify($scope.error.errorMessage, 'warning'); return; } - userManagementService.addUser($scope.user).then(function (data) { + + userManagementService.addUser($scope.user, $scope.selectedClaims).then(function (data) { if (data.message) { $scope.error.error = true; $scope.error.errorMessage = data.message; } else { - $scope.users.push(data); + $scope.users.push(data); // Push the new user into the array to update the DOM $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]); - }()); \ No newline at end of file diff --git a/PlexRequests.UI/Content/app/userManagement/userManagementService.js b/PlexRequests.UI/Content/app/userManagement/userManagementService.js index fe7b7ef07..b70d1fe26 100644 --- a/PlexRequests.UI/Content/app/userManagement/userManagementService.js +++ b/PlexRequests.UI/Content/app/userManagement/userManagementService.js @@ -2,27 +2,35 @@ 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 () { return $http.get('/usermanagement/users'); }; - var addUser = function (user) { - if (!user) { + var addUser = function (user, claims) { + if (!user || claims.length === 0) { return null; } + var claimJson = angular.toJson(claims); + var objectToSerialize = { 'claims': claimJson }; + var data = $.param(user) +"&"+ $.param(objectToSerialize); return $http({ url: '/usermanagement/createuser', method: "POST", - data: $.param(user) + data: data }); } + var getClaims = function() { + return $http.get('/usermanagement/claims'); + } + return { getUsers: getUsers, - addUser: addUser + addUser: addUser, + getClaims: getClaims }; } diff --git a/PlexRequests.UI/Modules/UserManagementModule.cs b/PlexRequests.UI/Modules/UserManagementModule.cs index 9667dac6f..f364d9588 100644 --- a/PlexRequests.UI/Modules/UserManagementModule.cs +++ b/PlexRequests.UI/Modules/UserManagementModule.cs @@ -30,9 +30,10 @@ namespace PlexRequests.UI.Modules Get["/"] = x => Load(); 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["/plex/{id}", true] = async (x,ct) => await PlexDetails(x.id); + Get["/claims"] = x => GetClaims(); } private ICustomUserMapper UserMapper { get; } @@ -91,7 +92,7 @@ namespace PlexRequests.UI.Modules 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)) { @@ -101,7 +102,7 @@ namespace PlexRequests.UI.Modules Message = "Please enter in a valid Username and Password" }); } - var user = UserMapper.CreateRegularUser(username, password); + var user = UserMapper.Ce(username, password); if (user.HasValue) { return Response.AsJson(user); @@ -139,6 +140,21 @@ namespace PlexRequests.UI.Modules return Nancy.Response.NoBody; } + + /// + /// Returns all claims for the users. + /// + /// + private Response GetClaims() + { + var retVal = new List(); + var claims = UserMapper.GetAllClaims(); + foreach (var c in claims) + { + retVal.Add(new {Name = c, Selected = false}); + } + return Response.AsJson(retVal); + } } } diff --git a/PlexRequests.UI/Views/UserManagement/Index.cshtml b/PlexRequests.UI/Views/UserManagement/Index.cshtml index 14a670a91..e04935869 100644 --- a/PlexRequests.UI/Views/UserManagement/Index.cshtml +++ b/PlexRequests.UI/Views/UserManagement/Index.cshtml @@ -3,7 +3,7 @@ @Html.LoadAngularAssets() -
+


@@ -12,15 +12,23 @@

-
+
+ +
+ + +
+
+
@@ -33,6 +41,7 @@
+