arvindh93/cakephp-ldap-utility

There is no license information available for the latest version (v1.0) of this package.

Cakephp-ldap-utility plugin for CakePHP

v1.0 2018-02-21 08:43 UTC

This package is not auto-updated.

Last update: 2024-05-02 06:05:31 UTC


README

Requirements

  • CakePHP 3.1+

Installation

You can install this plugin into your CakePHP application using composer.

The recommended way to install composer packages is:

composer require arvindh93/Cakephp-ldap

Usage

In your app's config/bootstrap.php add:

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

or using cake's console:

./bin/cake plugin load LdapUtility

Configuration:

Basic configuration for creating ldap handler instance

	$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);

Setup Ldap authentication config in Controller

    // 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',
        ]);
    }

Usage:

#Creating Query object for Search/Read operation: Search - $ldapHandler->search() Read - $ldapHandler->read()

#Operations on query object: select() - accepts an array of attributes to fetch from ldap entry setBaseDn() - accepts baseDn string defaults to config - baseDn where() - accepts filter string first() - execute the query and get the first entry details as array all() - executes the query and get all the possible entries as array

Example:

Search for entry with cn starting with test

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

Search for entry with cn starting with test and get first entry

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

Read a particular entry with cn=test.user

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