Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Auth;
- use Cake\Auth\BaseAuthenticate;
- use Cake\Network\Request;
- use Cake\Network\Response;
- class LdapAuthenticate extends BaseAuthenticate
- {
- protected $_host = 'your_host';
- public function authenticate(Request $request, Response $response)
- {
- $username = $request->data['username'];
- $password = $request->data['password'];
- $ds = @ldap_connect($this->_host);
- if (!$ds) {
- throw \Cake\Error\FatalErrorException('Unable to connect to LDAP host.');
- }
- $basedn = "cn=Users,dc=your_dc";
- $dn = "cn=$username, " . $basedn;
- $ldapbind = ldap_bind($ds, $dn, $password);
- if (!$ldapbind) {
- return false;
- }
- $entry = ldap_first_entry($ldapbind);
- $attrs = ldap_get_attributes($ldapbind, $entry);
- $user = [];
- // Loop
- for ($i = 0; $i < $attrs["count"]; $i++) {
- $user[$attrs[$i]] = ldap_values($ldapbind, $entry, $attrs[$i])[0];
- }
- // Then close it and return the authenticated user
- ldap_unbind($ldapbind);
- return $user;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement