|
17 | 17 | use Symfony\Component\HttpFoundation\Session\Session;
|
18 | 18 | use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
|
19 | 19 | use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
|
| 20 | +use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
20 | 21 | use Symfony\Component\HttpKernel\HttpKernelInterface;
|
21 | 22 | use Symfony\Component\HttpKernel\KernelEvents;
|
22 | 23 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
|
| 24 | +use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
23 | 25 | use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
|
| 26 | +use Symfony\Component\Security\Core\Exception\UnsupportedUserException; |
| 27 | +use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; |
| 28 | +use Symfony\Component\Security\Core\User\User; |
| 29 | +use Symfony\Component\Security\Core\User\UserInterface; |
| 30 | +use Symfony\Component\Security\Core\User\UserProviderInterface; |
24 | 31 | use Symfony\Component\Security\Http\Firewall\ContextListener;
|
25 | 32 | use Symfony\Component\EventDispatcher\EventDispatcher;
|
26 | 33 |
|
@@ -238,6 +245,40 @@ public function testHandleRemovesTokenIfNoPreviousSessionWasFound()
|
238 | 245 | $listener->handle($event);
|
239 | 246 | }
|
240 | 247 |
|
| 248 | + public function testTryAllUserProvidersUntilASupportingUserProviderIsFound() |
| 249 | + { |
| 250 | + $tokenStorage = new TokenStorage(); |
| 251 | + $refreshedUser = new User('foobar', 'baz'); |
| 252 | + $this->handleEventWithPreviousSession($tokenStorage, array(new NotSupportingUserProvider(), new SupportingUserProvider($refreshedUser))); |
| 253 | + |
| 254 | + $this->assertSame($refreshedUser, $tokenStorage->getToken()->getUser()); |
| 255 | + } |
| 256 | + |
| 257 | + public function testNextSupportingUserProviderIsTriedIfPreviousSupportingUserProviderDidNotLoadTheUser() |
| 258 | + { |
| 259 | + $tokenStorage = new TokenStorage(); |
| 260 | + $refreshedUser = new User('foobar', 'baz'); |
| 261 | + $this->handleEventWithPreviousSession($tokenStorage, array(new SupportingUserProvider(), new SupportingUserProvider($refreshedUser))); |
| 262 | + |
| 263 | + $this->assertSame($refreshedUser, $tokenStorage->getToken()->getUser()); |
| 264 | + } |
| 265 | + |
| 266 | + public function testTokenIsSetToNullIfNoUserWasLoadedByTheRegisteredUserProviders() |
| 267 | + { |
| 268 | + $tokenStorage = new TokenStorage(); |
| 269 | + $this->handleEventWithPreviousSession($tokenStorage, array(new NotSupportingUserProvider(), new SupportingUserProvider())); |
| 270 | + |
| 271 | + $this->assertNull($tokenStorage->getToken()); |
| 272 | + } |
| 273 | + |
| 274 | + /** |
| 275 | + * @expectedException \RuntimeException |
| 276 | + */ |
| 277 | + public function testRuntimeExceptionIsThrownIfNoSupportingUserProviderWasRegistered() |
| 278 | + { |
| 279 | + $this->handleEventWithPreviousSession(new TokenStorage(), array(new NotSupportingUserProvider(), new NotSupportingUserProvider())); |
| 280 | + } |
| 281 | + |
241 | 282 | protected function runSessionOnKernelResponse($newToken, $original = null)
|
242 | 283 | {
|
243 | 284 | $session = new Session(new MockArraySessionStorage());
|
@@ -265,4 +306,67 @@ protected function runSessionOnKernelResponse($newToken, $original = null)
|
265 | 306 |
|
266 | 307 | return $session;
|
267 | 308 | }
|
| 309 | + |
| 310 | + private function handleEventWithPreviousSession(TokenStorageInterface $tokenStorage, array $userProviders) |
| 311 | + { |
| 312 | + $session = new Session(new MockArraySessionStorage()); |
| 313 | + $session->set('_security_context_key', serialize(new UsernamePasswordToken(new User('foo', 'bar'), '', 'context_key'))); |
| 314 | + |
| 315 | + $request = new Request(); |
| 316 | + $request->setSession($session); |
| 317 | + $request->cookies->set('MOCKSESSID', true); |
| 318 | + |
| 319 | + $listener = new ContextListener($tokenStorage, $userProviders, 'context_key'); |
| 320 | + $listener->handle(new GetResponseEvent($this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock(), $request, HttpKernelInterface::MASTER_REQUEST)); |
| 321 | + } |
| 322 | +} |
| 323 | + |
| 324 | +class NotSupportingUserProvider implements UserProviderInterface |
| 325 | +{ |
| 326 | + public function loadUserByUsername($username) |
| 327 | + { |
| 328 | + throw new UsernameNotFoundException(); |
| 329 | + } |
| 330 | + |
| 331 | + public function refreshUser(UserInterface $user) |
| 332 | + { |
| 333 | + throw new UnsupportedUserException(); |
| 334 | + } |
| 335 | + |
| 336 | + public function supportsClass($class) |
| 337 | + { |
| 338 | + return false; |
| 339 | + } |
| 340 | +} |
| 341 | + |
| 342 | +class SupportingUserProvider implements UserProviderInterface |
| 343 | +{ |
| 344 | + private $refreshedUser; |
| 345 | + |
| 346 | + public function __construct(User $refreshedUser = null) |
| 347 | + { |
| 348 | + $this->refreshedUser = $refreshedUser; |
| 349 | + } |
| 350 | + |
| 351 | + public function loadUserByUsername($username) |
| 352 | + { |
| 353 | + } |
| 354 | + |
| 355 | + public function refreshUser(UserInterface $user) |
| 356 | + { |
| 357 | + if (!$user instanceof User) { |
| 358 | + throw new UnsupportedUserException(); |
| 359 | + } |
| 360 | + |
| 361 | + if (null === $this->refreshedUser) { |
| 362 | + throw new UsernameNotFoundException(); |
| 363 | + } |
| 364 | + |
| 365 | + return $this->refreshedUser; |
| 366 | + } |
| 367 | + |
| 368 | + public function supportsClass($class) |
| 369 | + { |
| 370 | + return 'Symfony\Component\Security\Core\User\User' === $class; |
| 371 | + } |
268 | 372 | }
|
0 commit comments