Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/EventListener/SwitchUserListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of the FOSHttpCacheBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\HttpCacheBundle\EventListener;

use FOS\HttpCacheBundle\UserContextInvalidator;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Http\Event\SwitchUserEvent;
use Symfony\Component\Security\Http\SecurityEvents;

class SwitchUserListener implements EventSubscriberInterface
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a phpdoc with the explanation why we do this? (basically the TLDR of our discussion in the issue you reported)

{
private $invalidator;

public function __construct(UserContextInvalidator $invalidator)
{
$this->invalidator = $invalidator;
}

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array
{
return [
SecurityEvents::SWITCH_USER => 'onSwitchUser',
];
}

public function onSwitchUser(SwitchUserEvent $event)
{
$request = $event->getRequest();
$this->invalidator->invalidateContext($request->getSession()->getId());
}
}
5 changes: 5 additions & 0 deletions src/Resources/config/user_context.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
<argument type="service" id="fos_http_cache.user_context_invalidator" />
</service>

<service id="fos_http_cache.user_context.switch_user_listener" class="FOS\HttpCacheBundle\EventListener\SwitchUserListener" public="false">
<argument type="service" id="fos_http_cache.user_context_invalidator" />
<tag name="kernel.event_subscriber" />
</service>

<service id="fos_http_cache.user_context.session_listener" class="FOS\HttpCacheBundle\EventListener\SessionListener" decorates="session_listener" public="false">
<argument type="service" id="fos_http_cache.user_context.session_listener.inner" />
<argument /> <!-- set by extension -->
Expand Down
71 changes: 71 additions & 0 deletions tests/Functional/EventListener/SwitchUserListenerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

/*
* This file is part of the FOSHttpCacheBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\HttpCacheBundle\Tests\Functional\EventListener;

use FOS\HttpCache\ProxyClient\Varnish;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use Symfony\Bundle\FrameworkBundle\Client;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\BrowserKit\Cookie;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\User\User;

class SwitchUserListenerTest extends WebTestCase
{
use MockeryPHPUnitIntegration;

public function testSwitchUserCompatibility()
{
$client = static::createClient();
$session = $client->getContainer()->get('session');
$this->loginAsAdmin($client, $session);

$client->request('GET', '/secured_area/switch_user?_switch_user=user');
$client->request('GET', '/secured_area/switch_user');
$this->assertSame('user', substr($client->getResponse()->getContent(), 0, 2000));

$client->request('GET', '/secured_area/switch_user?_switch_user=_exit');
$client->request('GET', '/secured_area/switch_user');
$this->assertSame('admin', substr($client->getResponse()->getContent(), 0, 2000));
}

public function testInvalidateContext()
{
$client = static::createClient();
$session = $client->getContainer()->get('session');
$this->loginAsAdmin($client, $session);

$mock = \Mockery::mock(Varnish::class);
$mock->shouldReceive('invalidateTags')
->once()
->with(['fos_http_cache_hashlookup-test']);

$mock->shouldReceive('flush')
->once()
->andReturn(1);

$client->getContainer()->set('fos_http_cache.proxy_client.varnish', $mock);
$client->request('GET', '/secured_area/switch_user?_switch_user=user');
}

private function loginAsAdmin(Client $client, Session $session, $firewallName = 'secured_area', $sessionId = 'test')
{
$token = new UsernamePasswordToken(new User('admin', 'admin'), null, $firewallName, ['ROLE_ADMIN', 'ROLE_ALLOWED_TO_SWITCH']);

$session->setId($sessionId);
$session->set(sprintf('_security_%s', $firewallName), serialize($token));
$session->save();

$client->getCookieJar()->set(new Cookie($session->getName(), $session->getId()));
}
}
7 changes: 7 additions & 0 deletions tests/Functional/Fixtures/Controller/TestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,11 @@ public function sessionAction()

return $response;
}

public function switchUserAction()
{
$user = $this->getUser();

return new Response($user->getUsername());
}
}
2 changes: 2 additions & 0 deletions tests/Functional/Fixtures/app/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,13 @@ security:
memory:
users:
user: { password: user, roles: 'ROLE_USER' }
admin: { password: admin, roles: ['ROLE_ADMIN', 'ROLE_ALLOWED_TO_SWITCH'] }
encoders:
Symfony\Component\Security\Core\User\User: plaintext
firewalls:
secured_area:
pattern: ^/secured_area
anonymous:
switch_user: true
logout:
path: /secured_area/logout
4 changes: 4 additions & 0 deletions tests/Functional/Fixtures/app/config/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ test_cached_session:
path: /secured_area/cached_session
defaults: { _controller: FOS\HttpCacheBundle\Tests\Functional\Fixtures\Controller\TestController::sessionAction }

test_switch_user:
path: /secured_area/switch_user
defaults: { _controller: FOS\HttpCacheBundle\Tests\Functional\Fixtures\Controller\TestController::switchUserAction }

test_noncached:
path: /noncached
defaults: { _controller: FOS\HttpCacheBundle\Tests\Functional\Fixtures\Controller\TestController::contentAction }
Expand Down
4 changes: 4 additions & 0 deletions tests/Functional/Fixtures/app/config/routing_41.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ test_cached_session:
path: /secured_area/cached_session
controller: FOS\HttpCacheBundle\Tests\Functional\Fixtures\Controller\TestController::sessionAction

test_switch_user:
path: /secured_area/switch_user
controller: FOS\HttpCacheBundle\Tests\Functional\Fixtures\Controller\TestController::switchUserAction

test_noncached:
path: /noncached
controller: FOS\HttpCacheBundle\Tests\Functional\Fixtures\Controller\TestController::contentAction
Expand Down