EDIT: This method works on Symfony 3.3, I don't know if this works on lower versions.
The right way to do this is by creating a Compiler Pass .
You can also: redefine the service by adding a new service with the same name: fos_user.listener.authentication in the app / config.yml file or in the package configuration file and adding a new class to it, as I did below and add this
Here's how to override automatic logging when registering a new user using the compiler transfer technique.
Compiler transfer
namespace arpa3\UserBundle\DependencyInjection; use arpa3\UserBundle\EventListener\AuthenticationListener; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; class OverrideServiceCompilerPass implements CompilerPassInterface { public function process(ContainerBuilder $container) { $definition = $container->getDefinition('fos_user.listener.authentication'); $definition->setClass(AuthenticationListener::class); } }
Service Override
namespace arpa3\UserBundle\EventListener; use FOS\UserBundle\Event\FilterUserResponseEvent; use FOS\UserBundle\Event\UserEvent; use FOS\UserBundle\FOSUserEvents; use FOS\UserBundle\Security\LoginManagerInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\Security\Core\Exception\AccountStatusException; class AuthenticationListener implements EventSubscriberInterface { private $loginManager; private $firewallName; public function __construct(LoginManagerInterface $loginManager, $firewallName) { $this->loginManager = $loginManager; $this->firewallName = $firewallName; } public static function getSubscribedEvents() { return array(
Register your Compiler Pass in your main package file
namespace arpa3\UserBundle; use arpa3\UserBundle\DependencyInjection\OverrideServiceCompilerPass; use Symfony\Component\HttpKernel\Bundle\Bundle; use Symfony\Component\DependencyInjection\ContainerBuilder; class arpa3UserBundle extends Bundle { public function getParent () { return 'FOSUserBundle'; } public function build ( ContainerBuilder $container ) { parent ::build( $container ); $container -> addCompilerPass( new OverrideServiceCompilerPass() ); } }
There are other ways to override the authentication service on your config.yml, but the solution above is the cleanest and most service-friendly solution I have found.
db306 source share