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.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
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.model.User;
024    import com.liferay.portal.security.auth.AuthTokenUtil;
025    import com.liferay.portal.service.ServiceContext;
026    import com.liferay.portal.service.ServiceContextFactory;
027    import com.liferay.portal.service.UserLocalServiceUtil;
028    import com.liferay.portal.struts.ActionConstants;
029    import com.liferay.portal.theme.ThemeDisplay;
030    import com.liferay.portal.util.PortalUtil;
031    import com.liferay.portal.util.PortletKeys;
032    import com.liferay.portal.util.WebKeys;
033    import com.liferay.portlet.PortletURLImpl;
034    
035    import javax.portlet.PortletRequest;
036    import javax.portlet.PortletURL;
037    
038    import javax.servlet.http.HttpServletRequest;
039    import javax.servlet.http.HttpServletResponse;
040    
041    import org.apache.struts.action.Action;
042    import org.apache.struts.action.ActionForm;
043    import org.apache.struts.action.ActionForward;
044    import org.apache.struts.action.ActionMapping;
045    
046    /**
047     * @author Mika Koivisto
048     */
049    public class VerifyEmailAddressAction extends Action {
050    
051            @Override
052            public ActionForward execute(
053                            ActionMapping actionMapping, ActionForm actionForm,
054                            HttpServletRequest request, HttpServletResponse response)
055                    throws Exception {
056    
057                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
058                            WebKeys.THEME_DISPLAY);
059    
060                    String cmd = ParamUtil.getString(request, Constants.CMD);
061    
062                    if (Validator.isNull(cmd)) {
063                            return actionMapping.findForward("portal.verify_email_address");
064                    }
065    
066                    if (themeDisplay.isSignedIn() && cmd.equals(Constants.SEND)) {
067                            sendEmailAddressVerification(request, response, themeDisplay);
068    
069                            return actionMapping.findForward("portal.verify_email_address");
070                    }
071    
072                    try {
073                            verifyEmailAddress(request, response, themeDisplay);
074    
075                            if (!themeDisplay.isSignedIn()) {
076                                    PortletURL portletURL = new PortletURLImpl(
077                                            request, PortletKeys.LOGIN, themeDisplay.getPlid(),
078                                            PortletRequest.RENDER_PHASE);
079    
080                                    response.sendRedirect(portletURL.toString());
081    
082                                    return null;
083                            }
084                            else {
085                                    return actionMapping.findForward(
086                                            ActionConstants.COMMON_REFERER_JSP);
087                            }
088                    }
089                    catch (Exception e) {
090                            if (e instanceof PortalException || e instanceof SystemException) {
091                                    SessionErrors.add(request, e.getClass());
092    
093                                    return actionMapping.findForward("portal.verify_email_address");
094                            }
095                            else {
096                                    PortalUtil.sendError(e, request, response);
097    
098                                    return null;
099                            }
100                    }
101            }
102    
103            protected void sendEmailAddressVerification(
104                            HttpServletRequest request, HttpServletResponse response,
105                            ThemeDisplay themeDisplay)
106                    throws Exception {
107    
108                    User user = themeDisplay.getUser();
109    
110                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
111                            request);
112    
113                    UserLocalServiceUtil.sendEmailAddressVerification(
114                            user, user.getEmailAddress(), serviceContext);
115            }
116    
117            protected void verifyEmailAddress(
118                            HttpServletRequest request, HttpServletResponse response,
119                            ThemeDisplay themeDisplay)
120                    throws Exception {
121    
122                    AuthTokenUtil.check(request);
123    
124                    String ticketKey = ParamUtil.getString(request, "ticketKey");
125    
126                    UserLocalServiceUtil.verifyEmailAddress(ticketKey);
127            }
128    
129    }