-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDataObjectService.php
More file actions
363 lines (309 loc) · 13.1 KB
/
DataObjectService.php
File metadata and controls
363 lines (309 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<?php
declare(strict_types=1);
/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/
namespace Pimcore\Bundle\StudioBackendBundle\DataObject\Service;
use Exception;
use Pimcore\Bundle\StaticResolverBundle\Models\DataObject\ClassDefinitionResolverInterface;
use Pimcore\Bundle\StaticResolverBundle\Models\DataObject\DataObjectServiceResolverInterface;
use Pimcore\Bundle\StaticResolverBundle\Models\Element\ServiceResolverInterface;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\DataObjectSearchServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\Query\DataObjectQuery;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\Query\QueryInterface;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\Request\DataObjectParameters;
use Pimcore\Bundle\StudioBackendBundle\DataIndex\SearchIndexFilterInterface;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Event\PreResponse\DataObjectEvent;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Schema\DataObject;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Schema\DataObjectAddParameters;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Schema\Type\DataObjectFolder;
use Pimcore\Bundle\StudioBackendBundle\DataObject\Util\Trait\ValidateObjectDataTrait;
use Pimcore\Bundle\StudioBackendBundle\Element\Service\ElementSaveServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\DatabaseException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ElementSavingFailedException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\ForbiddenException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidElementTypeException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidFilterServiceTypeException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidFilterTypeException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidQueryTypeException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\NotFoundException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\SearchException;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\UserNotFoundException;
use Pimcore\Bundle\StudioBackendBundle\Filter\Service\FilterServiceProviderInterface;
use Pimcore\Bundle\StudioBackendBundle\Response\Collection;
use Pimcore\Bundle\StudioBackendBundle\Security\Service\SecurityServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\ElementPermissions;
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\ElementTypes;
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\HttpResponseCodes;
use Pimcore\Bundle\StudioBackendBundle\Util\Trait\ElementProviderTrait;
use Pimcore\Bundle\StudioBackendBundle\Util\Trait\UserPermissionTrait;
use Pimcore\Model\DataObject\AbstractObject;
use Pimcore\Model\DataObject as DataObjectModel;
use Pimcore\Model\DataObject\ClassDefinition;
use Pimcore\Model\DataObject\Concrete;
use Pimcore\Model\FactoryInterface;
use Pimcore\Model\UserInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use function in_array;
use function sprintf;
/**
* @internal
*/
final readonly class DataObjectService implements DataObjectServiceInterface
{
use ElementProviderTrait;
use UserPermissionTrait;
use ValidateObjectDataTrait;
private const array ALLOWED_TYPES = [
AbstractObject::OBJECT_TYPE_OBJECT,
AbstractObject::OBJECT_TYPE_VARIANT,
];
private const string INDEX_SORT = 'index';
public function __construct(
private ClassDefinitionResolverInterface $classDefinitionResolver,
private DataServiceInterface $dataService,
private DataObjectSearchServiceInterface $dataObjectSearchService,
private DataObjectServiceResolverInterface $dataObjectServiceResolver,
private FactoryInterface $factory,
private FilterServiceProviderInterface $filterServiceProvider,
private EventDispatcherInterface $eventDispatcher,
private SecurityServiceInterface $securityService,
private ServiceResolverInterface $serviceResolver,
private ElementSaveServiceInterface $elementSaveService
) {
}
/**
* @throws DatabaseException
* @throws ElementSavingFailedException
* @throws ForbiddenException
* @throws InvalidElementTypeException
* @throws NotFoundException
* @throws UserNotFoundException
*/
public function addDataObject(
int $parentId,
DataObjectAddParameters $parameters,
): int {
$user = $this->securityService->getCurrentUser();
$parent = $this->getValidParent($user, $parentId);
if ($this->dataObjectServiceResolver->pathExists($parent->getFullPath() . '/' . $parameters->getKey())) {
throw new ElementSavingFailedException(null, 'Element with the same key and path already exists');
}
$class = $this->getValidClass($this->classDefinitionResolver, $parameters->getClassId());
$object = $this->getValidObjectByClass($parameters->getType(), $class->getName());
return $this->createNewObject(
$parent->getId(),
$object,
$class,
$user,
$parameters
);
}
/**
* @throws ForbiddenException|InvalidFilterServiceTypeException|InvalidQueryTypeException
* @throws InvalidFilterTypeException|NotFoundException|SearchException|UserNotFoundException
*/
public function getDataObjects(DataObjectParameters $parameters): Collection
{
/** @var SearchIndexFilterInterface $filterService */
$filterService = $this->filterServiceProvider->create(SearchIndexFilterInterface::SERVICE_TYPE);
$query = $filterService->applyFilters(
$parameters,
ElementTypes::TYPE_DATA_OBJECT
);
$query->setUser($this->securityService->getCurrentUser());
$this->setTreeSorting(
$this->getDataObjectElement(
$this->securityService->getCurrentUser(),
$parameters->getParentId() ?? 1
),
$query
);
$result = $this->dataObjectSearchService->searchDataObjects($query);
$items = $result->getItems();
foreach ($items as $item) {
$this->dispatchDataObjectEvent($item);
}
return new Collection($result->getTotalItems(), $items);
}
/**
* @throws SearchException|NotFoundException|UserNotFoundException
*/
public function getDataObject(int $id, bool $getDetailData = true): DataObject
{
$user = $this->securityService->getCurrentUser();
$dataObject = $this->dataObjectSearchService->getDataObjectById(
$id,
$user
);
if ($getDetailData) {
$this->getObjectDetailData($dataObject);
}
$this->dispatchDataObjectEvent($dataObject);
return $dataObject;
}
/**
* @throws SearchException|NotFoundException
*/
public function getDataObjectForUser(int $id, UserInterface $user): DataObject
{
$dataObject = $this->dataObjectSearchService->getDataObjectById($id, $user);
$this->dispatchDataObjectEvent($dataObject);
return $dataObject;
}
/**
* @throws SearchException|NotFoundException
*/
public function getDataObjectFolder(int $id, bool $checkPermissionsForCurrentUser = true): DataObjectFolder
{
$dataObject = $this->dataObjectSearchService->getDataObjectById(
$id,
$this->getUserForPermissionCheck($this->securityService, $checkPermissionsForCurrentUser)
);
if (!$dataObject instanceof DataObjectFolder) {
throw new NotFoundException(ElementTypes::TYPE_FOLDER, $id);
}
$this->dispatchDataObjectEvent($dataObject);
return $dataObject;
}
/**
* @throws SearchException|NotFoundException
*/
public function getDataObjectFolderForUser(int $id, UserInterface $user): DataObjectFolder
{
$dataObject = $this->dataObjectSearchService->getDataObjectById($id, $user);
if (!$dataObject instanceof DataObjectFolder) {
throw new NotFoundException(ElementTypes::TYPE_FOLDER, $id);
}
$this->dispatchDataObjectEvent($dataObject);
return $dataObject;
}
/**
* @throws ForbiddenException|NotFoundException
*/
public function getDataObjectElement(
UserInterface $user,
int $dataObjectId,
): DataObjectModel {
$dataObject = $this->getElement($this->serviceResolver, ElementTypes::TYPE_OBJECT, $dataObjectId);
$this->securityService->hasElementPermission($dataObject, $user, ElementPermissions::VIEW_PERMISSION);
if (!$dataObject instanceof DataObjectModel) {
throw new InvalidElementTypeException($dataObject->getType());
}
return $dataObject;
}
/**
* @throws ForbiddenException|NotFoundException
*/
public function getDataObjectElementByPath(
UserInterface $user,
string $path,
): DataObjectModel {
$dataObject = $this->getElementByPath($this->serviceResolver, ElementTypes::TYPE_OBJECT, $path);
$this->securityService->hasElementPermission($dataObject, $user, ElementPermissions::VIEW_PERMISSION);
if (!$dataObject instanceof DataObjectModel) {
throw new InvalidElementTypeException($dataObject->getType());
}
return $dataObject;
}
/**
* @throws ForbiddenException|InvalidQueryTypeException|NotFoundException|UserNotFoundException
*/
public function setTreeSorting(DataObjectModel $parent, QueryInterface $dataObjectQuery): void
{
if (!$dataObjectQuery instanceof DataObjectQuery) {
throw new InvalidQueryTypeException(
HttpResponseCodes::BAD_REQUEST->value,
'Query type has to be instance of ' . DataObjectQuery::class
);
}
if ($parent->getChildrenSortBy() === self::INDEX_SORT) {
$dataObjectQuery->orderByIndex();
return;
}
$dataObjectQuery->orderByPath(strtolower($parent->getChildrenSortOrder()));
}
/**
* @throws ForbiddenException|NotFoundException
*/
private function getValidParent(UserInterface $user, int $parentId): DataObjectModel
{
$parent = $this->getDataObjectElement($user, $parentId);
$this->securityService->hasElementPermission($parent, $user, ElementPermissions::CREATE_PERMISSION);
return $parent;
}
/**
* @throws DatabaseException|InvalidElementTypeException
*/
private function getValidObjectByClass(string $objectType, string $className): Concrete
{
if (!in_array($objectType, self::ALLOWED_TYPES, true)) {
throw new InvalidElementTypeException($objectType);
}
// class needs to be upper case for factory
$className = ucfirst($className);
$object = $this->factory->build('Pimcore\\Model\\DataObject\\' . $className);
if (!$object instanceof Concrete) {
throw new DatabaseException(sprintf('Class %s is not a valid data object class', $className));
}
return $object;
}
/**
* @throws ElementSavingFailedException
*/
private function createNewObject(
int $parentId,
Concrete $object,
ClassDefinition $class,
UserInterface $user,
DataObjectAddParameters $parameters,
): int {
try {
$object->setClassId($class->getId());
$object->setClassName($class->getName());
$object->setParentId($parentId);
$object->setKey($parameters->getKey());
$object->setType($parameters->getType());
$object->setCreationDate(time());
$object->setUserOwner($user->getId());
$object->setPublished(false);
$this->elementSaveService->save($object, $user, null);
return $object->getId();
} catch (Exception $exception) {
throw new ElementSavingFailedException(null, $exception->getMessage());
}
}
/**
* @throws InvalidElementTypeException|NotFoundException
*/
private function getObjectDetailData(DataObjectFolder|DataObject $dataObject): void
{
$element = $this->getElement($this->serviceResolver, ElementTypes::TYPE_OBJECT, $dataObject->getId());
$version = $this->getLatestVersionForUser($element, $this->securityService->getCurrentUser());
$element = $this->getVersionData($element, $version);
if (!$element instanceof DataObjectModel) {
return;
}
$this->dataService->setObjectDetailData(
$dataObject,
$element,
$version
);
}
private function dispatchDataObjectEvent(mixed $dataObject): void
{
$this->eventDispatcher->dispatch(
new DataObjectEvent($dataObject),
DataObjectEvent::EVENT_NAME
);
}
}