arvindh93 / cakephp-ldap-utility
Cakephp-ldap-utility plugin for CakePHP
Installs: 2
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:cakephp-plugin
Requires
- php: >=5.5.9
- cakephp/cakephp: >=3.2.10 <4.0.0
Requires (Dev)
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()