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