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

ContentTypeLimitationType.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\Core\Base\Exceptions\InvalidArgumentException;
17 use eZ\Publish\Core\Base\Exceptions\InvalidArgumentType;
18 use eZ\Publish\API\Repository\Values\User\Limitation\ContentTypeLimitation as APIContentTypeLimitation;
19 use eZ\Publish\API\Repository\Values\User\Limitation as APILimitationValue;
20 use eZ\Publish\SPI\Limitation\Target;
21 use eZ\Publish\SPI\Limitation\TargetAwareType as SPITargetAwareLimitationType;
22 use eZ\Publish\Core\FieldType\ValidationError;
23 use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
24
28 class ContentTypeLimitationType extends AbstractPersistenceLimitationType implements SPITargetAwareLimitationType
29 {
39 public function acceptValue(APILimitationValue $limitationValue)
40 {
41 if (!$limitationValue instanceof APIContentTypeLimitation) {
42 throw new InvalidArgumentType('$limitationValue', 'APIContentTypeLimitation', $limitationValue);
43 } elseif (!is_array($limitationValue->limitationValues)) {
44 throw new InvalidArgumentType('$limitationValue->limitationValues', 'array', $limitationValue->limitationValues);
45 }
46
47 foreach ($limitationValue->limitationValues as $key => $id) {
48 if (!is_string($id) && !is_int($id)) {
49 throw new InvalidArgumentType("\$limitationValue->limitationValues[{$key}]", 'int|string', $id);
50 }
51 }
52 }
53
63 public function validate(APILimitationValue $limitationValue)
64 {
65 $validationErrors = [];
66 foreach ($limitationValue->limitationValues as $key => $id) {
67 try {
68 $this->persistence->contentTypeHandler()->load($id);
69 } catch (APINotFoundException $e) {
70 $validationErrors[] = new ValidationError(
71 "limitationValues[%key%] => '%value%' does not exist in the backend",
72 null,
73 [
74 'value' => $id,
75 'key' => $key,
76 ]
77 );
78 }
79 }
80
81 return $validationErrors;
82 }
83
91 public function buildValue(array $limitationValues)
92 {
93 return new APIContentTypeLimitation(['limitationValues' => $limitationValues]);
94 }
95
111 public function evaluate(APILimitationValue $value, APIUserReference $currentUser, ValueObject $object, array $targets = null): ?bool
112 {
113 $targets = $targets ?? [];
114 foreach ($targets as $target) {
115 if (!$target instanceof Target\Version) {
116 continue;
117 }
118
119 $accessVote = $this->evaluateVersionTarget($target, $value);
120
121 // continue evaluation of targets if there was no explicit grant/deny
122 if ($accessVote === self::ACCESS_ABSTAIN) {
123 continue;
124 }
125
126 return $accessVote;
127 }
128
129 if (!$value instanceof APIContentTypeLimitation) {
130 throw new InvalidArgumentException('$value', 'Must be of type: APIContentTypeLimitation');
131 }
132
133 if ($object instanceof Content) {
134 $object = $object->getVersionInfo()->getContentInfo();
135 } elseif ($object instanceof VersionInfo) {
136 $object = $object->getContentInfo();
137 } elseif (!$object instanceof ContentInfo && !$object instanceof ContentCreateStruct) {
138 throw new InvalidArgumentException(
139 '$object',
140 'Must be of type: ContentCreateStruct, Content, VersionInfo or ContentInfo'
141 );
142 }
143
144 if (empty($value->limitationValues)) {
145 return false;
146 }
147
148 if ($object instanceof ContentCreateStruct) {
149 return in_array($object->contentType->id, $value->limitationValues);
150 }
151
152 /*
153 * @var $object ContentInfo
154 */
155 return in_array($object->contentTypeId, $value->limitationValues);
156 }
157
166 public function getCriterion(APILimitationValue $value, APIUserReference $currentUser)
167 {
168 if (empty($value->limitationValues)) {
169 // no limitation values
170 throw new \RuntimeException('$value->limitationValues is empty, it should not have been stored in the first place');
171 }
172
173 if (!isset($value->limitationValues[1])) {
174 // 1 limitation value: EQ operation
175 return new Criterion\ContentTypeId($value->limitationValues[0]);
176 }
177
178 // several limitation values: IN operation
179 return new Criterion\ContentTypeId($value->limitationValues);
180 }
181
188 public function valueSchema()
189 {
190 throw new \eZ\Publish\API\Repository\Exceptions\NotImplementedException(__METHOD__);
191 }
192
201 private function evaluateVersionTarget(
202 Target\Version $version,
203 APILimitationValue $value
204 ): ?bool {
205 $accessVote = self::ACCESS_ABSTAIN;
206
207 // ... unless there's a specific list of target translations
208 if (!empty($version->allContentTypeIdsList)) {
209 $accessVote = $this->evaluateMatchingAnyLimitation(
210 $version->allContentTypeIdsList,
211 $value->limitationValues
212 );
213 }
214
215 return $accessVote;
216 }
217
226 private function evaluateMatchingAnyLimitation(
227 array $contentTypeIdsList,
228 array $limitationValues
229 ): bool {
230 return empty(array_intersect(array_map('strval', $contentTypeIdsList), $limitationValues))
231 ? self::ACCESS_DENIED
232 : self::ACCESS_GRANTED;
233 }
234 }
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\VersionInfo
Definition: API/Repository/Values/Content/VersionInfo.php:27
eZ\Publish\API\Repository\Values\User\Limitation\ContentTypeLimitation
Definition: ContentTypeLimitation.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\ContentTypeLimitationType
ContentTypeLimitation is a Content limitation.
Definition: ContentTypeLimitationType.php:29
eZ\Publish\Core\Limitation\ContentTypeLimitationType\validate
validate(APILimitationValue $limitationValue)
Makes sure LimitationValue->limitationValues is valid according to valueSchema().
Definition: ContentTypeLimitationType.php:63
eZ\Publish\Core\Limitation\ContentTypeLimitationType\getCriterion
getCriterion(APILimitationValue $value, APIUserReference $currentUser)
Returns Criterion for use in find() query.
Definition: ContentTypeLimitationType.php:166
eZ\Publish\Core\Limitation\ContentTypeLimitationType\acceptValue
acceptValue(APILimitationValue $limitationValue)
Accepts a Limitation value and checks for structural validity.
Definition: ContentTypeLimitationType.php:39
eZ\Publish\Core\Limitation\ContentTypeLimitationType\evaluateVersionTarget
evaluateVersionTarget(Target\Version $version, APILimitationValue $value)
Evaluate permissions to create new Version.
Definition: ContentTypeLimitationType.php:201
eZ\Publish\Core\Limitation\ContentTypeLimitationType\evaluateMatchingAnyLimitation
evaluateMatchingAnyLimitation(array $contentTypeIdsList, array $limitationValues)
Allow access if any of the given ContentTypes matches any of the limitation values.
Definition: ContentTypeLimitationType.php:226
eZ\Publish\Core\Limitation\ContentTypeLimitationType\valueSchema
valueSchema()
Returns info on valid $limitationValues.
Definition: ContentTypeLimitationType.php:188
eZ\Publish\Core\Limitation\ContentTypeLimitationType\buildValue
buildValue(array $limitationValues)
Create the Limitation Value.
Definition: ContentTypeLimitationType.php:91
eZ\Publish\Core\Limitation\ContentTypeLimitationType\evaluate
evaluate(APILimitationValue $value, APIUserReference $currentUser, ValueObject $object, array $targets=null)
Evaluate permission against content & target(placement/parent/assignment).
Definition: ContentTypeLimitationType.php:111
eZ\Publish\SPI\Limitation\Target\Version
Definition: SPI/Limitation/Target/Version.php:25
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\TargetAwareType
Represents Limitation type.
Definition: TargetAwareType.php:23
eZ\Publish\SPI\Limitation\Target
Marker interface for PermissionResolver::canUser $targets objects.
Definition: SPI/Limitation/Target.php:19
eZ\Publish\SPI\Limitation\Type\ACCESS_ABSTAIN
const ACCESS_ABSTAIN
Definition: SPI/Limitation/Type.php:34
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
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.