1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* File containing the eZ\Publish\Core\Repository\LocationService class. |
5
|
|
|
* |
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
namespace eZ\Publish\Core\Repository; |
10
|
|
|
|
11
|
|
|
use eZ\Publish\API\Repository\PermissionCriterionResolver; |
12
|
|
|
use eZ\Publish\API\Repository\Values\Content\LocationUpdateStruct; |
13
|
|
|
use eZ\Publish\API\Repository\Values\Content\LocationCreateStruct; |
14
|
|
|
use eZ\Publish\API\Repository\Values\Content\ContentInfo; |
15
|
|
|
use eZ\Publish\API\Repository\Values\Content\Location as APILocation; |
16
|
|
|
use eZ\Publish\API\Repository\Values\Content\LocationList; |
17
|
|
|
use eZ\Publish\API\Repository\Values\Content\VersionInfo; |
18
|
|
|
use eZ\Publish\SPI\Persistence\Content\Location\UpdateStruct; |
19
|
|
|
use eZ\Publish\API\Repository\LocationService as LocationServiceInterface; |
20
|
|
|
use eZ\Publish\API\Repository\Repository as RepositoryInterface; |
21
|
|
|
use eZ\Publish\SPI\Persistence\Handler; |
22
|
|
|
use eZ\Publish\API\Repository\Values\Content\Query; |
23
|
|
|
use eZ\Publish\API\Repository\Values\Content\LocationQuery; |
24
|
|
|
use eZ\Publish\API\Repository\Values\Content\Query\Criterion; |
25
|
|
|
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\LogicalAnd as CriterionLogicalAnd; |
26
|
|
|
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\LogicalNot as CriterionLogicalNot; |
27
|
|
|
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\Subtree as CriterionSubtree; |
28
|
|
|
use eZ\Publish\API\Repository\Exceptions\NotFoundException as APINotFoundException; |
29
|
|
|
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentValue; |
30
|
|
|
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException; |
31
|
|
|
use eZ\Publish\Core\Base\Exceptions\BadStateException; |
32
|
|
|
use eZ\Publish\Core\Base\Exceptions\UnauthorizedException; |
33
|
|
|
use Exception; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Location service, used for complex subtree operations. |
37
|
|
|
* |
38
|
|
|
* @example Examples/location.php |
39
|
|
|
*/ |
40
|
|
|
class LocationService implements LocationServiceInterface |
41
|
|
|
{ |
42
|
|
|
/** |
43
|
|
|
* @var \eZ\Publish\Core\Repository\Repository |
44
|
|
|
*/ |
45
|
|
|
protected $repository; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var \eZ\Publish\SPI\Persistence\Handler |
49
|
|
|
*/ |
50
|
|
|
protected $persistenceHandler; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var array |
54
|
|
|
*/ |
55
|
|
|
protected $settings; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var \eZ\Publish\Core\Repository\Helper\DomainMapper |
59
|
|
|
*/ |
60
|
|
|
protected $domainMapper; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var \eZ\Publish\Core\Repository\Helper\NameSchemaService |
64
|
|
|
*/ |
65
|
|
|
protected $nameSchemaService; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var \eZ\Publish\API\Repository\PermissionCriterionResolver |
69
|
|
|
*/ |
70
|
|
|
protected $permissionCriterionResolver; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Setups service with reference to repository object that created it & corresponding handler. |
74
|
|
|
* |
75
|
|
|
* @param \eZ\Publish\API\Repository\Repository $repository |
76
|
|
|
* @param \eZ\Publish\SPI\Persistence\Handler $handler |
77
|
|
|
* @param \eZ\Publish\Core\Repository\Helper\DomainMapper $domainMapper |
78
|
|
|
* @param \eZ\Publish\Core\Repository\Helper\NameSchemaService $nameSchemaService |
79
|
|
|
* @param \eZ\Publish\API\Repository\PermissionCriterionResolver $permissionCriterionResolver |
80
|
|
|
* @param array $settings |
81
|
|
|
*/ |
82
|
|
|
public function __construct( |
83
|
|
|
RepositoryInterface $repository, |
84
|
|
|
Handler $handler, |
85
|
|
|
Helper\DomainMapper $domainMapper, |
86
|
|
|
Helper\NameSchemaService $nameSchemaService, |
87
|
|
|
PermissionCriterionResolver $permissionCriterionResolver, |
88
|
|
|
array $settings = array() |
89
|
|
|
) { |
90
|
|
|
$this->repository = $repository; |
|
|
|
|
91
|
|
|
$this->persistenceHandler = $handler; |
92
|
|
|
$this->domainMapper = $domainMapper; |
93
|
|
|
$this->nameSchemaService = $nameSchemaService; |
94
|
|
|
// Union makes sure default settings are ignored if provided in argument |
95
|
|
|
$this->settings = $settings + array( |
96
|
|
|
//'defaultSetting' => array(), |
97
|
|
|
); |
98
|
|
|
$this->permissionCriterionResolver = $permissionCriterionResolver; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Copies the subtree starting from $subtree as a new subtree of $targetLocation. |
103
|
|
|
* |
104
|
|
|
* Only the items on which the user has read access are copied. |
105
|
|
|
* |
106
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed copy the subtree to the given parent location |
107
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user does not have read access to the whole source subtree |
108
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the target location is a sub location of the given location |
109
|
|
|
* |
110
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Location $subtree - the subtree denoted by the location to copy |
111
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Location $targetParentLocation - the target parent location for the copy operation |
112
|
|
|
* |
113
|
|
|
* @return \eZ\Publish\API\Repository\Values\Content\Location The newly created location of the copied subtree |
114
|
|
|
*/ |
115
|
|
|
public function copySubtree(APILocation $subtree, APILocation $targetParentLocation) |
116
|
|
|
{ |
117
|
|
|
$loadedSubtree = $this->loadLocation($subtree->id); |
118
|
|
|
$loadedTargetLocation = $this->loadLocation($targetParentLocation->id); |
119
|
|
|
|
120
|
|
|
if (stripos($loadedTargetLocation->pathString, $loadedSubtree->pathString) !== false) { |
|
|
|
|
121
|
|
|
throw new InvalidArgumentException('targetParentLocation', 'target parent location is a sub location of the given subtree'); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// check create permission on target |
125
|
|
|
if (!$this->repository->canUser('content', 'create', $loadedSubtree->getContentInfo(), $loadedTargetLocation)) { |
|
|
|
|
126
|
|
|
throw new UnauthorizedException('content', 'create'); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** Check read access to whole source subtree |
130
|
|
|
* @var bool|\eZ\Publish\API\Repository\Values\Content\Query\Criterion |
131
|
|
|
*/ |
132
|
|
|
$contentReadCriterion = $this->permissionCriterionResolver->getPermissionsCriterion(); |
|
|
|
|
133
|
|
View Code Duplication |
if ($contentReadCriterion === false) { |
|
|
|
|
134
|
|
|
throw new UnauthorizedException('content', 'read'); |
135
|
|
|
} elseif ($contentReadCriterion !== true) { |
136
|
|
|
// Query if there are any content in subtree current user don't have access to |
137
|
|
|
$query = new Query( |
138
|
|
|
array( |
139
|
|
|
'limit' => 0, |
140
|
|
|
'filter' => new CriterionLogicalAnd( |
141
|
|
|
array( |
142
|
|
|
new CriterionSubtree($loadedSubtree->pathString), |
|
|
|
|
143
|
|
|
new CriterionLogicalNot($contentReadCriterion), |
|
|
|
|
144
|
|
|
) |
145
|
|
|
), |
146
|
|
|
) |
147
|
|
|
); |
148
|
|
|
$result = $this->repository->getSearchService()->findContent($query, array(), false); |
149
|
|
|
if ($result->totalCount > 0) { |
150
|
|
|
throw new UnauthorizedException('content', 'read'); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$this->repository->beginTransaction(); |
155
|
|
|
try { |
156
|
|
|
$newLocation = $this->persistenceHandler->locationHandler()->copySubtree( |
157
|
|
|
$loadedSubtree->id, |
|
|
|
|
158
|
|
|
$loadedTargetLocation->id |
|
|
|
|
159
|
|
|
); |
160
|
|
|
|
161
|
|
|
$content = $this->repository->getContentService()->loadContent($newLocation->contentId); |
162
|
|
|
$urlAliasNames = $this->nameSchemaService->resolveUrlAliasSchema($content); |
163
|
|
View Code Duplication |
foreach ($urlAliasNames as $languageCode => $name) { |
|
|
|
|
164
|
|
|
$this->persistenceHandler->urlAliasHandler()->publishUrlAliasForLocation( |
165
|
|
|
$newLocation->id, |
166
|
|
|
$loadedTargetLocation->id, |
|
|
|
|
167
|
|
|
$name, |
168
|
|
|
$languageCode, |
169
|
|
|
$content->contentInfo->alwaysAvailable |
170
|
|
|
); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$this->persistenceHandler->urlAliasHandler()->locationCopied( |
174
|
|
|
$loadedSubtree->id, |
|
|
|
|
175
|
|
|
$newLocation->id, |
176
|
|
|
$loadedTargetLocation->id |
|
|
|
|
177
|
|
|
); |
178
|
|
|
|
179
|
|
|
$this->repository->commit(); |
180
|
|
|
} catch (Exception $e) { |
181
|
|
|
$this->repository->rollback(); |
182
|
|
|
throw $e; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return $this->domainMapper->buildLocationDomainObject($newLocation); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Loads a location object from its $locationId. |
190
|
|
|
* |
191
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to read this location |
192
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If the specified location is not found |
193
|
|
|
* |
194
|
|
|
* @param mixed $locationId |
195
|
|
|
* |
196
|
|
|
* @return \eZ\Publish\API\Repository\Values\Content\Location |
197
|
|
|
*/ |
198
|
|
|
public function loadLocation($locationId) |
199
|
|
|
{ |
200
|
|
|
$spiLocation = $this->persistenceHandler->locationHandler()->load($locationId); |
201
|
|
|
$location = $this->domainMapper->buildLocationDomainObject($spiLocation); |
202
|
|
|
if (!$this->repository->canUser('content', 'read', $location->getContentInfo(), $location)) { |
|
|
|
|
203
|
|
|
throw new UnauthorizedException('content', 'read'); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
return $location; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Loads a location object from its $remoteId. |
211
|
|
|
* |
212
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to read this location |
213
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\BadStateException If more than one location with same remote ID was found |
214
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If the specified location is not found |
215
|
|
|
* |
216
|
|
|
* @param string $remoteId |
217
|
|
|
* |
218
|
|
|
* @return \eZ\Publish\API\Repository\Values\Content\Location |
219
|
|
|
*/ |
220
|
|
|
public function loadLocationByRemoteId($remoteId) |
221
|
|
|
{ |
222
|
|
|
if (!is_string($remoteId)) { |
223
|
|
|
throw new InvalidArgumentValue('remoteId', $remoteId); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
$spiLocation = $this->persistenceHandler->locationHandler()->loadByRemoteId($remoteId); |
227
|
|
|
$location = $this->domainMapper->buildLocationDomainObject($spiLocation); |
228
|
|
|
if (!$this->repository->canUser('content', 'read', $location->getContentInfo(), $location)) { |
|
|
|
|
229
|
|
|
throw new UnauthorizedException('content', 'read'); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
return $location; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* {@inheritdoc} |
237
|
|
|
*/ |
238
|
|
|
public function loadLocations(ContentInfo $contentInfo, APILocation $rootLocation = null) |
239
|
|
|
{ |
240
|
|
|
if (!$contentInfo->published) { |
241
|
|
|
throw new BadStateException('$contentInfo', 'ContentInfo has no published versions'); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
$spiLocations = $this->persistenceHandler->locationHandler()->loadLocationsByContent( |
245
|
|
|
$contentInfo->id, |
246
|
|
|
$rootLocation !== null ? $rootLocation->id : null |
247
|
|
|
); |
248
|
|
|
|
249
|
|
|
$locations = []; |
250
|
|
View Code Duplication |
foreach ($spiLocations as $spiLocation) { |
|
|
|
|
251
|
|
|
$location = $this->domainMapper->buildLocationDomainObject($spiLocation); |
252
|
|
|
if ($this->repository->canUser('content', 'read', $location->getContentInfo(), $location)) { |
|
|
|
|
253
|
|
|
$locations[] = $location; |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
return $locations; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Loads children which are readable by the current user of a location object sorted by sortField and sortOrder. |
262
|
|
|
* |
263
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Location $location |
264
|
|
|
* @param int $offset the start offset for paging |
265
|
|
|
* @param int $limit the number of locations returned |
266
|
|
|
* |
267
|
|
|
* @return \eZ\Publish\API\Repository\Values\Content\LocationList |
268
|
|
|
*/ |
269
|
|
|
public function loadLocationChildren(APILocation $location, $offset = 0, $limit = 25) |
270
|
|
|
{ |
271
|
|
|
if (!$this->domainMapper->isValidLocationSortField($location->sortField)) { |
272
|
|
|
throw new InvalidArgumentValue('sortField', $location->sortField, 'Location'); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
if (!$this->domainMapper->isValidLocationSortOrder($location->sortOrder)) { |
276
|
|
|
throw new InvalidArgumentValue('sortOrder', $location->sortOrder, 'Location'); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
if (!is_int($offset)) { |
280
|
|
|
throw new InvalidArgumentValue('offset', $offset); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
if (!is_int($limit)) { |
284
|
|
|
throw new InvalidArgumentValue('limit', $limit); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
$childLocations = array(); |
288
|
|
|
$searchResult = $this->searchChildrenLocations($location, $offset, $limit); |
289
|
|
|
foreach ($searchResult->searchHits as $searchHit) { |
290
|
|
|
$childLocations[] = $searchHit->valueObject; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
return new LocationList( |
294
|
|
|
array( |
295
|
|
|
'locations' => $childLocations, |
296
|
|
|
'totalCount' => $searchResult->totalCount, |
297
|
|
|
) |
298
|
|
|
); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* {@inheritdoc} |
303
|
|
|
*/ |
304
|
|
|
public function loadParentLocationsForDraftContent(VersionInfo $versionInfo) |
305
|
|
|
{ |
306
|
|
|
if (!$versionInfo->isDraft()) { |
307
|
|
|
throw new BadStateException( |
308
|
|
|
'$contentInfo', |
309
|
|
|
sprintf( |
310
|
|
|
'Content [%d] %s has been already published. Use LocationService::loadLocations instead.', |
311
|
|
|
$versionInfo->contentInfo->id, |
312
|
|
|
$versionInfo->contentInfo->name |
313
|
|
|
) |
314
|
|
|
); |
315
|
|
|
} |
316
|
|
|
$spiLocations = $this->persistenceHandler |
317
|
|
|
->locationHandler() |
318
|
|
|
->loadParentLocationsForDraftContent($versionInfo->contentInfo->id); |
319
|
|
|
|
320
|
|
|
$locations = []; |
321
|
|
|
$permissionResolver = $this->repository->getPermissionResolver(); |
322
|
|
View Code Duplication |
foreach ($spiLocations as $spiLocation) { |
|
|
|
|
323
|
|
|
$location = $this->domainMapper->buildLocationDomainObject($spiLocation); |
324
|
|
|
if ($permissionResolver->canUser('content', 'read', $location->getContentInfo(), [$location])) { |
325
|
|
|
$locations[] = $location; |
326
|
|
|
} |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
return $locations; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Returns the number of children which are readable by the current user of a location object. |
334
|
|
|
* |
335
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Location $location |
336
|
|
|
* |
337
|
|
|
* @return int |
338
|
|
|
*/ |
339
|
|
|
public function getLocationChildCount(APILocation $location) |
340
|
|
|
{ |
341
|
|
|
$searchResult = $this->searchChildrenLocations($location, 0, 0); |
342
|
|
|
|
343
|
|
|
return $searchResult->totalCount; |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* Searches children locations of the provided parent location id. |
348
|
|
|
* |
349
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Location $location |
350
|
|
|
* @param int $offset |
351
|
|
|
* @param int $limit |
352
|
|
|
* |
353
|
|
|
* @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult |
354
|
|
|
*/ |
355
|
|
|
protected function searchChildrenLocations(APILocation $location, $offset = 0, $limit = -1) |
356
|
|
|
{ |
357
|
|
|
$query = new LocationQuery([ |
358
|
|
|
'filter' => new Criterion\ParentLocationId($location->id), |
359
|
|
|
'offset' => $offset >= 0 ? (int)$offset : 0, |
360
|
|
|
'limit' => $limit >= 0 ? (int)$limit : null, |
361
|
|
|
'sortClauses' => $location->getSortClauses(), |
362
|
|
|
]); |
363
|
|
|
|
364
|
|
|
return $this->repository->getSearchService()->findLocations($query); |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* Creates the new $location in the content repository for the given content. |
369
|
|
|
* |
370
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to create this location |
371
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the content is already below the specified parent |
372
|
|
|
* or the parent is a sub location of the location of the content |
373
|
|
|
* or if set the remoteId exists already |
374
|
|
|
* |
375
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
376
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct $locationCreateStruct |
377
|
|
|
* |
378
|
|
|
* @return \eZ\Publish\API\Repository\Values\Content\Location the newly created Location |
379
|
|
|
*/ |
380
|
|
|
public function createLocation(ContentInfo $contentInfo, LocationCreateStruct $locationCreateStruct) |
381
|
|
|
{ |
382
|
|
|
$content = $this->repository->getContentService()->loadContent($contentInfo->id); |
383
|
|
|
$parentLocation = $this->loadLocation($locationCreateStruct->parentLocationId); |
384
|
|
|
|
385
|
|
|
if (!$this->repository->canUser('content', 'create', $content->contentInfo, $parentLocation)) { |
|
|
|
|
386
|
|
|
throw new UnauthorizedException('content', 'create'); |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
// Check if the parent is a sub location of one of the existing content locations (this also solves the |
390
|
|
|
// situation where parent location actually one of the content locations), |
391
|
|
|
// or if the content already has location below given location create struct parent |
392
|
|
|
$existingContentLocations = $this->loadLocations($content->contentInfo); |
393
|
|
|
if (!empty($existingContentLocations)) { |
394
|
|
|
foreach ($existingContentLocations as $existingContentLocation) { |
395
|
|
|
if (stripos($parentLocation->pathString, $existingContentLocation->pathString) !== false) { |
|
|
|
|
396
|
|
|
throw new InvalidArgumentException( |
397
|
|
|
'$locationCreateStruct', |
398
|
|
|
'Specified parent is a sub location of one of the existing content locations.' |
399
|
|
|
); |
400
|
|
|
} |
401
|
|
|
if ($parentLocation->id == $existingContentLocation->parentLocationId) { |
|
|
|
|
402
|
|
|
throw new InvalidArgumentException( |
403
|
|
|
'$locationCreateStruct', |
404
|
|
|
'Content is already below the specified parent.' |
405
|
|
|
); |
406
|
|
|
} |
407
|
|
|
} |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
$spiLocationCreateStruct = $this->domainMapper->buildSPILocationCreateStruct( |
411
|
|
|
$locationCreateStruct, |
412
|
|
|
$parentLocation, |
413
|
|
|
$content->contentInfo->mainLocationId !== null ? $content->contentInfo->mainLocationId : true, |
414
|
|
|
$content->contentInfo->id, |
415
|
|
|
$content->contentInfo->currentVersionNo |
416
|
|
|
); |
417
|
|
|
|
418
|
|
|
$this->repository->beginTransaction(); |
419
|
|
|
try { |
420
|
|
|
$newLocation = $this->persistenceHandler->locationHandler()->create($spiLocationCreateStruct); |
421
|
|
|
|
422
|
|
|
$urlAliasNames = $this->nameSchemaService->resolveUrlAliasSchema($content); |
423
|
|
View Code Duplication |
foreach ($urlAliasNames as $languageCode => $name) { |
|
|
|
|
424
|
|
|
$this->persistenceHandler->urlAliasHandler()->publishUrlAliasForLocation( |
425
|
|
|
$newLocation->id, |
426
|
|
|
$newLocation->parentId, |
427
|
|
|
$name, |
428
|
|
|
$languageCode, |
429
|
|
|
$content->contentInfo->alwaysAvailable, |
430
|
|
|
// @todo: this is legacy storage specific for updating ezcontentobject_tree.path_identification_string, to be removed |
431
|
|
|
$languageCode === $content->contentInfo->mainLanguageCode |
|
|
|
|
432
|
|
|
); |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
$this->repository->commit(); |
436
|
|
|
} catch (Exception $e) { |
437
|
|
|
$this->repository->rollback(); |
438
|
|
|
throw $e; |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
return $this->domainMapper->buildLocationDomainObject($newLocation); |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
/** |
445
|
|
|
* Updates $location in the content repository. |
446
|
|
|
* |
447
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to update this location |
448
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if if set the remoteId exists already |
449
|
|
|
* |
450
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Location $location |
451
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\LocationUpdateStruct $locationUpdateStruct |
452
|
|
|
* |
453
|
|
|
* @return \eZ\Publish\API\Repository\Values\Content\Location the updated Location |
454
|
|
|
*/ |
455
|
|
|
public function updateLocation(APILocation $location, LocationUpdateStruct $locationUpdateStruct) |
456
|
|
|
{ |
457
|
|
|
if ($locationUpdateStruct->priority !== null && !is_int($locationUpdateStruct->priority)) { |
458
|
|
|
throw new InvalidArgumentValue('priority', $locationUpdateStruct->priority, 'LocationUpdateStruct'); |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
if ($locationUpdateStruct->remoteId !== null && (!is_string($locationUpdateStruct->remoteId) || empty($locationUpdateStruct->remoteId))) { |
462
|
|
|
throw new InvalidArgumentValue('remoteId', $locationUpdateStruct->remoteId, 'LocationUpdateStruct'); |
463
|
|
|
} |
464
|
|
|
|
465
|
|
|
if ($locationUpdateStruct->sortField !== null && !$this->domainMapper->isValidLocationSortField($locationUpdateStruct->sortField)) { |
466
|
|
|
throw new InvalidArgumentValue('sortField', $locationUpdateStruct->sortField, 'LocationUpdateStruct'); |
467
|
|
|
} |
468
|
|
|
|
469
|
|
|
if ($locationUpdateStruct->sortOrder !== null && !$this->domainMapper->isValidLocationSortOrder($locationUpdateStruct->sortOrder)) { |
470
|
|
|
throw new InvalidArgumentValue('sortOrder', $locationUpdateStruct->sortOrder, 'LocationUpdateStruct'); |
471
|
|
|
} |
472
|
|
|
|
473
|
|
|
$loadedLocation = $this->loadLocation($location->id); |
474
|
|
|
|
475
|
|
|
if ($locationUpdateStruct->remoteId !== null) { |
476
|
|
|
try { |
477
|
|
|
$existingLocation = $this->loadLocationByRemoteId($locationUpdateStruct->remoteId); |
478
|
|
|
if ($existingLocation !== null && $existingLocation->id !== $loadedLocation->id) { |
|
|
|
|
479
|
|
|
throw new InvalidArgumentException('locationUpdateStruct', 'location with provided remote ID already exists'); |
480
|
|
|
} |
481
|
|
|
} catch (APINotFoundException $e) { |
|
|
|
|
482
|
|
|
} |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
if (!$this->repository->canUser('content', 'edit', $loadedLocation->getContentInfo(), $loadedLocation)) { |
|
|
|
|
486
|
|
|
throw new UnauthorizedException('content', 'edit'); |
487
|
|
|
} |
488
|
|
|
|
489
|
|
|
$updateStruct = new UpdateStruct(); |
490
|
|
|
$updateStruct->priority = $locationUpdateStruct->priority !== null ? $locationUpdateStruct->priority : $loadedLocation->priority; |
|
|
|
|
491
|
|
|
$updateStruct->remoteId = $locationUpdateStruct->remoteId !== null ? trim($locationUpdateStruct->remoteId) : $loadedLocation->remoteId; |
|
|
|
|
492
|
|
|
$updateStruct->sortField = $locationUpdateStruct->sortField !== null ? $locationUpdateStruct->sortField : $loadedLocation->sortField; |
|
|
|
|
493
|
|
|
$updateStruct->sortOrder = $locationUpdateStruct->sortOrder !== null ? $locationUpdateStruct->sortOrder : $loadedLocation->sortOrder; |
|
|
|
|
494
|
|
|
|
495
|
|
|
$this->repository->beginTransaction(); |
496
|
|
|
try { |
497
|
|
|
$this->persistenceHandler->locationHandler()->update($updateStruct, $loadedLocation->id); |
|
|
|
|
498
|
|
|
$this->repository->commit(); |
499
|
|
|
} catch (Exception $e) { |
500
|
|
|
$this->repository->rollback(); |
501
|
|
|
throw $e; |
502
|
|
|
} |
503
|
|
|
|
504
|
|
|
return $this->loadLocation($loadedLocation->id); |
|
|
|
|
505
|
|
|
} |
506
|
|
|
|
507
|
|
|
/** |
508
|
|
|
* Swaps the contents held by $location1 and $location2. |
509
|
|
|
* |
510
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to swap content |
511
|
|
|
* |
512
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Location $location1 |
513
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Location $location2 |
514
|
|
|
*/ |
515
|
|
|
public function swapLocation(APILocation $location1, APILocation $location2) |
516
|
|
|
{ |
517
|
|
|
$loadedLocation1 = $this->loadLocation($location1->id); |
518
|
|
|
$loadedLocation2 = $this->loadLocation($location2->id); |
519
|
|
|
|
520
|
|
|
if (!$this->repository->canUser('content', 'edit', $loadedLocation1->getContentInfo(), $loadedLocation1)) { |
|
|
|
|
521
|
|
|
throw new UnauthorizedException('content', 'edit'); |
522
|
|
|
} |
523
|
|
|
if (!$this->repository->canUser('content', 'edit', $loadedLocation2->getContentInfo(), $loadedLocation2)) { |
|
|
|
|
524
|
|
|
throw new UnauthorizedException('content', 'edit'); |
525
|
|
|
} |
526
|
|
|
|
527
|
|
|
$this->repository->beginTransaction(); |
528
|
|
|
try { |
529
|
|
|
$this->persistenceHandler->locationHandler()->swap($loadedLocation1->id, $loadedLocation2->id); |
|
|
|
|
530
|
|
|
$this->persistenceHandler->urlAliasHandler()->locationSwapped( |
531
|
|
|
$location1->id, |
532
|
|
|
$location1->parentLocationId, |
533
|
|
|
$location2->id, |
534
|
|
|
$location2->parentLocationId |
535
|
|
|
); |
536
|
|
|
$this->repository->commit(); |
537
|
|
|
} catch (Exception $e) { |
538
|
|
|
$this->repository->rollback(); |
539
|
|
|
throw $e; |
540
|
|
|
} |
541
|
|
|
} |
542
|
|
|
|
543
|
|
|
/** |
544
|
|
|
* Hides the $location and marks invisible all descendants of $location. |
545
|
|
|
* |
546
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to hide this location |
547
|
|
|
* |
548
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Location $location |
549
|
|
|
* |
550
|
|
|
* @return \eZ\Publish\API\Repository\Values\Content\Location $location, with updated hidden value |
551
|
|
|
*/ |
552
|
|
View Code Duplication |
public function hideLocation(APILocation $location) |
553
|
|
|
{ |
554
|
|
|
if (!$this->repository->canUser('content', 'hide', $location->getContentInfo(), $location)) { |
|
|
|
|
555
|
|
|
throw new UnauthorizedException('content', 'hide'); |
556
|
|
|
} |
557
|
|
|
|
558
|
|
|
$this->repository->beginTransaction(); |
559
|
|
|
try { |
560
|
|
|
$this->persistenceHandler->locationHandler()->hide($location->id); |
561
|
|
|
$this->repository->commit(); |
562
|
|
|
} catch (Exception $e) { |
563
|
|
|
$this->repository->rollback(); |
564
|
|
|
throw $e; |
565
|
|
|
} |
566
|
|
|
|
567
|
|
|
return $this->loadLocation($location->id); |
568
|
|
|
} |
569
|
|
|
|
570
|
|
|
/** |
571
|
|
|
* Unhides the $location. |
572
|
|
|
* |
573
|
|
|
* This method and marks visible all descendants of $locations |
574
|
|
|
* until a hidden location is found. |
575
|
|
|
* |
576
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to unhide this location |
577
|
|
|
* |
578
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Location $location |
579
|
|
|
* |
580
|
|
|
* @return \eZ\Publish\API\Repository\Values\Content\Location $location, with updated hidden value |
581
|
|
|
*/ |
582
|
|
View Code Duplication |
public function unhideLocation(APILocation $location) |
583
|
|
|
{ |
584
|
|
|
if (!$this->repository->canUser('content', 'hide', $location->getContentInfo(), $location)) { |
|
|
|
|
585
|
|
|
throw new UnauthorizedException('content', 'hide'); |
586
|
|
|
} |
587
|
|
|
|
588
|
|
|
$this->repository->beginTransaction(); |
589
|
|
|
try { |
590
|
|
|
$this->persistenceHandler->locationHandler()->unHide($location->id); |
591
|
|
|
$this->repository->commit(); |
592
|
|
|
} catch (Exception $e) { |
593
|
|
|
$this->repository->rollback(); |
594
|
|
|
throw $e; |
595
|
|
|
} |
596
|
|
|
|
597
|
|
|
return $this->loadLocation($location->id); |
598
|
|
|
} |
599
|
|
|
|
600
|
|
|
/** |
601
|
|
|
* Moves the subtree to $newParentLocation. |
602
|
|
|
* |
603
|
|
|
* If a user has the permission to move the location to a target location |
604
|
|
|
* he can do it regardless of an existing descendant on which the user has no permission. |
605
|
|
|
* |
606
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to move this location to the target |
607
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user does not have read access to the whole source subtree |
608
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException If the new parent is in a subtree of the location |
609
|
|
|
* |
610
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Location $location |
611
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Location $newParentLocation |
612
|
|
|
*/ |
613
|
|
|
public function moveSubtree(APILocation $location, APILocation $newParentLocation) |
614
|
|
|
{ |
615
|
|
|
$location = $this->loadLocation($location->id); |
616
|
|
|
$newParentLocation = $this->loadLocation($newParentLocation->id); |
617
|
|
|
|
618
|
|
|
// check create permission on target location |
619
|
|
|
if (!$this->repository->canUser('content', 'create', $location->getContentInfo(), $newParentLocation)) { |
|
|
|
|
620
|
|
|
throw new UnauthorizedException('content', 'create'); |
621
|
|
|
} |
622
|
|
|
|
623
|
|
|
/** Check read access to whole source subtree |
624
|
|
|
* @var bool|\eZ\Publish\API\Repository\Values\Content\Query\Criterion |
625
|
|
|
*/ |
626
|
|
|
$contentReadCriterion = $this->permissionCriterionResolver->getPermissionsCriterion(); |
|
|
|
|
627
|
|
View Code Duplication |
if ($contentReadCriterion === false) { |
|
|
|
|
628
|
|
|
throw new UnauthorizedException('content', 'read'); |
629
|
|
|
} elseif ($contentReadCriterion !== true) { |
630
|
|
|
// Query if there are any content in subtree current user don't have access to |
631
|
|
|
$query = new Query( |
632
|
|
|
array( |
633
|
|
|
'limit' => 0, |
634
|
|
|
'filter' => new CriterionLogicalAnd( |
635
|
|
|
array( |
636
|
|
|
new CriterionSubtree($location->pathString), |
|
|
|
|
637
|
|
|
new CriterionLogicalNot($contentReadCriterion), |
|
|
|
|
638
|
|
|
) |
639
|
|
|
), |
640
|
|
|
) |
641
|
|
|
); |
642
|
|
|
$result = $this->repository->getSearchService()->findContent($query, array(), false); |
643
|
|
|
if ($result->totalCount > 0) { |
644
|
|
|
throw new UnauthorizedException('content', 'read'); |
645
|
|
|
} |
646
|
|
|
} |
647
|
|
|
|
648
|
|
|
if (strpos($newParentLocation->pathString, $location->pathString) === 0) { |
|
|
|
|
649
|
|
|
throw new InvalidArgumentException( |
650
|
|
|
'$newParentLocation', |
651
|
|
|
'new parent location is in a subtree of the given $location' |
652
|
|
|
); |
653
|
|
|
} |
654
|
|
|
|
655
|
|
|
$this->repository->beginTransaction(); |
656
|
|
|
try { |
657
|
|
|
$this->persistenceHandler->locationHandler()->move($location->id, $newParentLocation->id); |
|
|
|
|
658
|
|
|
|
659
|
|
|
$content = $this->repository->getContentService()->loadContent($location->contentId); |
660
|
|
|
$urlAliasNames = $this->nameSchemaService->resolveUrlAliasSchema($content); |
661
|
|
View Code Duplication |
foreach ($urlAliasNames as $languageCode => $name) { |
|
|
|
|
662
|
|
|
$this->persistenceHandler->urlAliasHandler()->publishUrlAliasForLocation( |
663
|
|
|
$location->id, |
|
|
|
|
664
|
|
|
$newParentLocation->id, |
|
|
|
|
665
|
|
|
$name, |
666
|
|
|
$languageCode, |
667
|
|
|
$content->contentInfo->alwaysAvailable |
668
|
|
|
); |
669
|
|
|
} |
670
|
|
|
|
671
|
|
|
$this->persistenceHandler->urlAliasHandler()->locationMoved( |
672
|
|
|
$location->id, |
|
|
|
|
673
|
|
|
$location->parentLocationId, |
|
|
|
|
674
|
|
|
$newParentLocation->id |
|
|
|
|
675
|
|
|
); |
676
|
|
|
|
677
|
|
|
$this->repository->commit(); |
678
|
|
|
} catch (Exception $e) { |
679
|
|
|
$this->repository->rollback(); |
680
|
|
|
throw $e; |
681
|
|
|
} |
682
|
|
|
} |
683
|
|
|
|
684
|
|
|
/** |
685
|
|
|
* Deletes $location and all its descendants. |
686
|
|
|
* |
687
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user is not allowed to delete this location or a descendant |
688
|
|
|
* |
689
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Location $location |
690
|
|
|
*/ |
691
|
|
|
public function deleteLocation(APILocation $location) |
692
|
|
|
{ |
693
|
|
|
$location = $this->loadLocation($location->id); |
694
|
|
|
|
695
|
|
|
if (!$this->repository->canUser('content', 'manage_locations', $location->getContentInfo())) { |
|
|
|
|
696
|
|
|
throw new UnauthorizedException('content', 'manage_locations'); |
697
|
|
|
} |
698
|
|
|
if (!$this->repository->canUser('content', 'remove', $location->getContentInfo(), $location)) { |
|
|
|
|
699
|
|
|
throw new UnauthorizedException('content', 'remove'); |
700
|
|
|
} |
701
|
|
|
|
702
|
|
|
/** Check remove access to descendants |
703
|
|
|
* @var bool|\eZ\Publish\API\Repository\Values\Content\Query\Criterion |
704
|
|
|
*/ |
705
|
|
|
$contentReadCriterion = $this->permissionCriterionResolver->getPermissionsCriterion('content', 'remove'); |
706
|
|
View Code Duplication |
if ($contentReadCriterion === false) { |
|
|
|
|
707
|
|
|
throw new UnauthorizedException('content', 'remove'); |
708
|
|
|
} elseif ($contentReadCriterion !== true) { |
709
|
|
|
// Query if there are any content in subtree current user don't have access to |
710
|
|
|
$query = new Query( |
711
|
|
|
array( |
712
|
|
|
'limit' => 0, |
713
|
|
|
'filter' => new CriterionLogicalAnd( |
714
|
|
|
array( |
715
|
|
|
new CriterionSubtree($location->pathString), |
|
|
|
|
716
|
|
|
new CriterionLogicalNot($contentReadCriterion), |
|
|
|
|
717
|
|
|
) |
718
|
|
|
), |
719
|
|
|
) |
720
|
|
|
); |
721
|
|
|
$result = $this->repository->getSearchService()->findContent($query, array(), false); |
722
|
|
|
if ($result->totalCount > 0) { |
723
|
|
|
throw new UnauthorizedException('content', 'remove'); |
724
|
|
|
} |
725
|
|
|
} |
726
|
|
|
|
727
|
|
|
$this->repository->beginTransaction(); |
728
|
|
|
try { |
729
|
|
|
$this->persistenceHandler->locationHandler()->removeSubtree($location->id); |
|
|
|
|
730
|
|
|
$this->persistenceHandler->urlAliasHandler()->locationDeleted($location->id); |
|
|
|
|
731
|
|
|
$this->repository->commit(); |
732
|
|
|
} catch (Exception $e) { |
733
|
|
|
$this->repository->rollback(); |
734
|
|
|
throw $e; |
735
|
|
|
} |
736
|
|
|
} |
737
|
|
|
|
738
|
|
|
/** |
739
|
|
|
* Instantiates a new location create class. |
740
|
|
|
* |
741
|
|
|
* @param mixed $parentLocationId the parent under which the new location should be created |
742
|
|
|
* |
743
|
|
|
* @return \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct |
744
|
|
|
*/ |
745
|
|
|
public function newLocationCreateStruct($parentLocationId) |
746
|
|
|
{ |
747
|
|
|
return new LocationCreateStruct( |
748
|
|
|
array( |
749
|
|
|
'parentLocationId' => $parentLocationId, |
750
|
|
|
) |
751
|
|
|
); |
752
|
|
|
} |
753
|
|
|
|
754
|
|
|
/** |
755
|
|
|
* Instantiates a new location update class. |
756
|
|
|
* |
757
|
|
|
* @return \eZ\Publish\API\Repository\Values\Content\LocationUpdateStruct |
758
|
|
|
*/ |
759
|
|
|
public function newLocationUpdateStruct() |
760
|
|
|
{ |
761
|
|
|
return new LocationUpdateStruct(); |
762
|
|
|
} |
763
|
|
|
} |
764
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.