1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.security.auth;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.servlet.HttpHeaders;
20  import com.liferay.portal.kernel.util.PropsKeys;
21  import com.liferay.portal.kernel.util.StringPool;
22  import com.liferay.portal.kernel.util.Validator;
23  import com.liferay.portal.model.User;
24  import com.liferay.portal.service.UserLocalServiceUtil;
25  import com.liferay.portal.util.PortalUtil;
26  import com.liferay.portal.util.PrefsPropsUtil;
27  import com.liferay.portal.util.PropsValues;
28  
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  
32  /**
33   * <a href="RequestHeaderAutoLogin.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Brian Wing Shun Chan
36   * @author Wesley Gong
37   */
38  public class RequestHeaderAutoLogin extends CASAutoLogin {
39  
40      public String[] login(
41          HttpServletRequest request, HttpServletResponse response) {
42  
43          String[] credentials = null;
44  
45          try {
46              long companyId = PortalUtil.getCompanyId(request);
47  
48              String screenName = request.getHeader(
49                  HttpHeaders.LIFERAY_SCREEN_NAME);
50  
51              if (Validator.isNull(screenName)) {
52                  return credentials;
53              }
54  
55              User user = null;
56  
57              if (PrefsPropsUtil.getBoolean(
58                      companyId, PropsKeys.REQUEST_HEADER_AUTH_IMPORT_FROM_LDAP,
59                      PropsValues.REQUEST_HEADER_AUTH_IMPORT_FROM_LDAP)) {
60  
61                  try {
62                      user = importLDAPUser(
63                          companyId, StringPool.BLANK, screenName);
64                  }
65                  catch (Exception e) {
66                  }
67              }
68  
69              if (user == null) {
70                  user = UserLocalServiceUtil.getUserByScreenName(
71                      companyId, screenName);
72              }
73  
74              credentials = new String[3];
75  
76              credentials[0] = String.valueOf(user.getUserId());
77              credentials[1] = user.getPassword();
78              credentials[2] = Boolean.TRUE.toString();
79  
80              return credentials;
81          }
82          catch (Exception e) {
83              _log.error(e, e);
84          }
85  
86          return credentials;
87      }
88  
89      private static Log _log = LogFactoryUtil.getLog(
90          RequestHeaderAutoLogin.class);
91  
92  }