Guest User

Untitled

a guest
Feb 1st, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. <?php
  2. function get_groups($user) {
  3. // Active Directory server
  4. $ldap_host = "ad.domain";
  5.  
  6. // Active Directory DN, base path for our querying user
  7. $ldap_dn = "CN=Users,DC=ad,DC=domain";
  8.  
  9. // Active Directory user for querying
  10. $query_user = "jane@".$ldap_host;
  11. $password = "password1234!";
  12.  
  13. // Connect to AD
  14. $ldap = ldap_connect($ldap_host) or die("Could not connect to LDAP");
  15. ldap_bind($ldap,$query_user,$password) or die("Could not bind to LDAP");
  16.  
  17. // Search AD
  18. $results = ldap_search($ldap,$ldap_dn,"(samaccountname=$user)",array("memberof","primarygroupid"));
  19. $entries = ldap_get_entries($ldap, $results);
  20.  
  21. // No information found, bad user
  22. if($entries['count'] == 0) return false;
  23.  
  24. // Get groups and primary group token
  25. $output = $entries[0]['memberof'];
  26. $token = $entries[0]['primarygroupid'][0];
  27.  
  28. // Remove extraneous first entry
  29. array_shift($output);
  30.  
  31. // We need to look up the primary group, get list of all groups
  32. $results2 = ldap_search($ldap,$ldap_dn,"(objectcategory=group)",array("distinguishedname","primarygrouptoken"));
  33. $entries2 = ldap_get_entries($ldap, $results2);
  34.  
  35. // Remove extraneous first entry
  36. array_shift($entries2);
  37.  
  38. // Loop through and find group with a matching primary group token
  39. foreach($entries2 as $e) {
  40. if($e['primarygrouptoken'][0] == $token) {
  41. // Primary group found, add it to output array
  42. $output[] = $e['distinguishedname'][0];
  43. // Break loop
  44. break;
  45. }
  46. }
  47.  
  48. return $output;
  49. }
Add Comment
Please, Sign In to add comment