001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.action;
016    
017    import com.liferay.portal.DuplicateUserEmailAddressException;
018    import com.liferay.portal.NoSuchUserException;
019    import com.liferay.portal.ReservedUserEmailAddressException;
020    import com.liferay.portal.UserEmailAddressException;
021    import com.liferay.portal.kernel.servlet.SessionErrors;
022    import com.liferay.portal.kernel.util.Constants;
023    import com.liferay.portal.kernel.util.ParamUtil;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.security.auth.PrincipalException;
026    import com.liferay.portal.service.ServiceContext;
027    import com.liferay.portal.service.ServiceContextFactory;
028    import com.liferay.portal.service.UserServiceUtil;
029    import com.liferay.portal.struts.ActionConstants;
030    import com.liferay.portal.util.PortalUtil;
031    import com.liferay.portlet.admin.util.AdminUtil;
032    
033    import javax.servlet.http.HttpServletRequest;
034    import javax.servlet.http.HttpServletResponse;
035    
036    import org.apache.struts.action.Action;
037    import org.apache.struts.action.ActionForm;
038    import org.apache.struts.action.ActionForward;
039    import org.apache.struts.action.ActionMapping;
040    
041    /**
042     * @author Julio Camarero
043     * @author Jorge Ferrer
044     * @author Brian Wing Shun Chan
045     */
046    public class UpdateEmailAddressAction extends Action {
047    
048            @Override
049            public ActionForward execute(
050                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
051                            HttpServletResponse response)
052                    throws Exception {
053    
054                    String cmd = ParamUtil.getString(request, Constants.CMD);
055    
056                    if (Validator.isNull(cmd)) {
057                            return mapping.findForward("portal.update_email_address");
058                    }
059    
060                    try {
061                            updateEmailAddress(request);
062    
063                            return mapping.findForward(ActionConstants.COMMON_REFERER);
064                    }
065                    catch (Exception e) {
066                            if (e instanceof DuplicateUserEmailAddressException ||
067                                    e instanceof ReservedUserEmailAddressException ||
068                                    e instanceof UserEmailAddressException) {
069    
070                                    SessionErrors.add(request, e.getClass().getName());
071    
072                                    return mapping.findForward("portal.update_email_address");
073                            }
074                            else if (e instanceof NoSuchUserException ||
075                                             e instanceof PrincipalException) {
076    
077                                    SessionErrors.add(request, e.getClass().getName());
078    
079                                    return mapping.findForward("portal.error");
080                            }
081                            else {
082                                    PortalUtil.sendError(e, request, response);
083    
084                                    return null;
085                            }
086                    }
087            }
088    
089            protected void updateEmailAddress(HttpServletRequest request)
090                    throws Exception {
091    
092                    long userId = PortalUtil.getUserId(request);
093                    String password = AdminUtil.getUpdateUserPassword(request, userId);
094                    String emailAddress1 = ParamUtil.getString(request, "emailAddress1");
095                    String emailAddress2 = ParamUtil.getString(request, "emailAddress2");
096    
097                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
098                            request);
099    
100                    UserServiceUtil.updateEmailAddress(
101                            userId, password, emailAddress1, emailAddress2, serviceContext);
102            }
103    
104    }