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.exception.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.LDAPSettingsUtil;
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  
44          String[] credentials = null;
45  
46          try {
47              long companyId = PortalUtil.getCompanyId(request);
48  
49              if (!LDAPSettingsUtil.isSiteMinderEnabled(companyId)) {
50                  return credentials;
51              }
52  
53              String screenName = request.getHeader(
54                  PrefsPropsUtil.getString(
55                      companyId, PropsKeys.SITEMINDER_USER_HEADER,
56                      PropsValues.SITEMINDER_USER_HEADER));
57  
58              if (Validator.isNull(screenName)) {
59                  return credentials;
60              }
61  
62              User user = null;
63  
64              if (PrefsPropsUtil.getBoolean(
65                      companyId, PropsKeys.SITEMINDER_IMPORT_FROM_LDAP,
66                      PropsValues.SITEMINDER_IMPORT_FROM_LDAP)) {
67  
68                  try {
69                      user = importLDAPUser(
70                          companyId, StringPool.BLANK, screenName);
71                  }
72                  catch (SystemException se) {
73                  }
74              }
75  
76              if (user == null) {
77                  user = UserLocalServiceUtil.getUserByScreenName(
78                      companyId, screenName);
79              }
80  
81              credentials = new String[3];
82  
83              credentials[0] = String.valueOf(user.getUserId());
84              credentials[1] = user.getPassword();
85              credentials[2] = Boolean.TRUE.toString();
86  
87              return credentials;
88          }
89          catch (Exception e) {
90              _log.error(e, e);
91          }
92  
93          return credentials;
94      }
95  
96      private static Log _log = LogFactoryUtil.getLog(SiteMinderAutoLogin.class);
97  
98  }