Advertisement
Guest User

Untitled

a guest
Apr 10th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. <?php
  2. namespace App\Auth;
  3.  
  4. use Cake\Auth\BaseAuthenticate;
  5. use Cake\Network\Request;
  6. use Cake\Network\Response;
  7.  
  8. class LdapAuthenticate extends BaseAuthenticate
  9. {
  10.  
  11. protected $_host = 'your_host';
  12.  
  13. public function authenticate(Request $request, Response $response)
  14. {
  15. $username = $request->data['username'];
  16. $password = $request->data['password'];
  17. $ds = @ldap_connect($this->_host);
  18. if (!$ds) {
  19. throw \Cake\Error\FatalErrorException('Unable to connect to LDAP host.');
  20. }
  21. $basedn = "cn=Users,dc=your_dc";
  22. $dn = "cn=$username, " . $basedn;
  23. $ldapbind = ldap_bind($ds, $dn, $password);
  24. if (!$ldapbind) {
  25. return false;
  26. }
  27.  
  28. $entry = ldap_first_entry($ldapbind);
  29. $attrs = ldap_get_attributes($ldapbind, $entry);
  30. $user = [];
  31. // Loop
  32. for ($i = 0; $i < $attrs["count"]; $i++) {
  33. $user[$attrs[$i]] = ldap_values($ldapbind, $entry, $attrs[$i])[0];
  34. }
  35. // Then close it and return the authenticated user
  36. ldap_unbind($ldapbind);
  37. return $user;
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement