PluginProbe
User Access Manager / 2.2.3
User Access Manager v2.2.3
2.3.20 2.3.19 2.3.18 2.3.17 2.3.16 2.3.15 2.3.14 2.3.13 trunk 0.6 0.6.1 0.6.2 0.7 0.7 Beta 0.7.0.1 0.8 0.8.0.1 0.8.0.2 0.9 0.9.1 0.9.1.1 0.9.1.2 0.9.1.3 0.9.1.4 1.0 All 136 releases
← All changes | src/ObjectMembership/ObjectMembershipWithMapHandler.php +66 -45 2.3.202.2.3 View file →
@@ -1,5 +1,18 @@
1 1 <?php
2 +/**
3 + * MembershipWithMapHandler.php
4 + *
5 + * The MembershipWithMapHandler class file.
6 + *
7 + * PHP versions 5
8 + *
9 + * @author Alexander Schneider <alexanderschneider85@gmail.com>
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 + */
2 15
3 16 declare(strict_types=1);
4 17
5 18 namespace UserAccessManager\ObjectMembership;
@@ -8,62 +21,69 @@
8 21 use UserAccessManager\Object\ObjectMapHandler;
9 22 use UserAccessManager\UserGroup\AbstractUserGroup;
10 23 use UserAccessManager\UserGroup\AssignmentInformation;
11 24
25 +/**
26 + * Class MembershipWithMapHandler
27 + *
28 + * @package UserAccessManager\UserGroup
29 + */
12 30 abstract class ObjectMembershipWithMapHandler extends ObjectMembershipHandler
13 31 {
32 + /**
33 + * Returns the map.
34 + * @return array
35 + */
14 36 abstract protected function getMap(): array;
15 37
16 - protected function getHandledObjectsIncluding(array $objectTypes): array
17 - {
18 - $objectTypeNames = array_keys($objectTypes);
19 -
20 - return array_merge(
21 - parent::getHandledObjects(),
22 - array_combine($objectTypeNames, $objectTypeNames)
23 - );
24 - }
25 -
26 38 /**
39 + * Uses a map function to resolve the recursive membership.
40 + * @param AbstractUserGroup $userGroup
41 + * @param bool $lockRecursive
42 + * @param int|string $objectId
43 + * @param null|AssignmentInformation $assignmentInformation
44 + * @return bool
27 45 * @throws Exception
28 46 */
29 - private function getRecursiveMembershipByMap(AbstractUserGroup $userGroup, int|string|null $objectId): array
30 - {
31 - $parentMap = $this->getMap()[ObjectMapHandler::TREE_MAP_PARENTS][$this->generalObjectType] ?? [];
47 + protected function getMembershipByMap(
48 + AbstractUserGroup $userGroup,
49 + bool $lockRecursive,
50 + $objectId,
51 + &$assignmentInformation = null
52 + ): bool {
53 + // Reset value to prevent errors
32 54 $recursiveMembership = [];
33 55
34 - foreach (array_keys($parentMap[$objectId] ?? []) as $parentId) {
35 - $isObjectMember = $userGroup->isObjectMember(
36 - $this->generalObjectType,
37 - $parentId,
38 - $parentAssignmentInformation
39 - );
56 + if ($lockRecursive === true) {
57 + $map = $this->getMap();
58 + $generalMap = isset($map[ObjectMapHandler::TREE_MAP_PARENTS][$this->generalObjectType]) ?
59 + $map[ObjectMapHandler::TREE_MAP_PARENTS][$this->generalObjectType] : [];
40 60
41 - if ($isObjectMember === true) {
42 - $recursiveMembership[$this->generalObjectType][$parentId] = $parentAssignmentInformation;
61 + if (isset($generalMap[$objectId]) === true) {
62 + foreach ($generalMap[$objectId] as $parentId => $type) {
63 + $isObjectMember = $userGroup->isObjectMember(
64 + $this->generalObjectType,
65 + $parentId,
66 + $rmAssignmentInformation
67 + );
68 +
69 + if ($isObjectMember === true) {
70 + $recursiveMembership[$this->generalObjectType][$parentId] = $rmAssignmentInformation;
71 + }
72 + }
43 73 }
44 74 }
45 75
46 - return $recursiveMembership;
47 - }
48 -
49 - /**
50 - * @throws Exception
51 - */
52 - protected function getMembershipByMap(
53 - AbstractUserGroup $userGroup,
54 - bool $lockRecursive,
55 - int|string|null $objectId,
56 - ?AssignmentInformation &$assignmentInformation = null
57 - ): bool {
58 - $recursiveMembership = ($lockRecursive === true) ?
59 - $this->getRecursiveMembershipByMap($userGroup, $objectId) : [];
60 -
61 76 $isMember = $userGroup->isObjectAssignedToGroup($this->generalObjectType, $objectId, $assignmentInformation);
62 77 return $this->checkAccessWithRecursiveMembership($isMember, $recursiveMembership, $assignmentInformation);
63 78 }
64 79
65 80 /**
81 + * Returns the objects by the given type including the children.
82 + * @param AbstractUserGroup $userGroup
83 + * @param bool $lockRecursive
84 + * @param string $objectType
85 + * @return array
66 86 * @throws Exception
67 87 */
68 88 protected function getFullObjectsByMap(AbstractUserGroup $userGroup, bool $lockRecursive, string $objectType): array
69 89 {
@@ -68,18 +88,19 @@
68 88 protected function getFullObjectsByMap(AbstractUserGroup $userGroup, bool $lockRecursive, string $objectType): array
69 89 {
70 90 $objects = $this->getSimpleAssignedObjects($userGroup, $objectType);
71 91
72 - if ($lockRecursive === false) {
73 - return $objects;
74 - }
92 + if ($lockRecursive === true) {
93 + $map = $this->getMap();
94 + $map = isset($map[ObjectMapHandler::TREE_MAP_CHILDREN][$objectType]) ?
95 + $map[ObjectMapHandler::TREE_MAP_CHILDREN][$objectType] : [];
96 + $map = array_intersect_key($map, $objects);
75 97
76 - $childrenMap = $this->getMap()[ObjectMapHandler::TREE_MAP_CHILDREN][$objectType] ?? [];
77 -
78 - foreach (array_intersect_key($childrenMap, $objects) as $childrenIds) {
79 - foreach ($childrenIds as $parentId => $type) {
80 - if ($userGroup->isObjectMember($objectType, $parentId) === true) {
81 - $objects[$parentId] = $type;
98 + foreach ($map as $childrenIds) {
99 + foreach ($childrenIds as $parentId => $type) {
100 + if ($userGroup->isObjectMember($objectType, $parentId) === true) {
101 + $objects[$parentId] = $type;
102 + }
82 103 }
83 104 }
84 105 }
85 106