Files
mixtape/zero.Core/Identity/Permissions/Permission.cs
T

85 lines
2.4 KiB
C#
Raw Normal View History

using System.Security.Claims;
2021-11-19 16:11:12 +01:00
2021-11-20 13:52:28 +01:00
namespace zero.Identity;
2021-11-19 16:11:12 +01:00
2021-11-27 00:59:27 +01:00
public struct Permission
2021-11-19 16:11:12 +01:00
{
/// <summary>
/// Full key (stored in left part claim value) of the permission
/// </summary>
public string Key { get; set; }
/// <summary>
/// Title of this permission for output
/// </summary>
public string Label { get; set; }
2021-11-29 00:38:55 +01:00
/// <summary>
/// When a permission is disabled it can not be updated anymore
/// </summary>
public bool IsDisabled { get; set; }
/// <summary>
/// Child permissions
/// </summary>
public HashSet<Permission> Children { get; set; }
2021-11-19 16:11:12 +01:00
public Permission(string key, string label)
2021-11-19 16:11:12 +01:00
{
Key = key;
Label = label;
2021-11-29 00:38:55 +01:00
IsDisabled = false;
Children = new();
2021-11-19 16:11:12 +01:00
}
///// <summary>
///// Whether a resource (with authorization based on the key) can be read by any of the given permisssions
///// </summary>
//public static bool CanReadKey(IEnumerable<Permission> permissions, string key, bool isNormalized = false)
//{
// Permission permission = permissions.FirstOrDefault(p => isNormalized ? p.NormalizedKey == key : p.Key == key);
// return permission?.CanRead ?? false;
//}
2021-11-19 16:11:12 +01:00
///// <summary>
///// Whether a resource (with authorization based on the key) can be written to by any of the given permisssions
///// </summary>
//public static bool CanWriteKey(IEnumerable<Permission> permissions, string key, bool isNormalized = false)
//{
// Permission permission = permissions.FirstOrDefault(p => isNormalized ? p.NormalizedKey == key : p.Key == key);
// return permission?.CanWrite ?? false;
//}
2021-11-19 16:11:12 +01:00
///// <summary>
///// Create a permission from a claim
///// </summary>
//public static Permission FromClaim(Claim claim, string prefixToRemove = null)
//{
// if (claim == null)
// {
// return null;
// }
// return FromClaim(claim.Value, prefixToRemove);
//}
2021-11-19 16:11:12 +01:00
///// <summary>
///// Create a permission from a claim
///// </summary>
//public static Permission FromClaim(string claimValue, string prefixToRemove = null)
//{
// Permission permission = new Permission();
// string[] valueParts = claimValue.Split(':');
2021-11-19 16:11:12 +01:00
// permission.Key = valueParts[0];
// permission.NormalizedKey = permission.Key;
// permission.Value = valueParts.Length > 1 ? valueParts[1] : null;
2021-11-19 16:11:12 +01:00
// if (!prefixToRemove.IsNullOrEmpty())
// {
// permission.NormalizedKey = valueParts[0].TrimStart(prefixToRemove);
// }
2021-11-19 16:11:12 +01:00
// return permission;
//}
2021-11-19 16:11:12 +01:00
}