|
1 | 1 | using System;
|
2 | 2 | using System.Collections.Generic;
|
3 | 3 | using System.Collections.ObjectModel;
|
| 4 | +using System.Linq; |
| 5 | +using Altinn.Authorization.ABAC.Constants; |
4 | 6 | using Altinn.Authorization.ABAC.Utils;
|
5 | 7 |
|
6 | 8 | namespace Altinn.Authorization.ABAC.Xacml
|
@@ -96,6 +98,8 @@ public class XacmlPolicy
|
96 | 98 |
|
97 | 99 | private readonly ICollection<XacmlVariableDefinition> variableDefinitions = new Collection<XacmlVariableDefinition>();
|
98 | 100 |
|
| 101 | + private readonly IDictionary<string, IDictionary<string, Collection<string>>> categoryAttributes = new Dictionary<string, IDictionary<string, Collection<string>>>(); |
| 102 | + |
99 | 103 | private XacmlTarget target;
|
100 | 104 | private Uri policyId;
|
101 | 105 | private Uri ruleCombiningAlgId;
|
@@ -308,6 +312,51 @@ public ICollection<XacmlAdviceExpression> AdviceExpressions
|
308 | 312 | }
|
309 | 313 | }
|
310 | 314 |
|
| 315 | + /// <summary> |
| 316 | + /// Returns a dictionary of all unique attribute ids and a collection of all their values, which exists across all rules in the policy, for a given match attribute category. |
| 317 | + /// </summary> |
| 318 | + /// <param name="matchAttributeCategory">The Xacml match attribute category to collect attributes values of</param> |
| 319 | + /// <returns>Dictionary of attribute ids and list of values</returns> |
| 320 | + public IDictionary<string, Collection<string>> GetAttributeDictionaryByCategory(string matchAttributeCategory) |
| 321 | + { |
| 322 | + if (categoryAttributes.ContainsKey(matchAttributeCategory)) |
| 323 | + { |
| 324 | + return categoryAttributes[matchAttributeCategory]; |
| 325 | + } |
| 326 | + |
| 327 | + IDictionary<string, Collection<string>> categoryAttributeDict = new Dictionary<string, Collection<string>>(); |
| 328 | + categoryAttributes.Add(matchAttributeCategory, categoryAttributeDict); |
| 329 | + |
| 330 | + foreach (XacmlRule rule in Rules) |
| 331 | + { |
| 332 | + // should we care about permit? |
| 333 | + if (rule.Effect.Equals(XacmlEffectType.Permit) && rule.Target != null) |
| 334 | + { |
| 335 | + foreach (XacmlAnyOf anyOf in rule.Target.AnyOf) |
| 336 | + { |
| 337 | + foreach (XacmlAllOf allOf in anyOf.AllOf) |
| 338 | + { |
| 339 | + foreach (XacmlMatch xacmlMatch in allOf.Matches) |
| 340 | + { |
| 341 | + if (xacmlMatch.AttributeDesignator.Category.Equals(matchAttributeCategory)) |
| 342 | + { |
| 343 | + string attributeId = xacmlMatch.AttributeDesignator.AttributeId.AbsoluteUri; |
| 344 | + if (!categoryAttributeDict.ContainsKey(attributeId)) |
| 345 | + { |
| 346 | + categoryAttributeDict.Add(attributeId, new Collection<string>()); |
| 347 | + } |
| 348 | + |
| 349 | + categoryAttributeDict[attributeId].Add(xacmlMatch.AttributeValue.Value); |
| 350 | + } |
| 351 | + } |
| 352 | + } |
| 353 | + } |
| 354 | + } |
| 355 | + } |
| 356 | + |
| 357 | + return categoryAttributes[matchAttributeCategory]; |
| 358 | + } |
| 359 | + |
311 | 360 | /// <summary>
|
312 | 361 | /// The namespaces used in Policy
|
313 | 362 | /// </summary>
|
|
0 commit comments