dbp/relay-base-person-connector-ldap-bundle
Install the Bundle
composer require dbp/relay-base-person-connector-ldap-bundle
Ensure digital-blueprint/relay-base-person-bundle is also installed (dependency).
Configure the Bundle
Add to config/packages/dbp_relay_base_person_connector_ldap.yaml:
dbp_relay_base_person_connector_ldap:
host: 'ldap://your-ldap-server'
port: 389
base_dn: 'dc=example,dc=com'
username: 'cn=admin,dc=example,dc=com'
password: '%env(LDAP_PASSWORD)%'
user_search_filter: '(uid=%s)'
attributes_mapping:
first_name: 'givenName'
last_name: 'sn'
email: 'mail'
Secure credentials via .env (e.g., LDAP_PASSWORD=yourpassword).
Register the Connector
In a service file (e.g., config/services.yaml), bind the LDAP connector to the DbpRelayBasePersonBundle's person_connector service:
services:
App\Service\LdapPersonConnector:
tags:
- { name: 'dbp_relay_base_person.connector', priority: 100 }
First Use Case: Fetch a User
Inject the PersonConnectorInterface into a service and call:
$person = $this->personConnector->findByEmail('user@example.com');
Verify LDAP logs for connectivity issues.
User Provisioning
PersonConnectorInterface to sync LDAP users to your app:
$ldapUser = $this->personConnector->findByAttribute('uid', 'jdoe');
$this->userRepository->upsertFromLdap($ldapUser);
DbpRelayBasePersonBundle's Person entity to include LDAP-specific fields (e.g., ldapDn).Authentication Integration
LdapAuthenticator for SSO:
use Symfony\Component\Security\Http\Authenticator\LdapAuthenticator;
// Configure in security.yaml
firewalls:
main:
ldap: ~
ROLE_LDAP_GROUP_* in the authenticator.Batch Sync
use Symfony\Component\Console\Command\Command;
class SyncLdapUsersCommand extends Command {
protected function execute(InputInterface $input, OutputInterface $output) {
$users = $this->personConnector->search('(objectClass=person)');
foreach ($users as $user) {
$this->userRepository->saveFromLdap($user);
}
}
}
DbpRelayBasePersonBundle's PersonUpdatedEvent to trigger LDAP updates:
use Dbp\RelayBasePersonBundle\Event\PersonUpdatedEvent;
class LdapSyncListener {
public function onPersonUpdated(PersonUpdatedEvent $event) {
$this->ldapUpdater->syncToLdap($event->getPerson());
}
}
# config/packages/cache.yaml
cache:
app:
provider: 'cache.adapter.redis'
LdapRecord (from php-ldap-record):
$mockLdap = new LdapRecord\Entry(['dn' => 'cn=test', 'uid' => ['test']]);
$this->personConnector->expects($this->any())->method('search')->willReturn([$mockLdap]);
Connection Issues
config/packages/monolog.yaml:
handlers:
ldap:
type: stream
path: '%kernel.logs_dir%/ldap.log'
level: debug
channels: ['ldap']
host, port, and base_dn in the config. Use ldapsearch to test connectivity:
ldapsearch -x -H ldap://your-server -b "dc=example,dc=com" -D "cn=admin,dc=example,dc=com" -w password
Attribute Mapping Errors
email).ldapsearch -x -H ldap://your-server -b "dc=example,dc=com" "(objectClass=*)" + | grep "mail:"
attributes_mapping in config or extend the Person entity to handle custom fields.Performance with Large Directories
search_scope (e.g., search_scope: 'onelevel').Password Sync Risks
password_encoder to hash passwords locally before syncing to LDAP (if supported by your LDAP server).DbpRelayBasePersonBundle's profiler data:
// config/packages/dev/debug.yaml
framework:
profiler:
collectors:
dbp_relay_person: true
ldapsearch or Apache Directory Studio to inspect the LDAP server directly..env values with:
php bin/console debug:container --parameter dbp_relay_base_person_connector_ldap
Custom Connector Logic
Extend Dbp\RelayBasePersonConnectorLdapBundle\Connector\LdapPersonConnector to add logic:
class CustomLdapConnector extends LdapPersonConnector {
public function findByCustomAttribute($attribute, $value) {
return $this->search(sprintf('(%s=%s)', $attribute, $value));
}
}
Register as a service with the dbp_relay_base_person.connector tag.
Dynamic Attribute Mapping
Override mapAttributes() to handle dynamic fields:
protected function mapAttributes(array $ldapAttributes): array {
$mapping = parent::mapAttributes($ldapAttributes);
if (isset($ldapAttributes['extensionAttribute1'])) {
$mapping['custom_field'] = $ldapAttributes['extensionAttribute1'][0];
}
return $mapping;
}
Multi-LDAP Support
Implement a MultiLdapPersonConnector to query multiple LDAP servers:
class MultiLdapPersonConnector implements PersonConnectorInterface {
private $connectors;
public function __construct(iterable $connectors) {
$this->connectors = $connectors;
}
public function findByEmail($email) {
foreach ($this->connectors as $connector) {
if ($person = $connector->findByEmail($email)) {
return $person;
}
}
return null;
}
}
Configure multiple LDAP connectors in services.yaml and inject them.
Webhook Triggers Use Symfony Messenger to dispatch events when LDAP users are updated:
$this->messageBus->dispatch(
new LdapUserUpdatedMessage($ldapUser->getDn())
);
Create a worker to handle syncs asynchronously.
How can I help you explore Laravel packages today?