1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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.StringPool;
21  import com.liferay.portal.kernel.util.Validator;
22  import com.liferay.portal.model.User;
23  import com.liferay.portal.security.ldap.LDAPSettingsUtil;
24  import com.liferay.portal.service.UserLocalServiceUtil;
25  import com.liferay.portal.util.PortalUtil;
26  
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  
30  /**
31   * <a href="RequestHeaderAutoLogin.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Brian Wing Shun Chan
34   * @author Wesley Gong
35   */
36  public class RequestHeaderAutoLogin extends CASAutoLogin {
37  
38      public String[] login(
39          HttpServletRequest request, HttpServletResponse response) {
40  
41          String[] credentials = null;
42  
43          try {
44              long companyId = PortalUtil.getCompanyId(request);
45  
46              String screenName = request.getHeader(
47                  HttpHeaders.LIFERAY_SCREEN_NAME);
48  
49              if (Validator.isNull(screenName)) {
50                  return credentials;
51              }
52  
53              User user = null;
54  
55              if (LDAPSettingsUtil.isImportEnabled(companyId)) {
56                  try {
57                      user = importLDAPUser(
58                          companyId, StringPool.BLANK, screenName);
59                  }
60                  catch (Exception e) {
61                  }
62              }
63  
64              if (user == null) {
65                  user = UserLocalServiceUtil.getUserByScreenName(
66                      companyId, screenName);
67              }
68  
69              credentials = new String[3];
70  
71              credentials[0] = String.valueOf(user.getUserId());
72              credentials[1] = user.getPassword();
73              credentials[2] = Boolean.TRUE.toString();
74  
75              return credentials;
76          }
77          catch (Exception e) {
78              _log.error(e, e);
79          }
80  
81          return credentials;
82      }
83  
84      private static Log _log = LogFactoryUtil.getLog(
85          RequestHeaderAutoLogin.class);
86  
87  }