Skip to content

Commit be5ec1a

Browse files
minor symfony#60803 [SecurityBundle] Don't break security.http_utils service decoration (lyrixx)
This PR was merged into the 7.4 branch. Discussion ---------- [SecurityBundle] Don't break `security.http_utils` service decoration | Q | A | ------------- | --- | Branch? | 7.4 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | | License | MIT --- Actually, this service is really hard to override. My use case: I want to preserve a parameter in the URL. I ended with the following code, and I'm not happy with it ```php <?php namespace App\Security\HttpUtils; use App\Site\SiteContext; use Symfony\Component\DependencyInjection\Attribute\AsDecorator; use Symfony\Component\DependencyInjection\Attribute\Autowire; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestMatcherInterface; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Routing\Matcher\UrlMatcherInterface; use Symfony\Component\Security\Http\HttpUtils as SymfonyHttpUtils; #[AsDecorator('security.http_utils')] final class HttpUtils extends SymfonyHttpUtils { public function __construct( private readonly SiteContext $siteContext, ?UrlGeneratorInterface $urlGenerator = null, #[Autowire(service: 'router')] UrlMatcherInterface|RequestMatcherInterface|null $urlMatcher = null, // See symfony#60803 ?string $domainRegexp = null, ?string $secureDomainRegexp = null, ) { parent::__construct($urlGenerator, $urlMatcher, $domainRegexp, $secureDomainRegexp); } public function generateUri(Request $request, string $path): string { $uri = parent::generateUri($request, $path); $site = $this->siteContext->currentSite; if (!$site) { return $uri; } $position = strpos($uri, '?'); if ($position === false) { return $uri . '?site=' . $site->id; } parse_str(parse_url($uri, PHP_URL_QUERY) ?? '', $queryParams); if (array_key_exists('site', $queryParams)) { return $uri; } return substr($uri, 0, $position + 1) . 'site=' . $site->id . '&' . substr($uri, $position + 1); } } ``` This is not a real decorator. And I cannot do that, since the original service call itself --- >[!CAUTION] >I believe my patch is good, but with it I cannot achieve anymore what I need to do 😬 Instead, I could change the class with a compiler pass Commits ------- a099ba2 [SecurityBundle] Don't break `security.http_utils` service decoration
2 parents 17473e1 + a099ba2 commit be5ec1a

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

src/Symfony/Bundle/SecurityBundle/DependencyInjection/Compiler/AddSessionDomainConstraintPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function process(ContainerBuilder $container): void
3838
$domainRegexp = (empty($sessionOptions['cookie_secure']) ? 'https?://' : 'https://').$domainRegexp;
3939
}
4040

41-
$container->findDefinition('security.http_utils')
41+
$container->getDefinition('security.http_utils')
4242
->addArgument(\sprintf('{^%s$}i', $domainRegexp))
4343
->addArgument($secureDomainRegexp);
4444
}

src/Symfony/Bundle/SecurityBundle/SecurityBundle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function build(ContainerBuilder $container): void
8888
$extension->addUserProviderFactory(new LdapFactory());
8989
$container->addCompilerPass(new AddExpressionLanguageProvidersPass());
9090
$container->addCompilerPass(new AddSecurityVotersPass());
91-
$container->addCompilerPass(new AddSessionDomainConstraintPass(), PassConfig::TYPE_BEFORE_REMOVING);
91+
$container->addCompilerPass(new AddSessionDomainConstraintPass());
9292
$container->addCompilerPass(new CleanRememberMeVerifierPass());
9393
$container->addCompilerPass(new RegisterCsrfFeaturesPass());
9494
$container->addCompilerPass(new RegisterTokenUsageTrackingPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 200);

0 commit comments

Comments
 (0)