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

UserGroupLimitationType.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\UserGroupLimitation as APIUserGroupLimitation;
19 use eZ\Publish\API\Repository\Values\User\Limitation as APILimitationValue;
20 use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
21 use eZ\Publish\SPI\Limitation\Type as SPILimitationTypeInterface;
22 use eZ\Publish\Core\FieldType\ValidationError;
23
27 class UserGroupLimitationType extends AbstractPersistenceLimitationType implements SPILimitationTypeInterface
28 {
38 public function acceptValue(APILimitationValue $limitationValue)
39 {
40 if (!$limitationValue instanceof APIUserGroupLimitation) {
41 throw new InvalidArgumentType('$limitationValue', 'APIUserGroupLimitation', $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 // Accept a true value for b/c with 5.0
48 if ($value === true) {
49 $limitationValue->limitationValues[$key] = 1;
50 } elseif (is_string($value) && ctype_digit($value)) {
51 // Cast integers passed as string to int
52 $limitationValue->limitationValues[$key] = (int)$value;
53 } elseif (!is_int($value)) {
54 throw new InvalidArgumentType("\$limitationValue->limitationValues[{$key}]", 'int', $value);
55 }
56 }
57 }
58
68 public function validate(APILimitationValue $limitationValue)
69 {
70 $validationErrors = [];
71 foreach ($limitationValue->limitationValues as $key => $value) {
72 if ($value !== 1) {
73 $validationErrors[] = new ValidationError(
74 "limitationValues[%key%] => '%value%' must be 1 (owner)",
75 null,
76 [
77 'value' => $value,
78 'key' => $key,
79 ]
80 );
81 }
82 }
83
84 return $validationErrors;
85 }
86
94 public function buildValue(array $limitationValues)
95 {
96 return new APIUserGroupLimitation(['limitationValues' => $limitationValues]);
97 }
98
114 public function evaluate(APILimitationValue $value, APIUserReference $currentUser, ValueObject $object, array $targets = null)
115 {
116 if (!$value instanceof APIUserGroupLimitation) {
117 throw new InvalidArgumentException('$value', 'Must be of type: APIUserGroupLimitation');
118 }
119
120 if ($value->limitationValues[0] != 1) {
121 throw new BadStateException(
122 'Parent User Group limitation',
123 'expected limitation value to be 1 but got:' . $value->limitationValues[0]
124 );
125 }
126
127 if ($object instanceof Content) {
128 $object = $object->getVersionInfo()->getContentInfo();
129 } elseif ($object instanceof VersionInfo) {
130 $object = $object->getContentInfo();
131 } elseif (!$object instanceof ContentInfo && !$object instanceof ContentCreateStruct) {
132 throw new InvalidArgumentException(
133 '$object',
134 'Must be of type: ContentCreateStruct, Content, VersionInfo or ContentInfo'
135 );
136 }
137
139 if ($object->ownerId === $currentUser->getUserId()) {
140 return true;
141 }
142
143 /*
144 * As long as SPI userHandler and API UserService does not speak the same language, this is the ugly truth;
145 */
146 $locationHandler = $this->persistence->locationHandler();
147 $ownerLocations = $locationHandler->loadLocationsByContent($object->ownerId);
148 if (empty($ownerLocations)) {
149 return false;
150 }
151
152 $currentUserLocations = $locationHandler->loadLocationsByContent($currentUser->getUserId());
153 if (empty($currentUserLocations)) {
154 return false;
155 }
156
157 // @todo Needs to take care of inherited groups as well when UserHandler gets knowledge about user groups
158 foreach ($ownerLocations as $ownerLocation) {
159 foreach ($currentUserLocations as $currentUserLocation) {
160 if ($ownerLocation->parentId === $currentUserLocation->parentId) {
161 return true;
162 }
163 }
164 }
165
166 return false;
167 }
168
177 public function getCriterion(APILimitationValue $value, APIUserReference $currentUser)
178 {
179 if (empty($value->limitationValues)) {
180 // no limitation values
181 throw new \RuntimeException('$value->limitationValues is empty, it should not have been stored in the first place');
182 }
183
184 if ($value->limitationValues[0] != 1) {
185 throw new BadStateException(
186 'Parent User Group limitation',
187 'expected limitation value to be 1 but got:' . $value->limitationValues[0]
188 );
189 }
190
191 $groupIds = [];
192 $currentUserLocations = $this->persistence->locationHandler()->loadLocationsByContent($currentUser->getUserId());
193 if (!empty($currentUserLocations)) {
194 foreach ($currentUserLocations as $currentUserLocation) {
195 $groupIds[] = $currentUserLocation->parentId;
196 }
197 }
198
199 return new Criterion\UserMetadata(
200 Criterion\UserMetadata::GROUP,
201 Criterion\Operator::IN,
202 $groupIds
203 );
204 }
205
212 public function valueSchema()
213 {
214 throw new \eZ\Publish\API\Repository\Exceptions\NotImplementedException(__METHOD__);
215 }
216 }
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\UserGroupLimitation
Definition: UserGroupLimitation.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\UserGroupLimitationType
UserGroupLimitation is a Content Limitation.
Definition: UserGroupLimitationType.php:28
eZ\Publish\Core\Limitation\UserGroupLimitationType\valueSchema
valueSchema()
Returns info on valid $limitationValues.
Definition: UserGroupLimitationType.php:212
eZ\Publish\Core\Limitation\UserGroupLimitationType\validate
validate(APILimitationValue $limitationValue)
Makes sure LimitationValue->limitationValues is valid according to valueSchema().
Definition: UserGroupLimitationType.php:68
eZ\Publish\Core\Limitation\UserGroupLimitationType\acceptValue
acceptValue(APILimitationValue $limitationValue)
Accepts a Limitation value and checks for structural validity.
Definition: UserGroupLimitationType.php:38
eZ\Publish\Core\Limitation\UserGroupLimitationType\buildValue
buildValue(array $limitationValues)
Create the Limitation Value.
Definition: UserGroupLimitationType.php:94
eZ\Publish\Core\Limitation\UserGroupLimitationType\getCriterion
getCriterion(APILimitationValue $value, APIUserReference $currentUser)
Returns Criterion for use in find() query.
Definition: UserGroupLimitationType.php:177
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\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
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.