<commit_before>﻿//-----------------------------------------------------------------------
// <copyright file="AuthorizationRuleManager.cs" company="Marimer LLC">
//     Copyright (c) Marimer LLC. All rights reserved.
//     Website: http://www.lhotka.net/cslanet/
// </copyright>
// <summary>Manages the list of authorization </summary>
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Csla.Reflection;

namespace Csla.Rules
{
  /// <summary>
  /// Manages the list of authorization 
  /// rules for a business type.
  /// </summary>
  public class AuthorizationRuleManager
  {
#if !SILVERLIGHT
    private static Lazy<System.Collections.Concurrent.ConcurrentDictionary<RuleSetKey, AuthorizationRuleManager>> _perTypeRules =
      new Lazy<System.Collections.Concurrent.ConcurrentDictionary<RuleSetKey, AuthorizationRuleManager>>();

    internal static AuthorizationRuleManager GetRulesForType(Type type, string ruleSet)
    {
      if (ruleSet == ApplicationContext.DefaultRuleSet) ruleSet = null;

      var key = new RuleSetKey { Type = type, RuleSet = ruleSet };
      var result = _perTypeRules.Value.GetOrAdd(key, (t) => { return new AuthorizationRuleManager(); });
      InitializePerTypeRules(result, type);
      return result;
    }
#else
    private static Dictionary<RuleSetKey, AuthorizationRuleManager> _perTypeRules = new Dictionary<RuleSetKey, AuthorizationRuleManager>();

    internal static AuthorizationRuleManager GetRulesForType(Type type, string ruleSet)
    {
      // use null if RuleSet is "default" 
      if (ruleSet == ApplicationContext.DefaultRuleSet) ruleSet = null;

      AuthorizationRuleManager result = null;
      var key = new RuleSetKey { Type = type, RuleSet = ruleSet };
      if (!_perTypeRules.TryGetValue(key, out result))
      {
        lock (_perTypeRules)
        {
          if (!_perTypeRules.TryGetValue(key, out result))
          {
            result = new AuthorizationRuleManager();
            _perTypeRules.Add(key, result);
          }
        }
      }
      InitializePerTypeRules(result, type);
      return result;
    }
#endif

    internal static AuthorizationRuleManager GetRulesForType(Type type)
    {
      return GetRulesForType(type, null);
    }

    private static void InitializePerTypeRules(AuthorizationRuleManager mgr, Type type)
    {
      if (!mgr.InitializedPerType)
        lock (mgr)
          if (!mgr.InitializedPerType)
          {
            mgr.InitializedPerType = true;

            // Only call AddObjectAuthorizationRules when there are no rules for this type
            if (RulesExistForType(type)) return;
            try
            {
              // invoke method to add auth roles
              var flags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
              System.Reflection.MethodInfo method = type.GetMethod("AddObjectAuthorizationRules", flags);
              if (method != null)
                method.Invoke(null, null);
            }
            catch (Exception)
            {
              // remove all loaded rules for this type
              CleanupRulesForType(type);
              throw;  // and rethrow the exception
            }
          }
    }

    private static bool RulesExistForType(Type type)
    {
      lock (_perTypeRules)<commit_msg>Added BindingFlags.DeclaredOnly for reflection lookup.  bugid: 952<commit_after>﻿//-----------------------------------------------------------------------
// <copyright file="AuthorizationRuleManager.cs" company="Marimer LLC">
//     Copyright (c) Marimer LLC. All rights reserved.
//     Website: http://www.lhotka.net/cslanet/
// </copyright>
// <summary>Manages the list of authorization </summary>
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Csla.Reflection;

namespace Csla.Rules
{
  /// <summary>
  /// Manages the list of authorization 
  /// rules for a business type.
  /// </summary>
  public class AuthorizationRuleManager
  {
#if !SILVERLIGHT
    private static Lazy<System.Collections.Concurrent.ConcurrentDictionary<RuleSetKey, AuthorizationRuleManager>> _perTypeRules =
      new Lazy<System.Collections.Concurrent.ConcurrentDictionary<RuleSetKey, AuthorizationRuleManager>>();

    internal static AuthorizationRuleManager GetRulesForType(Type type, string ruleSet)
    {
      if (ruleSet == ApplicationContext.DefaultRuleSet) ruleSet = null;

      var key = new RuleSetKey { Type = type, RuleSet = ruleSet };
      var result = _perTypeRules.Value.GetOrAdd(key, (t) => { return new AuthorizationRuleManager(); });
      InitializePerTypeRules(result, type);
      return result;
    }
#else
    private static Dictionary<RuleSetKey, AuthorizationRuleManager> _perTypeRules = new Dictionary<RuleSetKey, AuthorizationRuleManager>();

    internal static AuthorizationRuleManager GetRulesForType(Type type, string ruleSet)
    {
      // use null if RuleSet is "default" 
      if (ruleSet == ApplicationContext.DefaultRuleSet) ruleSet = null;

      AuthorizationRuleManager result = null;
      var key = new RuleSetKey { Type = type, RuleSet = ruleSet };
      if (!_perTypeRules.TryGetValue(key, out result))
      {
        lock (_perTypeRules)
        {
          if (!_perTypeRules.TryGetValue(key, out result))
          {
            result = new AuthorizationRuleManager();
            _perTypeRules.Add(key, result);
          }
        }
      }
      InitializePerTypeRules(result, type);
      return result;
    }
#endif

    internal static AuthorizationRuleManager GetRulesForType(Type type)
    {
      return GetRulesForType(type, null);
    }

    private static void InitializePerTypeRules(AuthorizationRuleManager mgr, Type type)
    {
      if (!mgr.InitializedPerType)
        lock (mgr)
          if (!mgr.InitializedPerType)
          {
            mgr.InitializedPerType = true;

            // Only call AddObjectAuthorizationRules when there are no rules for this type
            if (RulesExistForType(type)) return;
            try
            {
              // invoke method to add auth roles
              var flags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly;
              System.Reflection.MethodInfo method = type.GetMethod("AddObjectAuthorizationRules", flags);
              if (method != null)
                method.Invoke(null, null);
            }
            catch (Exception)
            {
              // remove all loaded rules for this type
              CleanupRulesForType(type);
              throw;  // and rethrow the exception
            }
          }
    }

    private static bool RulesExistForType(Type type)
    {
      lock (_perTypeRules)
{'fraction_non_alphanumeric': 0.12699554149288078, 'fraction_numerical': 0.00043146843089313965, 'mean_word_length': 2.2693935119887163, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6027284', 'n_tokens_mistral': 1782, 'n_tokens_neox': 1770, 'n_words': 464}