Search in sources :

Example 1 with ChangePrimaryEmailRequest

use of com.zimbra.soap.admin.message.ChangePrimaryEmailRequest in project zm-mailbox by Zimbra.

the class ChangePrimaryEmailTest method testChangePrimaryEmail.

@Test
public void testChangePrimaryEmail() throws Exception {
    Account admin = Provisioning.getInstance().get(Key.AccountBy.name, "admin@zimbra.com");
    admin.setIsAdminAccount(true);
    ChangePrimaryEmailRequest request = new ChangePrimaryEmailRequest(AccountSelector.fromName("old@zimbra.com"), "new@zimbra.com");
    Element req = JaxbUtil.jaxbToElement(request);
    ChangePrimaryEmail handler = new ChangePrimaryEmail();
    handler.setResponseQName(AdminConstants.CHANGE_PRIMARY_EMAIL_REQUEST);
    handler.handle(req, ServiceTestUtil.getRequestContext(admin));
    // getting new account with old name as mock provisioning doesn't rename account
    Account newAcc = Provisioning.getInstance().get(Key.AccountBy.name, "old@zimbra.com");
    String change = newAcc.getPrimaryEmailChangeHistory()[0];
    Assert.assertEquals("old@zimbra.com", change.substring(0, change.indexOf("|")));
    Assert.assertEquals("old@zimbra.com", newAcc.getAliases()[0]);
}
Also used : Account(com.zimbra.cs.account.Account) Element(com.zimbra.common.soap.Element) ChangePrimaryEmailRequest(com.zimbra.soap.admin.message.ChangePrimaryEmailRequest) Test(org.junit.Test)

Example 2 with ChangePrimaryEmailRequest

use of com.zimbra.soap.admin.message.ChangePrimaryEmailRequest in project zm-mailbox by Zimbra.

the class ChangePrimaryEmail method handle.

@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Provisioning prov = Provisioning.getInstance();
    ChangePrimaryEmailRequest req = JaxbUtil.elementToJaxb(request);
    String newName = req.getNewName();
    if (StringUtils.isEmpty(newName)) {
        throw ServiceException.INVALID_REQUEST(String.format("missing <%s>", AdminConstants.E_NEW_NAME), null);
    }
    AccountSelector acctSel = req.getAccount();
    if (acctSel == null) {
        throw ServiceException.INVALID_REQUEST(String.format("missing <%s>", AdminConstants.E_ACCOUNT), null);
    }
    String accountSelectorKey = acctSel.getKey();
    AccountBy by = acctSel.getBy().toKeyAccountBy();
    Account account = prov.get(by, accountSelectorKey, zsc.getAuthToken());
    defendAgainstAccountHarvesting(account, by, accountSelectorKey, zsc, Admin.R_renameAccount);
    defendAgainstAccountOrCalendarResourceHarvesting(account, by, accountSelectorKey, zsc, Admin.R_addAccountAlias, Admin.R_addCalendarResourceAlias);
    String oldName = account.getName();
    String accountId = account.getId();
    checkAccountRight(zsc, account, Admin.R_renameAccount);
    checkDomainRightByEmail(zsc, newName, Admin.R_createAccount);
    checkDomainRightByEmail(zsc, oldName, Admin.R_createAlias);
    account.setOldMailAddress(oldName);
    try {
        ZimbraLog.security.debug("old mail address set to %s for account %s", oldName, accountId);
        Mailbox mbox = Provisioning.onLocalServer(account) ? MailboxManager.getInstance().getMailboxByAccount(account) : null;
        if (mbox == null) {
            throw ServiceException.FAILURE("unable to get mailbox for rename: " + accountId, null);
        }
        prov.renameAccount(accountId, newName);
        mbox.renameMailbox(oldName, newName);
        ZimbraLog.security.info(ZimbraLog.encodeAttrs(new String[] { "cmd", "ChangePrimaryEmail account renamed", "name", oldName, "newName", newName }));
        // get account again after rename
        account = prov.get(AccountBy.id, accountId, true, zsc.getAuthToken());
        if (account == null) {
            throw ServiceException.FAILURE("unable to get account after rename: " + accountId, null);
        }
        int sleepTime = DebugConfig.sleepTimeForTestingChangePrimaryEmail;
        try {
            ZimbraLog.security.debug("ChangePrimaryEmail thread sleeping for %d milliseconds", sleepTime);
            Thread.sleep(sleepTime);
        } catch (InterruptedException e) {
            ZimbraLog.security.debug("ChangePrimaryEmail sleep was interrupted", e);
        }
        prov.addAlias(account, oldName);
        ZimbraLog.security.info(ZimbraLog.encodeAttrs(new String[] { "cmd", "ChangePrimaryEmail added alias", "name", account.getName(), "alias", oldName }));
        Date renameTime = new Date();
        account.addPrimaryEmailChangeHistory(String.format("%s|%d", oldName, renameTime.getTime()));
    } finally {
        if (account != null) {
            prov.flushCache(CacheEntryType.account, new CacheEntry[] { new CacheEntry(Key.CacheEntryBy.id, account.getId()) });
            account.unsetOldMailAddress();
        }
    }
    Element response = zsc.createElement(AdminConstants.CHANGE_PRIMARY_EMAIL_RESPONSE);
    ToXML.encodeAccount(response, account);
    return response;
}
Also used : Account(com.zimbra.cs.account.Account) Element(com.zimbra.common.soap.Element) AccountSelector(com.zimbra.soap.type.AccountSelector) CacheEntry(com.zimbra.cs.account.Provisioning.CacheEntry) Provisioning(com.zimbra.cs.account.Provisioning) ChangePrimaryEmailRequest(com.zimbra.soap.admin.message.ChangePrimaryEmailRequest) Date(java.util.Date) AccountBy(com.zimbra.common.account.Key.AccountBy) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext)

Example 3 with ChangePrimaryEmailRequest

use of com.zimbra.soap.admin.message.ChangePrimaryEmailRequest in project zm-mailbox by Zimbra.

the class SoapProvisioning method changePrimaryEmail.

public void changePrimaryEmail(String zimbraId, String newName) throws ServiceException {
    AccountSelector acctSel = new AccountSelector(com.zimbra.soap.type.AccountBy.id, zimbraId);
    invokeJaxb(new ChangePrimaryEmailRequest(acctSel, newName));
}
Also used : AccountSelector(com.zimbra.soap.type.AccountSelector) ChangePrimaryEmailRequest(com.zimbra.soap.admin.message.ChangePrimaryEmailRequest)

Aggregations

ChangePrimaryEmailRequest (com.zimbra.soap.admin.message.ChangePrimaryEmailRequest)3 Element (com.zimbra.common.soap.Element)2 Account (com.zimbra.cs.account.Account)2 AccountSelector (com.zimbra.soap.type.AccountSelector)2 AccountBy (com.zimbra.common.account.Key.AccountBy)1 Provisioning (com.zimbra.cs.account.Provisioning)1 CacheEntry (com.zimbra.cs.account.Provisioning.CacheEntry)1 Mailbox (com.zimbra.cs.mailbox.Mailbox)1 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)1 Date (java.util.Date)1 Test (org.junit.Test)1