8000 Merge branch '4.0' into 4.1 · symfony/symfony@8bbda2c · GitHub
[go: up one dir, main page]

Skip to content

Commit 8bbda2c

Browse files
Merge branch '4.0' into 4.1
* 4.0: fixed CS Avoiding session migration for stateless firewall UsernamePasswordJsonAuthenticationListener fixed CS Avoid migration on stateless firewalls [Serializer] deserialize from xml: Fix a collection that contains the only one element [PhpUnitBridge] Fix error on some Windows OS [DI] Deduplicate generated proxy classes
2 parents 836f121 + e540a16 commit 8bbda2c

24 files changed

+493
-27
lines changed

src/Symfony/Bridge/PhpUnit/bin/simple-phpunit

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ if (!file_exists("$PHPUNIT_DIR/phpunit-$PHPUNIT_VERSION/phpunit") || md5_file(__
9999
throw new \RuntimeException("Could not find $remoteZip");
100100
}
101101
stream_copy_to_stream($remoteZipStream, fopen("$PHPUNIT_VERSION.zip", 'wb'));
102+
} elseif ('\\' === DIRECTORY_SEPARATOR) {
103+
passthru("certutil -urlcache -split -f \"https://github.com/sebastianbergmann/phpunit/archive/$PHPUNIT_VERSION.zip\" $PHPUNIT_VERSION.zip");
102104
} else {
103105
@unlink("$PHPUNIT_VERSION.zip");
104106
passthru("wget -q https://github.com/sebastianbergmann/phpunit/archive/$PHPUNIT_VERSION.zip");

src/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/Factory/GuardAuthenticationFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public function create(ContainerBuilder $container, $id, $config, $userProvider,
8080
$listener = $container->setDefinition($listenerId, new ChildDefinition('security.authentication.listener.guard'));
8181
$listener->replaceArgument(2, $id);
8282
$listener->replaceArgument(3, $authenticators);
83+
$listener->addMethodCall('setSessionAuthenticationStrategy', array(new Reference('security.authentication.session_strategy.'.$id)));
8384

8485
// determine the entryPointId to use
8586
$entryPointId = $this->determineEntryPoint($defaultEntryPoint, $config);

src/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/Factory/HttpBasicFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public function create(ContainerBuilder $container, $id, $config, $userProvider,
4141
$listener = $container->setDefinition($listenerId, new ChildDefinition('security.authentication.listener.basic'));
4242
$listener->replaceArgument(2, $id);
4343
$listener->replaceArgument(3, new Reference($entryPointId));
44+
$listener->addMethodCall('setSessionAuthenticationStrategy', array(new Reference('security.authentication.session_strategy.'.$id)));
4445

4546
return array($provider, $listenerId, $entryPointId);
4647
}

src/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/Factory/JsonLoginFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ protected function createListener($container, $id, $config, $userProvider)
8989
$listener->replaceArgument(4, isset($config['success_handler']) ? new Reference($this->createAuthenticationSuccessHandler($container, $id, $config)) : null);
9090
$listener->replaceArgument(5, isset($config['failure_handler']) ? new Reference($this->createAuthenticationFailureHandler($container, $id, $config)) : null);
9191
$listener->replaceArgument(6, array_intersect_key($config, $this->options));
92+
$listener->addMethodCall('setSessionAuthenticationStrategy', array(new Reference('security.authentication.session_strategy.'.$id)));
9293

9394
$listenerId .= '.'.$id;
9495
$container->setDefinition($listenerId, $listener);

src/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/Factory/RemoteUserFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function create(ContainerBuilder $container, $id, $config, $userProvider,
3838
$listener = $container->setDefinition($listenerId, new ChildDefinition('security.authentication.listener.remote_user'));
3939
$listener->replaceArgument(2, $id);
4040
$listener->replaceArgument(3, $config['user']);
41+
$listener->addMethodCall('setSessionAuthenticationStrategy', array(new Reference('security.authentication.session_strategy.'.$id)));
4142

4243
return array($providerId, $listenerId, $defaultEntryPoint);
4344
}

src/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/Factory/SimplePreAuthenticationFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public function create(ContainerBuilder $container, $id, $config, $userProvider,
5757
$listener = $container->setDefinition($listenerId, new ChildDefinition('security.authentication.listener.simple_preauth'));
5858
$listener->replaceArgument(2, $id);
5959
$listener->replaceArgument(3, new Reference($config['authenticator']));
60+
$listener->addMethodCall('setSessionAuthenticationStrategy', array(new Reference('security.authentication.session_strategy.'.$id)));
6061

6162
return array($provider, $listenerId, null);
6263
}

src/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/Factory/X509Factory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function create(ContainerBuilder $container, $id, $config, $userProvider,
3939
$listener->replaceArgument(2, $id);
4040
$listener->replaceArgument(3, $config['user']);
4141
$listener->replaceArgument(4, $config['credentials']);
42+
$listener->addMethodCall('setSessionAuthenticationStrategy', array(new Reference('security.authentication.session_strategy.'.$id)));
4243

4344
return array($providerId, $listenerId, $defaultEntryPoint);
4445
}

src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,11 @@ private function createFirewall(ContainerBuilder $container, $id, $firewall, &$a
285285
}
286286

287287
$listeners[] = new Reference($this->createContextListener($container, $contextKey));
288+
$sessionStrategyId = 'security.authentication.session_strategy';
289+
} else {
290+
$sessionStrategyId = 'security.authentication.session_strategy_noop';
288291
}
292+
$container->setAlias(new Alias('security.authentication.session_strategy.'.$id, false), $sessionStrategyId);
289293

290294
$config->replaceArgument(6, $contextKey);
291295

src/Symfony/Bundle/SecurityBundle/Resources/config/security.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@
6464
</service>
6565
<service id="Symfony\Component\Security\Http\Session\SessionAuthenticationStrategyInterface" alias="security.authentication.session_strategy" />
6666

67+
<service id="security.authentication.session_strategy_noop" class="Symfony\Component\Security\Http\Session\SessionAuthenticationStrategy" public="false">
68+
<argument>none</argument>
69+
</service>
70+
6771
<service id="security.encoder_factory.generic" class="Symfony\Component\Security\Core\Encoder\EncoderFactory">
6872
<argument type="collection" />
6973
</service>

src/Symfony/Bundle/SecurityBundle/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"require": {
1919
"php": "^7.1.3",
2020
"ext-xml": "*",
21-
"symfony/security": "~4.1",
21+
"symfony/security": "^4.1.1",
2222
"symfony/dependency-injection": "^3.4.3|^4.0.3",
2323
"symfony/http-kernel": "^4.1"
2424
},

src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ private function collectLineage($class, array &$lineage)
398398

399399
private function generateProxyClasses()
400400
{
401+
$alreadyGenerated = array();
401402
$definitions = $this->container->getDefinitions();
402403
$strip = '' === $this->docStar && method_exists('Symfony\Component\HttpKernel\Kernel', 'stripComments');
403404
$proxyDumper = $this->getProxyDumper();
@@ -406,8 +407,12 @@ private function generateProxyClasses()
406407
if (!$proxyDumper->isProxyCandidate($definition)) {
407408
continue;
408409
}
410+
if (isset($alreadyGenerated[$class = $definition->getClass()])) {
411+
continue;
412+
}
413+
$alreadyGenerated[$class] = true;
409414
// register class' reflector for resource tracking
410-
$this->container->getReflectionClass($definition->getClass());
415+
$this->container->getReflectionClass($class);
411416
$proxyCode = "\n".$proxyDumper->getProxyCode($definition);
412417
if ($strip) {
413418
$proxyCode = "<?php\n".$proxyCode;

src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,19 @@ public function testCircularReferenceAllowanceForInlinedDefinitionsForLazyServic
616616
$this->addToAssertionCount(1);
617617
}
618618

619+
public function testDedupLazyProxy()
620+
{
621 10000 +
$container = new ContainerBuilder();
622+
$container->register('foo', 'stdClass')->setLazy(true)->setPublic(true);
623+
$container->register('bar', 'stdClass')->setLazy(true)->setPublic(true);
624+
$container->compile();
625+
626+
$dumper = new PhpDumper($container);
627+
$dumper->setProxyDumper(new \DummyProxyDumper());
628+
629+
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services_dedup_lazy_proxy.php', $dumper->dump());
630+
}
631+
619632
public function testLazyArgumentProvideGenerator()
620633
{
621634
require_once self::$fixturesPath.'/includes/classes.php';

src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/classes.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,12 @@ public function isProxyCandidate(Definition $definition)
9090

9191
public function getProxyFactoryCode(Definition $definition, $id, $factoryCall = null)
9292
{
93-
return " // lazy factory\n\n";
93+
return " // lazy factory for {$definition->getClass()}\n\n";
9494
}
9595

9696
public function getProxyCode(Definition $definition)
9797
{
98-
return "// proxy code\n";
98+
return "// proxy code for {$definition->getClass()}\n";
9999
}
100100
}
101101

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
4+
use Symfony\Component\DependencyInjection\ContainerInterface;
5+
use Symfony\Component\DependencyInjection\Container;
6+
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
7+
use Symfony\Component\DependencyInjection\Exception\LogicException;
8+
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
9+
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
10+
11+
/**
12+
* This class has been auto-generated
13+
* by the Symfony Dependency Injection Component.
14+
*
15+
* @final since Symfony 3.3
16+
*/
17+
class ProjectServiceContainer extends Container
18+
{
19+
private $parameters;
20+
private $targetDirs = array();
21+
22+
public function __construct()
23+
{
24+
$this->services = array();
25+
$this->methodMap = array(
26+
'bar' => 'getBarService',
27+
'foo' => 'getFooService',
28+
);
29+
30+
$this->aliases = array();
31+
}
32+
33+
public function getRemovedIds()
34+
{
35+
return array(
36+
'Psr\\Container\\ContainerInterface' => true,
37+
'Symfony\\Component\\DependencyInjection\\ContainerInterface' => true,
38+
);
39+
}
40+
41+
public function compile()
42+
{
43+
throw new LogicException('You cannot compile a dumped container that was already compiled.');
44+
}
45+
46+
public function isCompiled()
47+
{
48+
return true;
49+
}
50+
51+
public function isFrozen()
52+
{
53+
@trigger_error(sprintf('The %s() method is deprecated since Symfony 3.3 and will be removed in 4.0. Use the isCompiled() method instead.', __METHOD__), E_USER_DEPRECATED);
54+
55+
return true;
56+
}
57+
58+
protected function createProxy($class, \Closure $factory)
59+
{
60+
return $factory();
61+
}
62+
63+
/**
64+
* Gets the public 'bar' shared service.
65+
*
66+
* @return \stdClass
67+
*/
68+
protected function getBarService($lazyLoad = true)
69+
{
70+
// lazy factory for stdClass
71+
72+
return new \stdClass();
73+
}
74+
75+
/**
76+
* Gets the public 'foo' shared service.
77+
*
78+
* @return \stdClass
79+
*/
80+
protected function getFooService($lazyLoad = true)
81+
{
82+
// lazy factory for stdClass
83+
84+
return new \stdClass();
85+
}
86+
}
87+
88+
// proxy code for stdClass

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services_non_shared_lazy.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ protected function getBarService()
8181
*/
8282
protected function getFooService($lazyLoad = true)
8383
{
84-
// lazy factory
84+
// lazy factory for stdClass
8585

8686
return new \stdClass();
8787
}
8888
}
8989

90-
// proxy code
90+
// proxy code for stdClass

src/Symfony/Component/Security/Guard/GuardAuthenticatorHandler.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\Security\Core\User\UserInterface;
2121
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
2222
use Symfony\Component\Security\Http\SecurityEvents;
23+
use Symfony\Component\Security\Http\Session\SessionAuthenticationStrategyInterface;
2324

2425
/**
2526
* A utility class that does much of the *work* during the guard authentication process.
@@ -34,8 +35,8 @@
3435
class GuardAuthenticatorHandler
3536
{
3637
private $tokenStorage;
37-
3838
private $dispatcher;
39+
private $sessionStrategy;
3940

4041
public function __construct(TokenStorageInterface $tokenStorage, EventDispatcherInterface $eventDispatcher = null)
4142
{
@@ -48,7 +49,7 @@ public function __construct(TokenStorageInterface $tokenStorage, EventDispatcher
4849
*/
4950
public function authenticateWithToken(TokenInterface $token, Request $request)
5051
{
51-
$this->migrateSession($request);
52+
$this->migrateSession($request, $token);
5253
$this->tokenStorage->setToken($token);
5354

5455
if (null !== $this->dispatcher) {
@@ -110,11 +111,22 @@ public function handleAuthenticationFailure(AuthenticationException $authenticat
110111
));
111112
}
112113

113-
private function migrateSession(Request $request)
114+
/**
115+
* Call this method if your authentication token is stored to a session.
116+
*
117+
* @final
118+
*/
119+
public function setSessionAuthenticationStrategy(SessionAuthenticationStrategyInterface $sessionStrategy)
114120
{
115-
if (!$request->hasSession() || !$request->hasPreviousSession()) {
121+
$this->sessionStrategy = $sessionStrategy;
122+
}
123+
124+
private function migrateSession(Request $request, TokenInterface $token)
125+
{
126+
if (!$this->sessionStrategy || !$request->hasSession() || !$request->hasPreviousSession()) {
116127
return;
117128
}
118-
$request->getSession()->migrate(true);
129+
130+
$this->sessionStrategy->onAuthentication($request, $token);
119131
}
120132
}

src/Symfony/Component/Security/Guard/Tests/GuardAuthenticatorHandlerTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class GuardAuthenticatorHandlerTest extends TestCase
2626
private $dispatcher;
2727
private $token;
2828
private $request;
29+
private $sessionStrategy;
2930
private $guardAuthenticator;
3031

3132
public function testAuthenticateWithToken()
@@ -118,12 +119,38 @@ public function getTokenClearingTests()
118119
return $tests;
119120
}
120121

122+
public function testNoFailureIfSessionStrategyNotPassed()
123+
{
124+
$this->configurePreviousSession();
125+
126+
$this->tokenStorage->expects($this->once())
127+
->method('setToken')
128+
->with($this->token);
129+ 10000
130+
$handler = new GuardAuthenticatorHandler($this->tokenStorage, $this->dispatcher);
131+
$handler->authenticateWithToken($this->token, $this->request);
132+
}
133+
134+
public function testSessionStrategyIsCalled()
135+
{
136+
$this->configurePreviousSession();
137+
138+
$this->sessionStrategy->expects($this->once())
139+
->method('onAuthentication')
140+
->with($this->request, $this->token);
141+
142+
$handler = new GuardAuthenticatorHandler($this->tokenStorage, $this->dispatcher);
143+
$handler->setSessionAuthenticationStrategy($this->sessionStrategy);
144+
$handler->authenticateWithToken($this->token, $this->request);
145+
}
146+
121147
protected function setUp()
122148
{
123149
$this->tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')->getMock();
124150
$this->dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock();
125151
$this->token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
126152
$this->request = new Request(array(), array(), array(), array(), array(), array());
153+
$this->sessionStrategy = $this->getMockBuilder('Symfony\Component\Security\Http\Session\SessionAuthenticationStrategyInterface')->getMock();
127154
$this->guardAuthenticator = $this->getMockBuilder(AuthenticatorInterface::class)->getMock();
128155
}
129156

@@ -135,4 +162,14 @@ protected function tearDown()
135162
$this->request = null;
136163
$this->guardAuthenticator = null;
137164
}
165+
166+
private function configurePreviousSession()
167+
{
168+
$session = $this->getMockBuilder('Symfony\Component\HttpFoundation\Session\SessionInterface')->getMock();
169+
$session->expects($this->any())
170+
->method('getName')
171+
->willReturn('test_session_name');
172+
$this->request->setSession($session);
173+
$this->request->cookies->set('test_session_name', 'session_cookie_val');
174+
}
138175
}

0 commit comments

Comments
 (0)
0