Installation
Run composer require connectholland/ldap-bundle in your Laravel project (note: this bundle is Symfony-based, so ensure compatibility with Laravel via Symfony bridge or use in a Symfony project).
Bundle Registration
Add the bundle to config/app.php under providers:
'providers' => [
// ...
ConnectHolland\LdapBundle\ConnectHollandLdapBundle::class,
],
Basic Configuration
Configure config/security.php (or app/config/security.yml if using Symfony):
security:
providers:
my_ldap:
connect_holland_ldap:
connection:
host: 'ldap.example.com'
port: 389
encryption: 'none' # or 'ssl', 'tls'
user_factory:
type: doctrine
user_class: 'App\User' # Laravel's Eloquent model
username_column: 'email' # Column to map LDAP username to
user_property_map:
firstName: 'givenName'
lastName: 'sn'
email: 'mail'
First Use Case Authenticate a user via LDAP:
use ConnectHolland\LdapBundle\Security\LdapUserProvider;
// In a controller or service
$ldapProvider = app(LdapUserProvider::class);
$user = $ldapProvider->loadUserByUsername('ldap_username');
LDAP Authentication with Existing Users
doctrine or sulu user factory to sync LDAP users with your database.// In a custom user factory service
public function loadUserByUsername($username)
{
$ldapUser = $this->ldap->findUser($username);
if (!$ldapUser) return null;
$user = User::where('email', $ldapUser->getEmail())->first();
if (!$user) {
$user = User::create([
'email' => $ldapUser->getEmail(),
'first_name' => $ldapUser->getFirstName(),
// ...
]);
}
return $user;
}
Role Mapping
spatie/laravel-permission):
user_factory:
role_map:
'cn=admins,ou=groups': 'admin'
'cn=users,ou=groups': 'user'
Fallback Authentication
security.php:
firewalls:
main:
provider: [my_ldap, my_database]
Laravel-Specific Adjustments:
Replace Symfony’s UserInterface with Laravel’s Illuminate\Contracts\Auth\Authenticatable.
Extend the bundle’s UserFactory to work with Laravel’s Eloquent:
use Illuminate\Database\Eloquent\Model;
class LaravelUserFactory extends DoctrineUserFactory
{
protected function createUser(array $data)
{
return Model::create($data);
}
}
Caching: Cache LDAP queries to reduce load (e.g., using Laravel’s cache):
$ldapUser = Cache::remember("ldap_user_{$username}", now()->addHours(1), function() use ($username) {
return $this->ldap->findUser($username);
});
Testing:
Use Laravel’s Mockery to simulate LDAP responses in tests:
$mockLdap = Mockery::mock('overload:ConnectHolland\LdapBundle\Ldap\Connection');
$mockLdap->shouldReceive('bind')->andReturn(true);
Deprecated Symfony Components:
digitalbush/laravel-ldap or update the bundle’s dependencies.User Factory Limitations:
doctrine factory assumes Doctrine ORM. For Laravel, override the factory or use a custom service.config/services.php:
'ldap.user_factory' => App\Services\LaravelUserFactory::class,
LDAP Schema Mismatches:
uid vs. sAMAccountName) may not align with your Laravel model.user_property_map in config or pre-process LDAP data:
$ldapUser->setUsername($ldapUser->getAttribute('sAMAccountName'));
Archived Status:
Enable LDAP Logging:
Add to config/logging.php:
'channels' => [
'ldap' => [
'driver' => 'single',
'path' => storage_path('logs/ldap.log'),
'level' => 'debug',
],
],
Then log LDAP operations:
\Log::channel('ldap')->debug('LDAP Query', ['data' => $ldapUser->getAttributes()]);
Common Errors:
connection config.user_class in config matches your Laravel model namespace.Custom User Factories: Create a service to extend functionality:
class CustomUserFactory extends AbstractUserFactory
{
public function loadUserByUsername($username)
{
// Custom logic (e.g., multi-domain LDAP)
return parent::loadUserByUsername($username);
}
}
Register it in config/services.php:
'ldap.user_factory' => App\Services\CustomUserFactory::class,
Event Listeners: Listen for LDAP events (e.g., user sync) using Laravel’s events:
Event::listen('ldap.user.synced', function ($user) {
// Trigger post-sync actions (e.g., send welcome email)
});
Laravel Auth Integration:
Override Laravel’s AuthManager to prioritize LDAP:
use ConnectHolland\LdapBundle\Security\LdapUserProvider;
protected function createUserProvider()
{
return new LdapUserProvider($this->app['config']['ldap']);
}
How can I help you explore Laravel packages today?