use of com.zimbra.cs.ldap.ZMutableEntry in project zm-mailbox by Zimbra.
the class LdapProvisioning method createIdentity.
private Identity createIdentity(Account account, String identityName, Map<String, Object> identityAttrs, boolean restoring) 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());
if (identityName.equalsIgnoreCase(ProvisioningConstants.DEFAULT_IDENTITY_NAME))
throw AccountServiceException.IDENTITY_EXISTS(identityName);
List<Identity> existing = getAllIdentities(account);
if (existing.size() >= account.getLongAttr(A_zimbraIdentityMaxNumEntries, 20))
throw AccountServiceException.TOO_MANY_IDENTITIES();
account.setCachedData(IDENTITY_LIST_CACHE_KEY, null);
boolean checkImmutable = !restoring;
CallbackContext callbackContext = new CallbackContext(CallbackContext.Op.CREATE);
AttributeManager.getInstance().preModify(identityAttrs, null, callbackContext, checkImmutable);
ZLdapContext zlc = null;
try {
zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.CREATE_IDENTITY);
String dn = getIdentityDn(ldapEntry, identityName);
ZMutableEntry entry = LdapClient.createMutableEntry();
entry.setDN(dn);
entry.mapToAttrs(identityAttrs);
entry.setAttr(A_objectClass, "zimbraIdentity");
if (!entry.hasAttribute(A_zimbraPrefIdentityId)) {
String identityId = LdapUtil.generateUUID();
entry.setAttr(A_zimbraPrefIdentityId, identityId);
}
entry.setAttr(Provisioning.A_zimbraCreateTimestamp, LdapDateUtil.toGeneralizedTime(new Date()));
zlc.createEntry(entry);
Identity identity = getIdentityByName(ldapEntry, identityName, zlc);
AttributeManager.getInstance().postModify(identityAttrs, identity, callbackContext);
return identity;
} catch (LdapEntryAlreadyExistException nabe) {
throw AccountServiceException.IDENTITY_EXISTS(identityName);
} catch (LdapException e) {
throw e;
} catch (AccountServiceException e) {
throw e;
} catch (ServiceException e) {
throw ServiceException.FAILURE("unable to create identity " + identityName, e);
} finally {
LdapClient.closeContext(zlc);
}
}
use of com.zimbra.cs.ldap.ZMutableEntry in project zm-mailbox by Zimbra.
the class LdapProvisioning method createAlwaysOnCluster.
@Override
public AlwaysOnCluster createAlwaysOnCluster(String name, Map<String, Object> clusterAttrs) throws ServiceException {
name = name.toLowerCase().trim();
CallbackContext callbackContext = new CallbackContext(CallbackContext.Op.CREATE);
AttributeManager.getInstance().preModify(clusterAttrs, null, callbackContext, true);
ZLdapContext zlc = null;
try {
zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.CREATE_SERVER);
ZMutableEntry entry = LdapClient.createMutableEntry();
entry.mapToAttrs(clusterAttrs);
Set<String> ocs = LdapObjectClass.getAlwaysOnClusterObjectClasses(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.alwaysOnClusterNameToDN(name);
entry.setDN(dn);
zlc.createEntry(entry);
AlwaysOnCluster cluster = getAlwaysOnClusterById(zimbraIdStr, zlc, true);
AttributeManager.getInstance().postModify(clusterAttrs, cluster, callbackContext);
return cluster;
} catch (LdapEntryAlreadyExistException nabe) {
throw AccountServiceException.ALWAYSONCLUSTER_EXISTS(name);
} catch (LdapException e) {
throw e;
} catch (AccountServiceException e) {
throw e;
} catch (ServiceException e) {
throw ServiceException.FAILURE("unable to create akwaysOnCluster: " + name, e);
} finally {
LdapClient.closeContext(zlc);
}
}
use of com.zimbra.cs.ldap.ZMutableEntry in project zm-mailbox by Zimbra.
the class LdapProvisioning method renameDomain.
// LdapProv
@Override
public void renameDomain(String zimbraId, String newDomainName) throws ServiceException {
newDomainName = newDomainName.toLowerCase().trim();
newDomainName = IDNUtil.toAsciiDomainName(newDomainName);
NameUtil.validNewDomainName(newDomainName);
ZLdapContext zlc = null;
try {
zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.RENAME_DOMAIN);
RenameDomain.RenameDomainLdapHelper helper = new RenameDomain.RenameDomainLdapHelper(this, zlc) {
private ZLdapContext toZLdapContext() {
return LdapClient.toZLdapContext(mProv, mZlc);
}
@Override
public void createEntry(String dn, Map<String, Object> attrs) throws ServiceException {
ZMutableEntry entry = LdapClient.createMutableEntry();
entry.mapToAttrs(attrs);
entry.setDN(dn);
ZLdapContext ldapContext = toZLdapContext();
ldapContext.createEntry(entry);
}
@Override
public void deleteEntry(String dn) throws ServiceException {
ZLdapContext ldapContext = toZLdapContext();
ldapContext.deleteEntry(dn);
}
@Override
public void renameEntry(String oldDn, String newDn) throws ServiceException {
ZLdapContext ldapContext = toZLdapContext();
ldapContext.renameEntry(oldDn, newDn);
}
@Override
public void searchDirectory(SearchDirectoryOptions options, NamedEntry.Visitor visitor) throws ServiceException {
((LdapProvisioning) mProv).searchDirectory(options, visitor);
}
@Override
public void renameAddressesInAllDistributionLists(Map<String, String> changedPairs) {
((LdapProvisioning) mProv).renameAddressesInAllDistributionLists(changedPairs);
}
@Override
public void renameXMPPComponent(String zimbraId, String newName) throws ServiceException {
((LdapProvisioning) mProv).renameXMPPComponent(zimbraId, newName);
}
@Override
public Account getAccountById(String id) throws ServiceException {
// note: we do NOT want to get a cached entry
return ((LdapProvisioning) mProv).getAccountByQuery(mProv.getDIT().mailBranchBaseDN(), ZLdapFilterFactory.getInstance().accountById(id), toZLdapContext(), true);
}
@Override
public DistributionList getDistributionListById(String id) throws ServiceException {
// note: we do NOT want to get a cahed entry
return ((LdapProvisioning) mProv).getDistributionListByQuery(mDIT.mailBranchBaseDN(), filterFactory.distributionListById(id), toZLdapContext(), false);
}
@Override
public DynamicGroup getDynamicGroupById(String id) throws ServiceException {
// note: we do NOT want to get a cahed entry
return ((LdapProvisioning) mProv).getDynamicGroupByQuery(filterFactory.dynamicGroupById(id), toZLdapContext(), false);
}
@Override
public void modifyLdapAttrs(Entry entry, Map<String, ? extends Object> attrs) throws ServiceException {
((LdapProvisioning) mProv).modifyLdapAttrs(entry, toZLdapContext(), attrs);
}
};
Domain oldDomain = getDomainById(zimbraId, zlc);
if (oldDomain == null)
throw AccountServiceException.NO_SUCH_DOMAIN(zimbraId);
RenameDomain rd = new RenameDomain(this, helper, oldDomain, newDomainName);
rd.execute();
} finally {
LdapClient.closeContext(zlc);
}
}
use of com.zimbra.cs.ldap.ZMutableEntry in project zm-mailbox by Zimbra.
the class LdapProvisioning method createSignature.
private Signature createSignature(Account account, String signatureName, Map<String, Object> signatureAttrs, boolean restoring) throws ServiceException {
signatureName = signatureName.trim();
removeAttrIgnoreCase("objectclass", signatureAttrs);
validateSignatureAttrs(signatureAttrs);
LdapEntry ldapEntry = (LdapEntry) (account instanceof LdapEntry ? account : getAccountById(account.getId()));
if (ldapEntry == null)
throw AccountServiceException.NO_SUCH_ACCOUNT(account.getName());
/*
* check if the signature name already exists
*
* We check if the signatureName is the same as the signature on the account.
* For signatures that are in the signature LDAP entries, JNDI will throw
* NameAlreadyBoundException for duplicate names.
*
*/
Signature acctSig = LdapSignature.getAccountSignature(this, account);
if (acctSig != null && signatureName.equalsIgnoreCase(acctSig.getName()))
throw AccountServiceException.SIGNATURE_EXISTS(signatureName);
boolean setAsDefault = false;
List<Signature> existing = getAllSignatures(account);
// If the signature id is supplied with the request, check that it
// is not associated with an existing signature
String signatureId = (String) signatureAttrs.get(Provisioning.A_zimbraSignatureId);
if (signatureId != null) {
for (Signature signature : existing) {
if (signatureId.equals(signature.getAttr(Provisioning.A_zimbraSignatureId))) {
throw AccountServiceException.SIGNATURE_EXISTS(signatureId);
}
}
}
int numSigs = existing.size();
if (numSigs >= account.getLongAttr(A_zimbraSignatureMaxNumEntries, 20))
throw AccountServiceException.TOO_MANY_SIGNATURES();
else if (numSigs == 0)
setAsDefault = true;
account.setCachedData(SIGNATURE_LIST_CACHE_KEY, null);
boolean checkImmutable = !restoring;
CallbackContext callbackContext = new CallbackContext(CallbackContext.Op.CREATE);
callbackContext.setData(DataKey.MAX_SIGNATURE_LEN, String.valueOf(account.getMailSignatureMaxLength()));
AttributeManager.getInstance().preModify(signatureAttrs, null, callbackContext, checkImmutable);
if (signatureId == null) {
signatureId = LdapUtil.generateUUID();
signatureAttrs.put(Provisioning.A_zimbraSignatureId, signatureId);
}
if (acctSig == null) {
// the slot on the account is not occupied, use it
signatureAttrs.put(Provisioning.A_zimbraSignatureName, signatureName);
// pass in setAsDefault as an optimization, since we are updating the account
// entry, we can update the default attr in one LDAP write
LdapSignature.createAccountSignature(this, account, signatureAttrs, setAsDefault);
return LdapSignature.getAccountSignature(this, account);
}
ZLdapContext zlc = null;
try {
zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.CREATE_SIGNATURE);
String dn = getSignatureDn(ldapEntry, signatureName);
ZMutableEntry entry = LdapClient.createMutableEntry();
entry.mapToAttrs(signatureAttrs);
entry.setAttr(A_objectClass, "zimbraSignature");
entry.setAttr(Provisioning.A_zimbraCreateTimestamp, LdapDateUtil.toGeneralizedTime(new Date()));
entry.setDN(dn);
zlc.createEntry(entry);
Signature signature = getSignatureById(account, ldapEntry, signatureId, zlc);
AttributeManager.getInstance().postModify(signatureAttrs, signature, callbackContext);
if (setAsDefault)
setDefaultSignature(account, signatureId);
return signature;
} catch (LdapEntryAlreadyExistException nabe) {
throw AccountServiceException.SIGNATURE_EXISTS(signatureName);
} catch (LdapException e) {
throw e;
} catch (AccountServiceException e) {
throw e;
} catch (ServiceException e) {
throw ServiceException.FAILURE("unable to create signature: " + signatureName, e);
} finally {
LdapClient.closeContext(zlc);
}
}
use of com.zimbra.cs.ldap.ZMutableEntry in project zm-mailbox by Zimbra.
the class LdapProvisioning method createXMPPComponent.
@Override
public XMPPComponent createXMPPComponent(String name, Domain domain, Server server, Map<String, Object> inAttrs) throws ServiceException {
name = name.toLowerCase().trim();
// sanity checking
removeAttrIgnoreCase("objectclass", inAttrs);
removeAttrIgnoreCase(A_zimbraDomainId, inAttrs);
removeAttrIgnoreCase(A_zimbraServerId, inAttrs);
CallbackContext callbackContext = new CallbackContext(CallbackContext.Op.CREATE);
AttributeManager.getInstance().preModify(inAttrs, null, callbackContext, true);
ZLdapContext zlc = null;
try {
zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.CREATE_XMPPCOMPONENT);
ZMutableEntry entry = LdapClient.createMutableEntry();
entry.mapToAttrs(inAttrs);
entry.setAttr(A_objectClass, "zimbraXMPPComponent");
String compId = LdapUtil.generateUUID();
entry.setAttr(A_zimbraId, compId);
entry.setAttr(A_zimbraCreateTimestamp, LdapDateUtil.toGeneralizedTime(new Date()));
entry.setAttr(A_cn, name);
String dn = mDIT.xmppcomponentNameToDN(name);
entry.setDN(dn);
entry.setAttr(A_zimbraDomainId, domain.getId());
entry.setAttr(A_zimbraServerId, server.getId());
zlc.createEntry(entry);
XMPPComponent comp = getXMPPComponentById(compId, zlc, true);
AttributeManager.getInstance().postModify(inAttrs, comp, callbackContext);
return comp;
} catch (LdapEntryAlreadyExistException nabe) {
throw AccountServiceException.IM_COMPONENT_EXISTS(name);
} finally {
LdapClient.closeContext(zlc);
}
}
Aggregations