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

LocationLimitationType.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\Limitation;
8
9 use eZ\Publish\API\Repository\Exceptions\NotFoundException as APINotFoundException;
10 use eZ\Publish\API\Repository\Values\ValueObject;
11 use eZ\Publish\API\Repository\Values\User\UserReference as APIUserReference;
12 use eZ\Publish\API\Repository\Values\Content\Content;
13 use eZ\Publish\API\Repository\Values\Content\ContentInfo;
14 use eZ\Publish\API\Repository\Values\Content\ContentCreateStruct;
15 use eZ\Publish\API\Repository\Values\Content\VersionInfo;
16 use eZ\Publish\API\Repository\Values\Content\Location;
17 use eZ\Publish\API\Repository\Values\Content\LocationCreateStruct;
18 use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
19 use eZ\Publish\Core\Base\Exceptions\InvalidArgumentType;
20 use eZ\Publish\API\Repository\Values\User\Limitation\LocationLimitation as APILocationLimitation;
21 use eZ\Publish\API\Repository\Values\User\Limitation as APILimitationValue;
22 use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
23 use eZ\Publish\SPI\Limitation\Type as SPILimitationTypeInterface;
24 use eZ\Publish\Core\FieldType\ValidationError;
25 use eZ\Publish\SPI\Persistence\Content\Location as SPILocation;
26
30 class LocationLimitationType extends AbstractPersistenceLimitationType implements SPILimitationTypeInterface
31 {
41 public function acceptValue(APILimitationValue $limitationValue)
42 {
43 if (!$limitationValue instanceof APILocationLimitation) {
44 throw new InvalidArgumentType('$limitationValue', 'APILocationLimitation', $limitationValue);
45 } elseif (!is_array($limitationValue->limitationValues)) {
46 throw new InvalidArgumentType('$limitationValue->limitationValues', 'array', $limitationValue->limitationValues);
47 }
48
49 foreach ($limitationValue->limitationValues as $key => $id) {
50 if (!is_string($id) && !is_int($id)) {
51 throw new InvalidArgumentType("\$limitationValue->limitationValues[{$key}]", 'int|string', $id);
52 }
53 }
54 }
55
65 public function validate(APILimitationValue $limitationValue)
66 {
67 $validationErrors = [];
68 foreach ($limitationValue->limitationValues as $key => $id) {
69 try {
70 $this->persistence->locationHandler()->load($id);
71 } catch (APINotFoundException $e) {
72 $validationErrors[] = new ValidationError(
73 "limitationValues[%key%] => '%value%' does not exist in the backend",
74 null,
75 [
76 'value' => $id,
77 'key' => $key,
78 ]
79 );
80 }
81 }
82
83 return $validationErrors;
84 }
85
93 public function buildValue(array $limitationValues)
94 {
95 return new APILocationLimitation(['limitationValues' => $limitationValues]);
96 }
97
113 public function evaluate(APILimitationValue $value, APIUserReference $currentUser, ValueObject $object, array $targets = null)
114 {
115 if (!$value instanceof APILocationLimitation) {
116 throw new InvalidArgumentException('$value', 'Must be of type: APILocationLimitation');
117 }
118
119 if ($object instanceof ContentCreateStruct) {
120 return $this->evaluateForContentCreateStruct($value, $targets);
121 } elseif ($object instanceof Content) {
122 $object = $object->getVersionInfo()->getContentInfo();
123 } elseif ($object instanceof VersionInfo) {
124 $object = $object->getContentInfo();
125 } elseif (!$object instanceof ContentInfo) {
126 throw new InvalidArgumentException(
127 '$object',
128 'Must be of type: ContentCreateStruct, Content, VersionInfo or ContentInfo'
129 );
130 }
131
132 // Load locations if no specific placement was provided
133 if ($targets === null) {
134 if ($object->published) {
135 $targets = $this->persistence->locationHandler()->loadLocationsByContent($object->id);
136 } elseif ($object->isTrashed()) {
137 $targets = $this->persistence->locationHandler()->loadLocationsByTrashContent($object->id);
138 } else {
139 // @todo Need support for draft locations to to work correctly
140 $targets = $this->persistence->locationHandler()->loadParentLocationsForDraftContent($object->id);
141 }
142 }
144 $targets = array_filter($targets, function ($target) {
145 return $target instanceof Location || $target instanceof SPILocation;
146 });
147
148 if (empty($targets)) {
149 throw new InvalidArgumentException('$targets', 'Must contain at least one Location object');
150 }
151
152 foreach ($targets as $target) {
153 if (in_array($target->id, $value->limitationValues)) {
154 return self::ACCESS_GRANTED;
155 }
156 }
157
158 return self::ACCESS_DENIED;
159 }
160
172 protected function evaluateForContentCreateStruct(APILimitationValue $value, array $targets = null)
173 {
174 // If targets is empty/null return false as user does not have access
175 // to content w/o location with this limitation
176 if (empty($targets)) {
177 return false;
178 }
179
180 $hasMandatoryTarget = false;
181 foreach ($targets as $target) {
182 if ($target instanceof LocationCreateStruct) {
183 $hasMandatoryTarget = true;
184
185 // For ContentCreateStruct all placements must match
186 if (!in_array($target->parentLocationId, $value->limitationValues)) {
187 return false;
188 }
189 }
190 }
191
192 if (false === $hasMandatoryTarget) {
193 throw new InvalidArgumentException(
194 '$targets',
195 'If $object is ContentCreateStruct must contain objects of type: LocationCreateStruct'
196 );
197 }
198
199 return true;
200 }
201
210 public function getCriterion(APILimitationValue $value, APIUserReference $currentUser)
211 {
212 if (empty($value->limitationValues)) {
213 // no limitation values
214 throw new \RuntimeException('$value->limitationValues is empty, it should not have been stored in the first place');
215 }
216
217 if (!isset($value->limitationValues[1])) {
218 // 1 limitation value: EQ operation
219 return new Criterion\LocationId($value->limitationValues[0]);
220 }
221
222 // several limitation values: IN operation
223 return new Criterion\LocationId($value->limitationValues);
224 }
225
232 public function valueSchema()
233 {
234 return self::VALUE_SCHEMA_LOCATION_ID;
235 }
236 }
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\Values\Content\ContentCreateStruct
Definition: API/Repository/Values/Content/ContentCreateStruct.php:15
eZ\Publish\API\Repository\Values\Content\ContentInfo
Definition: API/Repository/Values/Content/ContentInfo.php:31
eZ\Publish\API\Repository\Values\Content\Content
Definition: Publish/API/Repository/Values/Content/Content.php:21
eZ\Publish\API\Repository\Values\Content\LocationCreateStruct
This class is used to create a new Location for a content object.
Definition: API/Repository/Values/Content/LocationCreateStruct.php:15
eZ\Publish\API\Repository\Values\Content\Location
Definition: API/Repository/Values/Content/Location.php:32
eZ\Publish\API\Repository\Values\Content\VersionInfo
Definition: API/Repository/Values/Content/VersionInfo.php:27
eZ\Publish\API\Repository\Values\User\Limitation\LocationLimitation
Definition: LocationLimitation.php:12
eZ\Publish\API\Repository\Values\ValueObject
Definition: API/Repository/Values/ValueObject.php:21
eZ\Publish\Core\Base\Exceptions\InvalidArgumentException
Invalid Argument Type Exception implementation.
Definition: Core/Base/Exceptions/InvalidArgumentException.php:20
eZ\Publish\Core\Base\Exceptions\InvalidArgumentType
Invalid Argument Type Exception implementation.
Definition: InvalidArgumentType.php:17
eZ\Publish\Core\FieldType\ValidationError
Class for validation errors.
Definition: Core/FieldType/ValidationError.php:17
eZ\Publish\Core\Limitation\AbstractPersistenceLimitationType
LocationLimitation is a Content limitation.
Definition: AbstractPersistenceLimitationType.php:15
eZ\Publish\Core\Limitation\LocationLimitationType
LocationLimitation is a Content limitation.
Definition: LocationLimitationType.php:31
eZ\Publish\Core\Limitation\LocationLimitationType\validate
validate(APILimitationValue $limitationValue)
Makes sure LimitationValue->limitationValues is valid according to valueSchema().
Definition: LocationLimitationType.php:65
eZ\Publish\Core\Limitation\LocationLimitationType\valueSchema
valueSchema()
Returns info on valid $limitationValues.
Definition: LocationLimitationType.php:232
eZ\Publish\Core\Limitation\LocationLimitationType\evaluateForContentCreateStruct
evaluateForContentCreateStruct(APILimitationValue $value, array $targets=null)
Evaluate permissions for ContentCreateStruct against LocationCreateStruct placements.
Definition: LocationLimitationType.php:172
eZ\Publish\Core\Limitation\LocationLimitationType\acceptValue
acceptValue(APILimitationValue $limitationValue)
Accepts a Limitation value and checks for structural validity.
Definition: LocationLimitationType.php:41
eZ\Publish\Core\Limitation\LocationLimitationType\getCriterion
getCriterion(APILimitationValue $value, APIUserReference $currentUser)
Returns Criterion for use in find() query.
Definition: LocationLimitationType.php:210
eZ\Publish\Core\Limitation\LocationLimitationType\buildValue
buildValue(array $limitationValues)
Create the Limitation Value.
Definition: LocationLimitationType.php:93
eZ\Publish\API\Repository\Values\User\UserReference
This interface represents a user reference for use in sessions and Repository.
Definition: API/Repository/Values/User/UserReference.php:13
eZ\Publish\SPI\Limitation\Type
This interface represent the Limitation Type.
Definition: SPI/Limitation/Type.php:22
eZ\Publish\SPI\Limitation\Type\ACCESS_GRANTED
const ACCESS_GRANTED
Constants for return value of {.
Definition: SPI/Limitation/Type.php:33
eZ\Publish\SPI\Limitation\Type\VALUE_SCHEMA_LOCATION_ID
const VALUE_SCHEMA_LOCATION_ID
Constants for valueSchema() return values.
Definition: SPI/Limitation/Type.php:43
eZ\Publish\SPI\Limitation\Type\ACCESS_DENIED
const ACCESS_DENIED
Definition: SPI/Limitation/Type.php:35
eZ\Publish\SPI\Limitation\Type\evaluate
evaluate(APILimitationValue $value, APIUserReference $currentUser, APIValueObject $object, array $targets=null)
Evaluate ("Vote") against a main value object and targets for the context.
eZ\Publish\API\Repository\Values\Content\Query\Criterion
Definition: API/Repository/Values/Content/Query/Criterion/Ancestor.php:7
eZ\Publish\API\Repository\Values\User\Limitation
Definition: BlockingLimitation.php:7
eZ\Publish\Core\Limitation
Definition: AbstractPersistenceLimitationType.php:7
eZ\Publish\SPI\Persistence\Content\Location
Definition: SPI/Persistence/Content/Location/CreateStruct.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.