9using System.Collections.Generic;
12using System.Reflection;
23 private static Lazy<System.Collections.Concurrent.ConcurrentDictionary<RuleSetKey,
AuthorizationRuleManager>> _perTypeRules =
28 type = ApplicationContext.DataPortalActivator.ResolveType(type);
30 if (ruleSet == ApplicationContext.DefaultRuleSet)
33 var key =
new RuleSetKey { Type = type, RuleSet = ruleSet };
35 InitializePerTypeRules(result, type);
39 private bool InitializingPerType {
get;
set; }
48 if (RulesExistForType(type))
56 mgr.InitializingPerType =
true;
59 System.Reflection.MethodInfo method = FindObjectAuthorizationRulesMethod(type);
61 method.Invoke(
null,
null);
67 CleanupRulesForType(type);
72 mgr.InitializingPerType =
false;
77 private static System.Reflection.MethodInfo FindObjectAuthorizationRulesMethod(Type type)
79 System.Reflection.MethodInfo method;
80 method = type.GetMethods().Where(
81 m => m.IsStatic && m.CustomAttributes.Where(
86 const BindingFlags flags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy;
87 method = type.GetMethod(
"AddObjectAuthorizationRules", flags);
92 private static bool RulesExistForType(Type type)
97 return _perTypeRules.Value.Count(value => value.Key.Type == type) > 1;
102 private static void CleanupRulesForType(Type type)
108 var typeRules = _perTypeRules.Value.Where(value => value.Key.Type == type);
109 foreach (var key
in typeRules)
112 _perTypeRules.Value.TryRemove(key.Key, out manager);
117 private class RuleSetKey
119 public Type Type {
get;
set; }
120 public string RuleSet {
get;
set; }
122 public override bool Equals(
object obj)
124 var other = obj as RuleSetKey;
128 return this.Type.Equals(other.Type) && RuleSet == other.RuleSet;
131 public override int GetHashCode()
133 return (this.Type.FullName + RuleSet).GetHashCode();
140 public List<IAuthorizationRule>
Rules {
get;
private set; }
155 Rules =
new List<IAuthorizationRule>();
Attribute identifying static method invoked to add object authorization rules for type.
Manages the list of authorization rules for a business type.
bool Initialized
Gets or sets a value indicating whether the rules have been initialized.
bool InitializedPerType
Gets or sets a value indicating whether the rules have been initialized.
List< IAuthorizationRule > Rules
Gets the list of rule objects for the business type.
@ Any
Default value, rule can run in any context