001    /**
002     * Copyright (c) 2000-2013 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            @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    }