diff --git a/composer.json b/composer.json index b591b465..4dbee1b8 100644 --- a/composer.json +++ b/composer.json @@ -23,9 +23,7 @@ "symfony/event-dispatcher": "^4.4.20||^5.0.11||^6.0||^7.0", "symfony/http-kernel": "^4.4.20||^5.0.11||^6.0||^7.0", "symfony/polyfill-php80": "^1.22", - "symfony/psr-http-message-bridge": "^1.2||^2.0||^6.4||^7.0", - "symfony/security-core": "^4.4.20||^5.0.11||^6.0||^7.0", - "symfony/security-http": "^4.4.20||^5.0.11||^6.0||^7.0" + "symfony/psr-http-message-bridge": "^1.2||^2.0||^6.4||^7.0" }, "require-dev": { "doctrine/dbal": "^2.13||^3.3||^4.0", @@ -46,6 +44,8 @@ "symfony/monolog-bundle": "^3.4", "symfony/phpunit-bridge": "^5.2.6||^6.0||^7.0", "symfony/process": "^4.4.20||^5.0.11||^6.0||^7.0", + "symfony/security-core": "^4.4.20||^5.0.11||^6.0||^7.0", + "symfony/security-http": "^4.4.20||^5.0.11||^6.0||^7.0", "symfony/twig-bundle": "^4.4.20||^5.0.11||^6.0||^7.0", "symfony/yaml": "^4.4.20||^5.0.11||^6.0||^7.0", "vimeo/psalm": "^4.3||^5.16.0" diff --git a/src/DependencyInjection/Compiler/AddLoginListenerTagPass.php b/src/DependencyInjection/Compiler/AddLoginListenerTagPass.php index 24d3110c..5f935cb6 100644 --- a/src/DependencyInjection/Compiler/AddLoginListenerTagPass.php +++ b/src/DependencyInjection/Compiler/AddLoginListenerTagPass.php @@ -17,9 +17,17 @@ final class AddLoginListenerTagPass implements CompilerPassInterface */ public function process(ContainerBuilder $container): void { + if (!$container->hasDefinition(LoginListener::class)) { + return; + } $listenerDefinition = $container->getDefinition(LoginListener::class); - if (!class_exists(LoginSuccessEvent::class)) { + if (class_exists(LoginSuccessEvent::class)) { + $listenerDefinition->addTag('kernel.event_listener', [ + 'event' => LoginSuccessEvent::class, + 'method' => 'handleLoginSuccessEvent', + ]); + } elseif (class_exists(AuthenticationSuccessEvent::class)) { $listenerDefinition->addTag('kernel.event_listener', [ 'event' => AuthenticationSuccessEvent::class, 'method' => 'handleAuthenticationSuccessEvent', diff --git a/src/DependencyInjection/SentryExtension.php b/src/DependencyInjection/SentryExtension.php index 52675ded..a05a62e8 100644 --- a/src/DependencyInjection/SentryExtension.php +++ b/src/DependencyInjection/SentryExtension.php @@ -15,6 +15,7 @@ use Sentry\Options; use Sentry\SentryBundle\EventListener\ConsoleListener; use Sentry\SentryBundle\EventListener\ErrorListener; +use Sentry\SentryBundle\EventListener\LoginListener; use Sentry\SentryBundle\EventListener\MessengerListener; use Sentry\SentryBundle\EventListener\TracingConsoleListener; use Sentry\SentryBundle\EventListener\TracingRequestListener; @@ -34,6 +35,7 @@ use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\HttpClient\HttpClient; use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension; +use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; final class SentryExtension extends ConfigurableExtension { @@ -75,6 +77,10 @@ protected function loadInternal(array $mergedConfig, ContainerBuilder $container $this->registerTwigTracingConfiguration($container, $mergedConfig['tracing']); $this->registerCacheTracingConfiguration($container, $mergedConfig['tracing']); $this->registerHttpClientTracingConfiguration($container, $mergedConfig['tracing']); + + if (!interface_exists(TokenStorageInterface::class)) { + $container->removeDefinition(LoginListener::class); + } } /** diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index 02ad8994..ff0407e0 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -83,7 +83,6 @@ - diff --git a/tests/DependencyInjection/Compiler/AddLoginListenerTagPassTest.php b/tests/DependencyInjection/Compiler/AddLoginListenerTagPassTest.php index cc91f8f7..6fb3248b 100644 --- a/tests/DependencyInjection/Compiler/AddLoginListenerTagPassTest.php +++ b/tests/DependencyInjection/Compiler/AddLoginListenerTagPassTest.php @@ -28,4 +28,20 @@ public function testProcess(): void $this->assertSame([['event' => AuthenticationSuccessEvent::class, 'method' => 'handleAuthenticationSuccessEvent']], $listenerDefinition->getTag('kernel.event_listener')); } + + public function testProcessLoginSuccess(): void + { + if (!class_exists(LoginSuccessEvent::class)) { + $this->markTestSkipped('Skipping this test because LoginSuccessEvent does not exist.'); + } + + $container = new ContainerBuilder(); + $container->register(LoginListener::class)->setPublic(true); + $container->addCompilerPass(new AddLoginListenerTagPass()); + $container->compile(); + + $listenerDefinition = $container->getDefinition(LoginListener::class); + + $this->assertSame([['event' => LoginSuccessEvent::class, 'method' => 'handleLoginSuccessEvent']], $listenerDefinition->getTag('kernel.event_listener')); + } } diff --git a/tests/DependencyInjection/SentryExtensionTest.php b/tests/DependencyInjection/SentryExtensionTest.php index 232f0887..db949243 100644 --- a/tests/DependencyInjection/SentryExtensionTest.php +++ b/tests/DependencyInjection/SentryExtensionTest.php @@ -14,6 +14,7 @@ use Sentry\SentryBundle\DependencyInjection\SentryExtension; use Sentry\SentryBundle\EventListener\ConsoleListener; use Sentry\SentryBundle\EventListener\ErrorListener; +use Sentry\SentryBundle\EventListener\LoginListener; use Sentry\SentryBundle\EventListener\MessengerListener; use Sentry\SentryBundle\EventListener\RequestListener; use Sentry\SentryBundle\EventListener\SubRequestListener; @@ -418,6 +419,12 @@ public function testLoggerOptionFallbackToNullLoggerIfNotSet(): void $this->assertDefinitionMethodCallAt($methodCalls[3], 'setLogger', [new Reference(NullLogger::class, ContainerBuilder::IGNORE_ON_INVALID_REFERENCE)]); } + public function testLoginListener(): void + { + $container = $this->createContainerFromFixture('full'); + $this->assertTrue($container->hasDefinition(LoginListener::class)); + } + /** * @dataProvider releaseOptionDataProvider */