diff --git a/src/main/java/org/wso2/carbon/connector/ldap/LDAPConstants.java b/src/main/java/org/wso2/carbon/connector/ldap/LDAPConstants.java index dc42538..c0d30c2 100644 --- a/src/main/java/org/wso2/carbon/connector/ldap/LDAPConstants.java +++ b/src/main/java/org/wso2/carbon/connector/ldap/LDAPConstants.java @@ -30,6 +30,7 @@ public class LDAPConstants { public static final String LIMIT = "limit"; public static final String ATTRIBUTES = "attributes"; public static final String DN = "dn"; + public static final String SCOPE = "scope"; public static final String RESULT = "result"; public static final String MESSAGE = "message"; public static final String SUCCESS = "Success"; diff --git a/src/main/java/org/wso2/carbon/connector/ldap/SearchEntry.java b/src/main/java/org/wso2/carbon/connector/ldap/SearchEntry.java index 590829b..02083db 100644 --- a/src/main/java/org/wso2/carbon/connector/ldap/SearchEntry.java +++ b/src/main/java/org/wso2/carbon/connector/ldap/SearchEntry.java @@ -47,6 +47,7 @@ public class SearchEntry extends AbstractConnector { @Override public void connect(MessageContext messageContext) { + String objectClass = (String) getParameter(messageContext, LDAPConstants.OBJECT_CLASS); String filter = (String) getParameter(messageContext, LDAPConstants.FILTERS); String dn = (String) getParameter(messageContext, LDAPConstants.DN); String returnAttributes[] = {}; @@ -54,7 +55,8 @@ public void connect(MessageContext messageContext) { if (!(returnAttributesValue == null || returnAttributesValue.isEmpty())) { returnAttributes = returnAttributesValue.split(","); } - + String scope = (String) getParameter(messageContext, LDAPConstants.SCOPE); + int searchScope = getSearchScope(scope); int limit = 0; String searchLimit = (String) getParameter(messageContext, LDAPConstants.LIMIT); if (!StringUtils.isEmpty(searchLimit)) { @@ -73,10 +75,10 @@ public void connect(MessageContext messageContext) { OMElement result = factory.createOMElement(LDAPConstants.RESULT, ns); try { DirContext context = LDAPUtils.getDirectoryContext(messageContext); - String searchFilter = generateSearchFilter(filter, messageContext); + String searchFilter = generateSearchFilter(objectClass, filter, messageContext); try { - NamingEnumeration results = searchInUserBase( - dn, searchFilter, returnAttributes, SearchControls.SUBTREE_SCOPE, context, limit); + NamingEnumeration results = searchInUserBase(dn, searchFilter, returnAttributes, + searchScope, context, limit); SearchResult entityResult; if (!onlyOneReference) { if (results != null && results.hasMore()) { @@ -149,7 +151,7 @@ private OMElement prepareNode(SearchResult entityResult, OMFactory factory, OMNa String value = ""; if (elementType.equals("class java.lang.String")) { value = element.toString(); - } else if(elementType.equals("class [B")) { + } else if (elementType.equals("class [B")) { Attribute attributeValue = attributes.get(id); value = new String((byte[]) attributeValue.get()); } @@ -168,8 +170,7 @@ private OMElement prepareNode(SearchResult entityResult, OMFactory factory, OMNa returnAttributes[i] = splitResult[0]; } if (attribute != null) { - NamingEnumeration ne = null; - ne = attribute.getAll(); + NamingEnumeration ne = attribute.getAll(); while (ne.hasMoreElements()) { Object element = ne.next(); String elementType = element.getClass().toString(); @@ -251,7 +252,7 @@ private NamingEnumeration searchInUserBase(String dn, String searc } - private String generateSearchFilter(String filter, MessageContext messageContext) { + private String generateSearchFilter(String objectClass, String filter, MessageContext messageContext) { String attrFilter = ""; try { JSONObject object = new JSONObject(filter); @@ -265,6 +266,22 @@ private String generateSearchFilter(String filter, MessageContext messageContext } catch (JSONException e) { handleException("Error while passing the JSON object", e, messageContext); } - return attrFilter; + if (objectClass != null && !objectClass.isEmpty()) { + return "(&(objectClass=" + objectClass + ")" + attrFilter + ")"; + } else { + return attrFilter; + } + } + + private int getSearchScope(String scope) { + int searchScope = 2; + if(scope != null && !scope.isEmpty()) { + if (scope.equalsIgnoreCase("OBJECT")) { + searchScope = 0; + } else if (scope.equalsIgnoreCase("ONE_LEVEL")) { + searchScope = 1; + } + } + return searchScope; } } diff --git a/src/main/resources/ldap_entry/searchEntry.xml b/src/main/resources/ldap_entry/searchEntry.xml index 9fa1e69..271aa4e 100644 --- a/src/main/resources/ldap_entry/searchEntry.xml +++ b/src/main/resources/ldap_entry/searchEntry.xml @@ -21,6 +21,7 @@ description="Boolean value whether to guarantee or not only one reference."/> +