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.security.auth;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.servlet.HttpHeaders;
020    import com.liferay.portal.kernel.util.PropsKeys;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.model.User;
024    import com.liferay.portal.security.ldap.PortalLDAPImporterUtil;
025    import com.liferay.portal.service.UserLocalServiceUtil;
026    import com.liferay.portal.util.PortalUtil;
027    import com.liferay.portal.util.PrefsPropsUtil;
028    import com.liferay.portal.util.PropsValues;
029    
030    import javax.servlet.http.HttpServletRequest;
031    import javax.servlet.http.HttpServletResponse;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     * @author Wesley Gong
036     */
037    public class RequestHeaderAutoLogin implements AutoLogin {
038    
039            @Override
040            public String[] login(
041                    HttpServletRequest request, HttpServletResponse response) {
042    
043                    String[] credentials = null;
044    
045                    try {
046                            long companyId = PortalUtil.getCompanyId(request);
047    
048                            String screenName = request.getHeader(
049                                    HttpHeaders.LIFERAY_SCREEN_NAME);
050    
051                            if (Validator.isNull(screenName)) {
052                                    return credentials;
053                            }
054    
055                            User user = null;
056    
057                            if (PrefsPropsUtil.getBoolean(
058                                            companyId, PropsKeys.REQUEST_HEADER_AUTH_IMPORT_FROM_LDAP,
059                                            PropsValues.REQUEST_HEADER_AUTH_IMPORT_FROM_LDAP)) {
060    
061                                    try {
062                                            user = PortalLDAPImporterUtil.importLDAPUser(
063                                                    companyId, StringPool.BLANK, screenName);
064                                    }
065                                    catch (Exception e) {
066                                    }
067                            }
068    
069                            if (user == null) {
070                                    user = UserLocalServiceUtil.getUserByScreenName(
071                                            companyId, screenName);
072                            }
073    
074                            credentials = new String[3];
075    
076                            credentials[0] = String.valueOf(user.getUserId());
077                            credentials[1] = user.getPassword();
078                            credentials[2] = Boolean.TRUE.toString();
079    
080                            return credentials;
081                    }
082                    catch (Exception e) {
083                            _log.error(e, e);
084                    }
085    
086                    return credentials;
087            }
088    
089            private static Log _log = LogFactoryUtil.getLog(
090                    RequestHeaderAutoLogin.class);
091    
092    }