PHP code example of queencitycodefactory / ldap

1. Go to this page and download the library: Download queencitycodefactory/ldap 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/ */

    

queencitycodefactory / ldap example snippets


    use Ldap\Auth\LdapAuthenticate;

    public function initialize()
    {
        parent::initialize();
        $this->loadComponent('Flash');
        $this->loadComponent('Auth', [
            'loginAction' => [
                'controller' => 'Users',
                'action' => 'login'
            ],
            'authError' => 'Insufficient privileges to view requested resources. Please login to continue!',
            'authenticate' => [
                'Ldap.Ldap' => Configure::read('Ldap') + [
                    'fields' => [
                        'username' => 'username',
                        'password' => 'password'
                    ],
                    'flash' => [
                        'key' => 'ldap',
                        'element' => 'Flash/error',
                    ]
                ]
            ]
        ]);
    }

    /**
     * LDAP Configuration.
     *
     * Contains an array of settings to use for the LDAP configuration.
     *
     * ## Options
     *
     * - `domain` - The domain name to match against or auto complete so user isn't
     *    e returned when
     *    using closure.
     * - `baseDN` - The base DN for directory - Closure must be used here, the plugin
     *    is expecting a closure object to be set.
     * - `bindDN` - The bind DN for directory - Closure must be used here, the plugin
     *    is expecting a closure object to be set.
     * - `search` - The attribute to search against. Usually 'UserPrincipalName'
     * - `port` - The port to use. Default is 389 and is not st' => '127.0.0.1',
        'port' => 389,
        'search' => function($username) {
            return '(UserPrincipalName=' . $username . ')';
        },
        'searchAttributes' => ['*', 'memberof'],
        'baseDN' => function($username, $domain) {
            if (strpos($username, $domain) !== false) {
                $baseDN = 'OU=example,DC=domain,DC=local';
            } else {
                $baseDN = 'CN=Users,DC=domain,DC=local';
            }
            return $baseDN;
        },
        'bindDN' => function($username, $domain) {
            $bindDN = "CN=".$username.", OU=example";
            return $bindDN;
        },
        'errors' => [
            'data 773' => 'Some error for Flash',
            'data 532' => 'Some error for Flash',
        ],
        'logErrors' => true,
        'options' => [
            LDAP_OPT_NETWORK_TIMEOUT => 5,
            LDAP_OPT_PROTOCOL_VERSION => 3,
        ]
    ]