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]);
}
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;
}
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));
}
Aggregations