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

[server] Start building user/rbac system.

This commit is contained in:
Starbeamrainbowlabs 2017-12-29 10:36:55 +00:00
parent 233c3f7fe7
commit 4b7b24d892
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
6 changed files with 178 additions and 0 deletions

View file

@ -54,6 +54,9 @@
<Reference Include="GlidingSquirrel"> <Reference Include="GlidingSquirrel">
<HintPath>..\packages\GlidingSquirrel.0.6.1-alpha\lib\net462\GlidingSquirrel.dll</HintPath> <HintPath>..\packages\GlidingSquirrel.0.6.1-alpha\lib\net462\GlidingSquirrel.dll</HintPath>
</Reference> </Reference>
<Reference Include="SimpleHashing.Net">
<HintPath>..\packages\SimpleHashing.Net.1.0.1\lib\SimpleHashing.Net.dll</HintPath>
</Reference>
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
@ -145,6 +148,10 @@
<Compile Include="Client\Messages\LineRemoveMessage.cs" /> <Compile Include="Client\Messages\LineRemoveMessage.cs" />
<Compile Include="CommandConsole.cs" /> <Compile Include="CommandConsole.cs" />
<Compile Include="Utilities\Formatters.cs" /> <Compile Include="Utilities\Formatters.cs" />
<Compile Include="Userspace\UserManager.cs" />
<Compile Include="Userspace\RbacPermission.cs" />
<Compile Include="Userspace\RbacRole.cs" />
<Compile Include="Userspace\User.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<EmbeddedResource Include="commit-hash.txt" /> <EmbeddedResource Include="commit-hash.txt" />
@ -169,6 +176,7 @@
<Folder Include="ClientFiles\" /> <Folder Include="ClientFiles\" />
<Folder Include="Client\" /> <Folder Include="Client\" />
<Folder Include="Client\Messages\" /> <Folder Include="Client\Messages\" />
<Folder Include="Userspace\" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="packages.config" /> <None Include="packages.config" />

View file

@ -0,0 +1,33 @@
using System;
namespace Nibriboard.Userspace
{
public class RbacPermission
{
public readonly string Name;
public readonly string Description;
public RbacPermission(string inName, string inDescription)
{
Name = inName;
Description = inDescription;
}
public override bool Equals(object obj)
{
RbacPermission otherPermission = obj as RbacPermission;
if (obj == null)
return false;
return Name == otherPermission.Name;
}
public override int GetHashCode()
{
return ToString().GetHashCode();
}
public override string ToString()
{
return $"[RbacPermission -> {Name}: {Description}]";
}
}
}

View file

@ -0,0 +1,32 @@
using System;
using System.Linq;
using System.Collections.Generic;
namespace Nibriboard.Userspace
{
public class RbacRole
{
public readonly string Name;
public readonly List<RbacRole> SubRoles = new List<RbacRole>();
public readonly List<RbacPermission> Permissions = new List<RbacPermission>();
public RbacRole()
{
}
public RbacRole(string inRoleName, List<RbacPermission> inPermissions) : this(inRoleName, inPermissions, new List<RbacRole>())
{
}
public RbacRole(string inRoleName, List<RbacPermission> inPermissions, List<RbacRole> inSubRoles)
{
Name = inRoleName;
Permissions = inPermissions;
SubRoles = inSubRoles;
}
public bool HasPermission(RbacPermission permission)
{
return Permissions.Contains(permission) || SubRoles.Any((RbacRole obj) => obj.HasPermission(permission));
}
}
}

View file

@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using SimpleHashing.Net;
namespace Nibriboard.Userspace
{
public class User
{
private static ISimpleHash passwordHasher = new SimpleHash();
public DateTime CreationTime { get; set; }
public string Username { get; set; }
public string HashedPassword { get; set; }
public List<RbacRole> Roles { get; set; }
public User()
{
}
/// <summary>
/// Updates this user's password.
/// </summary>
/// <param name="newPassword">The new (unhashed) password.</param>
public void SetPassword(string newPassword)
{
HashedPassword = passwordHasher.Compute(newPassword);
}
/// <summary>
/// Checks whether a specified (unhashed) password matches
/// </summary>
/// <param name="providedPassword">The password to check.</param>
/// <returns>Whether the specified password matches the stored password or not.</returns>
public bool CheckPassword(string providedPassword)
{
return passwordHasher.Verify(providedPassword, HashedPassword);
}
/// <summary>
/// Recursively works out whether this user has the specified permission.
/// </summary>
/// <param name="permission">The permission to search for.</param>
/// <returns>Whether this user has the specified permission through one of their roles or not.</returns>
public bool HasPermission(RbacPermission permission)
{
return Roles.Any((RbacRole role) => role.HasPermission(permission));
}
}
}

View file

@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
namespace Nibriboard.Userspace
{
public class UserManager
{
public List<User> Users { get; private set; } = new List<User>();
public List<RbacPermission> Permissions { get; private set; } = new List<RbacPermission>();
public List<RbacRole> Roles { get; private set; } = new List<RbacRole>();
public UserManager()
{
Permissions.AddRange(new RbacPermission[] {
new RbacPermission("view-public-plane", "View public planes"),
new RbacPermission("view-own-plane", "View your own planes."),
new RbacPermission("view-any-plane", "View anyone's planes."),
new RbacPermission("create-plane", "Create a new plane."),
new RbacPermission("delete-own-plane", "Delete a plane."),
new RbacPermission("delete-any-plane", "Delete a plane."),
new RbacPermission("manage-own-plane-members", "Manage the users allowed to access one of your planes."),
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")
}));
Roles.Add(new RbacRole("Member", new List<RbacPermission>() {
GetPermission("view-own-plane"),
GetPermission("create-plane"),
GetPermission("delete-own-plane"),
GetPermission("manage-own-plane-members")
}, new List<RbacRole>() {
GetRole("Guest")
}));
Roles.Add(new RbacRole("Root", new List<RbacPermission>() {
GetPermission("view-any-plane"),
GetPermission("delete-any-plane"),
GetPermission("manage-any-plane-members")
}, new List<RbacRole>() {
GetRole("Member")
}));
}
public RbacPermission GetPermission(string permissionName)
{
return Permissions.Find((RbacPermission permission) => permission.Name == permissionName);
}
public RbacRole GetRole(string roleName)
{
return Roles.Find((RbacRole role) => role.Name == roleName);
}
}
}

View file

@ -5,5 +5,6 @@
<package id="NCuid" version="1.0.0" targetFramework="net461" /> <package id="NCuid" version="1.0.0" targetFramework="net461" />
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net461" /> <package id="Newtonsoft.Json" version="10.0.3" targetFramework="net461" />
<package id="SharpCompress" version="0.19.2" targetFramework="net461" /> <package id="SharpCompress" version="0.19.2" targetFramework="net461" />
<package id="SimpleHashing.Net" version="1.0.1" targetFramework="net462" />
<package id="System.ValueTuple" version="4.4.0" targetFramework="net462" /> <package id="System.ValueTuple" version="4.4.0" targetFramework="net462" />
</packages> </packages>