/** * @throws NotImplementedException If Content is missing location as this is not supported in current version */ public function previewContentAction(Request $request, $contentId, $versionNo, $language, $siteAccessName = null) { $this->previewHelper->setPreviewActive(true); try { $content = $this->contentService->loadContent($contentId, array($language), $versionNo); $location = $this->locationProvider->loadMainLocation($contentId); if (!$location instanceof Location) { throw new NotImplementedException("Preview for content without locations"); } $this->previewHelper->setPreviewedContent($content); $this->previewHelper->setPreviewedLocation($location); } catch (UnauthorizedException $e) { throw new AccessDeniedException(); } if (!$this->authorizationChecker->isGranted(new AuthorizationAttribute('content', 'versionread', array('valueObject' => $content)))) { throw new AccessDeniedException(); } $siteAccess = $this->previewHelper->getOriginalSiteAccess(); // Only switch if $siteAccessName is set and different from original if ($siteAccessName !== null && $siteAccessName !== $siteAccess->name) { $siteAccess = $this->previewHelper->changeConfigScope($siteAccessName); } $response = $this->kernel->handle($this->getForwardRequest($location, $content, $siteAccess, $request), HttpKernelInterface::SUB_REQUEST); $response->headers->remove('cache-control'); $response->headers->remove('expires'); $this->previewHelper->restoreConfigScope(); $this->previewHelper->setPreviewActive(false); return $response; }
public function testRestoreConfigScope() { $originalSiteAccess = new SiteAccess('foo', 'bar'); $event = new ScopeChangeEvent($originalSiteAccess); $this->eventDispatcher->expects($this->once())->method('dispatch')->with(MVCEvents::CONFIG_SCOPE_RESTORE, $this->equalTo($event)); $this->previewHelper->setSiteAccess($originalSiteAccess); $this->assertEquals($originalSiteAccess, $this->previewHelper->restoreConfigScope()); }
public function previewContentAction($contentId, $versionNo, $language, $siteAccessName) { try { $content = $this->contentService->loadContent($contentId, array($language), $versionNo); $location = $this->previewHelper->getPreviewLocation($contentId); } catch (UnauthorizedException $e) { throw new AccessDeniedException(); } if (!$this->securityContext->isGranted(new AuthorizationAttribute('content', 'versionread', array('valueObject' => $content)))) { throw new AccessDeniedException(); } $newSiteAccess = $this->previewHelper->changeConfigScope($siteAccessName); $response = $this->kernel->handle($this->getForwardRequest($location, $content, $newSiteAccess), HttpKernelInterface::SUB_REQUEST); $response->headers->remove('cache-control'); $response->headers->remove('expires'); $this->previewHelper->restoreConfigScope(); return $response; }
/** * @throws NotImplementedException If Content is missing location as this is not supported in current version */ public function previewContentAction(Request $request, $contentId, $versionNo, $language, $siteAccessName = null) { $this->previewHelper->setPreviewActive(true); try { $content = $this->contentService->loadContent($contentId, array($language), $versionNo); $location = $this->locationProvider->loadMainLocation($contentId); if (!$location instanceof Location) { throw new NotImplementedException('Preview for content without locations'); } $this->previewHelper->setPreviewedContent($content); $this->previewHelper->setPreviewedLocation($location); } catch (UnauthorizedException $e) { throw new AccessDeniedException(); } if (!$this->authorizationChecker->isGranted(new AuthorizationAttribute('content', 'versionread', array('valueObject' => $content)))) { throw new AccessDeniedException(); } $siteAccess = $this->previewHelper->getOriginalSiteAccess(); // Only switch if $siteAccessName is set and different from original if ($siteAccessName !== null && $siteAccessName !== $siteAccess->name) { $siteAccess = $this->previewHelper->changeConfigScope($siteAccessName); } try { $response = $this->kernel->handle($this->getForwardRequest($location, $content, $siteAccess, $request, $language), HttpKernelInterface::SUB_REQUEST, false); } catch (\Exception $e) { if ($location->isDraft() && $this->controllerChecker->usesCustomController($content, $location)) { // @todo This should probably be an exception that embeds the original one $message = <<<EOF <p>The view that rendered this location draft uses a custom controller, and resulted in a fatal error.</p> <p>Location View is deprecated, as it causes issues with preview, such as an empty location id when previewing the first version of a content.</p> EOF; throw new Exception($message, 0, $e); } else { throw $e; } } $response->headers->remove('cache-control'); $response->headers->remove('expires'); $this->previewHelper->restoreConfigScope(); $this->previewHelper->setPreviewActive(false); return $response; }