protected static string ExpirationDateConverter(FilterBase filter, string suggestedAdProperty) { Debug.Assert(string.Equals(suggestedAdProperty, "accountExpires", StringComparison.OrdinalIgnoreCase)); Debug.Assert(filter is ExpirationDateFilter); Nullable <DateTime> date = (Nullable <DateTime>)filter.Value; return(!date.HasValue ? "(|(accountExpires=9223372036854775807)(accountExpires=0))" : // Both values are used to represent "no expiration date set" $"(accountExpires={ADUtils.DateTimeToADString(date.Value)})"); }
public static string ExtensionTypeConverter(string attributeName, Type type, object value, MatchType mt) { StringBuilder ldapFilter = new StringBuilder("("); string ldapValue; if (typeof(bool) == type) { ldapValue = ((bool)value ? "TRUE" : "FALSE"); } else if (type is ICollection) { StringBuilder collectionFilter = new StringBuilder(); ICollection collection = (ICollection)value; foreach (object o in collection) { GlobalDebug.WriteLineIf(GlobalDebug.Info, "ADStoreCtx", "ExtensionTypeConverter collection filter type " + o.GetType().ToString()); collectionFilter.Append(ExtensionTypeConverter(attributeName, o.GetType(), o, mt)); } return(collectionFilter.ToString()); } else if (typeof(DateTime) == type) { ldapValue = ADUtils.DateTimeToADString((DateTime)value); } else { ldapValue = ADUtils.PAPIQueryToLdapQueryString(value.ToString()); } switch (mt) { case MatchType.Equals: ldapFilter.Append(attributeName); ldapFilter.Append('='); ldapFilter.Append(ldapValue); break; case MatchType.NotEquals: ldapFilter.Append("!("); ldapFilter.Append(attributeName); ldapFilter.Append('='); ldapFilter.Append(ldapValue); ldapFilter.Append(')'); break; case MatchType.GreaterThanOrEquals: ldapFilter.Append(attributeName); ldapFilter.Append(">="); ldapFilter.Append(ldapValue); break; case MatchType.LessThanOrEquals: ldapFilter.Append(attributeName); ldapFilter.Append("<="); ldapFilter.Append(ldapValue); break; case MatchType.GreaterThan: ldapFilter.Append('&'); // Greater-than-or-equals (or less-than-or-equals)) ldapFilter.Append('('); ldapFilter.Append(attributeName); ldapFilter.Append(mt == MatchType.GreaterThan ? ">=" : "<="); ldapFilter.Append(ldapValue); ldapFilter.Append(')'); // And not-equal ldapFilter.Append("(!("); ldapFilter.Append(attributeName); ldapFilter.Append('='); ldapFilter.Append(ldapValue); ldapFilter.Append("))"); // And exists (need to include because of tristate LDAP logic) ldapFilter.Append('('); ldapFilter.Append(attributeName); ldapFilter.Append("=*)"); break; case MatchType.LessThan: goto case MatchType.GreaterThan; } ldapFilter.Append(')'); return(ldapFilter.ToString()); }
public static string DateTimeFilterBuilder(string attributeName, DateTime searchValue, DateTime defaultValue, bool requirePresence, MatchType mt) { string ldapSearchValue = null; string ldapDefaultValue = null; bool defaultNeeded = false; ldapSearchValue = ADUtils.DateTimeToADString(searchValue); ldapDefaultValue = ADUtils.DateTimeToADString(defaultValue); StringBuilder ldapFilter = new StringBuilder("("); if (mt != MatchType.Equals && mt != MatchType.NotEquals) { defaultNeeded = true; } if (defaultNeeded || (mt == MatchType.NotEquals && requirePresence)) { ldapFilter.Append("&("); } switch (mt) { case MatchType.Equals: ldapFilter.Append(attributeName); ldapFilter.Append('='); ldapFilter.Append(ldapSearchValue); break; case MatchType.NotEquals: ldapFilter.Append("!("); ldapFilter.Append(attributeName); ldapFilter.Append('='); ldapFilter.Append(ldapSearchValue); ldapFilter.Append(')'); break; case MatchType.GreaterThanOrEquals: ldapFilter.Append(attributeName); ldapFilter.Append(">="); ldapFilter.Append(ldapSearchValue); break; case MatchType.LessThanOrEquals: ldapFilter.Append(attributeName); ldapFilter.Append("<="); ldapFilter.Append(ldapSearchValue); break; case MatchType.GreaterThan: ldapFilter.Append('&'); // Greater-than-or-equals (or less-than-or-equals)) ldapFilter.Append('('); ldapFilter.Append(attributeName); ldapFilter.Append(mt == MatchType.GreaterThan ? ">=" : "<="); ldapFilter.Append(ldapSearchValue); ldapFilter.Append(')'); // And not-equal ldapFilter.Append("(!("); ldapFilter.Append(attributeName); ldapFilter.Append('='); ldapFilter.Append(ldapSearchValue); ldapFilter.Append("))"); // And exists (need to include because of tristate LDAP logic) ldapFilter.Append('('); ldapFilter.Append(attributeName); ldapFilter.Append("=*)"); break; case MatchType.LessThan: goto case MatchType.GreaterThan; } ldapFilter.Append(')'); bool closeFilter = false; if (defaultNeeded) { ldapFilter.Append("(!"); ldapFilter.Append(attributeName); ldapFilter.Append('='); ldapFilter.Append(ldapDefaultValue); ldapFilter.Append(')'); closeFilter = true; } if (mt == MatchType.NotEquals && requirePresence) { ldapFilter.Append('('); ldapFilter.Append(attributeName); ldapFilter.Append("=*)"); closeFilter = true; } if (closeFilter) { ldapFilter.Append(')'); } return(ldapFilter.ToString()); }