1
0
Fork 0
mirror of https://github.com/sbrl/Nibriboard.git synced 2018-01-10 21:33:49 +00:00

[server] Continue work on new user + rbac system

This commit is contained in:
Starbeamrainbowlabs 2017-12-29 14:52:26 +00:00
parent 4b7b24d892
commit def3c2f003
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
2 changed files with 78 additions and 14 deletions

View file

@ -1,23 +1,60 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Runtime.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using SimpleHashing.Net; using SimpleHashing.Net;
namespace Nibriboard.Userspace namespace Nibriboard.Userspace
{ {
/// <summary>
/// Creates new <see cref="User" /> class instances for Newtonsoft.json.
/// </summary>
public class UserCreationConverter : CustomCreationConverter<User>
{
private UserManager userManager;
public UserCreationConverter(UserManager inUserManager)
{
userManager = inUserManager;
}
public override User Create(Type objectType)
{
return new User(userManager);
}
}
/// <summary>
/// Represents a single Nibriboard user.
/// </summary>
[JsonObject(MemberSerialization.OptOut)]
public class User public class User
{ {
private static ISimpleHash passwordHasher = new SimpleHash(); private static ISimpleHash passwordHasher = new SimpleHash();
private UserManager userManager;
public DateTime CreationTime { get; set; } public DateTime CreationTime { get; set; }
public string Username { get; set; } public string Username { get; set; }
public string HashedPassword { get; set; } public string HashedPassword { get; set; }
[JsonIgnore]
public List<RbacRole> Roles { get; set; } public List<RbacRole> Roles { get; set; }
public User() private List<string> rolesText = null;
public List<string> RolesText {
get {
return new List<string>(Roles.Select((RbacRole role) => role.Name));
}
set {
}
}
public User(UserManager inUserManager)
{ {
userManager = inUserManager;
} }
/// <summary> /// <summary>
@ -47,5 +84,11 @@ namespace Nibriboard.Userspace
{ {
return Roles.Any((RbacRole role) => role.HasPermission(permission)); return Roles.Any((RbacRole role) => role.HasPermission(permission));
} }
[OnDeserialized]
internal void OnDeserialized(StreamingContext context)
{
Roles = new List<RbacRole>(userManager.ResolveRoles(RolesText));
}
} }
} }

View file

@ -1,5 +1,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace Nibriboard.Userspace 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.") new RbacPermission("manage-any-plane-members", "Manage the users allowed to access one any plane.")
}); });
Roles.Add(new RbacRole("Guest", new List<RbacPermission>() { Roles.Add(new RbacRole("Guest", new List<RbacPermission>() {
GetPermission("view-public-plane") ResolvePermission("view-public-plane")
})); }));
Roles.Add(new RbacRole("Member", new List<RbacPermission>() { Roles.Add(new RbacRole("Member", new List<RbacPermission>() {
GetPermission("view-own-plane"), ResolvePermission("view-own-plane"),
GetPermission("create-plane"), ResolvePermission("create-plane"),
GetPermission("delete-own-plane"), ResolvePermission("delete-own-plane"),
GetPermission("manage-own-plane-members") ResolvePermission("manage-own-plane-members")
}, new List<RbacRole>() { }, new List<RbacRole>() {
GetRole("Guest") ResolveRole("Guest")
})); }));
Roles.Add(new RbacRole("Root", new List<RbacPermission>() { Roles.Add(new RbacRole("Root", new List<RbacPermission>() {
GetPermission("view-any-plane"), ResolvePermission("view-any-plane"),
GetPermission("delete-any-plane"), ResolvePermission("delete-any-plane"),
GetPermission("manage-any-plane-members") ResolvePermission("manage-any-plane-members")
}, new List<RbacRole>() { }, new List<RbacRole>() {
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<List<User>>(userData, new UserCreationConverter(this));
}
public RbacPermission ResolvePermission(string permissionName)
{ {
return Permissions.Find((RbacPermission permission) => permission.Name == 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); return Roles.Find((RbacRole role) => role.Name == roleName);
} }
public IEnumerable<RbacRole> ResolveRoles(IEnumerable<string> roleNames)
{
foreach (RbacRole role in Roles)
{
if(roleNames.Contains(role.Name))
yield return role;
}
}
} }
} }