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