コード例 #1
0
 public LinqDirectorySearcherTests()
 {
     Creator               = Mock.Of <IModelCreator>();
     Searcher              = new LinqDirectorySearcher <TestLdapModel>();
     Searcher.Base         = Mock.Of <IDirectorySearcherProxy>();
     Searcher.ModelCreator = Creator;
 }
コード例 #2
0
        public void DirectorySearcher_UsingMatchesStar_NoThrow()
        {
            var entry = new DirectoryEntry(
                "LDAP://127.0.0.1:1389", "cn=neoman,dc=example,dc=com",
                "testtest", AuthenticationTypes.None);
            var ctx = new LinqDirectorySearcher <MyModel>(entry);

            ctx.Filter      = e => e.Attributes["objectCategory"].Matches("*");
            ctx.SearchScope = SearchScope.Subtree;
            var results = ctx.FindAll();
        }
コード例 #3
0
        public void DirectorySearcher_BasicSearch()
        {
            var entry = new DirectoryEntry(
                "LDAP://127.0.0.1:1389", "cn=neoman,dc=example,dc=com",
                "testtest", AuthenticationTypes.None);
            var ctx = new LinqDirectorySearcher <MyModel>(entry);

            ctx.Filter      = u => u.Mail.StartsWith("user");
            ctx.SearchScope = SearchScope.Subtree;
            var results = ctx.FindAll();

            Assert.Equal(11, results.Count());
            Assert.Contains(results, r => r.Mail == "*****@*****.**");
        }
コード例 #4
0
        public T FindOne <T>(Specification <T> spec)
            where T : IEntry, new()
        {
            var searcher = new LinqDirectorySearcher <T>(RepoEntry);

            searcher.SearchScope = SearchScope.Subtree;
            searcher.Filter      = spec;
            var result = searcher.FindOne();

            /*var pnames = new string[results[0].Properties.PropertyNames.Count];
             * results[0].Properties.PropertyNames.CopyTo(pnames, 0);
             * var str = pnames.Join(",");*/
            return(result);
        }
コード例 #5
0
        public void DirectorySearcher_BasicSearchWithArrayAttributes()
        {
            var entry = new DirectoryEntry(
                "LDAP://127.0.0.1:1389", "cn=neoman,dc=example,dc=com",
                "testtest", AuthenticationTypes.None);
            var ctx = new LinqDirectorySearcher <MyModel>(entry);

            ctx.Filter      = u => u.AltMails.StartsWith("user6"); // (alt-mails=user6*)
            ctx.SearchScope = SearchScope.Subtree;
            var results = ctx.FindAll();

            Assert.Single(results);
            //throw new Exception($"{string.Join(", ", results.First().AltMails)}");
            Assert.Contains(results, r => r.AltMails == "*****@*****.**");
        }
コード例 #6
0
        public void ModelInstantiationPropertyError_InformsOfProperty()
        {
            var entry = new DirectoryEntry(
                "LDAP://127.0.0.1:1389", "cn=neoman,dc=example,dc=com",
                "testtest", AuthenticationTypes.None);
            var ctx = new LinqDirectorySearcher <BadUserModel>(entry);

            ctx.Filter      = u => u["mail"].StartsWith("user");
            ctx.SearchScope = SearchScope.Subtree;
            var ex = Assert.Throws <ArgumentException>(() => ctx.FindAll().First());

            Assert.Contains("'bogus'", ex.Message);
            Assert.Contains("'NonExistentButNonOptional'", ex.Message);
            Assert.Contains(typeof(BadUserModel).FullName, ex.Message);
            Assert.NotNull(ex.InnerException);
        }
コード例 #7
0
        public T[] FindAll <T>(Specification <T> spec)
            where T : IEntry, new()
        {
            var searcher = new LinqDirectorySearcher <T>(RepoEntry);

            searcher.SearchScope = SearchScope.Subtree;
            searcher.Filter      = spec;
            searcher.PropertiesToLoad.Clear();
            searcher.PropertiesToLoad.Add("mail");
            var results = searcher.FindAll();

            /*var pnames = new string[results[0].Properties.PropertyNames.Count];
             * results[0].Properties.PropertyNames.CopyTo(pnames, 0);
             * var str = pnames.Join(",");*/
            return(results.ToArray());
        }
コード例 #8
0
        /// <summary>
        /// Pages through LDAP results filtered by Linq spec.
        /// Caveat: 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="sortOpt">Sorting options.</param>
        /// <returns></returns>
        public IEnumerable <T> Page <T>(
            Specification <T> spec,
            int offsetPage     = 0, int pageSize = 10,
            SortOption sortOpt = null)
            where T : IEntry, new()
        {
            var searcher = new LinqDirectorySearcher <T>(RepoEntry);

            searcher.SearchScope     = SearchScope.Subtree;
            searcher.Filter          = spec;
            searcher.VirtualListView = new DirectoryVirtualListView(
                0, pageSize - 1, pageSize * offsetPage + 1);

            // Not obvious, but VLV must have a sort option.
            searcher.Sort = sortOpt ?? new SortOption("cn", SortDirection.Ascending);
            return(searcher.FindAll());
        }