Cakephp-ldap-auth plugin for CakePHP
Requirements
Installation
You can install this plugin into your CakePHP application using composer., (*1)
The recommended way to install composer packages is:, (*2)
composer require arvindh93/Cakephp-ldap
Usage
In your app's config/bootstrap.php
add:, (*3)
// In config/bootstrap.php
Plugin::load('LdapUtility');
or using cake's console:, (*4)
./bin/cake plugin load LdapUtility
Configuration:
Basic configuration for creating ldap handler instance, (*5)
$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, (*6)
// 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(), (*7)
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, (*8)
$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, (*9)
$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, (*10)
$ldapHandler->read()
->setBaseDn('cn=test.user,ou=people,dc=example,dc=com')
->select(['cn', 'sn', 'mail'])
->where('cn=test.user')
->first()