diff --git a/src/main/java/org/ohdsi/webapi/user/importer/providers/AbstractLdapProvider.java b/src/main/java/org/ohdsi/webapi/user/importer/providers/AbstractLdapProvider.java index a8d58621e0..d82532e9ab 100644 --- a/src/main/java/org/ohdsi/webapi/user/importer/providers/AbstractLdapProvider.java +++ b/src/main/java/org/ohdsi/webapi/user/importer/providers/AbstractLdapProvider.java @@ -8,11 +8,15 @@ import org.springframework.ldap.core.CollectingNameClassPairCallbackHandler; import org.springframework.ldap.core.LdapTemplate; import org.springframework.ldap.filter.AndFilter; -import org.springframework.ldap.filter.WhitespaceWildcardsFilter; +import org.springframework.ldap.filter.EqualsFilter; +import org.springframework.ldap.support.LdapEncoder; import org.springframework.ldap.support.LdapUtils; +import java.util.Arrays; import java.util.List; import java.util.function.Supplier; +import java.util.stream.Collectors; +import java.util.stream.IntStream; import static org.ohdsi.webapi.user.importer.providers.OhdsiLdapUtils.getCriteria; import static org.ohdsi.webapi.user.importer.providers.OhdsiLdapUtils.valueAsString; @@ -24,12 +28,29 @@ public abstract class AbstractLdapProvider implements LdapProvider { @Override public List findGroups(String searchStr) { - LdapTemplate ldapTemplate = getLdapTemplate(); - AndFilter filter = new AndFilter(); -// filter.and(getCriteria(OBJECTCLASS_ATTR, getGroupClasses())); - filter.and(new WhitespaceWildcardsFilter(CN_ATTR, searchStr)); - return ldapTemplate.search(LdapUtils.emptyLdapName(), filter.encode(), getAttributesMapper(LdapGroup::new)); + return ldapTemplate.search(LdapUtils.emptyLdapName(), getFilterString(searchStr), getAttributesMapper(LdapGroup::new)); + } + + private String getFilterString(String searchString) { + StringBuffer buff = new StringBuffer(); + buff.append('('); + buff.append(CN_ATTR).append("=").append(encodeSearchString(searchString)); + buff.append(')'); + return buff.toString(); + } + + private String encodeSearchString(String searchString) { + String wildCard = "*"; + + if (searchString.isEmpty() || wildCard.equals(searchString)) return searchString; // nothing to encode + + List tokens = Arrays.asList(StringUtils.split(searchString, wildCard)); + tokens.replaceAll(LdapEncoder::filterEncode); + String encodedSearchString = (searchString.startsWith(wildCard) ? wildCard : "") + + StringUtils.join(tokens, wildCard) + + (searchString.endsWith(wildCard) ? wildCard : ""); + return encodedSearchString; } @Override