← All changes
|
src/ObjectMembership/ObjectMembershipHandler.php
+130
-30
2.3.18
→
2.1.11
View file →
| @@ -1,52 +1,116 @@ | ||
| 1 | 1 | <?php |
| 2 | - | |
| 3 | -declare(strict_types=1); | |
| 4 | - | |
| 2 | +/** | |
| 3 | + * MembershipHandler.php | |
| 4 | + * | |
| 5 | + * The MembershipHandler class file. | |
| 6 | + * | |
| 7 | + * PHP versions 5 | |
| 8 | + * | |
| 9 | + * @author Alexander Schneider <[email protected]> | |
| 10 | + * @copyright 2008-2017 Alexander Schneider | |
| 11 | + * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 | |
| 12 | + * @version SVN: $id$ | |
| 13 | + * @link http://wordpress.org/extend/plugins/user-access-manager/ | |
| 14 | + */ | |
| 5 | 15 | namespace UserAccessManager\ObjectMembership; |
| 6 | 16 | |
| 7 | -use Exception; | |
| 8 | -use UserAccessManager\ObjectMembership\Exception\MissingObjectTypeException; | |
| 9 | 17 | use UserAccessManager\UserGroup\AbstractUserGroup; |
| 10 | 18 | use UserAccessManager\UserGroup\AssignmentInformation; |
| 11 | 19 | use UserAccessManager\UserGroup\AssignmentInformationFactory; |
| 12 | 20 | |
| 21 | +/** | |
| 22 | + * Class MembershipHandler | |
| 23 | + * | |
| 24 | + * @package UserAccessManager\UserGroup | |
| 25 | + */ | |
| 13 | 26 | abstract class ObjectMembershipHandler |
| 14 | 27 | { |
| 15 | - protected ?string $generalObjectType = null; | |
| 28 | + /** | |
| 29 | + * @var AssignmentInformationFactory | |
| 30 | + */ | |
| 31 | + protected $assignmentInformationFactory; | |
| 16 | 32 | |
| 17 | 33 | /** |
| 18 | - * @throws Exception | |
| 34 | + * @var string | |
| 19 | 35 | */ |
| 36 | + protected $generalObjectType; | |
| 37 | + | |
| 38 | + /** | |
| 39 | + * @var array | |
| 40 | + */ | |
| 41 | + protected $assignedObjects; | |
| 42 | + | |
| 43 | + /** | |
| 44 | + * MembershipHandler constructor. | |
| 45 | + * | |
| 46 | + * @param AssignmentInformationFactory $assignmentInformationFactory | |
| 47 | + * | |
| 48 | + * @throws \Exception | |
| 49 | + */ | |
| 20 | 50 | public function __construct( |
| 21 | - protected AssignmentInformationFactory $assignmentInformationFactory | |
| 51 | + AssignmentInformationFactory $assignmentInformationFactory | |
| 22 | 52 | ) { |
| 23 | 53 | if ($this->generalObjectType === null) { |
| 24 | 54 | throw new MissingObjectTypeException('Missing general object type, Object type must be set.'); |
| 25 | 55 | } |
| 56 | + | |
| 57 | + $this->assignmentInformationFactory = $assignmentInformationFactory; | |
| 26 | 58 | } |
| 27 | 59 | |
| 28 | - abstract public function getObjectName(int|string|null $objectId, string &$typeName = ''): int|string; | |
| 60 | + /** | |
| 61 | + * Returns the object name. | |
| 62 | + * | |
| 63 | + * @param string $objectId | |
| 64 | + * @param string $typeName | |
| 65 | + * | |
| 66 | + * @return string | |
| 67 | + */ | |
| 68 | + abstract public function getObjectName($objectId, &$typeName = ''); | |
| 29 | 69 | |
| 30 | - public function getGeneralObjectType(): string | |
| 70 | + /** | |
| 71 | + * Returns the general object type. | |
| 72 | + * | |
| 73 | + * @return string | |
| 74 | + */ | |
| 75 | + public function getGeneralObjectType() | |
| 31 | 76 | { |
| 32 | 77 | return $this->generalObjectType; |
| 33 | 78 | } |
| 34 | 79 | |
| 35 | - public function getHandledObjects(): array | |
| 80 | + /** | |
| 81 | + * Returns the handled objects. | |
| 82 | + * | |
| 83 | + * @return array | |
| 84 | + */ | |
| 85 | + public function getHandledObjects() | |
| 36 | 86 | { |
| 37 | 87 | return [$this->generalObjectType => $this->generalObjectType]; |
| 38 | 88 | } |
| 39 | 89 | |
| 40 | - public function handlesObject(mixed $objectType): bool | |
| 90 | + /** | |
| 91 | + * Returns if the object is handled by this handler. | |
| 92 | + * | |
| 93 | + * @param $objectType | |
| 94 | + * | |
| 95 | + * @return bool | |
| 96 | + */ | |
| 97 | + public function handlesObject($objectType) | |
| 41 | 98 | { |
| 42 | - return isset($this->getHandledObjects()[$objectType]); | |
| 99 | + $objectTypes = $this->getHandledObjects(); | |
| 100 | + return isset($objectTypes[$objectType]); | |
| 43 | 101 | } |
| 44 | 102 | |
| 103 | + /** | |
| 104 | + * Assigns recursive membership to the assignment information object. | |
| 105 | + * | |
| 106 | + * @param null|AssignmentInformation $assignmentInformation | |
| 107 | + * @param array $recursiveMembership | |
| 108 | + */ | |
| 45 | 109 | protected function assignRecursiveMembership( |
| 46 | - ?AssignmentInformation &$assignmentInformation, | |
| 110 | + &$assignmentInformation, | |
| 47 | 111 | array $recursiveMembership |
| 48 | - ): void { | |
| 112 | + ) { | |
| 49 | 113 | if ($assignmentInformation === null) { |
| 50 | 114 | $assignmentInformation = $this->assignmentInformationFactory->createAssignmentInformation(); |
| 51 | 115 | } |
| 52 | 116 | |
| @@ -52,13 +116,22 @@ | ||
| 52 | 116 | |
| 53 | 117 | $assignmentInformation->setRecursiveMembership($recursiveMembership); |
| 54 | 118 | } |
| 55 | 119 | |
| 120 | + /** | |
| 121 | + * Checks for the access and assigns the recursive membership array if access. | |
| 122 | + * | |
| 123 | + * @param bool $isMember | |
| 124 | + * @param array $recursiveMembership | |
| 125 | + * @param null|AssignmentInformation $assignmentInformation | |
| 126 | + * | |
| 127 | + * @return bool | |
| 128 | + */ | |
| 56 | 129 | protected function checkAccessWithRecursiveMembership( |
| 57 | - bool $isMember, | |
| 130 | + $isMember, | |
| 58 | 131 | array $recursiveMembership, |
| 59 | - ?AssignmentInformation &$assignmentInformation | |
| 60 | - ): bool { | |
| 132 | + &$assignmentInformation | |
| 133 | + ) { | |
| 61 | 134 | if ($isMember === true || count($recursiveMembership) > 0) { |
| 62 | 135 | $this->assignRecursiveMembership($assignmentInformation, $recursiveMembership); |
| 63 | 136 | return true; |
| 64 | 137 | } |
| @@ -65,25 +138,52 @@ | ||
| 65 | 138 | |
| 66 | 139 | return false; |
| 67 | 140 | } |
| 68 | 141 | |
| 69 | - protected function getSimpleAssignedObjects(AbstractUserGroup $userGroup, string $objectType): array | |
| 142 | + /** | |
| 143 | + * Returns the assigned objects as array map. | |
| 144 | + * | |
| 145 | + * @param AbstractUserGroup $userGroup | |
| 146 | + * @param string $objectType | |
| 147 | + * | |
| 148 | + * @return array | |
| 149 | + */ | |
| 150 | + protected function getSimpleAssignedObjects(AbstractUserGroup $userGroup, $objectType) | |
| 70 | 151 | { |
| 152 | + $objects = $userGroup->getAssignedObjects($objectType); | |
| 153 | + | |
| 71 | 154 | return array_map( |
| 72 | - fn(AssignmentInformation $assignmentInformation) => $assignmentInformation->getType(), | |
| 73 | - $userGroup->getAssignedObjects($objectType) | |
| 155 | + function (AssignmentInformation $element) { | |
| 156 | + return $element->getType(); | |
| 157 | + }, | |
| 158 | + $objects | |
| 74 | 159 | ); |
| 75 | 160 | } |
| 76 | 161 | |
| 162 | + /** | |
| 163 | + * Checks if the object is a member of the user group. | |
| 164 | + * | |
| 165 | + * @param AbstractUserGroup $userGroup | |
| 166 | + * @param bool $lockRecursive | |
| 167 | + * @param string $objectId | |
| 168 | + * @param null|AssignmentInformation $assignmentInformation | |
| 169 | + * | |
| 170 | + * @return bool | |
| 171 | + */ | |
| 77 | 172 | abstract public function isMember( |
| 78 | 173 | AbstractUserGroup $userGroup, |
| 79 | - bool $lockRecursive, | |
| 80 | - int|string|null $objectId, | |
| 81 | - ?AssignmentInformation &$assignmentInformation = null | |
| 82 | - ): bool; | |
| 174 | + $lockRecursive, | |
| 175 | + $objectId, | |
| 176 | + &$assignmentInformation = null | |
| 177 | + ); | |
| 83 | 178 | |
| 84 | - abstract public function getFullObjects( | |
| 85 | - AbstractUserGroup $userGroup, | |
| 86 | - bool $lockRecursive, | |
| 87 | - ?string $objectType = null | |
| 88 | - ): array; | |
| 179 | + /** | |
| 180 | + * Returns the full objects. | |
| 181 | + * | |
| 182 | + * @param AbstractUserGroup $userGroup | |
| 183 | + * @param bool $lockRecursive | |
| 184 | + * @param string $objectType | |
| 185 | + * | |
| 186 | + * @return array | |
| 187 | + */ | |
| 188 | + abstract public function getFullObjects(AbstractUserGroup $userGroup, $lockRecursive, $objectType = null); | |
| 89 | 189 | } |