Inheritance: implements eZ\Publish\Core\MVC\Symfony\SiteAccess\SiteAccessAware
 public function testPreviewedLocation()
 {
     $this->assertNull($this->previewHelper->getPreviewedLocation());
     $location = $this->getMock('\\eZ\\Publish\\API\\Repository\\Values\\Content\\Location');
     $this->previewHelper->setPreviewedLocation($location);
     $this->assertSame($location, $this->previewHelper->getPreviewedLocation());
 }
Example #2
0
    public function previewContentAction( $contentId, $versionNo, $language, $siteAccessName = null )
    {
        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();
        }

        $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 ),
            HttpKernelInterface::SUB_REQUEST
        );
        $response->headers->remove( 'cache-control' );
        $response->headers->remove( 'expires' );

        $this->previewHelper->restoreConfigScope();

        return $response;
    }
 /**
  * Renders the legacy website toolbar template.
  *
  * If the logged in user doesn't have the required permission, an empty response is returned
  *
  * @param mixed $locationId
  * @param Request $request
  *
  * @return Response
  */
 public function websiteToolbarAction($locationId, Request $request)
 {
     $response = new Response();
     if (isset($this->csrfProvider)) {
         $parameters['form_token'] = $this->csrfProvider->generateCsrfToken('legacy');
     }
     if ($this->previewHelper->isPreviewActive()) {
         $template = 'design:parts/website_toolbar_versionview.tpl';
         $previewedContent = $authValueObject = $this->previewHelper->getPreviewedContent();
         $previewedVersionInfo = $previewedContent->versionInfo;
         $parameters = array('object' => $previewedContent, 'version' => $previewedVersionInfo, 'language' => $previewedVersionInfo->initialLanguageCode, 'is_creator' => $previewedVersionInfo->creatorId === $this->getRepository()->getCurrentUser()->id);
     } elseif ($locationId === null) {
         return $response;
     } else {
         $authValueObject = $this->loadContentByLocationId($locationId);
         $template = 'design:parts/website_toolbar.tpl';
         $parameters = array('current_node_id' => $locationId, 'redirect_uri' => $request->attributes->get('semanticPathinfo'));
     }
     $authorizationAttribute = new AuthorizationAttribute('websitetoolbar', 'use', array('valueObject' => $authValueObject));
     if (!$this->authChecker->isGranted($authorizationAttribute)) {
         return $response;
     }
     $response->setContent($this->legacyTemplateEngine->render($template, $parameters));
     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));
     $helper = new ContentPreviewHelper($this->eventDispatcher, $this->configResolver);
     $helper->setSiteAccess($originalSiteAccess);
     $this->assertEquals($originalSiteAccess, $helper->restoreConfigScope());
 }
 public function testGetPreviewLocation()
 {
     $contentId = 123;
     $locationId = 456;
     $contentInfo = $this->getMockBuilder('eZ\\Publish\\API\\Repository\\Values\\Content\\ContentInfo')->setConstructorArgs(array(array('id' => $contentId, 'mainLocationId' => $locationId)))->getMockForAbstractClass();
     $location = $this->getMockBuilder('eZ\\Publish\\Core\\Repository\\Values\\Content\\Location')->setConstructorArgs(array(array('id' => $locationId, 'contentInfo' => $contentInfo)))->getMockForAbstractClass();
     $this->contentService->expects($this->once())->method('loadContentInfo')->with($contentId)->will($this->returnValue($contentInfo));
     $this->locationService->expects($this->once())->method('loadLocation')->with($locationId)->will($this->returnValue($location));
     $helper = new ContentPreviewHelper($this->contentService, $this->locationService, $this->eventDispatcher);
     $returnedLocation = $helper->getPreviewLocation($contentId);
     $this->assertSame($location, $returnedLocation);
     $this->assertSame($contentInfo, $returnedLocation->contentInfo);
 }
    /**
     * @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;
    }