--- /dev/null
+package com.liferay.portal.security.ldap;
+
+import java.util.Properties;
+
+import javax.naming.Context;
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+import javax.naming.directory.Attributes;
+import javax.naming.directory.SearchControls;
+import javax.naming.directory.SearchResult;
+import javax.naming.ldap.InitialLdapContext;
+import javax.naming.ldap.LdapContext;
+
+import com.liferay.portal.kernel.util.PropsKeys;
+import com.liferay.portal.kernel.util.StringPool;
+import com.liferay.portal.model.User;
+import com.liferay.portal.service.UserLocalServiceUtil;
+import com.liferay.portal.util.PrefsPropsUtil;
+
+public class LDAPLocalServiceImpl implements LDAPLocalService {
+
+ public static String baseDN = "";
+ public static String peoplebase = null;
+
+ public static LdapContext ctx = null;
+
+ public static LdapContext getContext(long companyId) throws Exception {
+
+ String baseProviderURL = PrefsPropsUtil.getString(companyId,
+ PropsKeys.LDAP_BASE_PROVIDER_URL);
+ String pricipal = PrefsPropsUtil.getString(companyId,
+ PropsKeys.LDAP_SECURITY_PRINCIPAL);
+ String credentials = PrefsPropsUtil.getString(companyId,
+ PropsKeys.LDAP_SECURITY_CREDENTIALS);
+
+
+ baseDN = PrefsPropsUtil.getString(companyId, PropsKeys.LDAP_BASE_DN);
+
+ return getContext(companyId, baseProviderURL, pricipal, credentials);
+ }
+
+ public static LdapContext getContext(long companyId, String providerURL,
+ String pricipal, String credentials) throws Exception {
+
+ if (ctx != null) {
+ return ctx;
+ }
+
+ Properties env = new Properties();
+
+ env.put(Context.INITIAL_CONTEXT_FACTORY, PrefsPropsUtil.getString(
+ companyId, PropsKeys.LDAP_FACTORY_INITIAL));
+ env.put(Context.PROVIDER_URL, providerURL);
+ env.put(Context.SECURITY_PRINCIPAL, pricipal);
+ env.put(Context.SECURITY_CREDENTIALS, credentials);
+
+ env.put("com.sun.jndi.ldap.connect.pool", "true");
+ env.put("com.sun.jndi.ldap.connect.pool.maxsize", "50");
+ env.put("com.sun.jndi.ldap.connect.pool.timeout", "100000");
+
+ try {
+ ctx = new InitialLdapContext(env, null);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ if (peoplebase == null) {
+ initPeopleBase(ctx);
+ }
+
+ return ctx;
+ }
+
+ public static void initPeopleBase(LdapContext ctx) {
+
+ SearchControls cons = new SearchControls(SearchControls.SUBTREE_SCOPE,
+ 5, 0, null, false, false);
+
+ try {
+ NamingEnumeration<SearchResult> l = ctx.search(baseDN,
+ "(objectClass=ENTPerson)", cons);
+
+ String dn = l.next().getNameInNamespace();
+ peoplebase = dn.substring(dn.indexOf(",") + 1, dn.length());
+
+ } catch (NamingException e) {
+ e.printStackTrace();
+ }
+ }
+
+ public Attributes getUserAttibutes(long companyId, String attribute, String value)
+ throws Exception {
+
+ NamingEnumeration<SearchResult> enu = null;
+
+ try {
+ SearchControls cons = new SearchControls(SearchControls.SUBTREE_SCOPE,
+ 100000, 0, null, false, false);
+
+ ctx = getContext(companyId);
+
+ StringBuilder filter = new StringBuilder();
+
+ filter.append(StringPool.OPEN_PARENTHESIS);
+ filter.append(StringPool.AMPERSAND);
+
+ filter.append(StringPool.OPEN_PARENTHESIS);
+ filter.append("objectClass");
+ filter.append(StringPool.EQUAL);
+ filter.append("ENTPerson");
+ filter.append(StringPool.CLOSE_PARENTHESIS);
+
+ filter.append(StringPool.OPEN_PARENTHESIS);
+ filter.append(attribute);
+ filter.append(StringPool.EQUAL);
+ filter.append(value);
+ filter.append(StringPool.CLOSE_PARENTHESIS);
+
+ filter.append(StringPool.CLOSE_PARENTHESIS);
+
+ enu = ctx.search(peoplebase, filter.toString(), cons);
+ } catch (Exception e) {
+ throw e;
+ } finally {
+ if (ctx != null) {
+ ctx.close();
+ ctx = null;
+ }
+ }
+
+ if (enu.hasMoreElements()) {
+ SearchResult searchResult = enu.next();
+ enu.close();
+ return searchResult.getAttributes();
+ } else {
+ return null;
+ }
+ }
+
+ public String getUserAttribute(long companyId, Attributes attributes, String attribute)
+ throws Exception {
+
+ if (attributes.get(attribute) != null) {
+ return attributes.get(attribute).get().toString();
+ }
+
+ return "";
+ }
+
+ public User getUser(long companyId, String attribute, String value) throws Exception{
+ Attributes attributes = getUserAttibutes(companyId, attribute, value);
+ String login = getUserAttribute(companyId, attributes, "ENTPersonLogin");
+ try {
+ return UserLocalServiceUtil.getUserByScreenName(companyId, login);
+ }
+ catch(Exception e){
+ return null;
+ }
+ }
+
+}