bengor-user/symfony-security-bridge-bundle
Install the Bundle
composer require bengor-user/symfony-security-bridge-bundle
Ensure your composer.json includes symfony/security-bundle and bengor-user/user-bundle as dependencies.
Register the Bundle
Add to config/bundles.php:
return [
// ...
BenGorUser\SymfonySecurityBridgeBundle\BenGorUserSymfonySecurityBridgeBundle::class => ['all' => true],
];
Configure Security
Extend your config/packages/security.yaml to integrate with UserBundle:
security:
providers:
user_provider:
id: user_bundle.security.user_provider
firewalls:
main:
provider: user_provider
form_login:
login_path: user_login
check_path: user_login_check
logout: true
First Use Case: Authentication Create a login form in a controller:
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
public function login(Request $request, AuthenticationUtils $authenticationUtils)
{
return $this->render('user/login.html.twig', [
'last_username' => $authenticationUtils->getLastUsername(),
'error' => $authenticationUtils->getLastAuthenticationError(),
]);
}
User Authentication Flow
UserBundle's UserManager to handle user creation/updates.SymfonySecurityBridgeBundle:
// In a controller/service
$user = $this->get('user_bundle.manager.user')->findUserBy(['email' => 'user@example.com']);
$this->get('security.token_storage')->setToken(new UsernamePasswordToken($user, null, 'main', $user->getRoles()));
Role-Based Access Control (RBAC)
UserBundle entities (e.g., User entity has roles property).# config/packages/security.yaml
access_control:
- { path: ^/admin, roles: ROLE_ADMIN }
Event Listeners for Security
security.interactive_login and security.authentication.success:
// src/EventListener/SecurityListener.php
public function onAuthenticationSuccess(GetResponseUserEvent $event)
{
$user = $event->getUser();
$this->get('user_bundle.manager.user')->updateLastLogin($user);
}
Auth facade to Symfony's security.token_storage:
// In a service provider
$this->app->bind('auth', function () {
return $this->app['security.token_storage']->getToken()->getUser();
});
UserBundle's UserProvider for custom logic:
class CustomUserProvider extends \BenGorUser\UserBundle\Security\User\UserProvider
{
public function loadUserByUsername($username)
{
// Custom logic (e.g., LDAP fallback)
return parent::loadUserByUsername($username);
}
}
Register in security.yaml:
providers:
custom_provider:
id: custom_user_provider
Deprecated Dependencies
symfony/security-bundle:^3.4 for Symfony 3.4+ with caution).UserBundle Integration
UserBundle is not actively maintained. Expect limited documentation or community support.UserBundle locally and inspect its Security namespace for undocumented features.Token Storage Quirks
token_storage is not thread-safe. Avoid storing it in long-lived services (e.g., command buses).security.token_storage only in request-scoped services or controllers.Role Inheritance
UserBundle may not support Symfony's ROLE_* role hierarchy by default. Manually define roles in your User entity:
// src/Entity/User.php
public function getRoles()
{
$roles = $this->roles;
if (empty($roles)) {
$roles[] = 'ROLE_USER';
}
return array_unique($roles);
}
Authentication Failures
security.authentication.failure events or enable debug mode:
# config/packages/dev/security.yaml
security:
debug: true
UserProvider is correctly configured in security.yaml.CSRF Token Errors
_csrf_token:
{{ form_start(form) }}
{{ form_widget(form._token) }} {# CSRF token #}
{{ form_widget(form.email) }}
{{ form_widget(form.password) }}
{{ form_end(form) }}
Custom Authentication
UserBundle's AuthenticationProvider:
class CustomAuthenticationProvider extends \BenGorUser\UserBundle\Security\Authentication\Provider\UserAuthenticationProvider
{
public function authenticate(Token $token)
{
// Custom auth logic (e.g., 2FA)
return parent::authenticate($token);
}
}
security.yaml:
firewalls:
main:
provider: user_provider
custom_auth: true # Trigger custom provider
Event Dispatching
UserBundle's events (e.g., user.register) to trigger custom logic:
$eventDispatcher->dispatch(
'user.register',
new UserEvents($user, $plainPassword)
);
Laravel-Like Middleware
// In a middleware service
public function handle($request, Closure $next)
{
if (!$this->auth->check()) {
return response('Unauthorized', 401);
}
return $next($request);
}
How can I help you explore Laravel packages today?