Php – How to find all the LDAP groups that a user is part of, when groupOfUniqueNames is used to define groups

ldapPHP

When I connect to the LDAP server and retrieve the user, I get the correct user record, but I don't see any memberOf, isMemberOf or any other similar attribute that tells me what groups they are in:

$query = "(&(uid={$username})(objectClass=person))";
$result = ldap_search($ldapconnection, $context, $query);
$user = ldap_get_entries($ldapconnection, $result);

However, If I retrieve the LDAP group, then I can see a list of users that includes all the right ones:

$query = "(&({cn=".$groupname.")(objectClass=groupOfUniqueNames))";
$result = ldap_search($ldapconnection, $context, $query);
$group = ldap_get_entries($ldapconnection, $result); // Users in array attribute

The groups are dynamic groupOfUniqueNames ones, and each user is a uniqueMember of the group.

Am I missing something, or is the server not configured to show memberOf (MS AD)? Is there any way to get the memberships for a particular user without looping over every single group there is?

Best Answer

I'm not sure why you're not getting the memberOf attribute back from your queries, but you should be able to retrieve a user's group membership with something like this:

$query = "(&(objectClass=groupOfUniqueNames)(uniqueMember=" . $username . "))";

It looks like uniqueMember is not indexed by default (http://msdn.microsoft.com/en-us/library/windows/desktop/ms680520(v=vs.85).aspx), so if you have access to do so, and run into performance issues, it might be worth indexing it.

Related Topic