Search in sources :

Example 11 with ZLdapFilter

use of com.zimbra.cs.ldap.ZLdapFilter in project zm-mailbox by Zimbra.

the class LdapProvisioning method countObjects.

@Override
public long countObjects(CountObjectsType type, Domain domain, UCService ucService) throws ServiceException {
    if (domain != null && !type.allowsDomain()) {
        throw ServiceException.INVALID_REQUEST("domain cannot be specified for counting type: " + type.toString(), null);
    }
    if (ucService != null && !type.allowsUCService()) {
        throw ServiceException.INVALID_REQUEST("UCService cannot be specified for counting type: " + type.toString(), null);
    }
    ZLdapFilter filter;
    // setup types for finding bases
    Set<ObjectType> types = Sets.newHashSet();
    switch(type) {
        case userAccount:
            types.add(ObjectType.accounts);
            filter = filterFactory.allNonSystemAccounts();
            break;
        case internalUserAccount:
            types.add(ObjectType.accounts);
            filter = filterFactory.allNonSystemInternalAccounts();
            break;
        case internalArchivingAccount:
            types.add(ObjectType.accounts);
            filter = filterFactory.allNonSystemArchivingAccounts();
            break;
        case account:
            types.add(ObjectType.accounts);
            types.add(ObjectType.resources);
            filter = filterFactory.allAccounts();
            break;
        case alias:
            types.add(ObjectType.aliases);
            filter = filterFactory.allAliases();
            break;
        case dl:
            types.add(ObjectType.distributionlists);
            types.add(ObjectType.dynamicgroups);
            filter = mDIT.filterGroupsByDomain(domain);
            if (domain != null && !InMemoryLdapServer.isOn()) {
                ZLdapFilter dnSubtreeMatchFilter = ((LdapDomain) domain).getDnSubtreeMatchFilter();
                filter = filterFactory.andWith(filter, dnSubtreeMatchFilter);
            }
            break;
        case calresource:
            types.add(ObjectType.resources);
            filter = filterFactory.allCalendarResources();
            break;
        case domain:
            types.add(ObjectType.domains);
            filter = filterFactory.allDomains();
            break;
        case cos:
            types.add(ObjectType.coses);
            filter = filterFactory.allCoses();
            break;
        case server:
            types.add(ObjectType.servers);
            filter = filterFactory.allServers();
            break;
        case accountOnUCService:
            if (ucService == null) {
                throw ServiceException.INVALID_REQUEST("UCService is required for counting type: " + type.toString(), null);
            }
            types.add(ObjectType.accounts);
            types.add(ObjectType.resources);
            filter = filterFactory.accountsOnUCService(ucService.getId());
            break;
        case cosOnUCService:
            if (ucService == null) {
                throw ServiceException.INVALID_REQUEST("UCService is required for counting type: " + type.toString(), null);
            }
            types.add(ObjectType.coses);
            filter = filterFactory.cosesOnUCService(ucService.getId());
            break;
        case domainOnUCService:
            if (ucService == null) {
                throw ServiceException.INVALID_REQUEST("UCService is required for counting type: " + type.toString(), null);
            }
            types.add(ObjectType.domains);
            filter = filterFactory.domainsOnUCService(ucService.getId());
            break;
        default:
            throw ServiceException.INVALID_REQUEST("unsupported counting type:" + type.toString(), null);
    }
    String[] bases = getSearchBases(domain, types);
    long num = 0;
    for (String base : bases) {
        num += countObjects(base, filter);
    }
    return num;
}
Also used : ZLdapFilter(com.zimbra.cs.ldap.ZLdapFilter) ObjectType(com.zimbra.cs.account.SearchDirectoryOptions.ObjectType) LdapDomain(com.zimbra.cs.account.ldap.entry.LdapDomain)

Example 12 with ZLdapFilter

use of com.zimbra.cs.ldap.ZLdapFilter in project zm-mailbox by Zimbra.

the class LdapProvisioning method getDNforAccountById.

public String getDNforAccountById(String zimbraId, ZLdapContext zlc, boolean loadFromMaster) {
    if (zimbraId == null) {
        return null;
    }
    ZLdapFilter filter = filterFactory.accountById(zimbraId);
    try {
        String[] retAttrs = new String[] { Provisioning.A_zimbraId };
        /* Just 1 attr to save bandwidth */
        ZSearchResultEntry sr;
        sr = getSearchResultForAccountByQuery(mDIT.mailBranchBaseDN(), filter, zlc, loadFromMaster, retAttrs);
        // search again under the admin base if not found and admin base is not under mail base
        if (sr == null && !mDIT.isUnder(mDIT.mailBranchBaseDN(), mDIT.adminBaseDN())) {
            sr = getSearchResultForAccountByQuery(mDIT.adminBaseDN(), filter, zlc, loadFromMaster, retAttrs);
        }
        if (sr != null) {
            return sr.getDN();
        }
    } catch (ServiceException e) {
        ZimbraLog.search.debug("unable to lookup DN for account via query: %s", filter.toFilterString(), e);
    }
    return null;
}
Also used : ZLdapFilter(com.zimbra.cs.ldap.ZLdapFilter) AccountServiceException(com.zimbra.cs.account.AccountServiceException) AuthFailedServiceException(com.zimbra.cs.account.AccountServiceException.AuthFailedServiceException) ServiceException(com.zimbra.common.service.ServiceException) ZSearchResultEntry(com.zimbra.cs.ldap.ZSearchResultEntry)

Example 13 with ZLdapFilter

use of com.zimbra.cs.ldap.ZLdapFilter in project zm-mailbox by Zimbra.

the class LdapExample method search.

public void search() throws Exception {
    String base = "cn=servers,cn=zimbra";
    String filter = "(objectClass=zimbraServer)";
    String[] returnAttrs = new String[] { "objectClass", "cn" };
    ZLdapFilter zFilter = ZLdapFilterFactory.getInstance().fromFilterString(FilterId.ZMCONFIGD, filter);
    ZSearchControls searchControls = ZSearchControls.createSearchControls(ZSearchScope.SEARCH_SCOPE_SUBTREE, ZSearchControls.SIZE_UNLIMITED, returnAttrs);
    GenericLdapConfig ldapConfig = getLdapConfig();
    ZLdapContext zlc = null;
    try {
        zlc = LdapClient.getContext(ldapConfig, LdapUsage.SEARCH);
        ZSearchResultEnumeration ne = zlc.searchDir(base, zFilter, searchControls);
        while (ne.hasMore()) {
            ZSearchResultEntry entry = ne.next();
            String dn = entry.getDN();
            ZAttributes attrs = entry.getAttributes();
            String cn = attrs.getAttrString("cn");
            String[] objectClasses = attrs.getMultiAttrString("objectClass");
            System.out.println("dn = " + dn);
            System.out.println("cn = " + cn);
            for (String objectClass : objectClasses) {
                System.out.println("objetClass = " + objectClass);
            }
        }
        ne.close();
    } catch (LdapSizeLimitExceededException e) {
        e.printStackTrace();
        throw e;
    } finally {
        // Note: this is important!! 
        LdapClient.closeContext(zlc);
    }
}
Also used : ZLdapFilter(com.zimbra.cs.ldap.ZLdapFilter) ZSearchControls(com.zimbra.cs.ldap.ZSearchControls) GenericLdapConfig(com.zimbra.cs.ldap.LdapServerConfig.GenericLdapConfig) ZLdapContext(com.zimbra.cs.ldap.ZLdapContext) ZSearchResultEnumeration(com.zimbra.cs.ldap.ZSearchResultEnumeration) ZAttributes(com.zimbra.cs.ldap.ZAttributes) LdapSizeLimitExceededException(com.zimbra.cs.ldap.LdapException.LdapSizeLimitExceededException) ZSearchResultEntry(com.zimbra.cs.ldap.ZSearchResultEntry)

Example 14 with ZLdapFilter

use of com.zimbra.cs.ldap.ZLdapFilter in project zm-mailbox by Zimbra.

the class TestLdapHelper method searchForEntryMultipleMatchedEntries.

@Test
public void searchForEntryMultipleMatchedEntries() throws Exception {
    LdapDIT dit = prov.getDIT();
    String base = dit.configBranchBaseDN();
    ZLdapFilter filter = filterFactory.allAccounts();
    boolean caughtException = false;
    try {
        ZSearchResultEntry entry = ldapHelper.searchForEntry(base, filter, null, false);
        assertNotNull(entry);
    } catch (LdapMultipleEntriesMatchedException e) {
        caughtException = true;
    }
    assertTrue(caughtException);
}
Also used : ZLdapFilter(com.zimbra.cs.ldap.ZLdapFilter) LdapDIT(com.zimbra.cs.account.ldap.LdapDIT) LdapMultipleEntriesMatchedException(com.zimbra.cs.ldap.LdapException.LdapMultipleEntriesMatchedException) ZSearchResultEntry(com.zimbra.cs.ldap.ZSearchResultEntry)

Example 15 with ZLdapFilter

use of com.zimbra.cs.ldap.ZLdapFilter in project zm-mailbox by Zimbra.

the class TestLdapHelper method searchDir.

@Test
public void searchDir() throws Exception {
    LdapDIT dit = prov.getDIT();
    String base = dit.configBranchBaseDN();
    ZLdapFilter filter = filterFactory.anyEntry();
    String[] returnAttrs = new String[] { "objectClass" };
    ZSearchControls searchControls = ZSearchControls.createSearchControls(ZSearchScope.SEARCH_SCOPE_ONELEVEL, ZSearchControls.SIZE_UNLIMITED, returnAttrs);
    ZSearchResultEnumeration ne = ldapHelper.searchDir(base, filter, searchControls);
    Set<String> expected = new HashSet<String>();
    expected.add(dit.adminBaseDN());
    expected.add(dit.appAdminBaseDN());
    expected.add(dit.zimletBaseDN());
    expected.add(dit.cosBaseDN());
    expected.add(dit.globalDynamicGroupBaseDN());
    expected.add(dit.serverBaseDN());
    expected.add(dit.xmppcomponentBaseDN());
    expected.add(dit.globalGrantDN());
    expected.add(dit.configDN());
    expected.add(dit.shareLocatorBaseDN());
    expected.add(dit.ucServiceBaseDN());
    int numFound = 0;
    while (ne.hasMore()) {
        ZSearchResultEntry sr = ne.next();
        assertTrue(expected.contains(sr.getDN()));
        numFound++;
    }
    ne.close();
    assertEquals(expected.size(), numFound);
}
Also used : ZLdapFilter(com.zimbra.cs.ldap.ZLdapFilter) ZSearchControls(com.zimbra.cs.ldap.ZSearchControls) LdapDIT(com.zimbra.cs.account.ldap.LdapDIT) ZSearchResultEnumeration(com.zimbra.cs.ldap.ZSearchResultEnumeration) HashSet(java.util.HashSet) ZSearchResultEntry(com.zimbra.cs.ldap.ZSearchResultEntry)

Aggregations

ZLdapFilter (com.zimbra.cs.ldap.ZLdapFilter)121 ZSearchResultEntry (com.zimbra.cs.ldap.ZSearchResultEntry)15 ZSearchResultEnumeration (com.zimbra.cs.ldap.ZSearchResultEnumeration)11 ZLdapContext (com.zimbra.cs.ldap.ZLdapContext)8 ZSearchControls (com.zimbra.cs.ldap.ZSearchControls)8 ArrayList (java.util.ArrayList)8 ServiceException (com.zimbra.common.service.ServiceException)7 Server (com.zimbra.cs.account.Server)7 LdapDIT (com.zimbra.cs.account.ldap.LdapDIT)7 Account (com.zimbra.cs.account.Account)6 AccountServiceException (com.zimbra.cs.account.AccountServiceException)6 AuthFailedServiceException (com.zimbra.cs.account.AccountServiceException.AuthFailedServiceException)6 LdapSizeLimitExceededException (com.zimbra.cs.ldap.LdapException.LdapSizeLimitExceededException)6 NamedEntry (com.zimbra.cs.account.NamedEntry)5 SearchAccountsOptions (com.zimbra.cs.account.SearchAccountsOptions)5 BySearchResultEntrySearcher (com.zimbra.cs.account.ldap.BySearchResultEntrySearcher)5 SearchLdapOptions (com.zimbra.cs.ldap.SearchLdapOptions)5 ProvTest (com.zimbra.qa.unittest.prov.ProvTest)5 LDAPException (com.unboundid.ldap.sdk.LDAPException)3 SearchRequest (com.unboundid.ldap.sdk.SearchRequest)3