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

OwnerLimitationType.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\Limitation;
8
9 use eZ\Publish\API\Repository\Values\ValueObject;
10 use eZ\Publish\API\Repository\Values\User\UserReference as APIUserReference;
11 use eZ\Publish\API\Repository\Values\Content\Content;
12 use eZ\Publish\API\Repository\Values\Content\ContentInfo;
13 use eZ\Publish\API\Repository\Values\Content\ContentCreateStruct;
14 use eZ\Publish\API\Repository\Values\Content\VersionInfo;
15 use eZ\Publish\Core\Base\Exceptions\BadStateException;
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\OwnerLimitation as APIOwnerLimitation;
19 use eZ\Publish\API\Repository\Values\User\Limitation as APILimitationValue;
20 use eZ\Publish\SPI\Limitation\Type as SPILimitationTypeInterface;
21 use eZ\Publish\Core\FieldType\ValidationError;
22 use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
23
27 class OwnerLimitationType extends AbstractPersistenceLimitationType implements SPILimitationTypeInterface
28 {
38 public function acceptValue(APILimitationValue $limitationValue)
39 {
40 if (!$limitationValue instanceof APIOwnerLimitation) {
41 throw new InvalidArgumentType('$limitationValue', 'APIOwnerLimitation', $limitationValue);
42 } elseif (!is_array($limitationValue->limitationValues)) {
43 throw new InvalidArgumentType('$limitationValue->limitationValues', 'array', $limitationValue->limitationValues);
44 }
45
46 foreach ($limitationValue->limitationValues as $key => $value) {
47 // Cast integers passed as string to int
48 if (is_string($value) && ctype_digit($value)) {
49 $limitationValue->limitationValues[$key] = (int)$value;
50 } elseif (!is_int($value)) {
51 throw new InvalidArgumentType("\$limitationValue->limitationValues[{$key}]", 'int', $value);
52 }
53 }
54 }
55
65 public function validate(APILimitationValue $limitationValue)
66 {
67 $validationErrors = [];
68 foreach ($limitationValue->limitationValues as $key => $value) {
69 if ($value !== 1 && $value !== 2) {
70 $validationErrors[] = new ValidationError(
71 "limitationValues[%key%] => '%value%' must be either 1 (owner) or 2 (session)",
72 null,
73 [
74 'value' => $value,
75 'key' => $key,
76 ]
77 );
78 }
79 }
80
81 return $validationErrors;
82 }
83
91 public function buildValue(array $limitationValues)
92 {
93 return new APIOwnerLimitation(['limitationValues' => $limitationValues]);
94 }
95
113 public function evaluate(APILimitationValue $value, APIUserReference $currentUser, ValueObject $object, array $targets = null)
114 {
115 if (!$value instanceof APIOwnerLimitation) {
116 throw new InvalidArgumentException('$value', 'Must be of type: APIOwnerLimitation');
117 }
118
119 if ($value->limitationValues[0] != 1 && $value->limitationValues[0] != 2) {
120 throw new BadStateException(
121 'Owner limitation',
122 'expected limitation value to be 1 or 2 but got:' . $value->limitationValues[0]
123 );
124 }
125
126 if ($object instanceof Content) {
127 $object = $object->getVersionInfo()->getContentInfo();
128 } elseif ($object instanceof VersionInfo) {
129 $object = $object->getContentInfo();
130 } elseif (!$object instanceof ContentInfo && !$object instanceof ContentCreateStruct) {
131 throw new InvalidArgumentException(
132 '$object',
133 'Must be of type: ContentCreateStruct, Content, VersionInfo or ContentInfo'
134 );
135 }
136
137 $userId = $currentUser->getUserId();
138
139 /*
140 * @var $object ContentInfo
141 */
142 $isOwner = $object->ownerId === $userId;
143 $isSelf = $object instanceof ContentInfo && $object->id === $userId;
144
145 return $isOwner || $isSelf;
146 }
147
158 public function getCriterion(APILimitationValue $value, APIUserReference $currentUser)
159 {
160 if (empty($value->limitationValues)) {
161 // no limitation values
162 throw new \RuntimeException('$value->limitationValues is empty, it should not have been stored in the first place');
163 }
164
165 if ($value->limitationValues[0] != 1 && $value->limitationValues[0] != 2) {
166 throw new BadStateException(
167 'Parent User Group limitation',
168 'expected limitation value to be 1 but got:' . $value->limitationValues[0]
169 );
170 }
171
172 return new Criterion\UserMetadata(
173 Criterion\UserMetadata::OWNER,
174 Criterion\Operator::EQ,
175 $currentUser->getUserId()
176 );
177 }
178
185 public function valueSchema()
186 {
187 throw new \eZ\Publish\API\Repository\Exceptions\NotImplementedException(__METHOD__);
188 }
189 }
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\Query\Criterion
Definition: API/Repository/Values/Content/Query/Criterion.php:15
eZ\Publish\API\Repository\Values\Content\VersionInfo
Definition: API/Repository/Values/Content/VersionInfo.php:27
eZ\Publish\API\Repository\Values\User\Limitation\OwnerLimitation
Definition: OwnerLimitation.php:12
eZ\Publish\API\Repository\Values\ValueObject
Definition: API/Repository/Values/ValueObject.php:21
eZ\Publish\Core\Base\Exceptions\BadStateException
BadState Exception implementation.
Definition: Core/Base/Exceptions/BadStateException.php:20
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\OwnerLimitationType
OwnerLimitation is a Content limitation.
Definition: OwnerLimitationType.php:28
eZ\Publish\Core\Limitation\OwnerLimitationType\valueSchema
valueSchema()
Returns info on valid $limitationValues.
Definition: OwnerLimitationType.php:185
eZ\Publish\Core\Limitation\OwnerLimitationType\buildValue
buildValue(array $limitationValues)
Create the Limitation Value.
Definition: OwnerLimitationType.php:91
eZ\Publish\Core\Limitation\OwnerLimitationType\validate
validate(APILimitationValue $limitationValue)
Makes sure LimitationValue->limitationValues is valid according to valueSchema().
Definition: OwnerLimitationType.php:65
eZ\Publish\Core\Limitation\OwnerLimitationType\getCriterion
getCriterion(APILimitationValue $value, APIUserReference $currentUser)
Returns Criterion for use in find() query.
Definition: OwnerLimitationType.php:158
eZ\Publish\Core\Limitation\OwnerLimitationType\evaluate
evaluate(APILimitationValue $value, APIUserReference $currentUser, ValueObject $object, array $targets=null)
Evaluate permission against content & target(placement/parent/assignment).
Definition: OwnerLimitationType.php:113
eZ\Publish\Core\Limitation\OwnerLimitationType\acceptValue
acceptValue(APILimitationValue $limitationValue)
Accepts a Limitation value and checks for structural validity.
Definition: OwnerLimitationType.php:38
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\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+.