001    /**
002     * Copyright (c) 2000-2013 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 actionMapping, ActionForm actionForm,
051                            HttpServletRequest request, HttpServletResponse response)
052                    throws Exception {
053    
054                    String cmd = ParamUtil.getString(request, Constants.CMD);
055    
056                    if (Validator.isNull(cmd)) {
057                            return actionMapping.findForward("portal.update_email_address");
058                    }
059    
060                    try {
061                            updateEmailAddress(request);
062    
063                            return actionMapping.findForward(
064                                    ActionConstants.COMMON_REFERER_JSP);
065                    }
066                    catch (Exception e) {
067                            if (e instanceof DuplicateUserEmailAddressException ||
068                                    e instanceof ReservedUserEmailAddressException ||
069                                    e instanceof UserEmailAddressException) {
070    
071                                    SessionErrors.add(request, e.getClass());
072    
073                                    return actionMapping.findForward("portal.update_email_address");
074                            }
075                            else if (e instanceof NoSuchUserException ||
076                                             e instanceof PrincipalException) {
077    
078                                    SessionErrors.add(request, e.getClass());
079    
080                                    return actionMapping.findForward("portal.error");
081                            }
082    
083                            PortalUtil.sendError(e, request, response);
084    
085                            return null;
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    }