Skip to content

Commit

Permalink
Add Search scope parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
dulanjalidilmi committed Apr 27, 2020
1 parent 0e3cf6b commit 50fb33e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
35 changes: 26 additions & 9 deletions src/main/java/org/wso2/carbon/connector/ldap/SearchEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,16 @@ 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[] = {};
String returnAttributesValue = (String) getParameter(messageContext, LDAPConstants.ATTRIBUTES);
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)) {
Expand All @@ -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<SearchResult> results = searchInUserBase(
dn, searchFilter, returnAttributes, SearchControls.SUBTREE_SCOPE, context, limit);
NamingEnumeration<SearchResult> results = searchInUserBase(dn, searchFilter, returnAttributes,
searchScope, context, limit);
SearchResult entityResult;
if (!onlyOneReference) {
if (results != null && results.hasMore()) {
Expand Down Expand Up @@ -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());
}
Expand All @@ -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();
Expand Down Expand Up @@ -251,7 +252,7 @@ private NamingEnumeration<SearchResult> 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);
Expand All @@ -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;
}
}
1 change: 1 addition & 0 deletions src/main/resources/ldap_entry/searchEntry.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
description="Boolean value whether to guarantee or not only one reference."/>
<parameter name="objectClass" description="The object class of the entry you need to search."/>
<parameter name="limit" description="Sets the limit for search results"/>
<parameter name="scope" description="The scope"/>
<parameter name="filters"
description="The keywords to use in the search. The keywords should be specified as
comma separated key-value pairs."/>
Expand Down

0 comments on commit 50fb33e

Please sign in to comment.