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.portlet.login.util;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.language.LanguageUtil;
019    import com.liferay.portal.kernel.security.auth.session.AuthenticatedSessionManagerUtil;
020    import com.liferay.portal.kernel.servlet.SessionMessages;
021    import com.liferay.portal.kernel.util.CookieKeys;
022    import com.liferay.portal.kernel.util.GetterUtil;
023    import com.liferay.portal.kernel.util.HtmlUtil;
024    import com.liferay.portal.kernel.util.ParamUtil;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.util.Validator;
027    import com.liferay.portal.model.Company;
028    import com.liferay.portal.model.CompanyConstants;
029    import com.liferay.portal.model.User;
030    import com.liferay.portal.service.ServiceContext;
031    import com.liferay.portal.service.ServiceContextFactory;
032    import com.liferay.portal.service.UserLocalServiceUtil;
033    import com.liferay.portal.theme.ThemeDisplay;
034    import com.liferay.portal.util.PortalUtil;
035    import com.liferay.portal.util.PortletKeys;
036    import com.liferay.portal.util.PropsValues;
037    import com.liferay.portal.util.WebKeys;
038    import com.liferay.portlet.PortletURLFactoryUtil;
039    
040    import java.util.LinkedHashMap;
041    import java.util.Map;
042    
043    import javax.portlet.ActionRequest;
044    import javax.portlet.PortletMode;
045    import javax.portlet.PortletModeException;
046    import javax.portlet.PortletPreferences;
047    import javax.portlet.PortletRequest;
048    import javax.portlet.PortletURL;
049    import javax.portlet.WindowState;
050    import javax.portlet.WindowStateException;
051    
052    import javax.servlet.http.HttpServletRequest;
053    import javax.servlet.http.HttpServletResponse;
054    import javax.servlet.http.HttpSession;
055    
056    /**
057     * @author Brian Wing Shun Chan
058     * @author Scott Lee
059     */
060    public class LoginUtil {
061    
062            /**
063             * @deprecated As of 7.0.0, replaced by {@link
064             *             AuthenticatedSessionManagerUtil#getAuthenticatedUserId(
065             *             HttpServletRequest, String, String, String)}
066             */
067            @Deprecated
068            public static long getAuthenticatedUserId(
069                            HttpServletRequest request, String login, String password,
070                            String authType)
071                    throws PortalException {
072    
073                    return AuthenticatedSessionManagerUtil.getAuthenticatedUserId(
074                            request, login, password, authType);
075            }
076    
077            public static Map<String, String> getEmailDefinitionTerms(
078                    PortletRequest portletRequest, String emailFromAddress,
079                    String emailFromName, boolean showPasswordTerms) {
080    
081                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
082                            WebKeys.THEME_DISPLAY);
083    
084                    Map<String, String> definitionTerms = new LinkedHashMap<>();
085    
086                    definitionTerms.put(
087                            "[$FROM_ADDRESS$]", HtmlUtil.escape(emailFromAddress));
088                    definitionTerms.put("[$FROM_NAME$]", HtmlUtil.escape(emailFromName));
089    
090                    if (showPasswordTerms) {
091                            definitionTerms.put(
092                                    "[$PASSWORD_RESET_URL$]",
093                                    LanguageUtil.get(
094                                            themeDisplay.getLocale(), "the-password-reset-url"));
095                    }
096    
097                    Company company = themeDisplay.getCompany();
098    
099                    definitionTerms.put("[$PORTAL_URL$]", company.getVirtualHostname());
100    
101                    definitionTerms.put(
102                            "[$REMOTE_ADDRESS$]",
103                            LanguageUtil.get(
104                                    themeDisplay.getLocale(), "the-browser's-remote-address"));
105                    definitionTerms.put(
106                            "[$REMOTE_HOST$]",
107                            LanguageUtil.get(
108                                    themeDisplay.getLocale(), "the-browser's-remote-host"));
109                    definitionTerms.put(
110                            "[$TO_ADDRESS$]",
111                            LanguageUtil.get(
112                                    themeDisplay.getLocale(),
113                                    "the-address-of-the-email-recipient"));
114                    definitionTerms.put(
115                            "[$TO_NAME$]",
116                            LanguageUtil.get(
117                                    themeDisplay.getLocale(), "the-name-of-the-email-recipient"));
118                    definitionTerms.put(
119                            "[$USER_ID$]",
120                            LanguageUtil.get(themeDisplay.getLocale(), "the-user-id"));
121    
122                    if (showPasswordTerms) {
123                            definitionTerms.put(
124                                    "[$USER_PASSWORD$]",
125                                    LanguageUtil.get(
126                                            themeDisplay.getLocale(), "the-user-password"));
127                    }
128    
129                    definitionTerms.put(
130                            "[$USER_SCREENNAME$]",
131                            LanguageUtil.get(themeDisplay.getLocale(), "the-user-screen-name"));
132    
133                    return definitionTerms;
134            }
135    
136            public static String getEmailFromAddress(
137                    PortletPreferences preferences, long companyId) {
138    
139                    return PortalUtil.getEmailFromAddress(
140                            preferences, companyId, PropsValues.LOGIN_EMAIL_FROM_ADDRESS);
141            }
142    
143            public static String getEmailFromName(
144                    PortletPreferences preferences, long companyId) {
145    
146                    return PortalUtil.getEmailFromName(
147                            preferences, companyId, PropsValues.LOGIN_EMAIL_FROM_NAME);
148            }
149    
150            public static String getLogin(
151                    HttpServletRequest request, String paramName, Company company) {
152    
153                    String login = request.getParameter(paramName);
154    
155                    if ((login == null) || login.equals(StringPool.NULL)) {
156                            login = GetterUtil.getString(
157                                    CookieKeys.getCookie(request, CookieKeys.LOGIN, false));
158    
159                            if (PropsValues.COMPANY_LOGIN_PREPOPULATE_DOMAIN &&
160                                    Validator.isNull(login) &&
161                                    company.getAuthType().equals(CompanyConstants.AUTH_TYPE_EA)) {
162    
163                                    login = "@" + company.getMx();
164                            }
165                    }
166    
167                    return login;
168            }
169    
170            public static PortletURL getLoginURL(HttpServletRequest request, long plid)
171                    throws PortletModeException, WindowStateException {
172    
173                    PortletURL portletURL = PortletURLFactoryUtil.create(
174                            request, PortletKeys.LOGIN, plid, PortletRequest.RENDER_PHASE);
175    
176                    portletURL.setParameter("saveLastPath", Boolean.FALSE.toString());
177                    portletURL.setParameter("mvcRenderCommandName", "/login/login");
178                    portletURL.setPortletMode(PortletMode.VIEW);
179                    portletURL.setWindowState(WindowState.MAXIMIZED);
180    
181                    return portletURL;
182            }
183    
184            /**
185             * @deprecated As of 7.0.0, replaced by {@link
186             *             AuthenticatedSessionManagerUtil#login(HttpServletRequest,
187             *             HttpServletResponse, String, String, boolean, String)}
188             */
189            @Deprecated
190            public static void login(
191                            HttpServletRequest request, HttpServletResponse response,
192                            String login, String password, boolean rememberMe, String authType)
193                    throws Exception {
194    
195                    AuthenticatedSessionManagerUtil.login(
196                            request, response, login, password, rememberMe, authType);
197            }
198    
199            /**
200             * @deprecated As of 7.0.0, replaced by {@link
201             *             AuthenticatedSessionManagerUtil#renewSession(
202             *             HttpServletRequest, HttpSession)}
203             */
204            @Deprecated
205            public static HttpSession renewSession(
206                            HttpServletRequest request, HttpSession session)
207                    throws Exception {
208    
209                    return AuthenticatedSessionManagerUtil.renewSession(request, session);
210            }
211    
212            public static void sendPassword(ActionRequest actionRequest)
213                    throws Exception {
214    
215                    String toAddress = ParamUtil.getString(actionRequest, "emailAddress");
216    
217                    sendPassword(actionRequest, null, null, toAddress, null, null);
218            }
219    
220            public static void sendPassword(
221                            ActionRequest actionRequest, String fromName, String fromAddress,
222                            String toAddress, String subject, String body)
223                    throws Exception {
224    
225                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
226                            actionRequest);
227    
228                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
229                            WebKeys.THEME_DISPLAY);
230    
231                    Company company = themeDisplay.getCompany();
232    
233                    if (!company.isSendPassword() && !company.isSendPasswordResetLink()) {
234                            return;
235                    }
236    
237                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
238                            User.class.getName(), actionRequest);
239    
240                    UserLocalServiceUtil.sendPassword(
241                            company.getCompanyId(), toAddress, fromName, fromAddress, subject,
242                            body, serviceContext);
243    
244                    SessionMessages.add(actionRequest, "requestProcessed", toAddress);
245            }
246    
247            /**
248             * @deprecated As of 7.0.0, replaced by {@link
249             *             AuthenticatedSessionManagerUtil#signOutSimultaneousLogins(
250             *             long)}
251             */
252            @Deprecated
253            public static void signOutSimultaneousLogins(long userId) throws Exception {
254                    AuthenticatedSessionManagerUtil.signOutSimultaneousLogins(userId);
255            }
256    
257    }