Exemple #1
0
        private static LdapQueryCondition CreateConditionValue <T>(LdapQueryCondition condition, LdapQueryConditionValueComparisonTypes type, string name, T value)
        {
            var cv = new LdapQueryConditionValue {
                Name = name, Value = value, Comparison = type
            };

            condition.Values.Add(cv);

            return(condition);
        }
Exemple #2
0
        private static LdapQueryCondition AddCondition(LdapQueryCondition parentCondition, LdapQueryConditionTypes type)
        {
            var condition = new LdapQueryCondition()
            {
                ConditionType = type, Parent = parentCondition
            };

            parentCondition.Conditions.Add(condition);

            return(condition);
        }
        public void CreateComplex2_Pass()
        {
            var ldapQuery = new LdapQueryCondition()
                            .And()
                            .EqualTo("objectClass", "user")
                            .And()
                            .EqualTo("sAMAccountName", "sanderson")
                            .EqualTo("givenName", "Shawn")
                            .EqualTo("sn", "Anderson")
                            .EqualTo("Mail", "*****@*****.**").Build();

            var result = ldapQuery.ToString();

            result.Should().Be("(&(objectClass=user)(&(sAMAccountName=sanderson)(givenName=Shawn)(sn=Anderson)([email protected])))");
        }
        public void CreateComplex1_Pass()
        {
            // Arrange
            var ldapQueryBuilder = new LdapQueryCondition()
                                   .And()
                                   .EqualTo("objectClass", "user")
                                   .Or()
                                   .StartsWith("cn", "shawn")
                                   .StartsWith("cn", "joe").Build();

            // Act
            var result = ldapQueryBuilder.ToString();

            // Assert
            result.Should().Be("(&(objectClass=user)(|(cn=shawn*)(cn=joe*)))");
        }
        public static string ToLdapQuery(this ILdapAccountEntry accountEntry)
        {
            var ldapQuery = new LdapQueryCondition()
            {
                ConditionType = LdapQueryConditionTypes.And
            };

            // Get the type of the result object
            Type ldapAccountEntryType = accountEntry.GetType();

            // Get a list of all of the properties we need to bind to
            var propertiesWithMyAttribute = ldapAccountEntryType.GetPropertiesWithAttribute <LdapPropertyAttribute>(true);

            foreach (var tp in propertiesWithMyAttribute)
            {
                var attr = tp.GetAttributeOfType <LdapPropertyAttribute>().First();

                if (attr.Name == "*")
                {
                    continue;                                   // skip the wildcard as we cannot use it in a query
                }
                var value = tp.GetValue(accountEntry);
                if (value != null)
                {
                    Type vt = value.GetType();

                    if (value is string && !string.IsNullOrEmpty(value.ToString()))
                    {
                        ldapQuery.EqualTo(attr.Name, value);
                    }
                    else if ((vt is IEnumerable) || (vt.IsGenericType && vt.GetGenericTypeDefinition().GetInterfaces().Any(x => x.IsAssignableFrom(typeof(IEnumerable)))))
                    {
                        var lq = ldapQuery.Or();

                        foreach (var v in value as IEnumerable)
                        {
                            lq.EqualTo(attr.Name, v);
                        }
                    }
                }
            }

            return(ldapQuery.Build().ToString());
        }
Exemple #6
0
 public static LdapQueryCondition EndsWith <T>(this LdapQueryCondition condition, string name, T value)
 {
     return(CreateConditionValue(condition, LdapQueryConditionValueComparisonTypes.EndsWith, name, value));
 }
Exemple #7
0
 public static LdapQueryCondition LessThanOrEqualTo <T>(this LdapQueryCondition condition, string name, T value)
 {
     return(CreateConditionValue(condition, LdapQueryConditionValueComparisonTypes.GreaterThanOrEqualTo, name, value));
 }
Exemple #8
0
 public static LdapQueryCondition And(this LdapQueryCondition condition)
 {
     return(AddCondition(condition, LdapQueryConditionTypes.And));
 }
Exemple #9
0
 public static LdapQueryCondition Or(this LdapQueryCondition condition)
 {
     return(AddCondition(condition, LdapQueryConditionTypes.Or));
 }