Search in sources :

Example 21 with Signature

use of com.zimbra.cs.account.Signature in project zm-mailbox by Zimbra.

the class LdapProvisioning method getAllSignatures.

@Override
public List<Signature> getAllSignatures(Account account) throws ServiceException {
    LdapEntry ldapEntry = (LdapEntry) (account instanceof LdapEntry ? account : getAccountById(account.getId()));
    if (ldapEntry == null)
        throw AccountServiceException.NO_SUCH_ACCOUNT(account.getName());
    @SuppressWarnings("unchecked") List<Signature> result = (List<Signature>) account.getCachedData(SIGNATURE_LIST_CACHE_KEY);
    if (result != null) {
        return result;
    }
    result = new ArrayList<Signature>();
    Signature acctSig = LdapSignature.getAccountSignature(this, account);
    if (acctSig != null)
        result.add(acctSig);
    result = getSignaturesByQuery(account, ldapEntry, filterFactory.allSignatures(), null, result);
    result = Collections.unmodifiableList(result);
    account.setCachedData(SIGNATURE_LIST_CACHE_KEY, result);
    return result;
}
Also used : Signature(com.zimbra.cs.account.Signature) LdapSignature(com.zimbra.cs.account.ldap.entry.LdapSignature) LdapEntry(com.zimbra.cs.account.ldap.entry.LdapEntry) LdapDistributionList(com.zimbra.cs.account.ldap.entry.LdapDistributionList) ArrayList(java.util.ArrayList) List(java.util.List) DistributionList(com.zimbra.cs.account.DistributionList)

Example 22 with Signature

use of com.zimbra.cs.account.Signature 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);
    }
}
Also used : ZMutableEntry(com.zimbra.cs.ldap.ZMutableEntry) LdapEntryAlreadyExistException(com.zimbra.cs.ldap.LdapException.LdapEntryAlreadyExistException) ZLdapContext(com.zimbra.cs.ldap.ZLdapContext) LdapEntry(com.zimbra.cs.account.ldap.entry.LdapEntry) Date(java.util.Date) AccountServiceException(com.zimbra.cs.account.AccountServiceException) AccountServiceException(com.zimbra.cs.account.AccountServiceException) AuthFailedServiceException(com.zimbra.cs.account.AccountServiceException.AuthFailedServiceException) ServiceException(com.zimbra.common.service.ServiceException) Signature(com.zimbra.cs.account.Signature) LdapSignature(com.zimbra.cs.account.ldap.entry.LdapSignature) CallbackContext(com.zimbra.cs.account.callback.CallbackContext) LdapException(com.zimbra.cs.ldap.LdapException)

Example 23 with Signature

use of com.zimbra.cs.account.Signature in project zm-mailbox by Zimbra.

the class LdapProvisioning method getSignaturesByQuery.

private List<Signature> getSignaturesByQuery(Account acct, LdapEntry entry, ZLdapFilter filter, ZLdapContext initZlc, List<Signature> result) throws ServiceException {
    if (result == null) {
        result = new ArrayList<Signature>();
    }
    try {
        String base = entry.getDN();
        ZSearchResultEnumeration ne = helper.searchDir(base, filter, ZSearchControls.SEARCH_CTLS_SUBTREE(), initZlc, LdapServerType.REPLICA);
        while (ne.hasMore()) {
            ZSearchResultEntry sr = ne.next();
            result.add(new LdapSignature(acct, sr.getDN(), sr.getAttributes(), this));
        }
        ne.close();
    } catch (ServiceException e) {
        throw ServiceException.FAILURE("unable to lookup signature via query: " + filter.toFilterString() + " message: " + e.getMessage(), e);
    }
    return result;
}
Also used : LdapSignature(com.zimbra.cs.account.ldap.entry.LdapSignature) AccountServiceException(com.zimbra.cs.account.AccountServiceException) AuthFailedServiceException(com.zimbra.cs.account.AccountServiceException.AuthFailedServiceException) ServiceException(com.zimbra.common.service.ServiceException) Signature(com.zimbra.cs.account.Signature) LdapSignature(com.zimbra.cs.account.ldap.entry.LdapSignature) ZSearchResultEnumeration(com.zimbra.cs.ldap.ZSearchResultEnumeration) ZSearchResultEntry(com.zimbra.cs.ldap.ZSearchResultEntry)

Example 24 with Signature

use of com.zimbra.cs.account.Signature in project zm-mailbox by Zimbra.

the class GetSignatures method handle.

public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Account account = getRequestedAccount(zsc);
    if (!canAccessAccount(zsc, account))
        throw ServiceException.PERM_DENIED("can not access account");
    Element response = zsc.createElement(AccountConstants.GET_SIGNATURES_RESPONSE);
    List<Signature> signatures = Provisioning.getInstance().getAllSignatures(account);
    for (Signature sig : signatures) ToXML.encodeSignature(response, sig);
    return response;
}
Also used : Account(com.zimbra.cs.account.Account) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) Element(com.zimbra.common.soap.Element) Signature(com.zimbra.cs.account.Signature)

Example 25 with Signature

use of com.zimbra.cs.account.Signature in project zm-mailbox by Zimbra.

the class ModifySignature method handle.

public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Account account = getRequestedAccount(zsc);
    if (!canModifyOptions(zsc, account))
        throw ServiceException.PERM_DENIED("can not modify options");
    Provisioning prov = Provisioning.getInstance();
    Element eSignature = request.getElement(AccountConstants.E_SIGNATURE);
    Signature signature = null;
    String id = eSignature.getAttribute(AccountConstants.A_ID);
    signature = prov.get(account, Key.SignatureBy.id, id);
    if (signature == null)
        throw AccountServiceException.NO_SUCH_SIGNATURE(id);
    List<Element> contents = eSignature.listElements(AccountConstants.E_CONTENT);
    Map<String, Object> attrs = new HashMap<String, Object>();
    for (Element eContent : contents) {
        String type = eContent.getAttribute(AccountConstants.A_TYPE);
        String attr = SignatureUtil.mimeTypeToAttrName(type);
        if (attr == null)
            throw ServiceException.INVALID_REQUEST("invalid type " + type, null);
        if (attrs.get(attr) != null)
            throw ServiceException.INVALID_REQUEST("only one " + type + " content is allowed", null);
        String content = eContent.getText();
        if (content != null)
            attrs.put(attr, content);
    }
    String name = eSignature.getAttribute(AccountConstants.A_NAME, null);
    if (name != null)
        attrs.put(Provisioning.A_zimbraSignatureName, name);
    Element eContactId = eSignature.getOptionalElement(AccountConstants.E_CONTACT_ID);
    if (eContactId != null)
        attrs.put(Provisioning.A_zimbraPrefMailSignatureContactId, eContactId.getText());
    prov.modifySignature(account, signature.getId(), attrs);
    Element response = zsc.createElement(AccountConstants.MODIFY_SIGNATURE_RESPONSE);
    return response;
}
Also used : Account(com.zimbra.cs.account.Account) HashMap(java.util.HashMap) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) Element(com.zimbra.common.soap.Element) Signature(com.zimbra.cs.account.Signature) Provisioning(com.zimbra.cs.account.Provisioning)

Aggregations

Signature (com.zimbra.cs.account.Signature)25 Account (com.zimbra.cs.account.Account)14 Element (com.zimbra.common.soap.Element)7 Test (org.junit.Test)7 HashMap (java.util.HashMap)6 AccountServiceException (com.zimbra.cs.account.AccountServiceException)5 LdapSignature (com.zimbra.cs.account.ldap.entry.LdapSignature)5 LdapEntry (com.zimbra.cs.account.ldap.entry.LdapEntry)4 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)4 ServiceException (com.zimbra.common.service.ServiceException)3 XMLElement (com.zimbra.common.soap.Element.XMLElement)3 AuthFailedServiceException (com.zimbra.cs.account.AccountServiceException.AuthFailedServiceException)3 Identity (com.zimbra.cs.account.Identity)2 Provisioning (com.zimbra.cs.account.Provisioning)2 ZLdapContext (com.zimbra.cs.ldap.ZLdapContext)2 ArrayList (java.util.ArrayList)2 DataSource (com.zimbra.cs.account.DataSource)1 DistributionList (com.zimbra.cs.account.DistributionList)1 NamedEntry (com.zimbra.cs.account.NamedEntry)1 SearchAccountsOptions (com.zimbra.cs.account.SearchAccountsOptions)1