Skip to content

Commit

Permalink
Fix user hash tag being applied to main response when using Sf Proxy (#…
Browse files Browse the repository at this point in the history
…64)

* Fix user hash tag being applied to main response when using Symfony Proxy

* CS

* Update UserContextSubscriber.php
  • Loading branch information
andrerom authored Apr 23, 2018
1 parent 8fed12e commit 79a76e9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
16 changes: 7 additions & 9 deletions src/EventSubscriber/UserContextSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,18 @@
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use FOS\HttpCache\Handler\TagHandler;

/**
* Tag /_fos_user_context_hash responses, so we can expire/clear it by tag.
*/
class UserContextSubscriber implements EventSubscriberInterface
{
/** @var \FOS\HttpCache\Handler\TagHandler */
private $tagHandler;
/** @var string */
private $tagHeader = 'xkey';

public function __construct(TagHandler $tagHandler)
public function __construct($tagHeader)
{
$this->tagHandler = $tagHandler;
$this->tagHeader = $tagHeader;
}

public static function getSubscribedEvents()
Expand Down Expand Up @@ -48,9 +47,8 @@ public function tagUserContext(FilterResponseEvent $event)
return;
}

// Improvement potential (mainly relevant if we plan to increase hash ttl notably):
// - Get user id to also add tag by id, so user (un)assign don't need to clear all hashes
// - If done in layer generating this, we could also tag role id so changes to roles only expire affected hashes
$this->tagHandler->addTags(['ez-user-context-hash']);
// We need to set tag directly on repsonse here to make sure this does not also get applied to the main request
// when using Symfony Proxy, as tag handler does not clear tags between requests.
$response->headers->set($this->tagHeader, 'ez-user-context-hash');

This comment has been minimized.

Copy link
@dbu

dbu Oct 4, 2018

This comment has been minimized.

Copy link
@andrerom

andrerom Oct 4, 2018

Author Contributor

Good, then we can get rid of this as well when we move to 2.x ;)
(eta before our 3.0 planned for Q1 next year)

}
}
2 changes: 1 addition & 1 deletion src/Resources/config/view_cache.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ services:

ezplatform.user_context_tagger.response_subscriber:
class: EzSystems\PlatformHttpCacheBundle\EventSubscriber\UserContextSubscriber
arguments: ['@fos_http_cache.handler.tag_handler']
arguments: ['%ezplatform.http_cache.tags.header%']
tags:
- { name: kernel.event_subscriber }

Expand Down

0 comments on commit 79a76e9

Please sign in to comment.