private PermissionRuleContext ConvertFromConfiguration(CustomPermissionConfigurationElement configElement)
        {
            if (String.IsNullOrWhiteSpace(configElement.PermissionName))
            {
                return(null);
            }

            IsAuthorizedMethod          ruleDelegate;
            Dictionary <string, object> propertyBag;
            ICustomRuleContainer        customContainer = CustomRuleContainerFromTypeName(configElement.RuleTypeName);

            if (customContainer == null)
            {
                return(null);
            }

            ruleDelegate = this.RuleDelegateFromMethodName(customContainer, configElement.RuleMethodName);
            if (ruleDelegate == null)
            {
                return(null);
            }
            string ruleName = RuleNameFromMethodName(customContainer, configElement.RuleMethodName);

            propertyBag = customContainer.PreparePropertiesForRule(configElement.RuleMethodName, new string[] { configElement.Argument1, configElement.Argument2, configElement.Argument3, configElement.Argument4, configElement.Argument5 });
            if (propertyBag == null)
            {
                propertyBag = new Dictionary <string, object>();
            }

            PermissionRuleContext ruleState = new PermissionRuleContext(ruleName, configElement.PermissionName, propertyBag, ruleDelegate);

            return(ruleState);
        }
        public string RuleNameFromMethodName(ICustomRuleContainer customContainer, string methodName)
        {
            Type[]     methodSignature = new Type[] { typeof(IIdentity), typeof(IEnumerable <string>), typeof(object), typeof(Dictionary <string, object>) };
            MethodInfo ruleMethodInfo  = customContainer.GetType().GetMethod(methodName, methodSignature);

            if (ruleMethodInfo == null)
            {
                TraceUtility.WriteTrace(this.GetType(), "RuleNameFromMethodName", null, String.Format("Could not obtain Method information from the Method name. TypeName: {0}, MethodName: {1}", customContainer.GetType().FullName, methodName), TraceUtility.TraceType.Warning);
                return(null);
            }

            PermissionRuleMethodAttribute permissionAttribute = ruleMethodInfo.GetCustomAttribute <PermissionRuleMethodAttribute>();

            if (permissionAttribute == null)
            {
                TraceUtility.WriteTrace(this.GetType(), "RuleNameFromMethodName", null, String.Format("The {1} method in the {0} class was marked as a Permission Rule Method, but it exhibited an incorrect method signature for rule processing. A Permission Rule Method should accept the following parameters: (IIdentity userIdentity, IEnumerable<string> userRoles, Dictionary<string, object> contextPropertyBag).", customContainer.GetType().FullName, ruleMethodInfo.Name), TraceUtility.TraceType.Warning);
                return(null);
            }

            return((!String.IsNullOrWhiteSpace(permissionAttribute.Name)) ? permissionAttribute.Name : ruleMethodInfo.Name);
        }
        public void RuleFromTypeAndMethodNameTest()
        {
            string typeName   = "Open.SPF.Core.Test.TestCustomRules, Open.SPF.Core.Test, Version=1.2.1, Culture=neutral, PublicKeyToken=b4313207536550be";
            string methodName = "CustomRuleIsAlwaysAuthorized";

            PermissionRules      uut             = new PermissionRules();
            ICustomRuleContainer customContainer = uut.CustomRuleContainerFromTypeName(typeName);

            Assert.IsNotNull(customContainer);
            PermissionRules.IsAuthorizedMethod del = uut.RuleDelegateFromMethodName(customContainer, methodName);
            Assert.IsNotNull(del);

            string ruleName = uut.RuleNameFromMethodName(customContainer, methodName);

            Assert.IsNotNull(ruleName);
            Assert.AreEqual(methodName, ruleName);
            Dictionary <string, object> contextPropertyBag = customContainer.PreparePropertiesForRule(ruleName, null);

            bool?isAuthorized = del.Invoke(PermissionRulesTests.TestIdentity, PermissionRulesTests.TestRoles, null, contextPropertyBag);

            Assert.IsTrue(isAuthorized.HasValue);
            Assert.IsTrue(isAuthorized.Value);
        }