|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace eZ\Publish\Core\MVC\Symfony\View\Builder; |
|
7
|
|
|
|
|
8
|
|
|
use eZ\Publish\API\Repository\Repository; |
|
9
|
|
|
use eZ\Publish\API\Repository\Values\Content\Content; |
|
10
|
|
|
use eZ\Publish\API\Repository\Values\Content\Location; |
|
11
|
|
|
use eZ\Publish\API\Repository\Values\Content\VersionInfo; |
|
12
|
|
|
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException; |
|
13
|
|
|
use eZ\Publish\Core\Base\Exceptions\UnauthorizedException; |
|
14
|
|
|
use eZ\Publish\Core\MVC\Symfony\View\Configurator; |
|
15
|
|
|
use eZ\Publish\Core\MVC\Symfony\View\ContentView; |
|
16
|
|
|
use eZ\Publish\Core\MVC\Symfony\Security\Authorization\Attribute as AuthorizationAttribute; |
|
17
|
|
|
use eZ\Publish\Core\MVC\Symfony\View\EmbedView; |
|
18
|
|
|
use eZ\Publish\Core\MVC\Symfony\View\ParametersInjector; |
|
19
|
|
|
use Symfony\Component\HttpKernel\Controller\ControllerReference; |
|
20
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
21
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Builds ContentView objects. |
|
25
|
|
|
*/ |
|
26
|
|
|
class ContentViewBuilder implements ViewBuilder |
|
27
|
|
|
{ |
|
28
|
|
|
/** @var \eZ\Publish\API\Repository\Repository */ |
|
29
|
|
|
private $repository; |
|
30
|
|
|
|
|
31
|
|
|
/** @var AuthorizationCheckerInterface */ |
|
32
|
|
|
private $authorizationChecker; |
|
33
|
|
|
|
|
34
|
|
|
/** @var \eZ\Publish\Core\MVC\Symfony\View\Configurator */ |
|
35
|
|
|
private $viewConfigurator; |
|
36
|
|
|
|
|
37
|
|
|
/** @var \eZ\Publish\Core\MVC\Symfony\View\ParametersInjector */ |
|
38
|
|
|
private $viewParametersInjector; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Default templates, indexed per viewType (full, line, ...). |
|
42
|
|
|
* @var array |
|
43
|
|
|
*/ |
|
44
|
|
|
private $defaultTemplates; |
|
45
|
|
|
|
|
46
|
|
|
public function __construct( |
|
47
|
|
|
Repository $repository, |
|
48
|
|
|
AuthorizationCheckerInterface $authorizationChecker, |
|
49
|
|
|
Configurator $viewConfigurator, |
|
50
|
|
|
ParametersInjector $viewParametersInjector |
|
51
|
|
|
) { |
|
52
|
|
|
$this->repository = $repository; |
|
53
|
|
|
$this->authorizationChecker = $authorizationChecker; |
|
54
|
|
|
$this->viewConfigurator = $viewConfigurator; |
|
55
|
|
|
$this->viewParametersInjector = $viewParametersInjector; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function matches($argument) |
|
59
|
|
|
{ |
|
60
|
|
|
return strpos($argument, 'ez_content:') !== false; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param array $parameters |
|
65
|
|
|
* |
|
66
|
|
|
* @return \eZ\Publish\Core\MVC\Symfony\View\ContentView|\eZ\Publish\Core\MVC\Symfony\View\View |
|
67
|
|
|
* If both contentId and locationId parameters are missing |
|
68
|
|
|
* @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException |
|
69
|
|
|
* If both contentId and locationId parameters are missing |
|
70
|
|
|
* @throws \eZ\Publish\Core\Base\Exceptions\UnauthorizedException |
|
71
|
|
|
*/ |
|
72
|
|
|
public function buildView(array $parameters) |
|
73
|
|
|
{ |
|
74
|
|
|
$view = new ContentView(null, [], $parameters['viewType']); |
|
75
|
|
|
$view->setIsEmbed($this->isEmbed($parameters)); |
|
76
|
|
|
|
|
77
|
|
|
if ($view->isEmbed() && $parameters['viewType'] === null) { |
|
78
|
|
|
$view->setViewType(EmbedView::DEFAULT_VIEW_TYPE); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if (isset($parameters['locationId'])) { |
|
82
|
|
|
$location = $this->loadLocation($parameters['locationId']); |
|
83
|
|
|
} elseif (isset($parameters['location'])) { |
|
84
|
|
|
$location = $parameters['location']; |
|
85
|
|
|
} else { |
|
86
|
|
|
$location = null; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
if (isset($parameters['content'])) { |
|
90
|
|
|
$content = $parameters['content']; |
|
91
|
|
|
} else { |
|
92
|
|
|
if (isset($parameters['contentId'])) { |
|
93
|
|
|
$contentId = $parameters['contentId']; |
|
94
|
|
|
} elseif (isset($location)) { |
|
95
|
|
|
$contentId = $location->contentId; |
|
96
|
|
|
} else { |
|
97
|
|
|
throw new InvalidArgumentException('Content', 'No content could be loaded from parameters'); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$content = $view->isEmbed() ? $this->loadContent($contentId) : $this->loadEmbeddedContent($contentId, $location); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$view->setContent($content); |
|
104
|
|
|
if (isset($location)) { |
|
105
|
|
|
if ($location->contentId !== $content->id) { |
|
106
|
|
|
throw new InvalidArgumentException('Location', 'Provided location does not belong to selected content'); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$view->setLocation($location); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
$this->viewConfigurator->configure($view); |
|
113
|
|
|
|
|
114
|
|
|
// deprecated controller actions are replaced with their new equivalent, viewAction and embedAction |
|
115
|
|
|
if (!$view->getControllerReference() instanceof ControllerReference) { |
|
116
|
|
|
if (in_array($parameters['_controller'], ['ez_content:viewLocation', 'ez_content:viewContent'])) { |
|
117
|
|
|
$view->setControllerReference(new ControllerReference('ez_content:viewAction')); |
|
118
|
|
|
} elseif (in_array($parameters['_controller'], ['ez_content:embedLocation', 'ez_content:embedContent'])) { |
|
119
|
|
|
$view->setControllerReference(new ControllerReference('ez_content:embedAction')); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$this->viewParametersInjector->injectViewParameters($view, $parameters); |
|
124
|
|
|
|
|
125
|
|
|
return $view; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Loads Content with id $contentId. |
|
130
|
|
|
* |
|
131
|
|
|
* @param mixed $contentId |
|
132
|
|
|
* |
|
133
|
|
|
* @return \eZ\Publish\API\Repository\Values\Content\Content |
|
134
|
|
|
* |
|
135
|
|
|
* @throws \eZ\Publish\Core\Base\Exceptions\UnauthorizedException |
|
136
|
|
|
*/ |
|
137
|
|
|
private function loadContent($contentId) |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->repository->getContentService()->loadContent($contentId); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Loads the embedded content with id $contentId. |
|
144
|
|
|
* Will load the content with sudo(), and check if the user can view_embed this content, for the given location |
|
145
|
|
|
* if provided. |
|
146
|
|
|
* |
|
147
|
|
|
* @param mixed $contentId |
|
148
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Location $location |
|
149
|
|
|
* |
|
150
|
|
|
* @return \eZ\Publish\API\Repository\Values\Content\Content |
|
151
|
|
|
* @throws \eZ\Publish\Core\Base\Exceptions\UnauthorizedException |
|
152
|
|
|
*/ |
|
153
|
|
|
private function loadEmbeddedContent($contentId, Location $location = null) |
|
154
|
|
|
{ |
|
155
|
|
|
$content = $this->repository->sudo( |
|
|
|
|
|
|
156
|
|
|
function (Repository $repository) use ($contentId) { |
|
157
|
|
|
return $repository->getContentService()->loadContent($contentId); |
|
158
|
|
|
} |
|
159
|
|
|
); |
|
160
|
|
|
|
|
161
|
|
|
if (!$this->canRead($content, $location)) { |
|
162
|
|
|
throw new UnauthorizedException( |
|
163
|
|
|
'content', 'read|view_embed', |
|
164
|
|
|
['contentId' => $contentId, 'locationId' => $location !== null ? $location->id : 'n/a'] |
|
165
|
|
|
); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
// Check that Content is published, since sudo allows loading unpublished content. |
|
169
|
|
|
if ( |
|
170
|
|
|
$content->getVersionInfo()->status !== VersionInfo::STATUS_PUBLISHED |
|
171
|
|
|
&& !$this->authorizationChecker->isGranted( |
|
172
|
|
|
new AuthorizationAttribute('content', 'versionread', array('valueObject' => $content)) |
|
173
|
|
|
) |
|
174
|
|
|
) { |
|
175
|
|
|
throw new UnauthorizedException('content', 'versionread', ['contentId' => $contentId]); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
return $content; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Loads a visible Location. |
|
183
|
|
|
* @todo Do we need to handle permissions here ? |
|
184
|
|
|
* |
|
185
|
|
|
* @param $locationId |
|
186
|
|
|
* |
|
187
|
|
|
* @return \eZ\Publish\API\Repository\Values\Content\Location |
|
188
|
|
|
*/ |
|
189
|
|
|
private function loadLocation($locationId) |
|
190
|
|
|
{ |
|
191
|
|
|
$location = $this->repository->sudo( |
|
|
|
|
|
|
192
|
|
|
function (Repository $repository) use ($locationId) { |
|
193
|
|
|
return $repository->getLocationService()->loadLocation($locationId); |
|
194
|
|
|
} |
|
195
|
|
|
); |
|
196
|
|
|
if ($location->invisible) { |
|
197
|
|
|
throw new NotFoundHttpException('Location cannot be displayed as it is flagged as invisible.'); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
return $location; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Checks if a user can read a content, or view it as an embed. |
|
205
|
|
|
* |
|
206
|
|
|
* @param Content $content |
|
207
|
|
|
* @param $location |
|
208
|
|
|
* |
|
209
|
|
|
* @return bool |
|
210
|
|
|
*/ |
|
211
|
|
|
private function canRead(Content $content, Location $location = null) |
|
212
|
|
|
{ |
|
213
|
|
|
$limitations = ['valueObject' => $content->contentInfo]; |
|
214
|
|
|
if (isset($location)) { |
|
215
|
|
|
$limitations['targets'] = $location; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
$readAttribute = new AuthorizationAttribute('content', 'read', $limitations); |
|
219
|
|
|
$viewEmbedAttribute = new AuthorizationAttribute('content', 'view_embed', $limitations); |
|
220
|
|
|
|
|
221
|
|
|
return |
|
222
|
|
|
$this->authorizationChecker->isGranted($readAttribute) || |
|
223
|
|
|
$this->authorizationChecker->isGranted($viewEmbedAttribute); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Checks if the view is an embed one. |
|
228
|
|
|
* Uses either the controller action (embedAction), or the viewType (embed/embed-inline). |
|
229
|
|
|
* |
|
230
|
|
|
* @param array $parameters The ViewBuilder parameters array. |
|
231
|
|
|
* |
|
232
|
|
|
* @return bool |
|
233
|
|
|
*/ |
|
234
|
|
|
private function isEmbed($parameters) |
|
235
|
|
|
{ |
|
236
|
|
|
if ($parameters['_controller'] === 'ez_content:embedAction') { |
|
237
|
|
|
return true; |
|
238
|
|
|
} |
|
239
|
|
|
if (in_array($parameters['viewType'], ['embed', 'embed-inline'])) { |
|
240
|
|
|
return true; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
return false; |
|
244
|
|
|
} |
|
245
|
|
|
} |
|
246
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: