Search in sources :

Example 26 with SearchResult

use of com.unboundid.ldap.sdk.SearchResult in project gitblit by gitblit.

the class LdapConnectionTest method testSearch.

@Test
public void testSearch() throws LDAPException {
    LdapConnection conn = new LdapConnection(settings);
    try {
        assertTrue(conn.connect());
        BindResult br = conn.bind();
        assertNotNull(br);
        SearchResult result;
        SearchResultEntry entry;
        result = conn.search(ACCOUNT_BASE, false, "(CN=UserOne)", null);
        assertNotNull(result);
        assertEquals(1, result.getEntryCount());
        entry = result.getSearchEntries().get(0);
        assertEquals("CN=UserOne,OU=US," + ACCOUNT_BASE, entry.getDN());
        result = conn.search(ACCOUNT_BASE, true, "(&(CN=UserOne)(surname=One))", null);
        assertNotNull(result);
        assertEquals(1, result.getEntryCount());
        entry = result.getSearchEntries().get(0);
        assertEquals("CN=UserOne,OU=US," + ACCOUNT_BASE, entry.getDN());
        result = conn.search(ACCOUNT_BASE, true, "(&(CN=UserOne)(surname=Two))", null);
        assertNotNull(result);
        assertEquals(0, result.getEntryCount());
        result = conn.search(ACCOUNT_BASE, true, "(surname=Two)", Arrays.asList("givenName", "surname"));
        assertNotNull(result);
        assertEquals(1, result.getEntryCount());
        entry = result.getSearchEntries().get(0);
        assertEquals("CN=UserTwo,OU=US," + ACCOUNT_BASE, entry.getDN());
        assertEquals(2, entry.getAttributes().size());
        assertEquals("User", entry.getAttributeValue("givenName"));
        assertEquals("Two", entry.getAttributeValue("surname"));
        result = conn.search(ACCOUNT_BASE, true, "(personalTitle=Mr*)", null);
        assertNotNull(result);
        assertEquals(3, result.getEntryCount());
        ArrayList<String> names = new ArrayList<>(3);
        names.add(result.getSearchEntries().get(0).getAttributeValue("surname"));
        names.add(result.getSearchEntries().get(1).getAttributeValue("surname"));
        names.add(result.getSearchEntries().get(2).getAttributeValue("surname"));
        assertTrue(names.contains("One"));
        assertTrue(names.contains("Two"));
        assertTrue(names.contains("Three"));
    } finally {
        conn.close();
    }
}
Also used : ArrayList(java.util.ArrayList) BindResult(com.unboundid.ldap.sdk.BindResult) SearchResult(com.unboundid.ldap.sdk.SearchResult) LdapConnection(com.gitblit.ldap.LdapConnection) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry) Test(org.junit.Test)

Example 27 with SearchResult

use of com.unboundid.ldap.sdk.SearchResult in project gitblit by gitblit.

the class LdapConnectionTest method testSearchRequest.

@Test
public void testSearchRequest() throws LDAPException {
    LdapConnection conn = new LdapConnection(settings);
    try {
        assertTrue(conn.connect());
        BindResult br = conn.bind();
        assertNotNull(br);
        SearchRequest req;
        SearchResult result;
        SearchResultEntry entry;
        req = new SearchRequest(ACCOUNT_BASE, SearchScope.BASE, "(CN=UserOne)");
        result = conn.search(req);
        assertNotNull(result);
        assertEquals(0, result.getEntryCount());
        req = new SearchRequest(ACCOUNT_BASE, SearchScope.ONE, "(CN=UserTwo)");
        result = conn.search(req);
        assertNotNull(result);
        assertEquals(0, result.getEntryCount());
        req = new SearchRequest(ACCOUNT_BASE, SearchScope.SUB, "(CN=UserThree)");
        result = conn.search(req);
        assertNotNull(result);
        assertEquals(1, result.getEntryCount());
        entry = result.getSearchEntries().get(0);
        assertEquals("CN=UserThree,OU=Canada," + ACCOUNT_BASE, entry.getDN());
        req = new SearchRequest(ACCOUNT_BASE, SearchScope.SUBORDINATE_SUBTREE, "(CN=UserFour)");
        result = conn.search(req);
        assertNotNull(result);
        assertEquals(1, result.getEntryCount());
        entry = result.getSearchEntries().get(0);
        assertEquals("CN=UserFour,OU=Canada," + ACCOUNT_BASE, entry.getDN());
    } finally {
        conn.close();
    }
}
Also used : SearchRequest(com.unboundid.ldap.sdk.SearchRequest) BindResult(com.unboundid.ldap.sdk.BindResult) SearchResult(com.unboundid.ldap.sdk.SearchResult) LdapConnection(com.gitblit.ldap.LdapConnection) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry) Test(org.junit.Test)

Example 28 with SearchResult

use of com.unboundid.ldap.sdk.SearchResult in project gitblit by gitblit.

the class LdapConnectionTest method testSearchUser.

@Test
public void testSearchUser() throws LDAPException {
    LdapConnection conn = new LdapConnection(settings);
    try {
        assertTrue(conn.connect());
        BindResult br = conn.bind();
        assertNotNull(br);
        SearchResult result;
        SearchResultEntry entry;
        result = conn.searchUser("UserOne");
        assertNotNull(result);
        assertEquals(1, result.getEntryCount());
        entry = result.getSearchEntries().get(0);
        assertEquals("CN=UserOne,OU=US," + ACCOUNT_BASE, entry.getDN());
        result = conn.searchUser("UserFour", Arrays.asList("givenName", "surname"));
        assertNotNull(result);
        assertEquals(1, result.getEntryCount());
        entry = result.getSearchEntries().get(0);
        assertEquals("CN=UserFour,OU=Canada," + ACCOUNT_BASE, entry.getDN());
        assertEquals(2, entry.getAttributes().size());
        assertEquals("User", entry.getAttributeValue("givenName"));
        assertEquals("Four", entry.getAttributeValue("surname"));
    } finally {
        conn.close();
    }
}
Also used : BindResult(com.unboundid.ldap.sdk.BindResult) SearchResult(com.unboundid.ldap.sdk.SearchResult) LdapConnection(com.gitblit.ldap.LdapConnection) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry) Test(org.junit.Test)

Example 29 with SearchResult

use of com.unboundid.ldap.sdk.SearchResult in project keywhiz by square.

the class LdapAuthenticator method rolesFromDN.

private Set<String> rolesFromDN(String userDN) throws LDAPException, GeneralSecurityException {
    SearchRequest searchRequest = new SearchRequest(config.getRoleBaseDN(), SearchScope.SUB, Filter.createEqualityFilter("uniqueMember", userDN));
    Set<String> roles = Sets.newLinkedHashSet();
    LDAPConnection connection = connectionFactory.getLDAPConnection();
    try {
        SearchResult sr = connection.search(searchRequest);
        for (SearchResultEntry sre : sr.getSearchEntries()) {
            X500Name x500Name = new X500Name(sre.getDN());
            RDN[] rdns = x500Name.getRDNs(BCStyle.CN);
            if (rdns.length == 0) {
                logger.error("Could not create X500 Name for role:" + sre.getDN());
            } else {
                String commonName = IETFUtils.valueToString(rdns[0].getFirst().getValue());
                roles.add(commonName);
            }
        }
    } finally {
        connection.close();
    }
    return roles;
}
Also used : SearchRequest(com.unboundid.ldap.sdk.SearchRequest) SearchResult(com.unboundid.ldap.sdk.SearchResult) LDAPConnection(com.unboundid.ldap.sdk.LDAPConnection) X500Name(org.bouncycastle.asn1.x500.X500Name) RDN(org.bouncycastle.asn1.x500.RDN) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry)

Example 30 with SearchResult

use of com.unboundid.ldap.sdk.SearchResult in project zm-mailbox by Zimbra.

the class UBIDLdapContext method deleteChildren.

@Override
public void deleteChildren(String dn) throws ServiceException {
    try {
        // use ZLdapFilter instead of just the native Filter so it's
        // convenient for stating
        ZLdapFilter filter = ZLdapFilterFactory.getInstance().anyEntry();
        // Filter filter = Filter.createPresenceFilter(LdapConstants.ATTR_OBJECTCLASS);
        SearchRequest searchRequest = new SearchRequest(dn, SearchScope.ONE, derefAliasPolicy, // size limit
        0, // time limit
        0, // getTypesOnly
        false, ((UBIDLdapFilter) filter).getNative());
        searchRequest.setAttributes("dn");
        SearchResult result = UBIDLdapOperation.SEARCH.execute(this, searchRequest, filter);
        List<SearchResultEntry> entries = result.getSearchEntries();
        for (SearchResultEntry entry : entries) {
            deleteEntry(entry.getDN());
        }
    } catch (LDAPException e) {
        throw mapToLdapException("unable to delete children", e);
    }
}
Also used : ZLdapFilter(com.zimbra.cs.ldap.ZLdapFilter) SearchRequest(com.unboundid.ldap.sdk.SearchRequest) LDAPException(com.unboundid.ldap.sdk.LDAPException) SearchResult(com.unboundid.ldap.sdk.SearchResult) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry)

Aggregations

SearchResult (com.unboundid.ldap.sdk.SearchResult)39 SearchResultEntry (com.unboundid.ldap.sdk.SearchResultEntry)24 LDAPException (com.unboundid.ldap.sdk.LDAPException)16 SearchRequest (com.unboundid.ldap.sdk.SearchRequest)14 SearchException (org.gluu.persist.exception.operation.SearchException)10 ArrayList (java.util.ArrayList)9 MappingException (org.gluu.persist.exception.mapping.MappingException)9 ConnectionException (org.gluu.persist.exception.operation.ConnectionException)8 Test (org.junit.Test)7 LdapConnection (com.gitblit.ldap.LdapConnection)6 EntryPersistenceException (org.gluu.persist.exception.mapping.EntryPersistenceException)6 BindResult (com.unboundid.ldap.sdk.BindResult)5 LDAPSearchException (com.unboundid.ldap.sdk.LDAPSearchException)5 TeamModel (com.gitblit.models.TeamModel)4 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)4 LDAPConnection (com.unboundid.ldap.sdk.LDAPConnection)4 SimplePagedResultsControl (com.unboundid.ldap.sdk.controls.SimplePagedResultsControl)4 ParseException (java.text.ParseException)4 AuthenticationException (org.gluu.persist.exception.operation.AuthenticationException)4 SearchScopeException (org.gluu.persist.exception.operation.SearchScopeException)4