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:
parent
4b7b24d892
commit
def3c2f003
2 changed files with 78 additions and 14 deletions
|
@ -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
|
||||
{
|
||||
/// <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
|
||||
{
|
||||
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<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>
|
||||
|
@ -47,5 +84,11 @@ namespace Nibriboard.Userspace
|
|||
{
|
||||
return Roles.Any((RbacRole role) => role.HasPermission(permission));
|
||||
}
|
||||
|
||||
[OnDeserialized]
|
||||
internal void OnDeserialized(StreamingContext context)
|
||||
{
|
||||
Roles = new List<RbacRole>(userManager.ResolveRoles(RolesText));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<RbacPermission>() {
|
||||
GetPermission("view-public-plane")
|
||||
ResolvePermission("view-public-plane")
|
||||
}));
|
||||
Roles.Add(new RbacRole("Member", new List<RbacPermission>() {
|
||||
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<RbacRole>() {
|
||||
GetRole("Guest")
|
||||
ResolveRole("Guest")
|
||||
}));
|
||||
Roles.Add(new RbacRole("Root", new List<RbacPermission>() {
|
||||
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<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);
|
||||
}
|
||||
public RbacRole GetRole(string roleName)
|
||||
public RbacRole ResolveRole(string 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue