use of com.zimbra.common.service.ServiceException in project zm-mailbox by Zimbra.
the class LdapProvisioning method renameSignature.
private void renameSignature(LdapEntry entry, LdapSignature signature, String newSignatureName) throws ServiceException {
ZLdapContext zlc = null;
try {
zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.RENAME_SIGNATURE);
String newDn = getSignatureDn(entry, newSignatureName);
zlc.renameEntry(signature.getDN(), newDn);
} catch (ServiceException e) {
throw ServiceException.FAILURE("unable to rename signature: " + newSignatureName, e);
} finally {
LdapClient.closeContext(zlc);
}
}
use of com.zimbra.common.service.ServiceException in project zm-mailbox by Zimbra.
the class LdapProvisioning method deleteAccount.
@Override
public void deleteAccount(String zimbraId) throws ServiceException {
Account acc = getAccountById(zimbraId);
LdapEntry entry = (LdapEntry) getAccountById(zimbraId);
if (acc == null)
throw AccountServiceException.NO_SUCH_ACCOUNT(zimbraId);
// remove the account from all DLs
// this doesn't throw any exceptions
removeAddressFromAllDistributionLists(acc.getName());
// delete all aliases of the account
String[] aliases = acc.getMailAlias();
if (aliases != null) {
for (int i = 0; i < aliases.length; i++) {
try {
// this also removes each alias from any DLs
removeAlias(acc, aliases[i]);
} catch (ServiceException se) {
if (AccountServiceException.NO_SUCH_ALIAS.equals(se.getCode())) {
ZimbraLog.account.warn("got no such alias from removeAlias call when deleting account; likely alias was previously in a bad state");
} else {
throw se;
}
}
}
}
// delete all grants granted to the account
try {
RightCommand.revokeAllRights(this, GranteeType.GT_USER, zimbraId);
} catch (ServiceException e) {
// eat the exception and continue
ZimbraLog.account.warn("cannot revoke grants", e);
}
// if ephemeral backend is not LDAP, need to explicitly delete ephemeral data
EphemeralStore.Factory factory = EphemeralStore.getFactory();
if (!(factory instanceof LdapEphemeralStore.Factory)) {
factory.getStore().deleteData(new LdapEntryLocation(acc));
}
final Map<String, Object> attrs = new HashMap<String, Object>(acc.getAttrs());
ZLdapContext zlc = null;
try {
zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.DELETE_ACCOUNT);
zlc.deleteChildren(entry.getDN());
zlc.deleteEntry(entry.getDN());
validate(ProvisioningValidator.DELETE_ACCOUNT_SUCCEEDED, attrs);
accountCache.remove(acc);
} catch (ServiceException e) {
throw ServiceException.FAILURE("unable to purge account: " + zimbraId, e);
} finally {
LdapClient.closeContext(zlc);
}
}
use of com.zimbra.common.service.ServiceException in project zm-mailbox by Zimbra.
the class LdapProvisioning method removeServerFromAllCOSes.
private void removeServerFromAllCOSes(String serverId, String serverName, ZLdapContext initZlc) {
List<Cos> coses = null;
try {
coses = searchCOS(filterFactory.cosesByMailHostPool(serverId), initZlc);
for (Cos cos : coses) {
Map<String, String> attrs = new HashMap<String, String>();
attrs.put("-" + Provisioning.A_zimbraMailHostPool, serverId);
ZimbraLog.account.info("Removing " + Provisioning.A_zimbraMailHostPool + " " + serverId + "(" + serverName + ") from cos " + cos.getName());
modifyAttrs(cos, attrs);
// invalidate cached cos
cosCache.remove((LdapCos) cos);
}
} catch (ServiceException se) {
ZimbraLog.account.warn("unable to remove " + serverId + " from all COSes ", se);
return;
}
}
use of com.zimbra.common.service.ServiceException in project zm-mailbox by Zimbra.
the class LdapProvisioning method renameDynamicGroup.
private void renameDynamicGroup(String zimbraId, String newEmail) throws ServiceException {
newEmail = IDNUtil.toAsciiEmail(newEmail);
validEmailAddress(newEmail);
boolean domainChanged = false;
ZLdapContext zlc = null;
try {
zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.RENAME_DYNAMICGROUP);
LdapDynamicGroup group = (LdapDynamicGroup) getDynamicGroupById(zimbraId, zlc, false);
if (group == null) {
throw AccountServiceException.NO_SUCH_DISTRIBUTION_LIST(zimbraId);
}
// prune cache
groupCache.remove(group);
String oldEmail = group.getName();
String oldDomain = EmailUtil.getValidDomainPart(oldEmail);
newEmail = newEmail.toLowerCase().trim();
String[] parts = EmailUtil.getLocalPartAndDomain(newEmail);
if (parts == null) {
throw ServiceException.INVALID_REQUEST("bad value for newName", null);
}
String newLocal = parts[0];
String newDomain = parts[1];
domainChanged = !oldDomain.equals(newDomain);
Domain domain = getDomainByAsciiName(newDomain, zlc);
if (domain == null) {
throw AccountServiceException.NO_SUCH_DOMAIN(newDomain);
}
if (domainChanged) {
// make sure the new domain is a local domain
if (!domain.isLocal()) {
throw ServiceException.INVALID_REQUEST("domain type must be local", null);
}
}
Map<String, Object> attrs = new HashMap<String, Object>();
ReplaceAddressResult replacedMails = replaceMailAddresses(group, Provisioning.A_mail, oldEmail, newEmail);
if (replacedMails.newAddrs().length == 0) {
// Set mail to newName if the account currently does not have a mail
attrs.put(Provisioning.A_mail, newEmail);
} else {
attrs.put(Provisioning.A_mail, replacedMails.newAddrs());
}
ReplaceAddressResult replacedAliases = replaceMailAddresses(group, Provisioning.A_zimbraMailAlias, oldEmail, newEmail);
if (replacedAliases.newAddrs().length > 0) {
attrs.put(Provisioning.A_zimbraMailAlias, replacedAliases.newAddrs());
String newDomainDN = mDIT.domainToAccountSearchDN(newDomain);
// check up front if any of renamed aliases already exists in the new domain (if domain also got changed)
if (domainChanged && addressExistsUnderDN(zlc, newDomainDN, replacedAliases.newAddrs())) {
throw AccountServiceException.DISTRIBUTION_LIST_EXISTS(newEmail);
}
}
ReplaceAddressResult replacedAllowAddrForDelegatedSender = replaceMailAddresses(group, Provisioning.A_zimbraPrefAllowAddressForDelegatedSender, oldEmail, newEmail);
if (replacedAllowAddrForDelegatedSender.newAddrs().length > 0) {
attrs.put(Provisioning.A_zimbraPrefAllowAddressForDelegatedSender, replacedAllowAddrForDelegatedSender.newAddrs());
}
// the naming rdn
String rdnAttrName = mDIT.dynamicGroupNamingRdnAttr();
attrs.put(rdnAttrName, newLocal);
// move over the distribution list entry
String oldDn = group.getDN();
String newDn = mDIT.dynamicGroupDNRename(oldDn, newLocal, domain.getName());
boolean dnChanged = (!oldDn.equals(newDn));
if (dnChanged) {
// cn will be changed during renameEntry, so no need to modify it
// OpenLDAP is OK modifying it, as long as it matches the new DN, but
// InMemoryDirectoryServer does not like it.
attrs.remove(A_cn);
zlc.renameEntry(oldDn, newDn);
}
// re-get the entry after move
group = (LdapDynamicGroup) getDynamicGroupById(zimbraId, zlc, false);
// doesn't throw exceptions, just logs
if (domainChanged) {
String newUid = group.getAttr(rdnAttrName);
moveAliases(zlc, replacedAliases, newDomain, newUid, oldDn, newDn, oldDomain, newDomain);
}
// could fail. So catch service exception here and log error
try {
// modify attrs on the mail entry
modifyAttrsInternal(group, zlc, attrs);
if (group.isIsACLGroup()) {
// modify attrs on the units (which are only present when group is an ACL Group)
String dynamicUnitNewLocal = dynamicGroupDynamicUnitLocalpart(newLocal);
String dynamicUnitNewEmail = dynamicUnitNewLocal + "@" + newDomain;
String dynamicUnitDN = mDIT.dynamicGroupUnitNameToDN(DYNAMIC_GROUP_DYNAMIC_UNIT_NAME, newDn);
ZMutableEntry entry = LdapClient.createMutableEntry();
entry.setAttr(A_mail, dynamicUnitNewEmail);
entry.setAttr(A_zimbraMailAlias, dynamicUnitNewEmail);
zlc.replaceAttributes(dynamicUnitDN, entry.getAttributes());
}
} catch (ServiceException e) {
ZimbraLog.account.error("dynamic group renamed to " + newLocal + " but failed to move old name's LDAP attributes", e);
throw e;
}
removeExternalAddrsFromAllDynamicGroups(group.getAllAddrsSet(), zlc);
} catch (LdapEntryAlreadyExistException nabe) {
throw AccountServiceException.DISTRIBUTION_LIST_EXISTS(newEmail);
} catch (LdapException e) {
throw e;
} catch (AccountServiceException e) {
throw e;
} catch (ServiceException e) {
throw ServiceException.FAILURE("unable to rename dynamic group: " + zimbraId, e);
} finally {
LdapClient.closeContext(zlc);
}
if (domainChanged) {
PermissionCache.invalidateCache();
}
}
use of com.zimbra.common.service.ServiceException in project zm-mailbox by Zimbra.
the class LdapProvisioning method getAllAlwaysOnClusters.
@Override
public List<AlwaysOnCluster> getAllAlwaysOnClusters() throws ServiceException {
List<AlwaysOnCluster> result = new ArrayList<AlwaysOnCluster>();
ZLdapFilter filter = filterFactory.allAlwaysOnClusters();
try {
ZSearchResultEnumeration ne = helper.searchDir(mDIT.alwaysOnClusterBaseDN(), filter, ZSearchControls.SEARCH_CTLS_SUBTREE());
while (ne.hasMore()) {
ZSearchResultEntry sr = ne.next();
LdapAlwaysOnCluster c = new LdapAlwaysOnCluster(sr.getDN(), sr.getAttributes(), null, this);
result.add(c);
}
ne.close();
} catch (ServiceException e) {
throw ServiceException.FAILURE("unable to list all alwaysOnClusters", e);
}
if (result.size() > 0)
alwaysOnClusterCache.put(result, true);
Collections.sort(result);
return result;
}
Aggregations