Guest User

Untitled

a guest
Jun 17th, 2018
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. protected function findLdapUser($username, $password, $otheruser = false){
  2. $config = Configure::read('ldap');
  3. if(!$username && !$password && $otheruser){
  4. $username = $config['username'];
  5. $password = $config['password'];
  6. }
  7. if($password == ""){return false;} //prevent anonmyous bind
  8. if(!$otheruser){
  9. $otheruser = $username;
  10. }
  11. $connection = ldap_connect($config['host'], $config['port']);
  12. if($connection === false){ //does not detect properly depending on enviroment!
  13. debug("cannot connect to ldap server");
  14. return 0; //cannot connect!
  15. }
  16. ldap_set_option($connection, LDAP_OPT_PROTOCOL_VERSION, $config['version']);
  17. if (!@ldap_bind($connection, $username . $config['userpostfix'], $password)){
  18. return false;
  19. }
  20.  
  21. //search for user data
  22. $fields = array('mail', 'name', 'telephoneNumber', 'physicalDeliveryOfficeName');
  23. //$filter = "sAMAccountName=" . $username;
  24. $filter = "userPrincipalName=" . $otheruser . $config['userpostfix'];
  25. $results = ldap_search($connection, "CN=USERS,".$config['basedn'], $filter, $fields);
  26. $info = ldap_get_entries($connection, $results);
  27. if($info['count'] == 0){return false;}
  28. @ldap_unbind($connection);
  29. $return['LdapUser']['email'] = $info[0]['mail'][0];
  30. $return['LdapUser']['fullname'] = $info[0]['name'][0];
  31. //supress warnings
  32. @$return['LdapUser']['office'] = $info[0]['physicaldeliveryofficename'][0];
  33. @$return['LdapUser']['phone'] = $info[0]['telephonenumber'][0];
  34. return $return;
  35. }
Add Comment
Please, Sign In to add comment