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.NoSuchUserException;
018    import com.liferay.portal.ReservedUserEmailAddressException;
019    import com.liferay.portal.UserEmailAddressException;
020    import com.liferay.portal.kernel.servlet.SessionErrors;
021    import com.liferay.portal.kernel.util.Constants;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.security.auth.AuthTokenUtil;
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 ReservedUserEmailAddressException ||
068                                    e instanceof UserEmailAddressException) {
069    
070                                    SessionErrors.add(request, e.getClass());
071    
072                                    return actionMapping.findForward("portal.update_email_address");
073                            }
074                            else if (e instanceof NoSuchUserException ||
075                                             e instanceof PrincipalException) {
076    
077                                    SessionErrors.add(request, e.getClass());
078    
079                                    return actionMapping.findForward("portal.error");
080                            }
081    
082                            PortalUtil.sendError(e, request, response);
083    
084                            return null;
085                    }
086            }
087    
088            protected void updateEmailAddress(HttpServletRequest request)
089                    throws Exception {
090    
091                    AuthTokenUtil.checkCSRFToken(
092                            request, UpdateEmailAddressAction.class.getName());
093    
094                    long userId = PortalUtil.getUserId(request);
095                    String password = AdminUtil.getUpdateUserPassword(request, userId);
096                    String emailAddress1 = ParamUtil.getString(request, "emailAddress1");
097                    String emailAddress2 = ParamUtil.getString(request, "emailAddress2");
098    
099                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
100                            request);
101    
102                    UserServiceUtil.updateEmailAddress(
103                            userId, password, emailAddress1, emailAddress2, serviceContext);
104            }
105    
106    }