Exemplo n.º 1
0
        private PermissionRuleContext ConvertFromConfiguration(CorePermissionConfiguration 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";
                bool isUnrestricted;
                if (Boolean.TryParse(configElement.IsUnrestricted, out isUnrestricted))
                {
                    propertyBag = PrepareStateForUnrestrictedRule(isUnrestricted);
                }
                else
                {
                    propertyBag = null;
                }
            }
            PermissionRuleContext ruleState = new PermissionRuleContext(ruleName, configElement.PermissionName, propertyBag, ruleDelegate);

            return(ruleState);
        }
Exemplo n.º 2
0
 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);
            }
        }
Exemplo n.º 4
0
        public Dictionary <string, PermissionRuleContextCollection> ReadRulesFromConfiguration()
        {
            Dictionary <string, PermissionRuleContextCollection> ruleContextLookup = new Dictionary <string, PermissionRuleContextCollection>();

            // process the core rules
            foreach (CorePermissionConfiguration configElement in Open.SPF.Configuration.SpfConfiguration.CorePermissions)
            {
                PermissionRuleContext ruleContext = ConvertFromConfiguration(configElement);
                AddRuleStateToLookup(ruleContextLookup, ruleContext);
            }

            // process the custom rules
            foreach (CustomPermissionConfiguration configElement in Open.SPF.Configuration.SpfConfiguration.CustomPermissions)
            {
                PermissionRuleContext ruleContext = ConvertFromConfiguration(configElement);
                AddRuleStateToLookup(ruleContextLookup, ruleContext);
            }

            return(ruleContextLookup);
        }
Exemplo n.º 5
0
        private PermissionRuleContext ConvertFromConfiguration(CustomPermissionConfiguration configElement)
        {
            if (String.IsNullOrWhiteSpace(configElement.PermissionName))
            {
                return(null);
            }
            if (String.IsNullOrWhiteSpace(configElement.RuleTypeName))
            {
                return(null);
            }
            if (String.IsNullOrWhiteSpace(configElement.RuleMethodName))
            {
                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, configElement.Arguments);
            if (propertyBag == null)
            {
                propertyBag = new Dictionary <string, object>();
            }

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

            return(ruleState);
        }
        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);
        }
Exemplo n.º 7
0
 public PermissionRuleContext(PermissionRuleContext clonedContext) : this(clonedContext, null, null)
 {
 }
 public void Add(PermissionRuleContext context)
 {
     _contextList.Add(context);
 }
Exemplo n.º 9
0
 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);
     }
 }
Exemplo n.º 10
0
        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);
        }