| | | 1 | | namespace Kestrun.Claims; |
| | | 2 | | |
| | | 3 | | /// <summary>A bag of named policies, each backed by a ClaimRule.</summary> |
| | | 4 | | /// <remarks> |
| | | 5 | | /// This is used to define multiple authorization policies in a structured way. |
| | | 6 | | /// </remarks> |
| | | 7 | | public sealed class ClaimPolicyConfig |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Gets the dictionary of named policies, each backed by a <see cref="ClaimRule"/>. |
| | | 11 | | /// </summary> |
| | 51 | 12 | | public Dictionary<string, ClaimRule> Policies { get; init; } = []; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Gets the number of defined policies. |
| | | 16 | | /// </summary> |
| | 0 | 17 | | public int Count => Policies.Count; |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Gets the names of all defined policies. |
| | | 21 | | /// </summary> |
| | 1 | 22 | | public IEnumerable<string> PolicyNames => Policies.Keys; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Gets a policy by name. |
| | | 26 | | /// </summary> |
| | | 27 | | /// <param name="policyName">The name of the policy to retrieve.</param> |
| | | 28 | | /// <returns>The <see cref="ClaimRule"/> associated with the specified policy name, or null if not found.</returns> |
| | 0 | 29 | | public ClaimRule? GetPolicy(string policyName) => Policies.TryGetValue(policyName, out var rule) ? rule : null; |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Add a policy by name. |
| | | 33 | | /// </summary> |
| | | 34 | | /// <param name="policyName">The name of the policy to set.</param> |
| | | 35 | | /// <param name="rule">The <see cref="ClaimRule"/> to associate with the specified policy name.</param> |
| | 0 | 36 | | public void AddPolicy(string policyName, ClaimRule rule) => Policies[policyName] = rule; |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Add multiple policies. |
| | | 40 | | /// </summary> |
| | | 41 | | /// <param name="policies">The collection of policies to add.</param> |
| | | 42 | | public void AddPolicies(IEnumerable<KeyValuePair<string, ClaimRule>> policies) |
| | | 43 | | { |
| | 4 | 44 | | foreach (var kvp in policies) |
| | | 45 | | { |
| | 1 | 46 | | Policies[kvp.Key] = kvp.Value; |
| | | 47 | | } |
| | 1 | 48 | | } |
| | | 49 | | } |