use of com.zimbra.common.service.ServiceException in project zm-mailbox by Zimbra.
the class LdapProvisioning method deleteIdentity.
@Override
public void deleteIdentity(Account account, String identityName) throws ServiceException {
LdapEntry ldapEntry = (LdapEntry) (account instanceof LdapEntry ? account : getAccountById(account.getId()));
if (ldapEntry == null)
throw AccountServiceException.NO_SUCH_ACCOUNT(account.getName());
if (identityName.equalsIgnoreCase(ProvisioningConstants.DEFAULT_IDENTITY_NAME))
throw ServiceException.INVALID_REQUEST("can't delete default identity", null);
account.setCachedData(IDENTITY_LIST_CACHE_KEY, null);
ZLdapContext zlc = null;
try {
zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.DELETE_IDENTITY);
Identity identity = getIdentityByName(ldapEntry, identityName, zlc);
if (identity == null)
throw AccountServiceException.NO_SUCH_IDENTITY(identityName);
String dn = getIdentityDn(ldapEntry, identityName);
zlc.deleteEntry(dn);
} catch (ServiceException e) {
throw ServiceException.FAILURE("unable to delete identity: " + identityName, e);
} finally {
LdapClient.closeContext(zlc);
}
}
use of com.zimbra.common.service.ServiceException in project zm-mailbox by Zimbra.
the class LdapProvisioning method createDataSource.
private DataSource createDataSource(Account account, DataSourceType dsType, String dsName, Map<String, Object> dataSourceAttrs, boolean passwdAlreadyEncrypted, boolean restoring) throws ServiceException {
removeAttrIgnoreCase("objectclass", dataSourceAttrs);
LdapEntry ldapEntry = (LdapEntry) (account instanceof LdapEntry ? account : getAccountById(account.getId()));
if (ldapEntry == null) {
throw AccountServiceException.NO_SUCH_ACCOUNT(account.getName());
}
List<DataSource> existing = getAllDataSources(account);
if (existing.size() >= account.getLongAttr(A_zimbraDataSourceMaxNumEntries, 20)) {
throw AccountServiceException.TOO_MANY_DATA_SOURCES();
}
String dsEmailAddr = (String) dataSourceAttrs.get(A_zimbraDataSourceEmailAddress);
if (!StringUtil.isNullOrEmpty(dsEmailAddr)) {
for (DataSource ds : existing) {
if (dsEmailAddr.equals(ds.getEmailAddress())) {
throw AccountServiceException.DATA_SOURCE_EXISTS(dsEmailAddr);
}
}
}
// must be the same
dataSourceAttrs.put(A_zimbraDataSourceName, dsName);
dataSourceAttrs.put(Provisioning.A_zimbraDataSourceType, dsType.toString());
account.setCachedData(DATA_SOURCE_LIST_CACHE_KEY, null);
boolean checkImmutable = !restoring;
CallbackContext callbackContext = new CallbackContext(CallbackContext.Op.CREATE);
AttributeManager.getInstance().preModify(dataSourceAttrs, null, callbackContext, checkImmutable);
ZLdapContext zlc = null;
try {
zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.CREATE_DATASOURCE);
String dn = getDataSourceDn(ldapEntry, dsName);
ZMutableEntry entry = LdapClient.createMutableEntry();
entry.setDN(dn);
entry.mapToAttrs(dataSourceAttrs);
entry.setAttr(A_objectClass, "zimbraDataSource");
String extraOc = LdapDataSource.getObjectClass(dsType);
if (extraOc != null) {
entry.addAttr(A_objectClass, Sets.newHashSet(extraOc));
}
String dsId = entry.getAttrString(A_zimbraDataSourceId);
if (dsId == null) {
dsId = LdapUtil.generateUUID();
entry.setAttr(A_zimbraDataSourceId, dsId);
}
String password = entry.getAttrString(A_zimbraDataSourcePassword);
if (password != null) {
String encrypted = passwdAlreadyEncrypted ? password : DataSource.encryptData(dsId, password);
entry.setAttr(A_zimbraDataSourcePassword, encrypted);
}
String oauthToken = entry.getAttrString(A_zimbraDataSourceOAuthToken);
if (oauthToken != null) {
String encrypted = passwdAlreadyEncrypted ? oauthToken : DataSource.encryptData(dsId, oauthToken);
entry.setAttr(A_zimbraDataSourceOAuthToken, encrypted);
}
String clientSecret = entry.getAttrString(A_zimbraDataSourceOAuthClientSecret);
if (clientSecret != null) {
String encrypted = passwdAlreadyEncrypted ? clientSecret : DataSource.encryptData(dsId, clientSecret);
entry.setAttr(A_zimbraDataSourceOAuthClientSecret, encrypted);
}
String smtpPassword = entry.getAttrString(A_zimbraDataSourceSmtpAuthPassword);
if (smtpPassword != null) {
String encrypted = passwdAlreadyEncrypted ? smtpPassword : DataSource.encryptData(dsId, smtpPassword);
entry.setAttr(A_zimbraDataSourceSmtpAuthPassword, encrypted);
}
entry.setAttr(Provisioning.A_zimbraCreateTimestamp, LdapDateUtil.toGeneralizedTime(new Date()));
zlc.createEntry(entry);
DataSource ds = getDataSourceById(ldapEntry, dsId, zlc);
AttributeManager.getInstance().postModify(dataSourceAttrs, ds, callbackContext);
return ds;
} catch (LdapEntryAlreadyExistException nabe) {
throw AccountServiceException.DATA_SOURCE_EXISTS(dsName);
} catch (LdapException e) {
throw e;
} catch (AccountServiceException e) {
throw e;
} catch (ServiceException e) {
throw ServiceException.FAILURE("unable to create data source: " + dsName, e);
} finally {
LdapClient.closeContext(zlc);
}
}
use of com.zimbra.common.service.ServiceException in project zm-mailbox by Zimbra.
the class LdapProvisioning method createServer.
@Override
public Server createServer(String name, Map<String, Object> serverAttrs) throws ServiceException {
name = name.toLowerCase().trim();
CallbackContext callbackContext = new CallbackContext(CallbackContext.Op.CREATE);
AttributeManager.getInstance().preModify(serverAttrs, null, callbackContext, true);
ZLdapContext zlc = null;
try {
zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.CREATE_SERVER);
ZMutableEntry entry = LdapClient.createMutableEntry();
entry.mapToAttrs(serverAttrs);
Set<String> ocs = LdapObjectClass.getServerObjectClasses(this);
entry.addAttr(A_objectClass, ocs);
String zimbraIdStr = LdapUtil.generateUUID();
entry.setAttr(A_zimbraId, zimbraIdStr);
entry.setAttr(A_zimbraCreateTimestamp, LdapDateUtil.toGeneralizedTime(new Date()));
entry.setAttr(A_cn, name);
String dn = mDIT.serverNameToDN(name);
if (!entry.hasAttribute(Provisioning.A_zimbraServiceHostname)) {
entry.setAttr(Provisioning.A_zimbraServiceHostname, name);
}
entry.setDN(dn);
zlc.createEntry(entry);
Server server = getServerById(zimbraIdStr, zlc, true);
AttributeManager.getInstance().postModify(serverAttrs, server, callbackContext);
return server;
} catch (LdapEntryAlreadyExistException nabe) {
throw AccountServiceException.SERVER_EXISTS(name);
} catch (LdapException e) {
throw e;
} catch (AccountServiceException e) {
throw e;
} catch (ServiceException e) {
throw ServiceException.FAILURE("unable to create server: " + name, e);
} finally {
LdapClient.closeContext(zlc);
}
}
use of com.zimbra.common.service.ServiceException in project zm-mailbox by Zimbra.
the class LdapProvisioning method searchLdapObjects.
private void searchLdapObjects(String base, ZLdapFilter filter, String[] returnAttrs, SearchDirectoryOptions opts, NamedEntry.Visitor visitor) throws ServiceException {
ZLdapContext zlc = null;
try {
zlc = LdapClient.getContext(LdapServerType.get(opts.getOnMaster()), opts.getUseConnPool(), LdapUsage.SEARCH);
SearchObjectsVisitor searchObjectsVisitor = new SearchObjectsVisitor(this, zlc, visitor, opts.getMaxResults(), opts.getMakeObjectOpt(), returnAttrs);
SearchLdapOptions searchObjectsOptions = new SearchLdapOptions(base, filter, returnAttrs, opts.getMaxResults(), null, ZSearchScope.SEARCH_SCOPE_SUBTREE, searchObjectsVisitor);
searchObjectsOptions.setUseControl(opts.isUseControl());
searchObjectsOptions.setManageDSAit(opts.isManageDSAit());
zlc.searchPaged(searchObjectsOptions);
} catch (LdapSizeLimitExceededException e) {
throw AccountServiceException.TOO_MANY_SEARCH_RESULTS("too many search results returned", e);
} catch (ServiceException e) {
throw ServiceException.FAILURE("unable to list all objects", e);
} finally {
LdapClient.closeContext(zlc);
}
}
use of com.zimbra.common.service.ServiceException in project zm-mailbox by Zimbra.
the class LdapProvisioning method getAllServers.
@Override
public List<Server> getAllServers(String service) throws ServiceException {
List<Server> result = new ArrayList<Server>();
ZLdapFilter filter;
if (service != null) {
filter = filterFactory.serverByService(service);
} else {
filter = filterFactory.allServers();
}
try {
Map<String, Object> serverDefaults = getConfig().getServerDefaults();
ZSearchResultEnumeration ne = helper.searchDir(mDIT.serverBaseDN(), filter, ZSearchControls.SEARCH_CTLS_SUBTREE());
while (ne.hasMore()) {
ZSearchResultEntry sr = ne.next();
LdapServer s = new LdapServer(sr.getDN(), sr.getAttributes(), serverDefaults, this);
result.add(s);
}
ne.close();
} catch (ServiceException e) {
throw ServiceException.FAILURE("unable to list all servers", e);
}
if (result.size() > 0)
serverCache.put(result, true);
Collections.sort(result);
return result;
}
Aggregations