use of com.zimbra.cs.ldap.ZAttributes in project zm-mailbox by Zimbra.
the class LdapExample method getAttributes.
public void getAttributes() throws Exception {
GenericLdapConfig ldapConfig = getLdapConfig();
ZLdapContext zlc = null;
try {
zlc = LdapClient.getContext(ldapConfig, LdapUsage.SEARCH);
/*
* get attributes zimbraId, cn and description on DN "cn=default,cn=cos,cn=zimbra"
*/
ZAttributes attrs = zlc.getAttributes("cn=default,cn=cos,cn=zimbra", new String[] { "zimbraId", "cn", "description" });
String zimbraId = attrs.getAttrString("zimbraId");
/*
* get all attributes on DN "cn=default,cn=cos,cn=zimbra"
*/
ZAttributes allAttrs = zlc.getAttributes("cn=default,cn=cos,cn=zimbra", null);
} finally {
// Note: this is important!!
LdapClient.closeContext(zlc);
}
}
use of com.zimbra.cs.ldap.ZAttributes in project zm-mailbox by Zimbra.
the class LdapGalSearch method getGalEntryByDn.
public static void getGalEntryByDn(ZLdapContext zlc, GalSearchConfig.GalType galType, String dn, LdapGalMapRules rules, SearchGalResult result) throws ServiceException {
String[] reqAttrs = rules.getLdapAttrs();
if (ZimbraLog.gal.isDebugEnabled()) {
StringBuffer returnAttrs = new StringBuffer();
for (String a : reqAttrs) {
returnAttrs.append(a + ",");
}
zlc.debug();
ZimbraLog.gal.debug("getGalEntryByDn: " + ", dn=" + dn + ", attrs=" + returnAttrs);
}
SearhcGalVisitor visitor = new SearhcGalVisitor(zlc, galType, null, rules, result);
try {
ZAttributes attrs = zlc.getAttributes(dn, reqAttrs);
visitor.visit(dn, attrs);
} catch (LdapEntryNotFoundException e) {
ZimbraLog.gal.debug("getGalEntryByDn: no such dn: " + dn, e);
} catch (ServiceException e) {
throw ServiceException.FAILURE("unable to search gal", e);
}
}
use of com.zimbra.cs.ldap.ZAttributes in project zm-mailbox by Zimbra.
the class TestLdapHelper method getAttributes.
@Test
public void getAttributes() throws Exception {
String dn = prov.getDIT().configDN();
ZAttributes attrs = ldapHelper.getAttributes(LdapUsage.UNITTEST, dn);
assertEquals("config", attrs.getAttrString(Provisioning.A_cn));
}
use of com.zimbra.cs.ldap.ZAttributes in project zm-mailbox by Zimbra.
the class LdapProvisioning method removeAliasInternal.
/*
* 1. remove alias from mail and zimbraMailAlias attributes of the entry
* 2. remove alias from all distribution lists
* 3. delete the alias entry
*
* A. entry exists, alias exists
* - if alias points to the entry: do 1, 2, 3
* - if alias points to other existing entry: do 1, and then throw NO_SUCH_ALIAS
* - if alias points to a non-existing entry: do 1, 2, 3, and then throw NO_SUCH_ALIAS
*
* B. entry exists, alias does not exist: do 1, 2, and then throw NO_SUCH_ALIAS
*
* C. entry does not exist, alias exists:
* - if alias points to other existing entry: do nothing (and then throw NO_SUCH_ACCOUNT/NO_SUCH_DISTRIBUTION_LIST in ProvUtil)
* - if alias points to a non-existing entry: do 2, 3 (and then throw NO_SUCH_ACCOUNT/NO_SUCH_DISTRIBUTION_LIST in ProvUtil)
*
* D. entry does not exist, alias does not exist: do 2 (and then throw NO_SUCH_ACCOUNT/NO_SUCH_DISTRIBUTION_LIST in ProvUtil)
*
*
*/
private void removeAliasInternal(NamedEntry entry, String alias) throws ServiceException {
LdapUsage ldapUsage = null;
if (entry instanceof Account) {
ldapUsage = LdapUsage.REMOVE_ALIAS_ACCOUNT;
} else if (entry instanceof Group) {
ldapUsage = LdapUsage.REMOVE_ALIAS_DL;
} else {
ldapUsage = LdapUsage.REMOVE_ALIAS;
}
ZLdapContext zlc = null;
try {
zlc = LdapClient.getContext(LdapServerType.MASTER, ldapUsage);
alias = alias.toLowerCase();
alias = IDNUtil.toAsciiEmail(alias);
String[] parts = alias.split("@");
String aliasName = parts[0];
String aliasDomain = parts[1];
Domain domain = getDomainByAsciiName(aliasDomain, zlc);
if (domain == null)
throw AccountServiceException.NO_SUCH_DOMAIN(aliasDomain);
String targetDn = (entry == null) ? null : ((LdapEntry) entry).getDN();
String targetDomainName = null;
if (entry != null) {
if (entry instanceof Account) {
targetDomainName = ((Account) entry).getDomainName();
} else if (entry instanceof Group) {
targetDomainName = ((Group) entry).getDomainName();
} else {
throw ServiceException.INVALID_REQUEST("invalid entry type for alias", null);
}
}
String aliasDn = mDIT.aliasDN(targetDn, targetDomainName, aliasName, aliasDomain);
ZAttributes aliasAttrs = null;
Alias aliasEntry = null;
try {
aliasAttrs = helper.getAttributes(zlc, aliasDn);
// see if the entry is an alias
if (!isEntryAlias(aliasAttrs))
throw AccountServiceException.NO_SUCH_ALIAS(alias);
aliasEntry = makeAlias(aliasDn, aliasAttrs);
} catch (ServiceException e) {
ZimbraLog.account.warn("alias " + alias + " does not exist");
}
NamedEntry targetEntry = null;
if (aliasEntry != null)
targetEntry = searchAliasTarget(aliasEntry, false);
boolean aliasPointsToEntry = ((entry != null) && (aliasEntry != null) && entry.getId().equals(aliasEntry.getAttr(Provisioning.A_zimbraAliasTargetId)));
boolean aliasPointsToOtherExistingEntry = ((aliasEntry != null) && (targetEntry != null) && ((entry == null) || (!entry.getId().equals(targetEntry.getId()))));
boolean aliasPointsToNonExistingEntry = ((aliasEntry != null) && (targetEntry == null));
// 1. remove alias from mail/zimbraMailAlias attrs
if (entry != null) {
try {
HashMap<String, String> attrs = new HashMap<String, String>();
attrs.put("-" + Provisioning.A_mail, alias);
attrs.put("-" + Provisioning.A_zimbraMailAlias, alias);
modifyAttrsInternal(entry, zlc, attrs);
} catch (ServiceException e) {
ZimbraLog.account.warn("unable to remove zimbraMailAlias/mail attrs: " + alias);
}
}
// 2. remove address from all DLs
if (!aliasPointsToOtherExistingEntry) {
removeAddressFromAllDistributionLists(alias);
}
// 3. remove the alias entry
if (aliasPointsToEntry || aliasPointsToNonExistingEntry) {
try {
zlc.deleteEntry(aliasDn);
} catch (ServiceException e) {
// should not happen, log it
ZimbraLog.account.warn("unable to remove alias entry at : " + aliasDn);
}
}
// throw NO_SUCH_ALIAS if necessary
if (((entry != null) && (aliasEntry == null)) || ((entry != null) && (aliasEntry != null) && !aliasPointsToEntry))
throw AccountServiceException.NO_SUCH_ALIAS(alias);
} finally {
LdapClient.closeContext(zlc);
}
}
use of com.zimbra.cs.ldap.ZAttributes in project zm-mailbox by Zimbra.
the class LdapProvisioning method getServerByName.
private Server getServerByName(String name, boolean nocache) throws ServiceException {
if (!nocache) {
Server s = serverCache.getByName(name);
if (s != null)
return s;
}
try {
String dn = mDIT.serverNameToDN(name);
ZAttributes attrs = helper.getAttributes(LdapUsage.GET_SERVER, dn);
LdapServer s = new LdapServer(dn, attrs, getConfig().getServerDefaults(), this);
serverCache.put(s);
return s;
} catch (LdapEntryNotFoundException e) {
return null;
} catch (ServiceException e) {
throw ServiceException.FAILURE("unable to lookup server by name: " + name + " message: " + e.getMessage(), e);
}
}
Aggregations