javaldapldap-queryphpldapadmin

How to get the Organization and OU of a user in LDAP


I am trying to query the group a user belongs to in LDAP. This is the structure of my directory.

What I need to achieve is to get the group the user belongs to. So here, I am expecting to get Group Two as user "Ola Torres" is member of that group.

User: uid:ola.torres

I have tried many queries but nothing has worked.

DC-MY_DC
|
O - Department One (Department)
  |- ou:groups
    | cn:Group One
    | cn: Group Two (Group)
       |member: Ola Torres
  |- ou:people
    |-cn: Ola Torres
O - Department Two
  | - ou: groups
     |-cn: Group Three
     |-cn: Group Four

These are my queries in java:

            SearchControls searchControl = new SearchControls();
            String[] attrs = { "cn" };
            searchControl.setReturningAttributes(attrs);
            searchControl.setSearchScope(SearchControls.SUBTREE_SCOPE);
            String criteria = "(&(objectClass=organizationalUnit)(ou:dn:=groups)(member=cn=ola.torres))";

I am receiving empty list. Please help. :(


Solution

  • For those who have encountered the same problem I had, it needs the complete member DN. I changed my search criteria to:

    (&(objectClass=organizationalUnit)(ou:dn:=groups)(member=cn=ola.torres, o=MY_DC,ou=people,dc=com, dc=company))
    

    It perfectly worked! :)