Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- protected function findLdapUser($username, $password, $otheruser = false){
- $config = Configure::read('ldap');
- if(!$username && !$password && $otheruser){
- $username = $config['username'];
- $password = $config['password'];
- }
- if($password == ""){return false;} //prevent anonmyous bind
- if(!$otheruser){
- $otheruser = $username;
- }
- $connection = ldap_connect($config['host'], $config['port']);
- if($connection === false){ //does not detect properly depending on enviroment!
- debug("cannot connect to ldap server");
- return 0; //cannot connect!
- }
- ldap_set_option($connection, LDAP_OPT_PROTOCOL_VERSION, $config['version']);
- if (!@ldap_bind($connection, $username . $config['userpostfix'], $password)){
- return false;
- }
- //search for user data
- $fields = array('mail', 'name', 'telephoneNumber', 'physicalDeliveryOfficeName');
- //$filter = "sAMAccountName=" . $username;
- $filter = "userPrincipalName=" . $otheruser . $config['userpostfix'];
- $results = ldap_search($connection, "CN=USERS,".$config['basedn'], $filter, $fields);
- $info = ldap_get_entries($connection, $results);
- if($info['count'] == 0){return false;}
- @ldap_unbind($connection);
- $return['LdapUser']['email'] = $info[0]['mail'][0];
- $return['LdapUser']['fullname'] = $info[0]['name'][0];
- //supress warnings
- @$return['LdapUser']['office'] = $info[0]['physicaldeliveryofficename'][0];
- @$return['LdapUser']['phone'] = $info[0]['telephonenumber'][0];
- return $return;
- }
Add Comment
Please, Sign In to add comment