org.springframework.ldap.filter.HardcodedFilter Java Examples

The following examples show how to use org.springframework.ldap.filter.HardcodedFilter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LdapManagerImpl.java    From zstack with Apache License 2.0 6 votes vote down vote up
private void handle(APIGetCandidateLdapEntryForBindingMsg msg) {
    APIGetLdapEntryReply reply = new APIGetLdapEntryReply();

    AndFilter andFilter = new AndFilter();
    andFilter.and(new HardcodedFilter(msg.getLdapFilter()));

    List<String> boundLdapEntryList = Q.New(LdapAccountRefVO.class)
            .select(LdapAccountRefVO_.ldapUid)
            .listValues();

    List<Object> result = ldapUtil.searchLdapEntry(andFilter.toString(), msg.getLimit(), new ResultFilter() {
        @Override
        public boolean needSelect(String dn) {
            return !boundLdapEntryList.contains(dn);
        }
    });

    reply.setInventories(result);

    bus.reply(msg, reply);
}
 
Example #2
Source File: UserServiceImpl.java    From cxf with Apache License 2.0 5 votes vote down vote up
private static Map<String, Attribute> getAttributesOfEntry(LdapTemplate ldapTemplate, String baseDN,
                                                           String objectClass, String searchFilter,
                                                           String[] searchAttributes) {

    Map<String, Attribute> ldapAttributes = null;

    AttributesMapper<Map<String, Attribute>> mapper =
        new AttributesMapper<Map<String, Attribute>>() {
        public Map<String, Attribute> mapFromAttributes(Attributes attrs) throws NamingException {
            Map<String, Attribute> map = new HashMap<>();
            NamingEnumeration<? extends Attribute> attrEnum = attrs.getAll();
            while (attrEnum.hasMore()) {
                Attribute att = attrEnum.next();
                map.put(att.getID(), att);
            }
            return map;
        }
    };

    AndFilter filter = new AndFilter();
    filter.and(new EqualsFilter("objectclass", objectClass)).and(new HardcodedFilter(searchFilter));

    List<?> result = ldapTemplate.search((baseDN == null) ? "" : baseDN, filter.toString(),
        SearchControls.SUBTREE_SCOPE, searchAttributes, mapper);
    if (result != null && !result.isEmpty()) {
        ldapAttributes = CastUtils.cast((Map<?, ?>)result.get(0));
    }

    return ldapAttributes;
}
 
Example #3
Source File: HardcodedFilterIntegrationTest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Test
public void verifyThatWildcardsAreUnescaped() {
	HardcodedFilter filter = new HardcodedFilter("cn=Some*");
	CountNameClassPairCallbackHandler handler = new CountNameClassPairCallbackHandler();
	ldapTemplate.search(LdapUtils.emptyLdapName(), filter.encode(), handler);
	int hits = handler.getNoOfRows();
	assertThat(hits > 1).isTrue();
}
 
Example #4
Source File: LdapManagerImpl.java    From zstack with Apache License 2.0 4 votes vote down vote up
@Transactional
private void handle(APICleanInvalidLdapBindingMsg msg) {
    APICleanInvalidLdapBindingEvent evt = new APICleanInvalidLdapBindingEvent(msg.getId());

    SimpleQuery<LdapAccountRefVO> sq = dbf.createQuery(LdapAccountRefVO.class);
    List<LdapAccountRefVO> refList = sq.list();
    if(refList == null || refList.isEmpty()){
        bus.publish(evt);
        return;
    }

    ArrayList<String> accountUuidList = new ArrayList<>();
    ArrayList<String> ldapAccountRefUuidList = new ArrayList<>();
    LdapTemplateContextSource ldapTemplateContextSource = ldapUtil.readLdapServerConfiguration();

    for (LdapAccountRefVO ldapAccRefVO : refList) {
        // no data in ldap
        String ldapDn = ldapAccRefVO.getLdapUid();
        if(!ldapUtil.validateDnExist(ldapTemplateContextSource, ldapDn)){
            accountUuidList.add(ldapAccRefVO.getAccountUuid());
            ldapAccountRefUuidList.add(ldapAccRefVO.getUuid());
            continue;
        }

        // filter
        String filter = LdapSystemTags.LDAP_CLEAN_BINDING_FILTER.getTokenByResourceUuid(ldapAccRefVO.getLdapServerUuid(), LdapSystemTags.LDAP_CLEAN_BINDING_FILTER_TOKEN);
        if(StringUtils.isEmpty(filter)){
            continue;
        }

        HardcodedFilter hardcodedFilter = new HardcodedFilter(filter);
        if(ldapUtil.validateDnExist(ldapTemplateContextSource, ldapDn, hardcodedFilter)){
            accountUuidList.add(ldapAccRefVO.getAccountUuid());
            ldapAccountRefUuidList.add(ldapAccRefVO.getUuid());
        }
    }

    if (!accountUuidList.isEmpty()) {
        // remove ldap bindings
        dbf.removeByPrimaryKeys(ldapAccountRefUuidList, LdapAccountRefVO.class);
        // return accounts of which ldap bindings had been removed
        SimpleQuery<AccountVO> sq1 = dbf.createQuery(AccountVO.class);
        sq1.add(AccountVO_.uuid, SimpleQuery.Op.IN, accountUuidList);
        evt.setInventories(AccountInventory.valueOf(sq1.list()));
    }

    bus.publish(evt);
}
 
Example #5
Source File: HardcodedFilterIntegrationTest.java    From spring-ldap with Apache License 2.0 4 votes vote down vote up
@Test
public void verifyThatFilterEditorWorks() {
	Filter filter = dummyFilterConsumer.getFilter();
	assertThat(filter instanceof HardcodedFilter).isTrue();
	assertThat(filter.toString()).isEqualTo("(&(objectclass=person)(!(objectclass=computer))");
}
 
Example #6
Source File: LdapQueryBuilder.java    From spring-ldap with Apache License 2.0 2 votes vote down vote up
/**
 * Specify a hardcoded filter. Please note that using this method, the filter string will not be
 * validated or escaped in any way. <b>Never</b> use direct user input and use it concatenating strings
 * to use as LDAP filters. Doing so opens up for &quot;LDAP injection&quot;, where malicious user
 * may inject specifically constructed data to form filters at their convenience. When user input is used
 * consider using {@link #where(String)} or {@link #filter(String, Object...)} instead.
 *
 * @param hardcodedFilter The hardcoded filter string to use in the search.
 * @return this instance.
 * @throws IllegalStateException if a filter has already been specified.
 */
public LdapQuery filter(String hardcodedFilter) {
    initRootContainer();
    rootContainer.append(new HardcodedFilter(hardcodedFilter));
    return this;
}