static Expression BeSafe(Expression target, Expression expression, Func <Expression, Expression> update)
        {
            var fallback = Cache.GetOrAdd(target.Type);

            if (fallback != null)
            {
                // coalesce instead, a bit intrusive but fast...
                return(update(Expression.Coalesce(target, fallback)));
            }

            // target can be null, which is why we are actually here...
            var targetFallback = Expression.Constant(null, target.Type);

            // expression can be default or null, which is basically the same...
            var expressionFallback = !IsNullableOrReferenceType(expression.Type)
                ? (Expression)Expression.Default(expression.Type) : Expression.Constant(null, expression.Type);

            return(Expression.Condition(Expression.Equal(target, targetFallback), expressionFallback, expression));
        }
Beispiel #2
0
        public LambdaExpression[] GetMapExpression(Type sourceType, Type destinationType, object parameters,
                                                   MemberInfo[] membersToExpand)
        {
            if (sourceType == null)
            {
                throw new ArgumentNullException(nameof(sourceType));
            }
            if (destinationType == null)
            {
                throw new ArgumentNullException(nameof(destinationType));
            }
            if (membersToExpand == null)
            {
                throw new ArgumentNullException(nameof(membersToExpand));
            }

            var cachedExpressions = _expressionCache.GetOrAdd(new ExpressionRequest(sourceType, destinationType, membersToExpand, null));

            return(cachedExpressions.Select(e => Prepare(e, parameters)).Cast <LambdaExpression>().ToArray());
        }
Beispiel #3
0
        public override async Task <AuthorizationPolicy> GetPolicyAsync(string policyName)
        {
            if (!policyName.StartsWith(PermissionConstant.PolicyPrefix, StringComparison.OrdinalIgnoreCase))
            {
                return(await base.GetPolicyAsync(policyName));
            }

            var policy = _policies.GetOrAdd(policyName, name =>
            {
                var permissions = policyName.Substring(PermissionConstant.PolicyPrefix.Length)
                                  .UnpackFromString(PermissionConstant.PolicyNameSplitSymbol);

                return(new AuthorizationPolicyBuilder()
                       .RequireAuthenticatedUser()
                       .AddRequirements(new PermissionAuthorizationRequirement(permissions))
                       .Build());
            });

            return(policy);
        }
        public Expression CreateMapExpression(Type sourceType, Type destinationType, IDictionary <string, object> parameters = null, params MemberInfo[] membersToExpand)
        {
            parameters = parameters ?? new Dictionary <string, object>();

            var cachedExpression = _expressionCache.GetOrAdd(new ExpressionRequest(sourceType, destinationType, membersToExpand, null));

            Expression x = cachedExpression;

            if (parameters.Any())
            {
                var visitor = new ConstantExpressionReplacementVisitor(parameters);
                x = visitor.Visit(cachedExpression);
            }

            // perform null-propagation if this feature is enabled.
            if (_configurationProvider.EnableNullPropagationForQueryMapping)
            {
                var nullVisitor = new NullsafeQueryRewriter();
                return(nullVisitor.Visit(x));
            }
            return(x);
        }
        /// <summary>
        /// Returns the list of all of the controllers and action methods of an MVC application which have AuthorizeAttribute and the specified policyName.
        /// </summary>
        public ICollection <ControllerMetadata> FindSecuredControllersWithPolicy(string policyName)
        {
            var result = _actionsWithPolicy.GetOrAdd(policyName, y =>
            {
                var controllers = new List <ControllerMetadata>(Controllers);
                foreach (var controller in controllers)
                {
                    controller.Actions = controller.Actions.Where(
                        model => model.Secured &&
                        (
                            model.Attributes.OfType <AuthorizeAttribute>().FirstOrDefault()
                            ?.Policy == policyName ||
                            controller.Attributes.OfType <AuthorizeAttribute>()
                            .FirstOrDefault()?.Policy == policyName
                        )).ToList();
                }

                return(controllers.Where(model => model.Actions.Any()).ToList());
            });

            return(result);
        }
 public int LockingConcurrentDictionary() =>
 lockingConcurrentDictionary.GetOrAdd(typeof(int));
 public Type GetOrAdd(TypeDescription td)
 {
     return(_emittedTypes.GetOrAdd(td));
 }
 public static Func <object> CreateCtor(Type type) => CtorCache.GetOrAdd(type);
Beispiel #9
0
        public T GetOrAdd(TypePair argumentTypes)
        {
            T result = _cache.GetOrAdd(argumentTypes);

            return(result);
        }
 public TypeDescription GetOrAdd(NewTypeRequest request)
 {
     return(_typeDescriptions.GetOrAdd(request));
 }
Beispiel #11
0
 public static object CreateInstance(Type type) => CtorCache.GetOrAdd(type)();
Beispiel #12
0
 public LateBoundCtor CreateCtor(Type type)
 {
     return(_ctorCache.GetOrAdd(type));
 }
Beispiel #13
0
        public T GetOrAdd(Type typeOfThisValue)
        {
            T result = _cache.GetOrAdd(new TypeKey(typeOfThisValue));

            return(result);
        }
Beispiel #14
0
 public static Func <object> CreateCtor(Type type)
 {
     return(_ctorCache.GetOrAdd(type));
 }