Skip to content

Commit

Permalink
Merge pull request #84 from Nyholm/contracts
Browse files Browse the repository at this point in the history
Use EventDispatcher from SymfonyContracts
  • Loading branch information
spideyfusion authored Aug 24, 2019
2 parents a4ccea1 + 9c96a22 commit 60431d4
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 24 deletions.
6 changes: 3 additions & 3 deletions Controller/AuthorizationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Trikoder\Bundle\OAuth2Bundle\Converter\UserConverterInterface;
use Trikoder\Bundle\OAuth2Bundle\Event\AuthorizationRequestResolveEvent;
use Trikoder\Bundle\OAuth2Bundle\Event\AuthorizationRequestResolveEventFactory;
Expand Down Expand Up @@ -58,8 +58,8 @@ public function indexAction(ServerRequestInterface $serverRequest, ResponseFacto

/** @var AuthorizationRequestResolveEvent $event */
$event = $this->eventDispatcher->dispatch(
OAuth2Events::AUTHORIZATION_REQUEST_RESOLVE,
$this->eventFactory->fromAuthorizationRequest($authRequest)
$this->eventFactory->fromAuthorizationRequest($authRequest),
OAuth2Events::AUTHORIZATION_REQUEST_RESOLVE
);

$authRequest->setUser($this->userConverter->toLeague($event->getUser()));
Expand Down
38 changes: 38 additions & 0 deletions DependencyInjection/CompilerPass/EventDispatcherCompilerPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Trikoder\Bundle\OAuth2Bundle\DependencyInjection\CompilerPass;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Trikoder\Bundle\OAuth2Bundle\Controller\AuthorizationController;
use Trikoder\Bundle\OAuth2Bundle\League\Repository\ScopeRepository;
use Trikoder\Bundle\OAuth2Bundle\League\Repository\UserRepository;
use Trikoder\Bundle\OAuth2Bundle\Service\BCEventDispatcher;

class EventDispatcherCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if ($container->has(EventDispatcherInterface::class)) {
return;
}

// Register a new service
$definition = new Definition(BCEventDispatcher::class);
$definition->addArgument(new Reference(\Symfony\Component\EventDispatcher\EventDispatcherInterface::class));
$container->setDefinition(BCEventDispatcher::class, $definition);

// Use our new service
$container->getDefinition(ScopeRepository::class)
->replaceArgument(3, new Reference(BCEventDispatcher::class));
$container->getDefinition(UserRepository::class)
->replaceArgument(1, new Reference(BCEventDispatcher::class));
$container->getDefinition(AuthorizationController::class)
->replaceArgument(1, new Reference(BCEventDispatcher::class));
}
}
11 changes: 3 additions & 8 deletions League/Repository/ScopeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use League\OAuth2\Server\Entities\ClientEntityInterface;
use League\OAuth2\Server\Exception\OAuthServerException;
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Trikoder\Bundle\OAuth2Bundle\Converter\ScopeConverterInterface;
use Trikoder\Bundle\OAuth2Bundle\Event\ScopeResolveEvent;
use Trikoder\Bundle\OAuth2Bundle\Manager\ClientManagerInterface;
Expand Down Expand Up @@ -79,13 +79,8 @@ public function finalizeScopes(
$scopes = $this->setupScopes($client, $this->scopeConverter->toDomainArray($scopes));

$event = $this->eventDispatcher->dispatch(
OAuth2Events::SCOPE_RESOLVE,
new ScopeResolveEvent(
$scopes,
new GrantModel($grantType),
$client,
$userIdentifier
)
new ScopeResolveEvent($scopes, new GrantModel($grantType), $client, $userIdentifier),
OAuth2Events::SCOPE_RESOLVE
);

return $this->scopeConverter->toLeagueArray($event->getScopes());
Expand Down
11 changes: 3 additions & 8 deletions League/Repository/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use League\OAuth2\Server\Entities\ClientEntityInterface;
use League\OAuth2\Server\Repositories\UserRepositoryInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Trikoder\Bundle\OAuth2Bundle\Converter\UserConverterInterface;
use Trikoder\Bundle\OAuth2Bundle\Event\UserResolveEvent;
use Trikoder\Bundle\OAuth2Bundle\Manager\ClientManagerInterface;
Expand Down Expand Up @@ -52,13 +52,8 @@ public function getUserEntityByUserCredentials(
$client = $this->clientManager->find($clientEntity->getIdentifier());

$event = $this->eventDispatcher->dispatch(
OAuth2Events::USER_RESOLVE,
new UserResolveEvent(
$username,
$password,
new GrantModel($grantType),
$client
)
new UserResolveEvent($username, $password, new GrantModel($grantType), $client),
OAuth2Events::USER_RESOLVE
);

$user = $event->getUser();
Expand Down
6 changes: 3 additions & 3 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
<argument type="service" id="Trikoder\Bundle\OAuth2Bundle\Manager\ScopeManagerInterface" />
<argument type="service" id="Trikoder\Bundle\OAuth2Bundle\Manager\ClientManagerInterface" />
<argument type="service" id="Trikoder\Bundle\OAuth2Bundle\Converter\ScopeConverter" />
<argument type="service" id="Symfony\Component\EventDispatcher\EventDispatcherInterface" />
<argument type="service" id="Symfony\Contracts\EventDispatcher\EventDispatcherInterface" />
</service>
<service id="trikoder.oauth2.league.repository.scope_repository" alias="Trikoder\Bundle\OAuth2Bundle\League\Repository\ScopeRepository" />
<service id="League\OAuth2\Server\Repositories\ScopeRepositoryInterface" alias="Trikoder\Bundle\OAuth2Bundle\League\Repository\ScopeRepository" />

<service id="Trikoder\Bundle\OAuth2Bundle\League\Repository\UserRepository">
<argument type="service" id="Trikoder\Bundle\OAuth2Bundle\Manager\ClientManagerInterface" />
<argument type="service" id="Symfony\Component\EventDispatcher\EventDispatcherInterface" />
<argument type="service" id="Symfony\Contracts\EventDispatcher\EventDispatcherInterface" />
<argument type="service" id="Trikoder\Bundle\OAuth2Bundle\Converter\UserConverter" />
</service>
<service id="trikoder.oauth2.league.repository.user_repository" alias="Trikoder\Bundle\OAuth2Bundle\League\Repository\UserRepository" />
Expand Down Expand Up @@ -113,7 +113,7 @@
<!-- Authorization controller -->
<service id="Trikoder\Bundle\OAuth2Bundle\Controller\AuthorizationController">
<argument type="service" id="League\OAuth2\Server\AuthorizationServer" />
<argument type="service" id="Symfony\Component\EventDispatcher\EventDispatcherInterface" />
<argument type="service" id="Symfony\Contracts\EventDispatcher\EventDispatcherInterface" />
<argument type="service" id="Trikoder\Bundle\OAuth2Bundle\Event\AuthorizationRequestResolveEventFactory" />
<argument type="service" id="Trikoder\Bundle\OAuth2Bundle\Converter\UserConverter" />
<tag name="controller.service_arguments" />
Expand Down
39 changes: 39 additions & 0 deletions Service/BCEventDispatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Trikoder\Bundle\OAuth2Bundle\Service;

use Symfony\Component\EventDispatcher\EventDispatcherInterface as LegacyEventDispatcher;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEventDispatcher;

/**
* This event dispatcher works as a BC layer for Symfony < 4.3.
*
* @author Tobias Nyholm <[email protected]>
*/
class BCEventDispatcher implements ContractsEventDispatcher
{
private $eventDispatcher;

public function __construct(LegacyEventDispatcher $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
}

/**
* This method is only used in this bundle. We will always call dispatch(object, string)
*/
public function dispatch($event/*, string $eventName = null*/)
{
$eventName = 1 < \func_num_args() ? func_get_arg(1) : null;
$eventName = $eventName ?? \get_class($event);

return $this->eventDispatcher->dispatch($eventName, $event);
}

public function addListener($eventName, $listener, $priority = 0)
{
return $this->eventDispatcher->addListener($eventName, $listener, $priority);
}
}
5 changes: 3 additions & 2 deletions Tests/Integration/AbstractIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
use Psr\Http\Message\ServerRequestInterface;
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Trikoder\Bundle\OAuth2Bundle\Converter\ScopeConverter;
use Trikoder\Bundle\OAuth2Bundle\Converter\UserConverter;
use Trikoder\Bundle\OAuth2Bundle\League\Entity\User;
Expand All @@ -49,6 +49,7 @@
use Trikoder\Bundle\OAuth2Bundle\Manager\ScopeManagerInterface;
use Trikoder\Bundle\OAuth2Bundle\Model\AccessToken;
use Trikoder\Bundle\OAuth2Bundle\Model\RefreshToken;
use Trikoder\Bundle\OAuth2Bundle\Service\BCEventDispatcher;
use Trikoder\Bundle\OAuth2Bundle\Tests\TestHelper;

abstract class AbstractIntegrationTest extends TestCase
Expand Down Expand Up @@ -108,7 +109,7 @@ protected function setUp(): void
$this->accessTokenManager = new AccessTokenManager();
$this->refreshTokenManager = new RefreshTokenManager();
$this->authCodeManager = new AuthorizationCodeManager();
$this->eventDispatcher = new EventDispatcher();
$this->eventDispatcher = new BCEventDispatcher(new EventDispatcher());

$scopeConverter = new ScopeConverter();
$scopeRepository = new ScopeRepository($this->scopeManager, $this->clientManager, $scopeConverter, $this->eventDispatcher);
Expand Down
2 changes: 2 additions & 0 deletions TrikoderOAuth2Bundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Symfony\Bundle\SecurityBundle\DependencyInjection\SecurityExtension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Trikoder\Bundle\OAuth2Bundle\DependencyInjection\CompilerPass\EventDispatcherCompilerPass;
use Trikoder\Bundle\OAuth2Bundle\DependencyInjection\Security\OAuth2Factory;
use Trikoder\Bundle\OAuth2Bundle\DependencyInjection\TrikoderOAuth2Extension;

Expand Down Expand Up @@ -41,6 +42,7 @@ private function configureSecurityExtension(ContainerBuilder $container): void

private function configureDoctrineExtension(ContainerBuilder $container): void
{
$container->addCompilerPass(new EventDispatcherCompilerPass());
$container->addCompilerPass(
DoctrineOrmMappingsPass::createXmlMappingDriver(
[
Expand Down
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
"doctrine/doctrine-bundle": "^1.8",
"doctrine/orm": "^2.6",
"league/oauth2-server": "^7.2",
"psr/event-dispatcher": "^1.0",
"psr/http-factory": "^1.0",
"sensio/framework-extra-bundle": "^5.3",
"symfony/event-dispatcher-contracts": "^1.1",
"symfony/framework-bundle": "^3.4|^4.2",
"symfony/psr-http-message-bridge": "^1.2",
"symfony/security-bundle": "^3.4|^4.2"
Expand Down

0 comments on commit 60431d4

Please sign in to comment.