Skip to content

Commit a83481e

Browse files
committed
bug #129 Fix Symfony 6.2 deprecation for Security helper (RobertMe)
This PR was merged into the 0.4-dev branch. Discussion ---------- Fix Symfony 6.2 deprecation for Security helper As the title states :) In Symfony 6.2 the Security helper of the Security component has been marked as deprecated, with a replacement helper being added in the Security bundle. This change fixes the deprecation by referencing the service by the name (`security.helper`) instead of the FQN, and for the "user" two implementations are added, one depending on the old class and one depending on the new class. Tested on PHP 7.2 and 8.1 using both high and low dependencies. Commits ------- 04329d0 Fix Symfony 6.2 deprecation for Security helper
2 parents 2ac15ea + 04329d0 commit a83481e

File tree

3 files changed

+50
-21
lines changed

3 files changed

+50
-21
lines changed

src/EventListener/AuthorizationRequestUserResolvingListener.php

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,43 @@
44

55
namespace League\Bundle\OAuth2ServerBundle\EventListener;
66

7-
use League\Bundle\OAuth2ServerBundle\Event\AuthorizationRequestResolveEvent;
8-
use Symfony\Component\Security\Core\Security;
9-
use Symfony\Component\Security\Core\User\UserInterface;
10-
11-
/**
12-
* Listener sets currently authenticated user to authorization request context
13-
*/
14-
final class AuthorizationRequestUserResolvingListener
15-
{
7+
use Symfony\Bundle\Security\Core\Security;
8+
use Symfony\Component\Security\Core\Security as LegacySecurity;
9+
10+
if (class_exists(Security::class)) {
1611
/**
17-
* @var Security
12+
* Listener sets currently authenticated user to authorization request context
1813
*/
19-
private $security;
20-
21-
public function __construct(Security $security)
14+
final class AuthorizationRequestUserResolvingListener
2215
{
23-
$this->security = $security;
24-
}
16+
use AuthorizationRequestUserResolvingListenerTrait;
2517

26-
public function onAuthorizationRequest(AuthorizationRequestResolveEvent $event): void
18+
/**
19+
* @var Security
20+
*/
21+
private $security;
22+
23+
public function __construct(Security $security)
24+
{
25+
$this->security = $security;
26+
}
27+
}
28+
} else {
29+
/**
30+
* Listener sets currently authenticated user to authorization request context
31+
*/
32+
final class AuthorizationRequestUserResolvingListener
2733
{
28-
$user = $this->security->getUser();
29-
if ($user instanceof UserInterface) {
30-
$event->setUser($user);
34+
use AuthorizationRequestUserResolvingListenerTrait;
35+
36+
/**
37+
* @var LegacySecurity
38+
*/
39+
private $security;
40+
41+
public function __construct(LegacySecurity $security)
42+
{
43+
$this->security = $security;
3144
}
3245
}
3346
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace League\Bundle\OAuth2ServerBundle\EventListener;
4+
5+
use League\Bundle\OAuth2ServerBundle\Event\AuthorizationRequestResolveEvent;
6+
use Symfony\Component\Security\Core\User\UserInterface;
7+
8+
trait AuthorizationRequestUserResolvingListenerTrait
9+
{
10+
public function onAuthorizationRequest(AuthorizationRequestResolveEvent $event): void
11+
{
12+
$user = $this->security->getUser();
13+
if ($user instanceof UserInterface) {
14+
$event->setUser($user);
15+
}
16+
}
17+
}

src/Resources/config/services.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
5858
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
5959
use Symfony\Component\HttpFoundation\RequestStack;
60-
use Symfony\Component\Security\Core\Security;
6160

6261
return static function (ContainerConfigurator $container): void {
6362
$container->services()
@@ -208,7 +207,7 @@
208207
// Authorization listeners
209208
->set('league.oauth2_server.listener.authorization_request_user_resolving', AuthorizationRequestUserResolvingListener::class)
210209
->args([
211-
service(Security::class),
210+
service('security.helper'),
212211
])
213212
->tag('kernel.event_listener', [
214213
'event' => OAuth2Events::AUTHORIZATION_REQUEST_RESOLVE,

0 commit comments

Comments
 (0)