Skip to content

Allow to install the bundle without Symfony security #912

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
Expand Down
10 changes: 9 additions & 1 deletion src/DependencyInjection/Compiler/AddLoginListenerTagPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,17 @@
*/
public function process(ContainerBuilder $container): void
{
if (!$container->hasDefinition(LoginListener::class)) {
return;

Check warning on line 21 in src/DependencyInjection/Compiler/AddLoginListenerTagPass.php

View check run for this annotation

Codecov / codecov/patch

src/DependencyInjection/Compiler/AddLoginListenerTagPass.php#L21

Added line #L21 was not covered by tests
}
$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',
Expand Down
6 changes: 6 additions & 0 deletions src/DependencyInjection/SentryExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down Expand Up @@ -75,6 +77,10 @@
$this->registerTwigTracingConfiguration($container, $mergedConfig['tracing']);
$this->registerCacheTracingConfiguration($container, $mergedConfig['tracing']);
$this->registerHttpClientTracingConfiguration($container, $mergedConfig['tracing']);

if (!interface_exists(TokenStorageInterface::class)) {
$container->removeDefinition(LoginListener::class);

Check warning on line 82 in src/DependencyInjection/SentryExtension.php

View check run for this annotation

Codecov / codecov/patch

src/DependencyInjection/SentryExtension.php#L82

Added line #L82 was not covered by tests
}
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@
<argument type="service" id="security.token_storage" on-invalid="ignore" />

<tag name="kernel.event_listener" event="kernel.request" method="handleKernelRequestEvent" />
<tag name="kernel.event_listener" event="Symfony\Component\Security\Http\Event\LoginSuccessEvent" method="handleLoginSuccessEvent" />
</service>

<service id="Sentry\SentryBundle\Command\SentryTestCommand" class="Sentry\SentryBundle\Command\SentryTestCommand">
Expand Down
16 changes: 16 additions & 0 deletions tests/DependencyInjection/Compiler/AddLoginListenerTagPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}
7 changes: 7 additions & 0 deletions tests/DependencyInjection/SentryExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*/
Expand Down
Loading