Skip to content

Commit

Permalink
bug #129 Fix Symfony 6.2 deprecation for Security helper (RobertMe)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
chalasr committed Apr 23, 2023
2 parents 2ac15ea + 04329d0 commit a83481e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 21 deletions.
51 changes: 32 additions & 19 deletions src/EventListener/AuthorizationRequestUserResolvingListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace League\Bundle\OAuth2ServerBundle\EventListener;

use League\Bundle\OAuth2ServerBundle\Event\AuthorizationRequestResolveEvent;
use Symfony\Component\Security\Core\User\UserInterface;

trait AuthorizationRequestUserResolvingListenerTrait
{
public function onAuthorizationRequest(AuthorizationRequestResolveEvent $event): void
{
$user = $this->security->getUser();
if ($user instanceof UserInterface) {
$event->setUser($user);
}
}
}
3 changes: 1 addition & 2 deletions src/Resources/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit a83481e

Please sign in to comment.