Search in sources :

Example 1 with LikeFilter

use of org.springframework.ldap.filter.LikeFilter in project gocd by gocd.

the class LdapUserSearch method search.

public List<User> search(String username, LdapConfig ldapConfig) {
    if (ldapConfig.getBasesConfig().isEmpty()) {
        throw new RuntimeException("Atleast one Search Base needs to be configured.");
    }
    OrFilter filter = new OrFilter();
    String searchString = MessageFormat.format("*{0}*", username);
    filter.or(new LikeFilter(SAM_ACCOUNT_NAME, searchString));
    filter.or(new LikeFilter(UID, searchString));
    filter.or(new LikeFilter(COMMON_NAME, searchString));
    filter.or(new LikeFilter(MAIL_ID, searchString));
    // This field is optional to search based on. Only for alias emails.
    filter.or(new LikeFilter(ALIAS_EMAIL_ID, searchString));
    //List ldapUserList = template.search(ldapConfig.searchBase(), filter.encode(), attributes);
    SearchControls controls = new SearchControls();
    controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    controls.setCountLimit(MAX_RESULTS);
    AttributesMapperCallbackHandler handler = getAttributesMapperCallbackHandler();
    for (BaseConfig baseConfig : ldapConfig.getBasesConfig()) {
        try {
            ldapTemplate.search(baseConfig.getValue(), filter.encode(), controls, handler);
        } catch (org.springframework.ldap.LimitExceededException e) {
            throw new NotAllResultsShownException(buildUserList(handler.getList()));
        }
    }
    return buildUserList(handler.getList());
}
Also used : LikeFilter(org.springframework.ldap.filter.LikeFilter) SearchControls(javax.naming.directory.SearchControls) OrFilter(org.springframework.ldap.filter.OrFilter) BaseConfig(com.thoughtworks.go.config.server.security.ldap.BaseConfig) AttributesMapperCallbackHandler(org.springframework.ldap.core.AttributesMapperCallbackHandler)

Aggregations

BaseConfig (com.thoughtworks.go.config.server.security.ldap.BaseConfig)1 SearchControls (javax.naming.directory.SearchControls)1 AttributesMapperCallbackHandler (org.springframework.ldap.core.AttributesMapperCallbackHandler)1 LikeFilter (org.springframework.ldap.filter.LikeFilter)1 OrFilter (org.springframework.ldap.filter.OrFilter)1