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

RoleLimitationType.php
Go to the documentation of this file.
1 <?php
2
7 declare(strict_types=1);
8
9 namespace Ibexa\Core\Limitation;
10
11 use eZ\Publish\API\Repository\Exceptions\NotImplementedException;
12 use eZ\Publish\API\Repository\Values\User\Role;
13 use eZ\Publish\API\Repository\Values\User\User;
14 use eZ\Publish\API\Repository\Values\User\UserGroup;
15 use eZ\Publish\API\Repository\Values\User\UserGroupRoleAssignment;
16 use eZ\Publish\API\Repository\Values\User\UserReference as APIUserReference;
17 use eZ\Publish\API\Repository\Exceptions\NotFoundException;
18 use eZ\Publish\API\Repository\Values\User\UserRoleAssignment;
19 use eZ\Publish\API\Repository\Values\ValueObject;
20 use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
21 use eZ\Publish\Core\Base\Exceptions\InvalidArgumentType;
22 use eZ\Publish\Core\FieldType\ValidationError;
23 use eZ\Publish\Core\Limitation\AbstractPersistenceLimitationType;
24 use eZ\Publish\SPI\Limitation\Type as SPILimitationTypeInterface;
25 use eZ\Publish\API\Repository\Values\User\Limitation as APILimitationValue;
26 use Ibexa\Contracts\Core\Repository\Values\User\Limitation\RoleLimitation;
27
28 final class RoleLimitationType extends AbstractPersistenceLimitationType implements SPILimitationTypeInterface
29 {
33 public function acceptValue(APILimitationValue $limitationValue): void
34 {
35 if (!$limitationValue instanceof RoleLimitation) {
36 throw new InvalidArgumentType(
37 '$limitationValue',
38 RoleLimitation::class,
39 $limitationValue
40 );
41 }
42
43 if (!is_array($limitationValue->limitationValues)) {
44 throw new InvalidArgumentType(
45 '$limitationValue->limitationValues',
46 'array',
47 $limitationValue->limitationValues
48 );
49 }
50
51 foreach ($limitationValue->limitationValues as $key => $id) {
52 if (!is_int($id)) {
53 throw new InvalidArgumentType("\$limitationValue->limitationValues[{$key}]", 'int|string', $id);
54 }
55 }
56 }
57
58 public function validate(APILimitationValue $limitationValue)
59 {
60 $validationErrors = [];
61
62 foreach ($limitationValue->limitationValues as $key => $id) {
63 try {
64 $this->persistence->userHandler()->loadRole($id);
65 } catch (NotFoundException $e) {
66 $validationErrors[] = new ValidationError(
67 "limitationValues[%key%] => '%value%' does not exist in the backend",
68 null,
69 [
70 'value' => $id,
71 'key' => $key,
72 ]
73 );
74 }
75 }
76
77 return $validationErrors;
78 }
79
85 public function buildValue(array $limitationValues): APILimitationValue
86 {
87 return new RoleLimitation(['limitationValues' => $limitationValues]);
88 }
89
90 public function evaluate(APILimitationValue $value, APIUserReference $currentUser, ValueObject $object, array $targets = null)
91 {
92 if (!$value instanceof RoleLimitation) {
93 throw new InvalidArgumentException(
94 '$value',
95 sprintf('Must be of type: %s', RoleLimitation::class)
96 );
97 }
98
99 if (
100 !$object instanceof Role
101 && !$object instanceof UserRoleAssignment
102 && !$object instanceof UserGroupRoleAssignment
103 && ($targets === null && ($object instanceof User || $object instanceof UserGroup))
104 ) {
105 return self::ACCESS_ABSTAIN;
106 }
107
108 if ($targets !== null) {
109 foreach ($targets as $target) {
110 if ($target instanceof Role && !$this->evaluateRole($value, $target)) {
111 return self::ACCESS_DENIED;
112 }
113
114 return self::ACCESS_GRANTED;
115 }
116 }
117
118 if ($object instanceof Role) {
119 return $this->evaluateRole($value, $object);
120 }
121
122 if ($object instanceof UserRoleAssignment || $object instanceof UserGroupRoleAssignment) {
123 return $this->evaluateRole($value, $object->getRole());
124 }
125
126 return self::ACCESS_DENIED;
127 }
128
129 public function getCriterion(APILimitationValue $value, APIUserReference $currentUser)
130 {
131 throw new NotImplementedException('Role Limitation Criterion');
132 }
133
134 public function valueSchema()
135 {
136 throw new NotImplementedException(__METHOD__);
137 }
138
139 private function evaluateRole(RoleLimitation $value, Role $role): bool
140 {
141 return in_array($role->id, $value->limitationValues);
142 }
143 }
Ibexa\Contracts\Core\Repository\Values\User\Limitation\RoleLimitation
Definition: src/contracts/Repository/Values/User/Limitation/RoleLimitation.php:14
Ibexa\Core\Limitation\RoleLimitationType
Definition: RoleLimitationType.php:29
Ibexa\Core\Limitation\RoleLimitationType\evaluate
evaluate(APILimitationValue $value, APIUserReference $currentUser, ValueObject $object, array $targets=null)
Definition: RoleLimitationType.php:90
Ibexa\Core\Limitation\RoleLimitationType\valueSchema
valueSchema()
Returns info on valid $limitationValues.
Definition: RoleLimitationType.php:134
Ibexa\Core\Limitation\RoleLimitationType\buildValue
buildValue(array $limitationValues)
Definition: RoleLimitationType.php:85
Ibexa\Core\Limitation\RoleLimitationType\getCriterion
getCriterion(APILimitationValue $value, APIUserReference $currentUser)
Returns Criterion for use in find() query.
Definition: RoleLimitationType.php:129
Ibexa\Core\Limitation\RoleLimitationType\evaluateRole
evaluateRole(RoleLimitation $value, Role $role)
Definition: RoleLimitationType.php:139
Ibexa\Core\Limitation\RoleLimitationType\acceptValue
acceptValue(APILimitationValue $limitationValue)
Definition: RoleLimitationType.php:33
Ibexa\Core\Limitation\RoleLimitationType\validate
validate(APILimitationValue $limitationValue)
Makes sure LimitationValue->limitationValues is valid according to valueSchema().
Definition: RoleLimitationType.php:58
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\Exceptions\NotImplementedException
This Exception is thrown if a feature has not been implemented intentionally.
Definition: API/Repository/Exceptions/NotImplementedException.php:15
eZ\Publish\API\Repository\Values\User\Role
Definition: API/Repository/Values/User/Role.php:19
eZ\Publish\API\Repository\Values\User\UserGroupRoleAssignment
Definition: API/Repository/Values/User/UserGroupRoleAssignment.php:15
eZ\Publish\API\Repository\Values\User\UserGroup
Definition: API/Repository/Values/User/UserGroup.php:17
eZ\Publish\API\Repository\Values\User\UserRoleAssignment
Definition: API/Repository/Values/User/UserRoleAssignment.php:15
eZ\Publish\API\Repository\Values\User\User
Definition: Publish/API/Repository/Values/User/User.php:24
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\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_ABSTAIN
const ACCESS_ABSTAIN
Definition: SPI/Limitation/Type.php:34
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\ACCESS_DENIED
const ACCESS_DENIED
Definition: SPI/Limitation/Type.php:35
Ibexa\Core\Limitation
Definition: MemberOfLimitationType.php:9
eZ\Publish\API\Repository\Values\User\Limitation
Definition: BlockingLimitation.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.