tidusjar 8 years ago
commit 36b7c91dfb

@ -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<string> GetAllClaims()
{
var properties = typeof(UserClaims).GetConstantsValues<string>();
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<string> GetAllClaims();
IEnumerable<UsersModel> GetUsers();
Task<IEnumerable<UsersModel>> GetUsersAsync();
UsersModel GetUser(Guid userId);

@ -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<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
{

@ -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<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.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: ""
};
// Select a user to populate on the right side
$scope.selectUser = function (id) {
$scope.selectedUser = {};
$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) {
@ -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 () {
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]);
}());

@ -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
};
}

@ -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;
}
/// <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/userManagementService.js"></script>
<div ng-controller="userManagementController" ng-init="getUsers()">
<div ng-controller="userManagementController" ng-init="init()">
<br />
<br />
@ -12,15 +12,23 @@
<br>
<br>
<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">
<input id="username" type="text" placeholder="user" ng-model="user.username" class="form-control form-control-custom" />
</div>
<div class="form-group">
<input id="password" type="password" placeholder="password" ng-model="user.password" class="form-control form-control-custom" />
</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" />
</form>
<form>
<div class="form-group">
<div class="input-group">
@ -33,6 +41,7 @@
</div>
</div>
</form>
<table class="table table-striped table-hover table-responsive table-condensed">
<thead>
<tr>

Loading…
Cancel
Save