11

How to query multiple users from LDAP.

I am using DirContext.search(base,filter,scope); in my java program

as of now its working fine with one value filter. filter=("uid=name")

but my requirement is to pass multiple names to the filter at a time like

filter=("uid=name1,name2,name3....")  .
1

1 Answer 1

31

LDAP uses a "PREFIX" notation for its filters.

For example:

OR condition

(|(attr1=val1)(attr2=val2)(attr1=val2))

AND condition

(&(attr1=val1)(attr2=val2)(attr1=val2))

In your case, the filter criteria will be this:

filter = "(|(uid=name1)(uid=name2)(uid=name3))"

The above filter means:

Find any user who has uid=name1 OR uid=name2 OR uid=name3.

This should list you users whose user IDs are name1, name2 or name3.


More Exmples:

Equality: (attribute=abc) , e.g. (&(objectclass=user)(displayName=JohnDoe))

Negation: (!(attribute=abc)) , e.g. (!objectClass=group)

Presence: (attribute=*) , e.g. (mailNickName=*)

Absence: (!(attribute=*)) , e.g. (!proxyAddresses=*)

Greater than: (attribute>=abc) , e.g. (storageQuota>=100000)

Less than: (attribute<=abc) , e.g. (storageQuota<=100000)

Proximity: (attribute~=abc) , e.g. (displayName~=JohnDoe)

*(~= may not be compatible with all directory servers !!)

Wildcards: e.g. (sn=J*) or (mail=*@example.com) or (givenName=*John*)


Hope this helps!

Sign up to request clarification or add additional context in comments.

2 Comments

Is there a limit to how many ids that can be passed in? Would 2000 userids be unreasonable?
@HPWD I had a similar need and curiosity. I didn’t try 2000+ users but it worked with 468. From the first group query, through the second user-specific query with a massive OrFilter on each resulting member’s CN (plus a POJO mapper), I clocked 2.5 sec for the roundtrip. This was from local accessing a directory of potentially 100,000 entries

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.