use of com.zimbra.cs.account.ldap.entry.LdapEntry in project zm-mailbox by Zimbra.
the class LdapProvisioning method refreshEntry.
void refreshEntry(Entry entry, ZLdapContext initZlc) throws ServiceException {
try {
String dn = ((LdapEntry) entry).getDN();
ZAttributes attributes = helper.getAttributes(initZlc, dn);
Map<String, Object> attrs = attributes.getAttrs();
Map<String, Object> defaults = null;
Map<String, Object> secondaryDefaults = null;
Map<String, Object> overrideDefaults = null;
if (entry instanceof Account) {
//
// We can get here from either modifyAttrsInternal or reload path.
//
// If we got here from modifyAttrsInternal, zimbraCOSId on account
// might have been changed, added, removed, but entry now still contains
// the old attrs. Create a temp Account object from the new attrs, and then
// use the same cos of the temp Account object for our entry object.
//
// If we got here from reload, attrs are likely not changed, the callsites
// just want a refreshed object. For this case it's best if we still
// always resolve the COS correctly. makeAccount is a cheap call and won't
// add any overhead like loading cos/domain from LDAP: even if cos/domain
// has to be loaded (because not in cache) in the getCOS(temp) call, it's
// just the same as calling (buggy) getCOS(entry) before.
//
// We only need the temp object for the getCOS call, don't need to setup
// primary/secondary defaults on the temp object because:
// zimbraCOSId is only on account(of course), and that's all needed
// for determining the COS for the account in the getCOS call: if
// zimbraCOSId is not set on account, it will fallback to the domain
// default COS, then fallback to the system default COS.
//
Account temp = makeAccountNoDefaults(dn, attributes);
Cos cos = getCOS(temp);
if (cos != null)
defaults = cos.getAccountDefaults();
Domain domain = getDomain((Account) entry);
if (domain != null)
secondaryDefaults = domain.getAccountDefaults();
} else if (entry instanceof Domain) {
defaults = getConfig().getDomainDefaults();
} else if (entry instanceof Server) {
defaults = getConfig().getServerDefaults();
AlwaysOnCluster aoc = getAlwaysOnCluster((Server) entry);
if (aoc != null) {
overrideDefaults = aoc.getServerOverrides();
}
}
if (defaults == null && secondaryDefaults == null)
entry.setAttrs(attrs);
else
entry.setAttrs(attrs, defaults, secondaryDefaults, overrideDefaults);
extendLifeInCacheOrFlush(entry);
} catch (ServiceException e) {
throw ServiceException.FAILURE("unable to refresh entry", e);
}
}
use of com.zimbra.cs.account.ldap.entry.LdapEntry 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();
}
// 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.cs.account.ldap.entry.LdapEntry in project zm-mailbox by Zimbra.
the class LdapProvisioning method modifyDataSource.
@Override
public void modifyDataSource(Account account, String dataSourceId, Map<String, Object> attrs) throws ServiceException {
removeAttrIgnoreCase("objectclass", attrs);
LdapEntry ldapEntry = (LdapEntry) (account instanceof LdapEntry ? account : getAccountById(account.getId()));
if (ldapEntry == null)
throw AccountServiceException.NO_SUCH_ACCOUNT(account.getName());
LdapDataSource ds = (LdapDataSource) getDataSourceById(ldapEntry, dataSourceId, null);
if (ds == null)
throw AccountServiceException.NO_SUCH_DATA_SOURCE(dataSourceId);
account.setCachedData(DATA_SOURCE_LIST_CACHE_KEY, null);
attrs.remove(A_zimbraDataSourceId);
String name = (String) attrs.get(A_zimbraDataSourceName);
boolean newName = (name != null && !name.equals(ds.getName()));
if (newName)
attrs.remove(A_zimbraDataSourceName);
String password = (String) attrs.get(A_zimbraDataSourcePassword);
if (password != null) {
attrs.put(A_zimbraDataSourcePassword, DataSource.encryptData(ds.getId(), password));
}
String oauthToken = (String) attrs.get(A_zimbraDataSourceOAuthToken);
if (oauthToken != null) {
attrs.put(A_zimbraDataSourceOAuthToken, DataSource.encryptData(ds.getId(), oauthToken));
}
String clientSecret = (String) attrs.get(A_zimbraDataSourceOAuthClientSecret);
if (clientSecret != null) {
attrs.put(A_zimbraDataSourceOAuthClientSecret, DataSource.encryptData(ds.getId(), clientSecret));
}
String smtpPassword = (String) attrs.get(A_zimbraDataSourceSmtpAuthPassword);
if (smtpPassword != null) {
attrs.put(A_zimbraDataSourceSmtpAuthPassword, DataSource.encryptData(ds.getId(), smtpPassword));
}
modifyAttrs(ds, attrs, true);
if (newName) {
// the datasoruce cache could've been loaded again if getAllDataSources were called in pre/poseModify callback, so we clear it again
account.setCachedData(DATA_SOURCE_LIST_CACHE_KEY, null);
ZLdapContext zlc = null;
try {
zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.RENAME_DATASOURCE);
String newDn = getDataSourceDn(ldapEntry, name);
zlc.renameEntry(ds.getDN(), newDn);
} catch (ServiceException e) {
throw ServiceException.FAILURE("unable to rename datasource: " + name, e);
} finally {
LdapClient.closeContext(zlc);
}
}
}
use of com.zimbra.cs.account.ldap.entry.LdapEntry 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.cs.account.ldap.entry.LdapEntry in project zm-mailbox by Zimbra.
the class LdapProvisioning method modifyIdentity.
@Override
public void modifyIdentity(Account account, String identityName, Map<String, Object> identityAttrs) throws ServiceException {
removeAttrIgnoreCase("objectclass", identityAttrs);
validateIdentityAttrs(identityAttrs);
LdapEntry ldapEntry = (LdapEntry) (account instanceof LdapEntry ? account : getAccountById(account.getId()));
if (ldapEntry == null)
throw AccountServiceException.NO_SUCH_ACCOUNT(account.getName());
// clear cache
account.setCachedData(IDENTITY_LIST_CACHE_KEY, null);
if (identityName.equalsIgnoreCase(ProvisioningConstants.DEFAULT_IDENTITY_NAME)) {
modifyAttrs(account, identityAttrs);
} else {
LdapIdentity identity = (LdapIdentity) getIdentityByName(ldapEntry, identityName, null);
if (identity == null)
throw AccountServiceException.NO_SUCH_IDENTITY(identityName);
String name = (String) identityAttrs.get(A_zimbraPrefIdentityName);
boolean newName = (name != null && !name.equals(identityName));
if (newName)
identityAttrs.remove(A_zimbraPrefIdentityName);
modifyAttrs(identity, identityAttrs, true);
if (newName) {
// the identity cache could've been loaded again if getAllIdentities were called in pre/poseModify callback, so we clear it again
account.setCachedData(IDENTITY_LIST_CACHE_KEY, null);
renameIdentity(ldapEntry, identity, name);
}
}
}
Aggregations