Skip to content

Commit f88e600

Browse files
xabbuhfabpot
authored andcommitted
migrate session after remember me authentication
1 parent 3dc2244 commit f88e600

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

src/Symfony/Component/Security/Http/Firewall/RememberMeListener.php

+8
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
2121
use Symfony\Component\Security\Http\SecurityEvents;
2222
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
23+
use Symfony\Component\Security\Http\Session\SessionAuthenticationStrategy;
2324

2425
/**
2526
* RememberMeListener implements authentication capabilities via a cookie.
@@ -33,6 +34,7 @@ class RememberMeListener implements ListenerInterface
3334
private $authenticationManager;
3435
private $logger;
3536
private $dispatcher;
37+
private $sessionStrategy;
3638

3739
/**
3840
* Constructor.
@@ -50,6 +52,7 @@ public function __construct(SecurityContextInterface $securityContext, RememberM
5052
$this->authenticationManager = $authenticationManager;
5153
$this->logger = $logger;
5254
$this->dispatcher = $dispatcher;
55+
$this->sessionStrategy = new SessionAuthenticationStrategy(SessionAuthenticationStrategy::MIGRATE);
5356
}
5457

5558
/**
@@ -70,6 +73,11 @@ public function handle(GetResponseEvent $event)
7073

7174
try {
7275
$token = $this->authenticationManager->authenticate($token);
76+
77+
if ($request->hasSession() && $request->getSession()->isStarted()) {
78+
$this->sessionStrategy->onAuthentication($request, $token);
79+
}
80+
7381
$this->securityContext->setToken($token);
7482

7583
if (null !== $this->dispatcher) {

src/Symfony/Component/Security/Tests/Http/Firewall/RememberMeListenerTest.php

+63
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,69 @@ public function testOnCoreSecurity()
138138
$listener->handle($event);
139139
}
140140

141+
public function testSessionStrategy()
142+
{
143+
list($listener, $tokenStorage, $service, $manager) = $this->getListener(false, true, true);
144+
145+
$tokenStorage
146+
->expects($this->once())
147+
->method('getToken')
148+
->will($this->returnValue(null))
149+
;
150+
151+
$token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
152+
$service
153+
->expects($this->once())
154+
->method('autoLogin')
155+
->will($this->returnValue($token))
156+
;
157+
158+
$tokenStorage
159+
->expects($this->once())
160+
->method('setToken')
161+
->with($this->equalTo($token))
162+
;
163+
164+
$manager
165+
->expects($this->once())
166+
->method('authenticate')
167+
->will($this->returnValue($token))
168+
;
169+
170+
$session = $this->getMock('\Symfony\Component\HttpFoundation\Session\SessionInterface');
171+
$session
172+
->expects($this->once())
173+
->method('isStarted')
174+
->will($this->returnValue(true))
175+
;
176+
$session
177+
->expects($this->once())
178+
->method('migrate')
179+
;
180+
181+
$request = $this->getMock('\Symfony\Component\HttpFoundation\Request');
182+
$request
183+
->expects($this->any())
184+
->method('hasSession')
185+
->will($this->returnValue(true))
186+
;
187+
188+
$request
189+
->expects($this->any())
190+
->method('getSession')
191+
->will($this->returnValue($session))
192+
;
193+
194+
$event = $this->getGetResponseEvent();
195+
$event
196+
->expects($this->once())
197+
->method('getRequest')
198+
->will($this->returnValue($request))
199+
;
200+
201+
$listener->handle($event);
202+
}
203+
141204
protected function getGetResponseEvent()
142205
{
143206
return $this->getMock('Symfony\Component\HttpKernel\Event\GetResponseEvent', array(), array(), '', false);

0 commit comments

Comments
 (0)