Skip to content

Commit

Permalink
EZP-29144: Make websiteToolbarAction's Response cacheable (ezsystems#152
Browse files Browse the repository at this point in the history
)

* EZP-29144: Make websiteToolbarAction's Response cacheable

* fixup! EZP-29144: Make websiteToolbarAction's Response cacheable

* fixup! EZP-29144: Make websiteToolbarAction's Response cacheable

* fixup! EZP-29144: Make websiteToolbarAction's Response cacheable
  • Loading branch information
natanael89 authored and andrerom committed Jul 6, 2018
1 parent e200c6f commit ef6e9c8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
44 changes: 43 additions & 1 deletion bundle/Controller/WebsiteToolbarController.php
Expand Up @@ -39,12 +39,24 @@ class WebsiteToolbarController extends Controller
/** @var ContentPreviewHelper */
private $previewHelper;

/** @var bool */
private $viewCache;

/** @var bool */
private $ttlCache;

/** @var int */
private $defaultTtl;

public function __construct(
EngineInterface $engine,
ContentService $contentService,
LocationService $locationService,
AuthorizationCheckerInterface $authChecker,
ContentPreviewHelper $previewHelper,
$viewCache,
$ttlCache,
$defaultTtl,
CsrfTokenManagerInterface $csrfTokenManager = null
) {
$this->legacyTemplateEngine = $engine;
Expand All @@ -53,6 +65,9 @@ public function __construct(
$this->authChecker = $authChecker;
$this->csrfTokenManager = $csrfTokenManager;
$this->previewHelper = $previewHelper;
$this->viewCache = $viewCache;
$this->ttlCache = $ttlCache;
$this->defaultTtl = $defaultTtl;
}

/**
Expand All @@ -67,7 +82,7 @@ public function __construct(
*/
public function websiteToolbarAction($locationId, Request $request)
{
$response = new Response();
$response = $this->buildResponse();

if (isset($this->csrfTokenManager)) {
$parameters['form_token'] = $this->csrfTokenManager->getToken('legacy')->getValue();
Expand Down Expand Up @@ -109,6 +124,33 @@ public function websiteToolbarAction($locationId, Request $request)
return $response;
}

/**
* Build the response so that depending on settings it's cacheable.
*
* @return \Symfony\Component\HttpFoundation\Response
*/
protected function buildResponse()
{
$response = new Response();
if ($this->viewCache === true) {
$response->setPublic();

if ($this->ttlCache === true) {
$response->setSharedMaxAge(
$this->defaultTtl
);
}

// Make the response vary against Cookie header ensures that an HTTP
// reverse proxy caches the different possible variations of the
// response as it can depend on user role for instance. X-User-Hash cannot
// be used since the website toolbar can have Owner( Self ) Policy Limitation.
$response->setVary('Cookie');
}

return $response;
}

/**
* @return \eZ\Publish\API\Repository\Values\Content\Content
*/
Expand Down
3 changes: 3 additions & 0 deletions bundle/Resources/config/services.yml
Expand Up @@ -172,6 +172,9 @@ services:
- "@ezpublish.api.service.location"
- "@security.authorization_checker"
- "@ezpublish.content_preview_helper"
- "$content.view_cache$"
- "$content.ttl_cache$"
- "$content.default_ttl$"
- "@?security.csrf.token_manager"

ezpublish_legacy.router:
Expand Down

0 comments on commit ef6e9c8

Please sign in to comment.