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.SystemException;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
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.security.ldap.PortalLDAPUtil;
25  import com.liferay.portal.service.UserLocalServiceUtil;
26  import com.liferay.portal.util.PortalUtil;
27  import com.liferay.portal.util.PrefsPropsUtil;
28  import com.liferay.portal.util.PropsValues;
29  
30  import javax.servlet.http.HttpServletRequest;
31  import javax.servlet.http.HttpServletResponse;
32  
33  /**
34   * <a href="SiteMinderAutoLogin.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Mika Koivisto
37   * @author Wesley Gong
38   */
39  public class SiteMinderAutoLogin extends CASAutoLogin {
40  
41      public String[] login(
42              HttpServletRequest request, HttpServletResponse response)
43          throws AutoLoginException {
44  
45          String[] credentials = null;
46  
47          try {
48              long companyId = PortalUtil.getCompanyId(request);
49  
50              if (!PortalLDAPUtil.isSiteMinderEnabled(companyId)) {
51                  return credentials;
52              }
53  
54              String screenName = request.getHeader(
55                  PrefsPropsUtil.getString(
56                      companyId, PropsKeys.SITEMINDER_USER_HEADER,
57                      PropsValues.SITEMINDER_USER_HEADER));
58  
59              if (Validator.isNull(screenName)) {
60                  return credentials;
61              }
62  
63              User user = null;
64  
65              if (PrefsPropsUtil.getBoolean(
66                      companyId, PropsKeys.SITEMINDER_IMPORT_FROM_LDAP,
67                      PropsValues.SITEMINDER_IMPORT_FROM_LDAP)) {
68  
69                  try {
70                      user = importLDAPUser(
71                          companyId, StringPool.BLANK, screenName);
72                  }
73                  catch (SystemException se) {
74                  }
75              }
76  
77              if (user == null) {
78                  user = UserLocalServiceUtil.getUserByScreenName(
79                      companyId, screenName);
80              }
81  
82              credentials = new String[3];
83  
84              credentials[0] = String.valueOf(user.getUserId());
85              credentials[1] = user.getPassword();
86              credentials[2] = Boolean.TRUE.toString();
87  
88              return credentials;
89          }
90          catch (Exception e) {
91              _log.error(e, e);
92          }
93  
94          return credentials;
95      }
96  
97      private static Log _log = LogFactoryUtil.getLog(SiteMinderAutoLogin.class);
98  
99  }