Skip to content

Commit e5bde80

Browse files
committed
symfony 6 (requires SF 5.3+)
1 parent b7682aa commit e5bde80

File tree

8 files changed

+46
-32
lines changed

8 files changed

+46
-32
lines changed

Controller/GraphQL/LoginController.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22
namespace TheCodingMachine\GraphQLite\Bundle\Controller\GraphQL;
33

44

5+
use RuntimeException;
56
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
67
use Symfony\Component\HttpFoundation\Request;
78
use Symfony\Component\HttpFoundation\Session\SessionInterface;
9+
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
810
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
911
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
10-
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
1112
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
13+
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
14+
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
1215
use Symfony\Component\Security\Core\User\UserInterface;
1316
use Symfony\Component\Security\Core\User\UserProviderInterface;
1417
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
@@ -22,7 +25,7 @@ class LoginController
2225
*/
2326
private $userProvider;
2427
/**
25-
* @var UserPasswordEncoderInterface
28+
* @var UserPasswordHasherInterface
2629
*/
2730
private $passwordEncoder;
2831
/**
@@ -42,7 +45,7 @@ class LoginController
4245
*/
4346
private $eventDispatcher;
4447

45-
public function __construct(UserProviderInterface $userProvider, UserPasswordEncoderInterface $passwordEncoder, TokenStorageInterface $tokenStorage, SessionInterface $session, EventDispatcherInterface $eventDispatcher, string $firewallName)
48+
public function __construct(UserProviderInterface $userProvider, UserPasswordHasherInterface $passwordEncoder, TokenStorageInterface $tokenStorage, SessionInterface $session, EventDispatcherInterface $eventDispatcher, string $firewallName)
4649
{
4750
$this->userProvider = $userProvider;
4851
$this->passwordEncoder = $passwordEncoder;
@@ -58,12 +61,16 @@ public function __construct(UserProviderInterface $userProvider, UserPasswordEnc
5861
public function login(string $userName, string $password, Request $request): UserInterface
5962
{
6063
try {
61-
$user = $this->userProvider->loadUserByUsername($userName);
62-
} catch (UsernameNotFoundException $e) {
64+
$user = $this->userProvider->loadUserByIdentifier($userName);
65+
} catch (UserNotFoundException $e) {
6366
// FIXME: should we return null instead???
6467
throw InvalidUserPasswordException::create($e);
6568
}
6669

70+
if (!$user instanceof PasswordAuthenticatedUserInterface) {
71+
throw new RuntimeException('$user has to implements ' . PasswordAuthenticatedUserInterface::class);
72+
}
73+
6774
if (!$this->passwordEncoder->isPasswordValid($user, $password)) {
6875
throw InvalidUserPasswordException::create();
6976
}
@@ -72,7 +79,7 @@ public function login(string $userName, string $password, Request $request): Use
7279

7380
// Handle getting or creating the user entity likely with a posted form
7481
// The third parameter "main" can change according to the name of your firewall in security.yml
75-
$token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
82+
$token = new UsernamePasswordToken($user, 'main', $user->getRoles());
7683
$this->tokenStorage->setToken($token);
7784

7885
// If the firewall name is not main, then the set value would be instead:

DependencyInjection/GraphQLiteCompilerPass.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Symfony\Component\Cache\Adapter\ApcuAdapter;
1313
use Symfony\Component\Cache\Adapter\PhpFilesAdapter;
1414
use Symfony\Component\Cache\Psr16Cache;
15+
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
1516
use TheCodingMachine\GraphQLite\Mappers\StaticClassListTypeMapperFactory;
1617
use Webmozart\Assert\Assert;
1718
use function class_exists;
@@ -33,7 +34,6 @@
3334
use Symfony\Component\DependencyInjection\Reference;
3435
use Symfony\Component\HttpFoundation\Session\SessionInterface;
3536
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
36-
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
3737
use Symfony\Component\Security\Core\User\UserInterface;
3838
use TheCodingMachine\CacheUtils\ClassBoundCache;
3939
use TheCodingMachine\CacheUtils\ClassBoundCacheContract;
@@ -104,7 +104,7 @@ public function process(ContainerBuilder $container): void
104104
$disableLogin = false;
105105
if ($container->getParameter('graphqlite.security.enable_login') === 'auto'
106106
&& (!$container->has($firewallConfigServiceName) ||
107-
!$container->has(UserPasswordEncoderInterface::class) ||
107+
!$container->has(UserPasswordHasherInterface::class) ||
108108
!$container->has(TokenStorageInterface::class) ||
109109
!$container->has(SessionInterface::class)
110110
)) {
@@ -122,7 +122,7 @@ public function process(ContainerBuilder $container): void
122122
if (!$container->has(SessionInterface::class)) {
123123
throw new GraphQLException('In order to enable the login/logout mutations (via the graphqlite.security.enable_login parameter), you need to enable session support (via the "framework.session.enabled" config parameter).');
124124
}
125-
if (!$container->has(UserPasswordEncoderInterface::class) || !$container->has(TokenStorageInterface::class) || !$container->has($firewallConfigServiceName)) {
125+
if (!$container->has(UserPasswordHasherInterface::class) || !$container->has(TokenStorageInterface::class) || !$container->has($firewallConfigServiceName)) {
126126
throw new GraphQLException('In order to enable the login/logout mutations (via the graphqlite.security.enable_login parameter), you need to install the security bundle. Please be sure to correctly configure the user provider (in the security.providers configuration settings)');
127127
}
128128
}

DependencyInjection/GraphQLiteExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
class GraphQLiteExtension extends Extension
1919
{
2020

21-
public function getAlias()
21+
public function getAlias(): string
2222
{
2323
return 'graphqlite';
2424
}

GraphQLiteBundle.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace TheCodingMachine\GraphQLite\Bundle;
55

6+
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
67
use TheCodingMachine\GraphQLite\Bundle\DependencyInjection\GraphQLiteExtension;
78
use TheCodingMachine\GraphQLite\Bundle\DependencyInjection\OverblogGraphiQLEndpointWiringPass;
89
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
@@ -20,7 +21,7 @@ public function build(ContainerBuilder $container): void
2021
$container->addCompilerPass(new OverblogGraphiQLEndpointWiringPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -1);
2122
}
2223

23-
public function getContainerExtension()
24+
public function getContainerExtension(): ?ExtensionInterface
2425
{
2526
if (null === $this->extension) {
2627
$this->extension = new GraphQLiteExtension();

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77

88
# GraphQLite bundle
99

10-
Symfony 4 bundle for the thecodingmachine/graphqlite package.
10+
Symfony 5 bundle for the thecodingmachine/graphqlite package.
1111

1212
See [thecodingmachine/graphqlite](https://github.com/thecodingmachine/graphqlite)

Tests/FunctionalTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
namespace TheCodingMachine\GraphQLite\Bundle\Tests;
44

5+
use Symfony\Component\Security\Core\User\InMemoryUser;
56
use function json_decode;
67
use PHPUnit\Framework\TestCase;
78
use Psr\Container\ContainerInterface;
89
use Symfony\Component\HttpFoundation\Request;
910
use Symfony\Component\HttpFoundation\Session\Session;
1011
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
1112
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
12-
use Symfony\Component\Security\Core\User\User;
1313
use TheCodingMachine\GraphQLite\Bundle\Controller\GraphQLiteController;
1414
use TheCodingMachine\GraphQLite\GraphQLRuntimeException as GraphQLException;
1515
use TheCodingMachine\GraphQLite\Schema;
@@ -545,9 +545,9 @@ public function testMaxQueryDepth(): void
545545
private function logIn(ContainerInterface $container)
546546
{
547547
// put a token into the storage so the final calls can function
548-
$user = new User('foo', 'pass');
549-
$token = new UsernamePasswordToken($user, '', 'provider', ['ROLE_USER']);
550-
$container->get('security.token_storage')->setToken($token);
548+
$user = new InMemoryUser('foo', 'pass');
549+
$token = new UsernamePasswordToken($user, 'provider', ['ROLE_USER']);
550+
$container->get('security.untracked_token_storage')->setToken($token);
551551
}
552552

553553
/**

Tests/GraphQLiteTestingKernel.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
99
use Symfony\Bundle\SecurityBundle\SecurityBundle;
1010
use Symfony\Component\Config\Loader\LoaderInterface;
11+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1112
use Symfony\Component\DependencyInjection\ContainerBuilder;
12-
use Symfony\Component\HttpFoundation\Session\Storage\Handler\NullSessionHandler;
1313
use Symfony\Component\HttpKernel\Kernel;
1414
use TheCodingMachine\GraphQLite\Bundle\GraphQLiteBundle;
15-
use Symfony\Component\Security\Core\User\User;
15+
use Symfony\Component\Security\Core\User\InMemoryUser;
1616
use function class_exists;
1717
use function serialize;
1818

19-
class GraphQLiteTestingKernel extends Kernel
19+
class GraphQLiteTestingKernel extends Kernel implements CompilerPassInterface
2020
{
2121
use MicroKernelTrait;
2222

@@ -84,7 +84,7 @@ public function __construct(bool $enableSession = true,
8484
$this->typesNamespace = $typesNamespace;
8585
}
8686

87-
public function registerBundles()
87+
public function registerBundles(): iterable
8888
{
8989
$bundles = [ new FrameworkBundle() ];
9090
if (class_exists(SecurityBundle::class)) {
@@ -112,7 +112,7 @@ public function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
112112
if ($this->enableSession) {
113113
$frameworkConf['session'] =[
114114
'enabled' => true,
115-
'handler_id' => NullSessionHandler::class
115+
'storage_factory_id' => 'session.storage.factory.mock_file',
116116
];
117117
}
118118

@@ -143,12 +143,11 @@ public function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
143143
],
144144
'firewalls' => [
145145
'main' => [
146-
'anonymous' => true,
147146
'provider' => 'in_memory'
148147
]
149148
],
150-
'encoders' => [
151-
User::class => 'plaintext',
149+
'password_hashers' => [
150+
InMemoryUser::class => 'plaintext',
152151
],
153152
));
154153
}
@@ -196,8 +195,15 @@ protected function configureRoutes(/*RoutingConfigurator*/ $routes)
196195
$routes->import(__DIR__.'/../Resources/config/routes.xml');
197196
}
198197

199-
public function getCacheDir()
198+
public function getCacheDir(): string
200199
{
201200
return __DIR__.'/../cache/'.($this->enableSession?'withSession':'withoutSession').$this->enableLogin.($this->enableSecurity?'withSecurity':'withoutSecurity').$this->enableMe.'_'.($this->introspection?'withIntrospection':'withoutIntrospection').'_'.$this->maximumQueryComplexity.'_'.$this->maximumQueryDepth.'_'.md5(serialize($this->controllersNamespace).'_'.md5(serialize($this->typesNamespace)));
202201
}
202+
203+
public function process(ContainerBuilder $container): void
204+
{
205+
if ($container->hasDefinition('security.untracked_token_storage')) {
206+
$container->getDefinition('security.untracked_token_storage')->setPublic(true);
207+
}
208+
}
203209
}

composer.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,24 @@
2020
"ext-json": "*",
2121
"thecodingmachine/graphqlite" : "^5.0",
2222
"thecodingmachine/graphqlite-symfony-validator-bridge" : "^5.0",
23-
"symfony/framework-bundle": "^4.2 || ^5",
24-
"symfony/validator": "^4.2 || ^5",
25-
"symfony/translation": "^4.2 || ^5",
23+
"symfony/framework-bundle": "^5.3 || ^6",
24+
"symfony/validator": "^5.3 || ^6",
25+
"symfony/translation": "^5.3 || ^6",
2626
"doctrine/annotations": "^1.13",
2727
"symfony/psr-http-message-bridge": "^2.0",
2828
"nyholm/psr7": "^1.1",
2929
"laminas/laminas-diactoros": "^2.2.2",
3030
"overblog/graphiql-bundle": "^0.1.2 | ^0.2",
3131
"thecodingmachine/cache-utils": "^1",
32-
"symfony/console": "^4.1.9 | ^5"
32+
"symfony/console": "^5.3 || ^6"
3333
},
3434
"require-dev": {
35-
"symfony/security-bundle": "^4.2 || ^5",
36-
"symfony/yaml": "^4.2 || ^5",
35+
"symfony/security-bundle": "^5.3 || ^6",
36+
"symfony/yaml": "^5.3 || ^6",
3737
"phpstan/phpstan": "^0.12.90",
3838
"beberlei/porpaginas": "^1.2",
3939
"php-coveralls/php-coveralls": "^2.1.0",
40-
"symfony/phpunit-bridge": "^5.3",
40+
"symfony/phpunit-bridge": "^5.3 || ^6",
4141
"thecodingmachine/phpstan-strict-rules": "^v0.12.1",
4242
"composer/package-versions-deprecated": "^1.8",
4343
"phpstan/phpstan-webmozart-assert": "^0.12.12"

0 commit comments

Comments
 (0)