Skip to content

Commit

Permalink
Fix Symfony 6.2 deprecation for Security helper
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertMe committed Mar 8, 2023
1 parent ff14fe0 commit 04329d0
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 04329d0

Please sign in to comment.