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.portlet.login.action;
016    
017    import com.liferay.portal.CookieNotSupportedException;
018    import com.liferay.portal.NoSuchUserException;
019    import com.liferay.portal.PasswordExpiredException;
020    import com.liferay.portal.UserEmailAddressException;
021    import com.liferay.portal.UserIdException;
022    import com.liferay.portal.UserLockoutException;
023    import com.liferay.portal.UserPasswordException;
024    import com.liferay.portal.UserScreenNameException;
025    import com.liferay.portal.kernel.servlet.SessionErrors;
026    import com.liferay.portal.kernel.util.ParamUtil;
027    import com.liferay.portal.kernel.util.Validator;
028    import com.liferay.portal.security.auth.AuthException;
029    import com.liferay.portal.struts.PortletAction;
030    import com.liferay.portal.theme.ThemeDisplay;
031    import com.liferay.portal.util.PortalUtil;
032    import com.liferay.portal.util.PropsValues;
033    import com.liferay.portal.util.WebKeys;
034    import com.liferay.portlet.PortletPreferencesFactoryUtil;
035    import com.liferay.portlet.login.util.LoginUtil;
036    
037    import javax.portlet.ActionRequest;
038    import javax.portlet.ActionResponse;
039    import javax.portlet.PortletConfig;
040    import javax.portlet.PortletPreferences;
041    import javax.portlet.RenderRequest;
042    import javax.portlet.RenderResponse;
043    
044    import javax.servlet.http.HttpServletRequest;
045    import javax.servlet.http.HttpServletResponse;
046    
047    import org.apache.struts.action.ActionForm;
048    import org.apache.struts.action.ActionForward;
049    import org.apache.struts.action.ActionMapping;
050    
051    /**
052     * @author Brian Wing Shun Chan
053     */
054    public class LoginAction extends PortletAction {
055    
056            public void processAction(
057                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
058                            ActionRequest actionRequest, ActionResponse actionResponse)
059                    throws Exception {
060    
061                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
062                            WebKeys.THEME_DISPLAY);
063    
064                    /*if (actionRequest.getRemoteUser() != null) {
065                            actionResponse.sendRedirect(themeDisplay.getPathMain());
066    
067                            return;
068                    }*/
069    
070                    try {
071                            PortletPreferences preferences =
072                                    PortletPreferencesFactoryUtil.getPortletSetup(actionRequest);
073    
074                            login(themeDisplay, actionRequest, actionResponse, preferences);
075                    }
076                    catch (Exception e) {
077                            if (e instanceof AuthException) {
078                                    Throwable cause = e.getCause();
079    
080                                    if (cause instanceof PasswordExpiredException ||
081                                            cause instanceof UserLockoutException) {
082    
083                                            SessionErrors.add(
084                                                    actionRequest, cause.getClass().getName());
085                                    }
086                                    else {
087                                            SessionErrors.add(actionRequest, e.getClass().getName());
088                                    }
089                            }
090                            else if (e instanceof CookieNotSupportedException ||
091                                             e instanceof NoSuchUserException ||
092                                             e instanceof PasswordExpiredException ||
093                                             e instanceof UserEmailAddressException ||
094                                             e instanceof UserIdException ||
095                                             e instanceof UserLockoutException ||
096                                             e instanceof UserPasswordException ||
097                                             e instanceof UserScreenNameException) {
098    
099                                    SessionErrors.add(actionRequest, e.getClass().getName());
100                            }
101                            else {
102                                    PortalUtil.sendError(e, actionRequest, actionResponse);
103                            }
104                    }
105            }
106    
107            public ActionForward render(
108                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
109                            RenderRequest renderRequest, RenderResponse renderResponse)
110                    throws Exception {
111    
112                    return mapping.findForward("portlet.login.login");
113            }
114    
115            protected boolean isCheckMethodOnProcessAction() {
116                    return _CHECK_METHOD_ON_PROCESS_ACTION;
117            }
118    
119            protected void login(
120                            ThemeDisplay themeDisplay, ActionRequest actionRequest,
121                            ActionResponse actionResponse, PortletPreferences preferences)
122                    throws Exception {
123    
124                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
125                            actionRequest);
126                    HttpServletResponse response = PortalUtil.getHttpServletResponse(
127                            actionResponse);
128    
129                    String login = ParamUtil.getString(actionRequest, "login");
130                    String password = ParamUtil.getString(actionRequest, "password");
131                    boolean rememberMe = ParamUtil.getBoolean(actionRequest, "rememberMe");
132    
133                    String authType = preferences.getValue("authType", null);
134    
135                    LoginUtil.login(
136                            request, response, login, password, rememberMe, authType);
137    
138                    if (PropsValues.PORTAL_JAAS_ENABLE) {
139                            actionResponse.sendRedirect(
140                                    themeDisplay.getPathMain() + "/portal/protected");
141                    }
142                    else {
143                            String redirect = ParamUtil.getString(actionRequest, "redirect");
144    
145                            if (Validator.isNotNull(redirect)) {
146                                    redirect = PortalUtil.escapeRedirect(redirect);
147    
148                                    actionResponse.sendRedirect(redirect);
149                            }
150                            else {
151                                    actionResponse.sendRedirect(themeDisplay.getPathMain());
152                            }
153                    }
154            }
155    
156            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
157    
158    }