diff --git a/src/EventListener/AuthorizationRequestUserResolvingListener.php b/src/EventListener/AuthorizationRequestUserResolvingListener.php index 190d514b..f17ab90d 100644 --- a/src/EventListener/AuthorizationRequestUserResolvingListener.php +++ b/src/EventListener/AuthorizationRequestUserResolvingListener.php @@ -4,30 +4,43 @@ namespace League\Bundle\OAuth2ServerBundle\EventListener; -use League\Bundle\OAuth2ServerBundle\Event\AuthorizationRequestResolveEvent; -use Symfony\Component\Security\Core\Security; -use Symfony\Component\Security\Core\User\UserInterface; - -/** - * Listener sets currently authenticated user to authorization request context - */ -final class AuthorizationRequestUserResolvingListener -{ +use Symfony\Bundle\Security\Core\Security; +use Symfony\Component\Security\Core\Security as LegacySecurity; + +if (class_exists(Security::class)) { /** - * @var Security + * Listener sets currently authenticated user to authorization request context */ - private $security; - - public function __construct(Security $security) + final class AuthorizationRequestUserResolvingListener { - $this->security = $security; - } + use AuthorizationRequestUserResolvingListenerTrait; - public function onAuthorizationRequest(AuthorizationRequestResolveEvent $event): void + /** + * @var Security + */ + private $security; + + public function __construct(Security $security) + { + $this->security = $security; + } + } +} else { + /** + * Listener sets currently authenticated user to authorization request context + */ + final class AuthorizationRequestUserResolvingListener { - $user = $this->security->getUser(); - if ($user instanceof UserInterface) { - $event->setUser($user); + use AuthorizationRequestUserResolvingListenerTrait; + + /** + * @var LegacySecurity + */ + private $security; + + public function __construct(LegacySecurity $security) + { + $this->security = $security; } } } diff --git a/src/EventListener/AuthorizationRequestUserResolvingListenerTrait.php b/src/EventListener/AuthorizationRequestUserResolvingListenerTrait.php new file mode 100644 index 00000000..8e95d4f2 --- /dev/null +++ b/src/EventListener/AuthorizationRequestUserResolvingListenerTrait.php @@ -0,0 +1,17 @@ +security->getUser(); + if ($user instanceof UserInterface) { + $event->setUser($user); + } + } +} diff --git a/src/Resources/config/services.php b/src/Resources/config/services.php index f9c4a600..89810e0d 100644 --- a/src/Resources/config/services.php +++ b/src/Resources/config/services.php @@ -57,7 +57,6 @@ use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\HttpFoundation\RequestStack; -use Symfony\Component\Security\Core\Security; return static function (ContainerConfigurator $container): void { $container->services() @@ -208,7 +207,7 @@ // Authorization listeners ->set('league.oauth2_server.listener.authorization_request_user_resolving', AuthorizationRequestUserResolvingListener::class) ->args([ - service(Security::class), + service('security.helper'), ]) ->tag('kernel.event_listener', [ 'event' => OAuth2Events::AUTHORIZATION_REQUEST_RESOLVE,