PHP code example of arvindh93 / cakephp-ldap-utility

1. Go to this page and download the library: Download arvindh93/cakephp-ldap-utility library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

arvindh93 / cakephp-ldap-utility example snippets


// In config/bootstrap.php
Plugin::load('LdapUtility');

	$config = [
		'host' => 'ldap.example.com',
        'port' => 389,
        'baseDn' => 'dc=example,dc=com',
        'startTLS' => true,
        'hideErrors' => true,
        'commonBindDn' => 'cn=readonly.user,ou=people,dc=example,dc=com',
        'commonBindPassword' => 'secret'
	]
	$ldapHandler = new LdapUtility\Ldap($config);

    // In your controller, for e.g. src/Api/AppController.php
    public function initialize()
    {
        parent::initialize();

        $this->loadComponent('Auth', [
            'storage' => 'Memory',
            'authenticate', [
                'LdapUtility.Ldap' => [
					'host' => 'ldap.example.com',
			        'port' => 389,
			        'baseDn' => 'dc=example,dc=com',
			        'startTLS' => true,
			        'hideErrors' => true,
			        'commonBindDn' => 'cn=readonly.user,ou=people,dc=example,dc=com',
			        'commonBindPassword' => 'secret',
			        'fields' => [
			            'username' => 'cn',
			            'suffix' => 'ou=people,dc=test,dc=com'
			        ]
				]
            ],

            'unauthorizedRedirect' => false,
            'checkAuthIn' => 'Controller.initialize',
        ]);
    }

	$ldapHandler->search()
		->setBaseDn('ou=people,dc=example,dc=com')
		->select(['cn', 'sn', 'mail'])
		->where('cn=test*')
		->all()

	$ldapHandler->search()
		->setBaseDn('ou=people,dc=example,dc=com')
		->select(['cn', 'sn', 'mail'])
		->where('cn=test*')
		->first()

	$ldapHandler->read()
		->setBaseDn('cn=test.user,ou=people,dc=example,dc=com')
		->select(['cn', 'sn', 'mail'])
		->where('cn=test.user')
		->first()