001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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            public String[] login(
040                    HttpServletRequest request, HttpServletResponse response) {
041    
042                    String[] credentials = null;
043    
044                    try {
045                            long companyId = PortalUtil.getCompanyId(request);
046    
047                            String screenName = request.getHeader(
048                                    HttpHeaders.LIFERAY_SCREEN_NAME);
049    
050                            if (Validator.isNull(screenName)) {
051                                    return credentials;
052                            }
053    
054                            User user = null;
055    
056                            if (PrefsPropsUtil.getBoolean(
057                                            companyId, PropsKeys.REQUEST_HEADER_AUTH_IMPORT_FROM_LDAP,
058                                            PropsValues.REQUEST_HEADER_AUTH_IMPORT_FROM_LDAP)) {
059    
060                                    try {
061                                            user = PortalLDAPImporterUtil.importLDAPUser(
062                                                    companyId, StringPool.BLANK, screenName);
063                                    }
064                                    catch (Exception e) {
065                                    }
066                            }
067    
068                            if (user == null) {
069                                    user = UserLocalServiceUtil.getUserByScreenName(
070                                            companyId, screenName);
071                            }
072    
073                            credentials = new String[3];
074    
075                            credentials[0] = String.valueOf(user.getUserId());
076                            credentials[1] = user.getPassword();
077                            credentials[2] = Boolean.TRUE.toString();
078    
079                            return credentials;
080                    }
081                    catch (Exception e) {
082                            _log.error(e, e);
083                    }
084    
085                    return credentials;
086            }
087    
088            private static Log _log = LogFactoryUtil.getLog(
089                    RequestHeaderAutoLogin.class);
090    
091    }