public Task<IList<Person>> SearchAsync (Filter filter, int sizeLimit, CancellationToken cancellationToken) { ValidateConfiguration (); if (conn == null) throw new InvalidOperationException ("Must Login before searching."); // // Compile the filter // var compiledFilter = CompileFilter (filter); if (string.IsNullOrEmpty (compiledFilter)) compiledFilter = "(objectClass=*)"; // // Since the LDAP library doesn't support async, wrap it // in a new task. // return Task.Factory.StartNew (() => { // // Search // return Search (compiledFilter, sizeLimit); }, cancellationToken); }
IEnumerable<Person> Search (Filter filter) { if (filter is OrFilter) { var f = (OrFilter)filter; var r = Enumerable.Empty<Person> (); foreach (var sf in f.Filters) { r = r.Concat (Search (sf)); } return r.Distinct (); } else if (filter is AndFilter) { throw new NotImplementedException (); } else if (filter is NotFilter) { throw new NotImplementedException (); } else if (filter is EqualsFilter) { var f = (EqualsFilter)filter; var upper = f.Value.ToUpperInvariant (); var prop = properties [f.PropertyName]; var q = from p in people let v = prop.GetValue (p, null) where v != null && v.ToString ().ToUpperInvariant () == upper select p; return q; } else if (filter is ContainsFilter) { var f = (ContainsFilter)filter; var re = new Regex (Regex.Escape (f.Value).Replace ("\\ ", "|"), RegexOptions.IgnoreCase); var prop = properties [f.PropertyName]; var q = from p in people let v = prop.GetValue (p, null) where v != null && re.IsMatch (v.ToString ()) select p; return q; } else { throw new NotSupportedException ("Unsupported filter type: " + filter.GetType ()); } }
/// <summary> /// Combines this filter with another using an OR relation. /// </summary> /// <remarks> /// This is a convenience function for creating an <see cref="AndFilter"/>. /// </remarks> /// <param name='filter'> /// The other filter in the OR relationship. /// </param> public OrFilter Or (Filter filter) { if (filter == null) throw new ArgumentNullException ("filter"); return new OrFilter (this, filter); }
/// <summary> /// Combines this filter with another using an AND relation. /// </summary> /// <remarks> /// This is a convenience function for creating an <see cref="AndFilter"/>. /// </remarks> /// <param name='filter'> /// The other filter in the AND relationship. /// </param> public AndFilter And (Filter filter) { if (filter == null) throw new ArgumentNullException ("filter"); return new AndFilter (this, filter); }
public Task<IList<Person>> SearchAsync(Filter filter, int sizeLimit, CancellationToken cancellationToken) { return Task.Factory.StartNew (() => { var s = Search (filter); var list = s.ToList (); return (IList<Person>)list; }, cancellationToken); }
/// <summary> /// Compiles the filter. /// </summary> /// <remarks> /// <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa746475(v=vs.85).aspx">Search /// Filter Syntax</a> /// </remarks> /// <returns> /// The filter. /// </returns> /// <param name='filter'> /// Filter. /// </param> string CompileFilter (Filter filter) { if (filter == null) return string.Empty; if (filter is AndFilter) { return "(&" + string.Join (string.Empty, ((AndFilter)filter).Filters.Select (CompileFilter)) + ")"; } else if (filter is OrFilter) { return "(|" + string.Join (string.Empty, ((OrFilter)filter).Filters.Select (CompileFilter)) + ")"; } else if (filter is NotFilter) { return "(!" + CompileFilter (((NotFilter)filter).InnerFilter) + ")"; } else if (filter is EqualsFilter) { var f = (EqualsFilter)filter; var attrs = GetAttributeTypes (f.PropertyName); var q = attrs.Select (a => "(" + a + "=" + f.Value + ")"); if (attrs.Length == 1) return q.First (); else return "(|" + string.Join ("", q) + ")"; } else if (filter is ContainsFilter) { var f = (ContainsFilter)filter; var attrs = GetAttributeTypes (f.PropertyName); var q = attrs.Select (a => "(" + a + "=*" + f.Value + "*)"); if (attrs.Length == 1) return q.First (); else return "(|" + string.Join ("", q) + ")"; } else { throw new NotSupportedException (filter.GetType ().Name); } }
public NotFilter(Filter innerFilter) { if (innerFilter == null) throw new ArgumentNullException ("innerFilter"); InnerFilter = innerFilter; }