private static LDAPAttributeFilter Build(SearchRequestFilter filter)
        {
            var result = new LDAPAttributeFilter
            {
                Type = (SearchFilterTypes)filter.Type
            };

            switch (filter.Type)
            {
            case SearchRequestFilterTypes.Present:
                result.AttributeName = filter.Value;
                break;

            case SearchRequestFilterTypes.EqualityMatch:
            case SearchRequestFilterTypes.GreaterOrEqual:
            case SearchRequestFilterTypes.LessOrEqual:
            case SearchRequestFilterTypes.ApproxMatch:
                result.AttributeName  = filter.Attribute.AttributeDescription.Value;
                result.AttributeValue = filter.Attribute.AssertionValue.Value;
                break;

            case SearchRequestFilterTypes.Or:
            case SearchRequestFilterTypes.And:
                foreach (var f in filter.Filters)
                {
                    var child = Build(f);
                    result.Filters.Add(child);
                }

                break;
            }
            return(result);
        }
예제 #2
0
        private Expression BuildExpression(LDAPAttributeFilter ldapAttributeFilter, ParameterExpression parentExpression = null)
        {
            ParameterExpression rootExpression = Expression.Parameter(typeof(LDAPEntry), "r");

            if (parentExpression != null)
            {
                rootExpression = parentExpression;
            }

            Expression comparison = null;

            if (ldapAttributeFilter.Type == SearchFilterTypes.Or || ldapAttributeFilter.Type == SearchFilterTypes.And)
            {
                int        i        = 0;
                Expression leftExpr = null;
                foreach (var child in ldapAttributeFilter.Filters)
                {
                    i++;
                    var childExpression = BuildExpression(child, rootExpression);
                    if (i == 1)
                    {
                        leftExpr = childExpression;
                    }
                    else
                    {
                        switch (ldapAttributeFilter.Type)
                        {
                        case SearchFilterTypes.And:
                            leftExpr = Expression.And(leftExpr, childExpression);
                            break;

                        case SearchFilterTypes.Or:
                            leftExpr = Expression.Or(leftExpr, childExpression);
                            break;
                        }
                    }
                }

                if (parentExpression != null)
                {
                    return(leftExpr);
                }

                Expression <Func <LDAPEntry, bool> > result = Expression.Lambda <Func <LDAPEntry, bool> >(leftExpr, rootExpression);
                return(result);
            }

            var lst              = new List <string>();
            var methodInfo       = typeof(List <string>).GetMethod("Contains", new Type[] { typeof(string) });
            var memberExpression = Expression.PropertyOrField(rootExpression, "Attributes");
            var attribute        = Expression.Parameter(typeof(LDAPEntryAttribute), "a");
            var attributeName    = Expression.PropertyOrField(attribute, "Name");
            var attributesValue  = Expression.PropertyOrField(attribute, "Values");
            var nameConstant     = Expression.Constant(ldapAttributeFilter.AttributeName, typeof(string));
            var valueConstant    = Expression.Constant(ldapAttributeFilter.AttributeValue, typeof(string));
            var equalityName     = Expression.Call(typeof(string), "Equals", null, attributeName, nameConstant, Expression.Constant(StringComparison.CurrentCultureIgnoreCase));
            var equalityValue    = Expression.Call(attributesValue, methodInfo, valueConstant);

            switch (ldapAttributeFilter.Type)
            {
            case SearchFilterTypes.Present:
                comparison = equalityName;
                break;

            case SearchFilterTypes.EqualityMatch:
                comparison = Expression.AndAlso(equalityName, equalityValue);
                break;

            case SearchFilterTypes.GreaterOrEqual:
                comparison = Expression.GreaterThanOrEqual(equalityName, equalityValue);
                break;

            case SearchFilterTypes.LessOrEqual:
                comparison = Expression.LessThanOrEqual(equalityName, equalityValue);
                break;
            }

            var predicate = Expression.Lambda <Func <LDAPEntryAttribute, bool> >(comparison, attribute);
            var anyCall   = Expression.Call(typeof(Enumerable), "Any", new[] { typeof(LDAPEntryAttribute) }, memberExpression, predicate);

            if (parentExpression != null)
            {
                return(anyCall);
            }

            return(Expression.Lambda <Func <LDAPEntry, bool> >(anyCall, rootExpression));
        }