internal static AuthorizationContext CreateDefaultAuthorizationContext(IList <IAuthorizationPolicy> authorizationPolicies)
        {
            AuthorizationContext _authorizationContext;

            // This is faster than Policy evaluation.
            if (authorizationPolicies != null && authorizationPolicies.Count == 1 && authorizationPolicies[0] is UnconditionalPolicy)
            {
                _authorizationContext = new SimpleAuthorizationContext(authorizationPolicies);
            }
            // degenerate case
            else if (authorizationPolicies == null || authorizationPolicies.Count <= 0)
            {
                return(DefaultAuthorizationContext.Empty);
            }
            else
            {
                // there are some policies, run them until they are all done
                DefaultEvaluationContext evaluationContext = new DefaultEvaluationContext();
                object[] policyState = new object[authorizationPolicies.Count];
                object   done        = new object();

                int oldContextCount;
                do
                {
                    oldContextCount = evaluationContext.Generation;

                    for (int i = 0; i < authorizationPolicies.Count; i++)
                    {
                        if (policyState[i] == done)
                        {
                            continue;
                        }

                        IAuthorizationPolicy policy = authorizationPolicies[i];
                        if (policy == null)
                        {
                            policyState[i] = done;
                            continue;
                        }

                        if (policy.Evaluate(evaluationContext, ref policyState[i]))
                        {
                            policyState[i] = done;
                        }
                    }
                } while (oldContextCount < evaluationContext.Generation);

                _authorizationContext = new DefaultAuthorizationContext(evaluationContext);
            }

            return(_authorizationContext);
        }
Exemple #2
0
        internal static AuthorizationContext CreateDefaultAuthorizationContext(IList <IAuthorizationPolicy> authorizationPolicies)
        {
            AuthorizationContext context;

            if (((authorizationPolicies != null) && (authorizationPolicies.Count == 1)) && (authorizationPolicies[0] is UnconditionalPolicy))
            {
                context = new SimpleAuthorizationContext(authorizationPolicies);
            }
            else
            {
                int generation;
                if ((authorizationPolicies == null) || (authorizationPolicies.Count <= 0))
                {
                    return(DefaultAuthorizationContext.Empty);
                }
                DefaultEvaluationContext evaluationContext = new DefaultEvaluationContext();
                object[] objArray = new object[authorizationPolicies.Count];
                object   obj2     = new object();
                do
                {
                    generation = evaluationContext.Generation;
                    for (int i = 0; i < authorizationPolicies.Count; i++)
                    {
                        if (objArray[i] != obj2)
                        {
                            IAuthorizationPolicy policy = authorizationPolicies[i];
                            if (policy == null)
                            {
                                objArray[i] = obj2;
                            }
                            else if (policy.Evaluate(evaluationContext, ref objArray[i]))
                            {
                                objArray[i] = obj2;
                                if (DiagnosticUtility.ShouldTraceVerbose)
                                {
                                    TraceUtility.TraceEvent(TraceEventType.Verbose, 0xc0003, System.IdentityModel.SR.GetString("AuthorizationPolicyEvaluated", new object[] { policy.Id }));
                                }
                            }
                        }
                    }
                }while (generation < evaluationContext.Generation);
                context = new DefaultAuthorizationContext(evaluationContext);
            }
            if (DiagnosticUtility.ShouldTraceInformation)
            {
                TraceUtility.TraceEvent(TraceEventType.Information, 0xc0002, System.IdentityModel.SR.GetString("AuthorizationContextCreated", new object[] { context.Id }));
            }
            return(context);
        }
Exemple #3
0
        internal static AuthorizationContext CreateDefaultAuthorizationContext(IList <IAuthorizationPolicy> authorizationPolicies)
        {
            AuthorizationContext authorizationContext;

            // This is faster than Policy evaluation.
            if (authorizationPolicies != null && authorizationPolicies.Count == 1 && authorizationPolicies[0] is UnconditionalPolicy)
            {
                authorizationContext = new SimpleAuthorizationContext(authorizationPolicies);
            }
            // degenerate case
            else if (authorizationPolicies == null || authorizationPolicies.Count <= 0)
            {
                return(DefaultAuthorizationContext.Empty);
            }
            else
            {
                // there are some policies, run them until they are all done
                DefaultEvaluationContext evaluationContext = new DefaultEvaluationContext();
                object[] policyState = new object[authorizationPolicies.Count];
                object   done        = new object();

                int oldContextCount;
                do
                {
                    oldContextCount = evaluationContext.Generation;

                    for (int i = 0; i < authorizationPolicies.Count; i++)
                    {
                        if (policyState[i] == done)
                        {
                            continue;
                        }

                        IAuthorizationPolicy policy = authorizationPolicies[i];
                        if (policy == null)
                        {
                            policyState[i] = done;
                            continue;
                        }

                        if (policy.Evaluate(evaluationContext, ref policyState[i]))
                        {
                            policyState[i] = done;

                            if (DiagnosticUtility.ShouldTraceVerbose)
                            {
                                TraceUtility.TraceEvent(TraceEventType.Verbose, TraceCode.AuthorizationPolicyEvaluated,
                                                        SR.GetString(SR.AuthorizationPolicyEvaluated, policy.Id));
                            }
                        }
                    }
                } while (oldContextCount < evaluationContext.Generation);

                authorizationContext = new DefaultAuthorizationContext(evaluationContext);
            }

            if (DiagnosticUtility.ShouldTraceInformation)
            {
                TraceUtility.TraceEvent(TraceEventType.Information, TraceCode.AuthorizationContextCreated,
                                        SR.GetString(SR.AuthorizationContextCreated, authorizationContext.Id));
            }

            return(authorizationContext);
        }