001    /**
002     * Copyright (c) 2000-2010 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.util.HttpUtil;
018    import com.liferay.portal.kernel.util.ParamUtil;
019    import com.liferay.portal.kernel.util.PropsKeys;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.theme.ThemeDisplay;
023    import com.liferay.portal.util.PortalUtil;
024    import com.liferay.portal.util.PortletKeys;
025    import com.liferay.portal.util.PrefsPropsUtil;
026    import com.liferay.portal.util.PropsValues;
027    import com.liferay.portal.util.WebKeys;
028    import com.liferay.portlet.PortletURLImpl;
029    import com.liferay.portlet.login.util.LoginUtil;
030    
031    import javax.portlet.PortletMode;
032    import javax.portlet.PortletRequest;
033    import javax.portlet.PortletURL;
034    import javax.portlet.WindowState;
035    
036    import javax.servlet.http.HttpServletRequest;
037    import javax.servlet.http.HttpServletResponse;
038    import javax.servlet.http.HttpSession;
039    
040    import org.apache.struts.action.Action;
041    import org.apache.struts.action.ActionForm;
042    import org.apache.struts.action.ActionForward;
043    import org.apache.struts.action.ActionMapping;
044    
045    /**
046     * @author Brian Wing Shun Chan
047     * @author Scott Lee
048     */
049    public class LoginAction extends Action {
050    
051            public ActionForward execute(
052                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
053                            HttpServletResponse response)
054                    throws Exception {
055    
056                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
057                            WebKeys.THEME_DISPLAY);
058    
059                    String login = ParamUtil.getString(request, "login");
060                    String password = ParamUtil.getString(request, "password");
061                    boolean rememberMe = ParamUtil.getBoolean(request, "rememberMe");
062                    String authType = ParamUtil.getString(request, "authType");
063    
064                    if (Validator.isNotNull(login) && Validator.isNotNull(password)) {
065                            LoginUtil.login(
066                                    request, response, login, password, rememberMe, authType);
067                    }
068    
069                    HttpSession session = request.getSession();
070    
071                    if ((session.getAttribute("j_username") != null) &&
072                            (session.getAttribute("j_password") != null)) {
073    
074                            if (PropsValues.PORTAL_JAAS_ENABLE) {
075                                    return mapping.findForward("/portal/touch_protected.jsp");
076                            }
077                            else {
078                                    response.sendRedirect(themeDisplay.getPathMain());
079    
080                                    return null;
081                            }
082                    }
083    
084                    String redirect = PortalUtil.getCommunityLoginURL(themeDisplay);
085    
086                    if (Validator.isNull(redirect)) {
087                            redirect = PropsValues.AUTH_LOGIN_URL;
088                    }
089    
090                    if (Validator.isNull(redirect)) {
091                            PortletURL portletURL = new PortletURLImpl(
092                                    request, PortletKeys.LOGIN, themeDisplay.getPlid(),
093                                    PortletRequest.RENDER_PHASE);
094    
095                            portletURL.setWindowState(WindowState.MAXIMIZED);
096                            portletURL.setPortletMode(PortletMode.VIEW);
097    
098                            portletURL.setParameter("saveLastPath", "0");
099                            portletURL.setParameter("struts_action", "/login/login");
100    
101                            redirect = portletURL.toString();
102                    }
103    
104                    if (PropsValues.COMPANY_SECURITY_AUTH_REQUIRES_HTTPS) {
105                            String portalURL = PortalUtil.getPortalURL(request);
106    
107                            String portalURLSecure = PortalUtil.getPortalURL(request, true);
108    
109                            if (!portalURL.equals(portalURLSecure)) {
110                                    redirect = StringUtil.replaceFirst(
111                                            redirect, portalURL, portalURLSecure);
112                            }
113                    }
114    
115                    String loginRedirect = ParamUtil.getString(request, "redirect");
116    
117                    if (Validator.isNotNull(loginRedirect)) {
118                            if (PrefsPropsUtil.getBoolean(
119                                            themeDisplay.getCompanyId(), PropsKeys.CAS_AUTH_ENABLED,
120                                            PropsValues.CAS_AUTH_ENABLED)) {
121    
122                                    redirect = loginRedirect;
123                            }
124                            else {
125                                    String loginPortletNamespace = PortalUtil.getPortletNamespace(
126                                            PropsValues.AUTH_LOGIN_PORTLET_NAME);
127    
128                                    String loginRedirectParameter =
129                                            loginPortletNamespace + "redirect";
130    
131                                    redirect = HttpUtil.setParameter(
132                                            redirect, "p_p_id", PropsValues.AUTH_LOGIN_PORTLET_NAME);
133                                    redirect = HttpUtil.setParameter(
134                                            redirect, "p_p_lifecycle", "0");
135                                    redirect = HttpUtil.setParameter(
136                                            redirect, loginRedirectParameter, loginRedirect);
137                            }
138                    }
139    
140                    response.sendRedirect(redirect);
141    
142                    return null;
143            }
144    
145    }