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; } = new List(); private List rolesText = null; public List RolesText { get { return new List(Roles.Select((RbacRole role) => role.Name)); } set { rolesText = value; } } public User(UserManager inUserManager) { userManager = inUserManager; } /// /// Updates this user's password. /// /// The new (unhashed) password. public void SetPassword(string newPassword) { HashedPassword = passwordHasher.Compute(newPassword); } /// /// Checks whether a specified (unhashed) password matches /// /// The password to check. /// Whether the specified password matches the stored password or not. public bool CheckPassword(string providedPassword) { return passwordHasher.Verify(providedPassword, HashedPassword); } /// /// Recursively works out whether this user has the specified permission. /// /// The permission to search for. /// Whether this user has the specified permission through one of their roles or not. public bool HasPermission(RbacPermission permission) { return Roles.Any((RbacRole role) => role.HasPermission(permission)); } public bool HasRole(RbacRole targetRole) { return Roles.Any((RbacRole role) => role.HasRole(targetRole)); } [OnDeserialized] internal void OnDeserialized(StreamingContext context) { Roles = new List(userManager.ResolveRoles(rolesText)); } } }