From def3c2f00332fdc397e2a4ab6bcfdfab76c0eb85 Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Fri, 29 Dec 2017 14:52:26 +0000 Subject: [PATCH] [server] Continue work on new user + rbac system --- Nibriboard/Userspace/User.cs | 47 +++++++++++++++++++++++++++-- Nibriboard/Userspace/UserManager.cs | 45 +++++++++++++++++++-------- 2 files changed, 78 insertions(+), 14 deletions(-) diff --git a/Nibriboard/Userspace/User.cs b/Nibriboard/Userspace/User.cs index 5beef87..c5a4ba6 100644 --- a/Nibriboard/Userspace/User.cs +++ b/Nibriboard/Userspace/User.cs @@ -1,23 +1,60 @@ using System; using System.Collections.Generic; using System.Linq; - +using System.Runtime.Serialization; +using Newtonsoft.Json; +using Newtonsoft.Json.Converters; using SimpleHashing.Net; namespace Nibriboard.Userspace { + /// + /// Creates new class instances for Newtonsoft.json. + /// + public class UserCreationConverter : CustomCreationConverter + { + private UserManager userManager; + public UserCreationConverter(UserManager inUserManager) + { + userManager = inUserManager; + } + + public override User Create(Type objectType) + { + return new User(userManager); + } + } + + /// + /// Represents a single Nibriboard user. + /// + [JsonObject(MemberSerialization.OptOut)] public class User { private static ISimpleHash passwordHasher = new SimpleHash(); + private UserManager userManager; + public DateTime CreationTime { get; set; } public string Username { get; set; } public string HashedPassword { get; set; } + [JsonIgnore] public List Roles { get; set; } - public User() + private List rolesText = null; + public List RolesText { + get { + return new List(Roles.Select((RbacRole role) => role.Name)); + } + set { + + } + } + + public User(UserManager inUserManager) { + userManager = inUserManager; } /// @@ -47,5 +84,11 @@ namespace Nibriboard.Userspace { return Roles.Any((RbacRole role) => role.HasPermission(permission)); } + + [OnDeserialized] + internal void OnDeserialized(StreamingContext context) + { + Roles = new List(userManager.ResolveRoles(RolesText)); + } } } diff --git a/Nibriboard/Userspace/UserManager.cs b/Nibriboard/Userspace/UserManager.cs index 876cfe5..833460f 100644 --- a/Nibriboard/Userspace/UserManager.cs +++ b/Nibriboard/Userspace/UserManager.cs @@ -1,5 +1,9 @@ using System; using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using Newtonsoft.Json; namespace Nibriboard.Userspace { @@ -22,32 +26,49 @@ namespace Nibriboard.Userspace new RbacPermission("manage-any-plane-members", "Manage the users allowed to access one any plane.") }); Roles.Add(new RbacRole("Guest", new List() { - GetPermission("view-public-plane") + ResolvePermission("view-public-plane") })); Roles.Add(new RbacRole("Member", new List() { - GetPermission("view-own-plane"), - GetPermission("create-plane"), - GetPermission("delete-own-plane"), - GetPermission("manage-own-plane-members") + ResolvePermission("view-own-plane"), + ResolvePermission("create-plane"), + ResolvePermission("delete-own-plane"), + ResolvePermission("manage-own-plane-members") }, new List() { - GetRole("Guest") + ResolveRole("Guest") })); Roles.Add(new RbacRole("Root", new List() { - GetPermission("view-any-plane"), - GetPermission("delete-any-plane"), - GetPermission("manage-any-plane-members") + ResolvePermission("view-any-plane"), + ResolvePermission("delete-any-plane"), + ResolvePermission("manage-any-plane-members") }, new List() { - GetRole("Member") + ResolveRole("Member") })); } - public RbacPermission GetPermission(string permissionName) + public async Task LoadUserData(StreamReader userDataStream) + { + LoadUserData(await userDataStream.ReadToEndAsync()); + } + public void LoadUserData(string userData) + { + Users = JsonConvert.DeserializeObject>(userData, new UserCreationConverter(this)); + } + + public RbacPermission ResolvePermission(string permissionName) { return Permissions.Find((RbacPermission permission) => permission.Name == permissionName); } - public RbacRole GetRole(string roleName) + public RbacRole ResolveRole(string roleName) { return Roles.Find((RbacRole role) => role.Name == roleName); } + public IEnumerable ResolveRoles(IEnumerable roleNames) + { + foreach (RbacRole role in Roles) + { + if(roleNames.Contains(role.Name)) + yield return role; + } + } } }