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;
}
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;
}
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);
}
}
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);
}
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);
}
Aggregations