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.AuthTokenUtil;
026    import com.liferay.portal.security.auth.PrincipalException;
027    import com.liferay.portal.service.ServiceContext;
028    import com.liferay.portal.service.ServiceContextFactory;
029    import com.liferay.portal.service.UserServiceUtil;
030    import com.liferay.portal.struts.ActionConstants;
031    import com.liferay.portal.util.PortalUtil;
032    import com.liferay.portlet.admin.util.AdminUtil;
033    
034    import javax.servlet.http.HttpServletRequest;
035    import javax.servlet.http.HttpServletResponse;
036    
037    import org.apache.struts.action.Action;
038    import org.apache.struts.action.ActionForm;
039    import org.apache.struts.action.ActionForward;
040    import org.apache.struts.action.ActionMapping;
041    
042    /**
043     * @author Julio Camarero
044     * @author Jorge Ferrer
045     * @author Brian Wing Shun Chan
046     */
047    public class UpdateEmailAddressAction extends Action {
048    
049            @Override
050            public ActionForward execute(
051                            ActionMapping actionMapping, ActionForm actionForm,
052                            HttpServletRequest request, HttpServletResponse response)
053                    throws Exception {
054    
055                    String cmd = ParamUtil.getString(request, Constants.CMD);
056    
057                    if (Validator.isNull(cmd)) {
058                            return actionMapping.findForward("portal.update_email_address");
059                    }
060    
061                    try {
062                            updateEmailAddress(request);
063    
064                            return actionMapping.findForward(
065                                    ActionConstants.COMMON_REFERER_JSP);
066                    }
067                    catch (Exception e) {
068                            if (e instanceof DuplicateUserEmailAddressException ||
069                                    e instanceof ReservedUserEmailAddressException ||
070                                    e instanceof UserEmailAddressException) {
071    
072                                    SessionErrors.add(request, e.getClass());
073    
074                                    return actionMapping.findForward("portal.update_email_address");
075                            }
076                            else if (e instanceof NoSuchUserException ||
077                                             e instanceof PrincipalException) {
078    
079                                    SessionErrors.add(request, e.getClass());
080    
081                                    return actionMapping.findForward("portal.error");
082                            }
083    
084                            PortalUtil.sendError(e, request, response);
085    
086                            return null;
087                    }
088            }
089    
090            protected void updateEmailAddress(HttpServletRequest request)
091                    throws Exception {
092    
093                    AuthTokenUtil.checkCSRFToken(
094                            request, UpdateEmailAddressAction.class.getName());
095    
096                    long userId = PortalUtil.getUserId(request);
097                    String password = AdminUtil.getUpdateUserPassword(request, userId);
098                    String emailAddress1 = ParamUtil.getString(request, "emailAddress1");
099                    String emailAddress2 = ParamUtil.getString(request, "emailAddress2");
100    
101                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
102                            request);
103    
104                    UserServiceUtil.updateEmailAddress(
105                            userId, password, emailAddress1, emailAddress2, serviceContext);
106            }
107    
108    }