Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

ViewController.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\MVC\Symfony\Controller\Content;
8
9 use eZ\Publish\API\Repository\Repository;
10 use eZ\Publish\API\Repository\Values\Content\Content;
11 use eZ\Publish\API\Repository\Values\Content\Location;
12 use eZ\Publish\Core\Base\Exceptions\NotFoundException;
13 use eZ\Publish\Core\MVC\Symfony\Controller\Controller;
14 use eZ\Publish\Core\MVC\Symfony\MVCEvents;
15 use eZ\Publish\Core\MVC\Symfony\Event\APIContentExceptionEvent;
16 use eZ\Publish\Core\MVC\Symfony\Security\Authorization\Attribute as AuthorizationAttribute;
17 use eZ\Publish\Core\Base\Exceptions\UnauthorizedException;
18 use eZ\Publish\Core\MVC\Symfony\View\ContentView;
19 use eZ\Publish\Core\MVC\Symfony\View\ViewManagerInterface;
20 use Symfony\Component\HttpFoundation\Response;
21 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
22 use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
23 use Symfony\Component\Security\Core\Exception\AccessDeniedException;
24 use DateTime;
25 use Exception;
26
32 class ViewController extends Controller
33 {
35 protected $viewManager;
36
38 private $authorizationChecker;
39
40 public function __construct(ViewManagerInterface $viewManager, AuthorizationCheckerInterface $authorizationChecker)
41 {
42 $this->viewManager = $viewManager;
43 $this->authorizationChecker = $authorizationChecker;
44 }
45
62 public function viewAction(ContentView $view)
63 {
64 return $view;
65 }
66
75 public function embedAction(ContentView $view)
76 {
77 return $view;
78 }
79
88 protected function buildResponse($etag = null, DateTime $lastModified = null)
89 {
90 $request = $this->getRequest();
91 $response = new Response();
92 if ($this->getParameter('content.view_cache') === true) {
93 $response->setPublic();
94 if ($etag !== null) {
95 $response->setEtag($etag);
96 }
97
98 if ($this->getParameter('content.ttl_cache') === true) {
99 $response->setSharedMaxAge(
100 $this->getParameter('content.default_ttl')
101 );
102 }
103
104 // Make the response vary against X-User-Hash header ensures that an HTTP
105 // reverse proxy caches the different possible variations of the
106 // response as it can depend on user role for instance.
107 if ($request->headers->has('X-User-Hash')) {
108 $response->setVary('X-User-Hash');
109 }
110
111 if ($lastModified != null) {
112 $response->setLastModified($lastModified);
113 }
114 }
115
116 return $response;
117 }
118
136 public function viewLocation($locationId, $viewType, $layout = false, array $params = [])
137 {
138 @trigger_error(
139 "ViewController::viewLocation() is deprecated since kernel 6.0.0, and will be removed in the future.\n" .
140 'Use ViewController::viewAction() instead.',
141 E_USER_DEPRECATED
142 );
143
144 $this->performAccessChecks();
145 $response = $this->buildResponse();
146
147 try {
148 if (isset($params['location']) && $params['location'] instanceof Location) {
149 $location = $params['location'];
150 } else {
151 $location = $this->getRepository()->getLocationService()->loadLocation($locationId);
152 if ($location->invisible) {
153 throw new NotFoundHttpException("Location #$locationId cannot be displayed as it is flagged as invisible.");
154 }
155 }
156
157 $response->headers->set('X-Location-Id', $locationId);
158 $response->setContent(
159 $this->renderLocation(
160 $location,
161 $viewType,
162 $layout,
163 $params
164 )
165 );
166
167 return $response;
168 } catch (UnauthorizedException $e) {
169 throw new AccessDeniedException();
170 } catch (NotFoundException $e) {
171 throw new NotFoundHttpException($e->getMessage(), $e);
172 } catch (NotFoundHttpException $e) {
173 throw $e;
174 } catch (Exception $e) {
175 return $this->handleViewException($response, $params, $e, $viewType, null, $locationId);
176 }
177 }
178
196 public function embedLocation($locationId, $viewType, $layout = false, array $params = [])
197 {
198 @trigger_error(
199 "ViewController::embedLocation() is deprecated since kernel 6.0.0, and will be removed in the future.\n" .
200 'Use ViewController::viewAction() instead.',
201 E_USER_DEPRECATED
202 );
203
204 $this->performAccessChecks();
205 $response = $this->buildResponse();
206
207 try {
209 $location = $this->getRepository()->sudo(
210 function (Repository $repository) use ($locationId) {
211 return $repository->getLocationService()->loadLocation($locationId);
212 }
213 );
214
215 if ($location->invisible) {
216 throw new NotFoundHttpException("Location #{$locationId} cannot be displayed as it is flagged as invisible.");
217 }
218
219 // Check both 'content/read' and 'content/view_embed'.
220 if (
221 !$this->authorizationChecker->isGranted(
222 new AuthorizationAttribute(
223 'content',
224 'read',
225 ['valueObject' => $location->contentInfo, 'targets' => $location]
226 )
227 )
228 && !$this->authorizationChecker->isGranted(
229 new AuthorizationAttribute(
230 'content',
231 'view_embed',
232 ['valueObject' => $location->contentInfo, 'targets' => $location]
233 )
234 )
235 ) {
236 throw new AccessDeniedException();
237 }
238
239 if ($response->isNotModified($this->getRequest())) {
240 return $response;
241 }
242
243 $response->headers->set('X-Location-Id', $locationId);
244 $response->setContent(
245 $this->renderLocation(
246 $location,
247 $viewType,
248 $layout,
249 $params
250 )
251 );
252
253 return $response;
254 } catch (UnauthorizedException $e) {
255 throw new AccessDeniedException();
256 } catch (NotFoundException $e) {
257 throw new NotFoundHttpException($e->getMessage(), $e);
258 } catch (Exception $e) {
259 return $this->handleViewException($response, $params, $e, $viewType, null, $locationId);
260 }
261 }
262
280 public function viewContent($contentId, $viewType, $layout = false, array $params = [])
281 {
282 @trigger_error(
283 "ViewController::viewContent() is deprecated since kernel 6.0.0, and will be removed in the future.\n" .
284 'Use ViewController::viewAction() instead.',
285 E_USER_DEPRECATED
286 );
287
288 if ($viewType === 'embed') {
289 return $this->embedContent($contentId, $viewType, $layout, $params);
290 }
291
292 $this->performAccessChecks();
293 $response = $this->buildResponse();
294
295 try {
296 $content = $this->getRepository()->getContentService()->loadContent($contentId);
297
298 if ($response->isNotModified($this->getRequest())) {
299 return $response;
300 }
301
302 if (!isset($params['location']) && !isset($params['locationId'])) {
303 $params['location'] = $this->getRepository()->getLocationService()->loadLocation($content->contentInfo->mainLocationId);
304 }
305 $response->headers->set('X-Location-Id', $content->contentInfo->mainLocationId);
306 $response->setContent(
307 $this->renderContent($content, $viewType, $layout, $params)
308 );
309
310 return $response;
311 } catch (UnauthorizedException $e) {
312 throw new AccessDeniedException();
313 } catch (NotFoundException $e) {
314 throw new NotFoundHttpException($e->getMessage(), $e);
315 } catch (Exception $e) {
316 return $this->handleViewException($response, $params, $e, $viewType, $contentId);
317 }
318 }
319
337 public function embedContent($contentId, $viewType, $layout = false, array $params = [])
338 {
339 @trigger_error(
340 "ViewController::embedContent() is deprecated since kernel 6.0.0, and will be removed in the future.\n" .
341 'Use ViewController::viewAction() instead.',
342 E_USER_DEPRECATED
343 );
344
345 $this->performAccessChecks();
346 $response = $this->buildResponse();
347
348 try {
350 $content = $this->getRepository()->sudo(
351 function (Repository $repository) use ($contentId) {
352 return $repository->getContentService()->loadContent($contentId);
353 }
354 );
355
356 // Check both 'content/read' and 'content/view_embed'.
357 if (
358 !$this->authorizationChecker->isGranted(
359 new AuthorizationAttribute('content', 'read', ['valueObject' => $content])
360 )
361 && !$this->authorizationChecker->isGranted(
362 new AuthorizationAttribute('content', 'view_embed', ['valueObject' => $content])
363 )
364 ) {
365 throw new AccessDeniedException();
366 }
367
368 // Check that Content is published, since sudo allows loading unpublished content.
369 if (
370 !$content->getVersionInfo()->isPublished()
371 && !$this->authorizationChecker->isGranted(
372 new AuthorizationAttribute('content', 'versionread', ['valueObject' => $content])
373 )
374 ) {
375 throw new AccessDeniedException();
376 }
377
378 if ($response->isNotModified($this->getRequest())) {
379 return $response;
380 }
381
382 $response->setContent(
383 $this->renderContent($content, $viewType, $layout, $params)
384 );
385
386 return $response;
387 } catch (UnauthorizedException $e) {
388 throw new AccessDeniedException();
389 } catch (NotFoundException $e) {
390 throw new NotFoundHttpException($e->getMessage(), $e);
391 } catch (Exception $e) {
392 return $this->handleViewException($response, $params, $e, $viewType, $contentId);
393 }
394 }
395
396 protected function handleViewException(Response $response, $params, Exception $e, $viewType, $contentId = null, $locationId = null)
397 {
398 $event = new APIContentExceptionEvent(
399 $e,
400 [
401 'contentId' => $contentId,
402 'locationId' => $locationId,
403 'viewType' => $viewType,
404 ]
405 );
406 $this->getEventDispatcher()->dispatch(MVCEvents::API_CONTENT_EXCEPTION, $event);
407 if ($event->hasContentView()) {
408 $response->setContent(
409 $this->viewManager->renderContentView(
410 $event->getContentView(),
411 $params
412 )
413 );
414
415 return $response;
416 }
417
418 throw $e;
419 }
420
431 protected function renderLocation(Location $location, $viewType, $layout = false, array $params = [])
432 {
433 return $this->viewManager->renderLocation($location, $viewType, $params + ['noLayout' => !$layout]);
434 }
435
446 protected function renderContent(Content $content, $viewType, $layout = false, array $params = [])
447 {
448 return $this->viewManager->renderContent($content, $viewType, $params + ['noLayout' => !$layout]);
449 }
450
454 protected function performAccessChecks()
455 {
456 if (!$this->isGranted(new AuthorizationAttribute('content', 'read'))) {
457 throw new AccessDeniedException();
458 }
459 }
460 }
eZ\Publish\API\Repository\Exceptions\NotFoundException
This Exception is thrown if an object referenced by an id or identifier could not be found in the rep...
Definition: API/Repository/Exceptions/NotFoundException.php:16
eZ\Publish\API\Repository\Exceptions\UnauthorizedException
This Exception is thrown if the user has is not allowed to perform a service operation.
Definition: API/Repository/Exceptions/UnauthorizedException.php:15
eZ\Publish\API\Repository\Values\Content\Content
Definition: Publish/API/Repository/Values/Content/Content.php:21
eZ\Publish\API\Repository\Values\Content\Location
Definition: API/Repository/Values/Content/Location.php:32
eZ\Publish\Core\Base\Exceptions\NotFoundException
Not Found Exception implementation.
Definition: Core/Base/Exceptions/NotFoundException.php:21
eZ\Publish\Core\Base\Exceptions\UnauthorizedException
UnauthorizedException Exception implementation.
Definition: Core/Base/Exceptions/UnauthorizedException.php:21
eZ\Publish\Core\MVC\Symfony\Controller\Content\ViewController
This controller provides the content view feature.
Definition: ViewController.php:33
eZ\Publish\Core\MVC\Symfony\Controller\Content\ViewController\__construct
__construct(ViewManagerInterface $viewManager, AuthorizationCheckerInterface $authorizationChecker)
Definition: ViewController.php:40
eZ\Publish\Core\MVC\Symfony\Controller\Content\ViewController\handleViewException
handleViewException(Response $response, $params, Exception $e, $viewType, $contentId=null, $locationId=null)
Definition: ViewController.php:396
eZ\Publish\Core\MVC\Symfony\Controller\Content\ViewController\viewContent
viewContent($contentId, $viewType, $layout=false, array $params=[])
Main action for viewing content.
Definition: ViewController.php:280
eZ\Publish\Core\MVC\Symfony\Controller\Content\ViewController\$authorizationChecker
$authorizationChecker
Definition: ViewController.php:38
eZ\Publish\Core\MVC\Symfony\Controller\Content\ViewController\renderContent
renderContent(Content $content, $viewType, $layout=false, array $params=[])
Creates the content to be returned when viewing a Content.
Definition: ViewController.php:446
eZ\Publish\Core\MVC\Symfony\Controller\Content\ViewController\performAccessChecks
performAccessChecks()
Performs the access checks.
Definition: ViewController.php:454
eZ\Publish\Core\MVC\Symfony\Controller\Content\ViewController\buildResponse
buildResponse($etag=null, DateTime $lastModified=null)
Build the response so that depending on settings it's cacheable.
Definition: ViewController.php:88
eZ\Publish\Core\MVC\Symfony\Controller\Content\ViewController\viewAction
viewAction(ContentView $view)
This is the default view action or a ContentView object.
Definition: ViewController.php:62
eZ\Publish\Core\MVC\Symfony\Controller\Content\ViewController\$viewManager
$viewManager
Definition: ViewController.php:35
eZ\Publish\Core\MVC\Symfony\Controller\Content\ViewController\embedAction
embedAction(ContentView $view)
Embed a content.
Definition: ViewController.php:75
eZ\Publish\Core\MVC\Symfony\Controller\Content\ViewController\viewLocation
viewLocation($locationId, $viewType, $layout=false, array $params=[])
Main action for viewing content through a location in the repository.
Definition: ViewController.php:136
eZ\Publish\Core\MVC\Symfony\Controller\Content\ViewController\renderLocation
renderLocation(Location $location, $viewType, $layout=false, array $params=[])
Creates the content to be returned when viewing a Location.
Definition: ViewController.php:431
eZ\Publish\Core\MVC\Symfony\Controller\Controller
Definition: Publish/Core/MVC/Symfony/Controller/Controller.php:16
eZ\Publish\Core\MVC\Symfony\Controller\Controller\isGranted
isGranted(AuthorizationAttribute $attribute)
Checks if current user has granted access to provided attribute.
Definition: Publish/Core/MVC/Symfony/Controller/Controller.php:123
eZ\Publish\Core\MVC\Symfony\Controller\Controller\getEventDispatcher
getEventDispatcher()
Definition: Publish/Core/MVC/Symfony/Controller/Controller.php:111
eZ\Publish\Core\MVC\Symfony\Controller\Controller\getRequest
getRequest()
Definition: Publish/Core/MVC/Symfony/Controller/Controller.php:103
eZ\Publish\Core\MVC\Symfony\Controller\Controller\getRepository
getRepository()
Definition: Publish/Core/MVC/Symfony/Controller/Controller.php:95
eZ\Publish\Core\MVC\Symfony\Controller\Controller\getParameter
getParameter($parameterName, $defaultValue=null)
Returns value for $parameterName and fallbacks to $defaultValue if not defined.
Definition: Publish/Core/MVC/Symfony/Controller/Controller.php:27
eZ\Publish\Core\MVC\Symfony\Event\APIContentExceptionEvent
This event is dispatched when an Exception from eZ Publish API is thrown and could not be caught befo...
Definition: APIContentExceptionEvent.php:18
eZ\Publish\Core\MVC\Symfony\MVCEvents
Definition: MVCEvents.php:10
eZ\Publish\Core\MVC\Symfony\MVCEvents\API_CONTENT_EXCEPTION
const API_CONTENT_EXCEPTION
The API_CONTENT_EXCEPTION event occurs when the API throws an exception that could not be caught inte...
Definition: MVCEvents.php:36
eZ\Publish\Core\MVC\Symfony\Security\Authorization\Attribute
Authorization attribute class to be used with SecurityContextInterface::isGranted().
Definition: Attribute.php:31
eZ\Publish\Core\MVC\Symfony\View\ContentView
Main object to be rendered by the View Manager when viewing a content.
Definition: Publish/Core/MVC/Symfony/View/ContentView.php:40
eZ\Publish\API\Repository\Repository
Repository interface.
Definition: API/Repository/Repository.php:16
eZ\Publish\Core\MVC\Symfony\View\ViewManagerInterface
Definition: ViewManagerInterface.php:14
eZ\Publish\Core\MVC\Symfony\Controller\Content
Definition: DownloadController.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.