1 package com.liferay.portal.security.ldap;
3 import java.util.Properties;
5 import javax.naming.Context;
6 import javax.naming.NamingEnumeration;
7 import javax.naming.NamingException;
8 import javax.naming.directory.Attributes;
9 import javax.naming.directory.SearchControls;
10 import javax.naming.directory.SearchResult;
11 import javax.naming.ldap.InitialLdapContext;
12 import javax.naming.ldap.LdapContext;
14 import com.liferay.portal.kernel.util.PropsKeys;
15 import com.liferay.portal.kernel.util.StringPool;
16 import com.liferay.portal.model.User;
17 import com.liferay.portal.service.UserLocalServiceUtil;
18 import com.liferay.portal.util.PrefsPropsUtil;
20 public class LDAPLocalServiceImpl implements LDAPLocalService {
22 public static String baseDN = "";
23 public static String peoplebase = null;
25 public static LdapContext ctx = null;
27 public static LdapContext getContext(long companyId) throws Exception {
29 String baseProviderURL = PrefsPropsUtil.getString(companyId,
30 PropsKeys.LDAP_BASE_PROVIDER_URL);
31 String pricipal = PrefsPropsUtil.getString(companyId,
32 PropsKeys.LDAP_SECURITY_PRINCIPAL);
33 String credentials = PrefsPropsUtil.getString(companyId,
34 PropsKeys.LDAP_SECURITY_CREDENTIALS);
37 baseDN = PrefsPropsUtil.getString(companyId, PropsKeys.LDAP_BASE_DN);
39 return getContext(companyId, baseProviderURL, pricipal, credentials);
42 public static LdapContext getContext(long companyId, String providerURL,
43 String pricipal, String credentials) throws Exception {
49 Properties env = new Properties();
51 env.put(Context.INITIAL_CONTEXT_FACTORY, PrefsPropsUtil.getString(
52 companyId, PropsKeys.LDAP_FACTORY_INITIAL));
53 env.put(Context.PROVIDER_URL, providerURL);
54 env.put(Context.SECURITY_PRINCIPAL, pricipal);
55 env.put(Context.SECURITY_CREDENTIALS, credentials);
57 env.put("com.sun.jndi.ldap.connect.pool", "true");
58 env.put("com.sun.jndi.ldap.connect.pool.maxsize", "50");
59 env.put("com.sun.jndi.ldap.connect.pool.timeout", "100000");
62 ctx = new InitialLdapContext(env, null);
63 } catch (Exception e) {
67 if (peoplebase == null) {
74 public static void initPeopleBase(LdapContext ctx) {
76 SearchControls cons = new SearchControls(SearchControls.SUBTREE_SCOPE,
77 5, 0, null, false, false);
80 NamingEnumeration<SearchResult> l = ctx.search(baseDN,
81 "(objectClass=ENTPerson)", cons);
83 String dn = l.next().getNameInNamespace();
84 peoplebase = dn.substring(dn.indexOf(",") + 1, dn.length());
86 } catch (NamingException e) {
91 public Attributes getUserAttibutes(long companyId, String attribute, String value)
94 NamingEnumeration<SearchResult> enu = null;
97 SearchControls cons = new SearchControls(SearchControls.SUBTREE_SCOPE,
98 100000, 0, null, false, false);
100 ctx = getContext(companyId);
102 StringBuilder filter = new StringBuilder();
104 filter.append(StringPool.OPEN_PARENTHESIS);
105 filter.append(StringPool.AMPERSAND);
107 filter.append(StringPool.OPEN_PARENTHESIS);
108 filter.append("objectClass");
109 filter.append(StringPool.EQUAL);
110 filter.append("ENTPerson");
111 filter.append(StringPool.CLOSE_PARENTHESIS);
113 filter.append(StringPool.OPEN_PARENTHESIS);
114 filter.append(attribute);
115 filter.append(StringPool.EQUAL);
116 filter.append(value);
117 filter.append(StringPool.CLOSE_PARENTHESIS);
119 filter.append(StringPool.CLOSE_PARENTHESIS);
121 enu = ctx.search(peoplebase, filter.toString(), cons);
122 } catch (Exception e) {
131 if (enu.hasMoreElements()) {
132 SearchResult searchResult = enu.next();
134 return searchResult.getAttributes();
140 public String getUserAttribute(long companyId, Attributes attributes, String attribute)
143 if (attributes.get(attribute) != null) {
144 return attributes.get(attribute).get().toString();
150 public User getUser(long companyId, String attribute, String value) throws Exception{
151 Attributes attributes = getUserAttibutes(companyId, attribute, value);
152 String login = getUserAttribute(companyId, attributes, "ENTPersonLogin");
154 return UserLocalServiceUtil.getUserByScreenName(companyId, login);