Installation
composer require cdesign/ldap-bundle
Add the bundle to config/bundles.php:
return [
// ...
Cdesign\LdapBundle\CdesignLdapBundle::class => ['all' => true],
];
Configuration
Define LDAP settings in config/packages/design_ldap.yaml:
cdesign_ldap:
host: 'ldap://your-ldap-server'
port: 389
base_dn: 'dc=example,dc=com'
username: 'cn=admin,dc=example,dc=com'
password: 'admin_password'
use_ssl: false
use_start_tls: false
First Use Case: Authentication Extend Symfony’s security system to use LDAP:
# config/packages/security.yaml
security:
providers:
ldap_provider:
id: cdesign_ldap.user_provider
firewalls:
main:
provider: ldap_provider
form_login:
login_path: login
check_path: login_check
Fetch Users: Use the built-in UserProvider to validate credentials:
$user = $this->get('ldap.user_provider')->loadUserByUsername('ldap_username');
$authenticator = $this->get('ldap.authenticator');
$authenticator->authenticate($user, 'password');
Custom User Mapping
Override Cdesign\LdapBundle\Security\User\LdapUserProvider to map LDAP attributes to Symfony’s UserInterface:
class CustomLdapUserProvider extends LdapUserProvider
{
public function loadUserByUsername($username)
{
$user = parent::loadUserByUsername($username);
$user->setEmail($this->getLdapAttribute($user, 'mail')); // Custom mapping
return $user;
}
}
Register the provider in services.yaml:
services:
App\Security\CustomLdapUserProvider:
tags: ['security.user_provider']
LDAP Sync Periodically sync local users with LDAP (e.g., via a cron job):
$syncService = $this->get('ldap.sync_service');
$syncService->syncUsers(); // Customize logic in service
Group-Based Access Control Fetch user groups from LDAP and assign roles:
$groups = $this->get('ldap.user_provider')->getGroups($user);
$roles = array_map(fn($group) => 'ROLE_'.strtoupper($group), $groups);
$user->setRoles($roles);
LdapUser properties.LdapUser to persist additional fields in a custom user entity.ldap.login.success or ldap.login.failure events for post-auth logic.Connection Issues
use_ssl or use_start_tls is correctly configured for secure connections.Attribute Mapping
ldap_search() to confirm attribute names (e.g., uid, mail, memberOf).$username = strtolower($username);
Performance
cdesign_ldap:
cache:
enabled: true
provider: 'app.cache.app'
config/packages/design_ldap.yaml:
cdesign_ldap:
debug: true
Custom Authenticator
Extend Cdesign\LdapBundle\Security\Authenticator\LdapAuthenticator for custom logic (e.g., multi-factor auth):
class CustomLdapAuthenticator extends LdapAuthenticator
{
public function authenticate(Credentials $credentials)
{
// Add pre-auth logic
$user = parent::authenticate($credentials);
// Add post-auth logic
return $user;
}
}
Dynamic Configuration Load LDAP settings from environment variables or a database:
cdesign_ldap:
host: '%env(LDAP_HOST)%'
password: '%env(LDAP_PASSWORD)%'
Multi-Domain Support
Use a custom LdapUserProvider to route users to different LDAP servers based on domain:
public function loadUserByUsername($username)
{
$domain = explode('@', $username)[1];
$this->setHost("ldap://{$domain}-ldap-server");
return parent::loadUserByUsername($username);
}
How can I help you explore Laravel packages today?