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