Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do I get All Active Directory Members of Nested Groups - PHP

Posted on 2008-10-17
2
Medium Priority
?
2,652 Views
Last Modified: 2013-12-24
In Active Directory I would like to find ALL physical users that are in the Group, for example:
OU=Distribution Lists,OU=My_OU,DC=my-connection,DC=ch

Obtaining one level is rather straightforward - I get physical users and more Groups if they are present. I am using 'member' property for that.
Problem starts with going deeper, in scenario when Group contains Groups (which can then contain other Groups or finally physical users I am looking for). I wrote a recursive function for that, however I am getting only physical users that are just  one level down from Group I started a search from. While debugging I see that Groups are being processed by recursive function but are not passed to function's RETURN variable; only one level users are passed to this variable. What am I doing wrong ?
/*
$members  Members(s) of a group
$ad  resource $link_identifier
*/
 
function check_ad_data($members, $ad) 
{
  // get rid of array's 'count' key => value so not to include it for iteration
  unset($members['count']);
  
  foreach($members as $member) {
    
    // get entries of member from Active Directory
    $attributes = array("objectGUID", "mail", "name", "objectClass", "member");
    $filter = "(mail=*)";
    $result = ldap_search($ad, $member, $filter, $attributes); 
    $entries = ldap_get_entries($ad, $result);
    
    
    // if member is the Person
    if($entries[0]['objectclass'][1] == 'person') {
      // append a person's name to array
      $users[] = $entries[0]['name'][0];
    }
    
    // if member is the Group
    if($entries[0]['objectclass'][1] == 'group') {
      // obtain all members of this group
      $membersOfGroup = $entries[0]['member'];    
      // call a recursive function
      check_ad_data($membersOfGroup, $ad);
    }
    
  }
 
  // return All the users
  return $users;
 
}

Open in new window

0
Comment
Question by:greeni
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 71

Accepted Solution

by:
Chris Dent earned 2000 total points
ID: 22739135

Don't you have to set the return value to a variable?

e.g.

$users = check_ad_data ...

And if that is the case, shouldn't you also be doing that here:

    if($entries[0]['objectclass'][1] == 'group') {
      ...
      $users = check_ad_data($membersOfGroup, $ad);
    }

Because if $users only exists for the lifetime of the function (rather than globally) it will only update within the function. Maybe...

Chris
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 22739178

It's worth mentioning that the reason I'm not entirely sure about the above is that for anything like this I would use a global variable / array to store the results of the function. But then, I'm much more into the AD / administration side than the coding side :)

Chris
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question