Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wildcard is not used now by default. User has to set it explicitly if needed #2277

Merged
merged 2 commits into from
May 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,12 +28,29 @@ public abstract class AbstractLdapProvider implements LdapProvider {

@Override
public List<LdapGroup> 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<String> 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
Expand Down