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);
     }
 }
        protected List <string> ReadPermissionRolesFromRules(Dictionary <string, PermissionRuleContextCollection> permissionRulesLookup)
        {
            List <string> permissionRoles = new List <string>();

            foreach (string permissionName in permissionRulesLookup.Keys)
            {
                PermissionRuleContextCollection contextCollection = permissionRulesLookup[permissionName];
                foreach (PermissionRuleContext rule in contextCollection.Items)
                {
                    if (!rule.PropertyBag.ContainsKey("PermittedRole"))
                    {
                        continue;
                    }

                    string permittedRole = (string)rule.PropertyBag["PermittedRole"];
                    if (!permissionRoles.Contains(permittedRole))
                    {
                        permissionRoles.Add(permittedRole);
                    }
                }
            }

            return(permissionRoles);
        }
        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);
        }