Installation:
composer require alexhenriet/common-bundle
Ensure config/packages/alexhenriet_common.yaml exists before requiring the package to avoid errors:
common:
ldap_host: 'ldap-host.lan'
login_prefix: 'DOMAIN'
Enable the Bundle:
Add to config/bundles.php:
Alexhenriet\Bundle\CommonBundle\CommonBundle::class => ['all' => true],
First Use Case:
Replace your default authenticator in config/packages/security.yaml:
security:
enable_authenticator_manager: true
firewalls:
main:
custom_authenticators:
- Alexhenriet\Bundle\CommonBundle\Security\BypassableLdapLoginFormAuthenticator
Configuration:
Extend alexhenriet_common.yaml with LDAP-specific settings:
common:
ldap_host: 'ldap.example.com'
ldap_port: 389
bypass_user_identifiers: ['admin', 'dev']
bypass_environments: ['dev', 'test']
Login Form:
Use _username and _password as input names in templates/security/login.html.twig:
<form method="post">
<input type="text" name="_username" />
<input type="password" name="_password" />
<button type="submit">Login</button>
</form>
Bypass Logic:
Users in bypass_user_identifiers or environments in bypass_environments skip LDAP checks.
Extend Base Controller:
use Alexhenriet\Bundle\CommonBundle\Controller\AbstractController;
class UserController extends AbstractController {
public function index(): Response {
return $this->render('user/index.html.twig');
}
}
Service Injection:
Add to config/services.yaml to resolve private controllers:
App\Controller\:
resource: '../src/Controller/'
tags: ['controller.service_arguments']
Dependency Injection:
Leverage Symfony’s built-in DI for services (e.g., Security, Twig).
alexhenriet_common.yaml per environment (e.g., config/packages/dev/alexhenriet_common.yaml).BypassableLdapLoginFormAuthenticator for additional validation:
class CustomAuthenticator extends BypassableLdapLoginFormAuthenticator {
protected function getUser($username, $password) {
// Custom logic
}
}
$this->getUser().Configuration Order:
ConfigException if alexhenriet_common.yaml is missing during composer require.LDAP Dependencies:
Class 'LDAP' not found if ext-ldap is missing.sudo apt install php-ldap on Ubuntu).Controller Visibility:
Controller ... cannot be fetched from the container if services.yaml lacks the resource tag.App\Controller\ service configuration (see above).Bypass Logic:
bypass_user_identifiers uses exact matches (case-sensitive).var/log/dev.log) for LDAP errors. Test connectivity with:
ldapsearch -x -H ldap://ldap-host.lan -b "dc=example,dc=com"
Custom Authenticators:
Override BypassableLdapLoginFormAuthenticator to add:
Abstract Controller:
Extend AbstractController to add shared methods:
class BaseController extends AbstractController {
protected function requireRole(string $role): void {
$this->denyAccessUnlessGranted($role, null);
}
}
Configuration:
Add new keys to alexhenriet_common.yaml and extend the bundle’s CommonExtension class to load them.
%kernel.environment% in configs to dynamically set bypass_environments.LDAPMock or disable LDAP checks in tests:
# config/packages/test/alexhenriet_common.yaml
common:
ldap_host: null # Disable LDAP in tests
How can I help you explore Laravel packages today?