001    /**
002     * Copyright (c) 2000-2012 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 mapping, ActionForm form, HttpServletRequest request,
054                            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 mapping.findForward("portal.verify_email_address");
064                    }
065    
066                    if (themeDisplay.isSignedIn() && cmd.equals(Constants.SEND)) {
067                            sendEmailAddressVerification(request, response, themeDisplay);
068    
069                            return mapping.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 mapping.findForward(ActionConstants.COMMON_REFERER);
086                            }
087                    }
088                    catch (Exception e) {
089                            if (e instanceof PortalException || e instanceof SystemException) {
090                                    SessionErrors.add(request, e.getClass().getName());
091    
092                                    return mapping.findForward("portal.verify_email_address");
093                            }
094                            else {
095                                    PortalUtil.sendError(e, request, response);
096    
097                                    return null;
098                            }
099                    }
100            }
101    
102            protected void sendEmailAddressVerification(
103                            HttpServletRequest request, HttpServletResponse response,
104                            ThemeDisplay themeDisplay)
105                    throws Exception {
106    
107                    User user = themeDisplay.getUser();
108    
109                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
110                            request);
111    
112                    UserLocalServiceUtil.sendEmailAddressVerification(
113                            user, user.getEmailAddress(), serviceContext);
114            }
115    
116            protected void verifyEmailAddress(
117                            HttpServletRequest request, HttpServletResponse response,
118                            ThemeDisplay themeDisplay)
119                    throws Exception {
120    
121                    AuthTokenUtil.check(request);
122    
123                    String ticketKey = ParamUtil.getString(request, "ticketKey");
124    
125                    UserLocalServiceUtil.verifyEmailAddress(ticketKey);
126            }
127    
128    }