/// <summary> /// Pages through LDAP results filtered by Linq spec. /// Caveats: /// (1) paging iterates through all prior pages - a limitation of LDAP, /// (2) sorting can only take one attribute, and that attribute must be /// indexed in LDAP, or the server-side sort will fail. /// </summary> /// <typeparam name="T">The mapped type.</typeparam> /// <param name="spec">The filter specification.</param> /// <param name="offsetPage">How many pages into the results. 0 = first page.</param> /// <param name="pageSize">Size of a page. Default = 10.</param> /// <param name="sortKeys">Sorting options.</param> /// <returns></returns> public virtual IEnumerable <T> PageWithPageControl <T>( Specification <T> spec, int offsetPage = 0, int pageSize = 10, SortKey[] sortKeys = null ) where T : IEntry, new() { ILinqSearchResponse <T> result = null; int curPage = 0; while (curPage++ <= offsetPage) { var search = new LinqSearchRequest <T>(DistinguishedName, spec.AsExpression(), Scope); var pageControl = new PageResultRequestControl(pageSize); var soc = new SearchOptionsControl(SearchOption.DomainScope); search.Controls.Add(pageControl); search.Controls.Add(soc); if (sortKeys != null) { var sortControl = new SortRequestControl(sortKeys); search.Controls.Add(sortControl); } result = Connection.SendRequest(search); } return(result?.Entries); }
public void Filter_Get_ThrowsWhenBaseNotString() { var inst = new LinqSearchRequest <Entry>(); (inst as SearchRequest).Filter = null; Assert.Throws <InvalidCastException>(() => inst.Filter); }
public void CtorLinq_SetsBaseFilterStringFromExpr() { Expression <Func <Entry, bool> > expr = m => m["123"] == "456"; var expectedStr = new LdapFilterCompiler().Compile(expr); var inst = new LinqSearchRequest <Entry>("bogusdn", expr, SearchScope.Base); Assert.Equal(expectedStr, (inst as SearchRequest).Filter); }
public void Filter_Set_SetsBaseFilterStringFromExpression() { Expression <Func <Entry, bool> > expr = m => m["123"] == "456"; var expectedStr = new LdapFilterCompiler().Compile(expr); var inst = new LinqSearchRequest <Entry>(); inst.Filter = expr; Assert.Equal(expectedStr, (inst as SearchRequest).Filter); }
public void Filter_Get_ReturnsParsedBaseFilterWhenString() { var inst = new LinqSearchRequest <Entry>(); (inst as SearchRequest).Filter = "(asdf=jkl)"; Expression <Func <Entry, bool> > expectedExpr = m => m["asdf"] == "jkl"; Assert.Equal(expectedExpr.ToString(), inst.Filter.ToString()); }
protected void AssertSendRequestWraps(Linq2LdapConnection conn) { var search = new LinqSearchRequest <LinqSearchResultEntry>(); var testResponse = (SearchResponse)FormatterServices.GetUninitializedObject(typeof(SearchResponse)); conn.LdapConnectionProxy = Mock.Of <ILdapConnectionProxy>( c => c.SendRequest(search) == testResponse); var result = conn.SendRequest(search); Mock.Get(conn.LdapConnectionProxy) .Verify(c => c.SendRequest(search), Times.Once()); Assert.Equal(testResponse, result.Native); }
public T[] FindAll <T>(Specification <T> spec, int?limit = null) where T : IEntry, new() { var search = new LinqSearchRequest <T>(DistinguishedName, spec.AsExpression(), Scope); if (limit != null) { search.SizeLimit = limit.Value; } var result = Connection.SendRequest(search); return(result.Entries.ToArray()); }
/// <summary> /// Pages through LDAP results filtered by Linq spec. /// Caveats: /// (1) paging iterates through all prior pages - a limitation of LDAP, /// (2) sorting can only take one attribute, and that attribute must be /// indexed in LDAP, or the server-side sort will fail. /// </summary> /// <typeparam name="T">The mapped type.</typeparam> /// <param name="spec">The filter specification.</param> /// <param name="offsetPage">How many pages into the results. 0 = first page.</param> /// <param name="pageSize">Size of a page. Default = 10.</param> /// <param name="sortKeys">Sorting options.</param> /// <returns></returns> public virtual IEnumerable <T> PageWithVLV <T>( Specification <T> spec, int offsetPage = 0, int pageSize = 10, SortKey[] sortKeys = null ) where T : IEntry, new() { var search = new LinqSearchRequest <T>(DistinguishedName, spec.AsExpression(), Scope); var pageControl = new VlvRequestControl(0, pageSize - 1, pageSize * offsetPage + 1); var soc = new SearchOptionsControl(SearchOption.DomainScope); search.Controls.Add(pageControl); search.Controls.Add(soc); if (sortKeys != null) { var sortControl = new SortRequestControl(sortKeys); search.Controls.Add(sortControl); } return(Connection.SendRequest(search).Entries); }
public void Protocols_BasicSearch() { var cred = new NetworkCredential() { UserName = "******", Password = "******" }; var ldapId = new LdapDirectoryIdentifier("127.0.0.1:1389"); var conn = new Linq2LdapConnection(ldapId, cred, AuthType.Basic); var search = new LinqSearchRequest <MyModel>( "dc=example, dc=com", m => m.Mail.StartsWith("user3"), SearchScope.Subtree); // Use a more bare-bones entry type from Core unless they want to derive from the protocols-specific base type. // Docu. var response = conn.SendRequest(search); var user3 = response.Entries[0]; Assert.NotNull(user3); Assert.Equal("[email protected], dc=example, dc=com", user3.DistinguishedName); }