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); }
private PermissionRuleContext ConvertFromConfiguration(CorePermissionConfigurationElement configElement) { if (String.IsNullOrWhiteSpace(configElement.PermissionName)) { return(null); } IsAuthorizedMethod ruleDelegate; string ruleName; Dictionary <string, object> propertyBag; if (!String.IsNullOrWhiteSpace(configElement.PermittedRole)) { ruleDelegate = this.IsUserRoleAuthorizedRule; ruleName = "IsUserRoleAuthorizedRule"; propertyBag = PrepareStateForRoleRule(configElement.PermittedRole); } else { ruleDelegate = this.IsUnrestrictedRule; ruleName = "IsUnrestrictedRule"; propertyBag = PrepareStateForUnrestrictedRule(configElement.IsUnrestricted); } PermissionRuleContext ruleState = new PermissionRuleContext(ruleName, configElement.PermissionName, propertyBag, ruleDelegate); return(ruleState); }
public Dictionary <string, PermissionRuleContextCollection> ReadRulesFromConfiguration() { Dictionary <string, PermissionRuleContextCollection> ruleContextLookup = new Dictionary <string, PermissionRuleContextCollection>(); ApplicationPermissionConfigurationSettings config = null; try { config = (ApplicationPermissionConfigurationSettings)System.Configuration.ConfigurationManager.GetSection ("appPermissionConfiguration"); } catch (Exception) { } if (config != null) { // process the core rules foreach (CorePermissionConfigurationElement configElement in config.CorePermissionConfigurationItems) { PermissionRuleContext ruleContext = ConvertFromConfiguration(configElement); AddRuleStateToLookup(ruleContextLookup, ruleContext); } // process the custom rules foreach (CustomPermissionConfigurationElement configElement in config.CustomPermissionConfigurationItems) { PermissionRuleContext ruleContext = ConvertFromConfiguration(configElement); AddRuleStateToLookup(ruleContextLookup, ruleContext); } } return(ruleContextLookup); }
public PermissionRuleContext(PermissionRuleContext clonedContext, object contextObject, Dictionary <string, object> additionalProperties) { ContextId = Guid.NewGuid(); RuleName = clonedContext.RuleName; PermissionName = clonedContext.PermissionName; ContextObject = contextObject ?? clonedContext.ContextObject; PropertyBag = MergeProperties(clonedContext.PropertyBag, additionalProperties); RuleDelegate = clonedContext.RuleDelegate; }
public PermissionRuleContext this[Guid contextId] { get { PermissionRuleContext context = null; foreach (PermissionRuleContext testContext in _contextList) { if (testContext.ContextId == contextId) { context = testContext; break; } } return(context); } }
public PermissionResultCollection InquirePermission(string permissionName, IIdentity userIdentity, IEnumerable <string> userRoles, object contextObject, Dictionary <string, object> contextProperties) { if (!_permissionRulesLookup.ContainsKey(permissionName)) { return(new PermissionResultCollection()); // There are no know rules for the requested permission, therefore the permission CANNOT be granted } PermissionRuleContextCollection ruleContextCollection = _permissionRulesLookup[permissionName]; List <Task <bool?> > taskList = new List <Task <bool?> >(); PermissionRuleContextCollection invocationContextCollection = new PermissionRuleContextCollection(); foreach (PermissionRuleContext configurationContext in ruleContextCollection.Items) { PermissionRuleContext invocationContext = new PermissionRuleContext(configurationContext, contextObject, contextProperties); invocationContextCollection.Add(invocationContext); Task <bool?> ruleTask = _coreRules.TaskFromPermissionRuleContext(invocationContext, userIdentity, userRoles); ruleTask.Start(); taskList.Add(ruleTask); } Task.WaitAll(taskList.ToArray()); PermissionResultCollection results = new PermissionResultCollection(); foreach (Task <bool?> ruleTask in taskList) { PermissionRuleContext invocationContext = invocationContextCollection[(Guid)ruleTask.AsyncState]; if (invocationContext == null) { throw new InvalidOperationException("An unexpected contition occurred while processing the permission rules. Could not identify the proper rule context."); } results.Add(new PermissionResult(ruleTask.Result, invocationContext.RuleName)); } return(results); }
public void Add(PermissionRuleContext context) { _contextList.Add(context); }
private void AddRuleStateToLookup(Dictionary <string, PermissionRuleContextCollection> ruleContextLookup, PermissionRuleContext ruleContext) { if (ruleContext != null) { PermissionRuleContextCollection contextCollection; if (ruleContextLookup.ContainsKey(ruleContext.PermissionName)) { contextCollection = ruleContextLookup[ruleContext.PermissionName]; } else { contextCollection = new PermissionRuleContextCollection(); ruleContextLookup.Add(ruleContext.PermissionName, contextCollection); } contextCollection.Add(ruleContext); } }
public Task <bool?> TaskFromPermissionRuleContext(PermissionRuleContext ruleContext, IIdentity userIdentity, IEnumerable <string> userRoles) { Task <bool?> ruleTask = new Task <bool?>(a => ruleContext.RuleDelegate.Invoke(userIdentity, userRoles, ruleContext.ContextObject, ruleContext.PropertyBag), ruleContext.ContextId); return(ruleTask); }
public PermissionRuleContext(PermissionRuleContext clonedContext) : this(clonedContext, null, null) { }