PluginProbe
User Access Manager / 2.2.23
User Access Manager v2.2.23
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 +64 -45 trunk2.2.23 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,68 @@
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 = $map[ObjectMapHandler::TREE_MAP_PARENTS][$this->generalObjectType] ?? [];
40 59
41 - if ($isObjectMember === true) {
42 - $recursiveMembership[$this->generalObjectType][$parentId] = $parentAssignmentInformation;
60 + if (isset($generalMap[$objectId]) === true) {
61 + foreach ($generalMap[$objectId] as $parentId => $type) {
62 + $isObjectMember = $userGroup->isObjectMember(
63 + $this->generalObjectType,
64 + $parentId,
65 + $rmAssignmentInformation
66 + );
67 +
68 + if ($isObjectMember === true) {
69 + $recursiveMembership[$this->generalObjectType][$parentId] = $rmAssignmentInformation;
70 + }
71 + }
43 72 }
44 73 }
45 74
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 75 $isMember = $userGroup->isObjectAssignedToGroup($this->generalObjectType, $objectId, $assignmentInformation);
62 76 return $this->checkAccessWithRecursiveMembership($isMember, $recursiveMembership, $assignmentInformation);
63 77 }
64 78
65 79 /**
80 + * Returns the objects by the given type including the children.
81 + * @param AbstractUserGroup $userGroup
82 + * @param bool $lockRecursive
83 + * @param string $objectType
84 + * @return array
66 85 * @throws Exception
67 86 */
68 87 protected function getFullObjectsByMap(AbstractUserGroup $userGroup, bool $lockRecursive, string $objectType): array
69 88 {
@@ -68,18 +87,18 @@
68 87 protected function getFullObjectsByMap(AbstractUserGroup $userGroup, bool $lockRecursive, string $objectType): array
69 88 {
70 89 $objects = $this->getSimpleAssignedObjects($userGroup, $objectType);
71 90
72 - if ($lockRecursive === false) {
73 - return $objects;
74 - }
91 + if ($lockRecursive === true) {
92 + $map = $this->getMap();
93 + $map = $map[ObjectMapHandler::TREE_MAP_CHILDREN][$objectType] ?? [];
94 + $map = array_intersect_key($map, $objects);
75 95
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;
96 + foreach ($map as $childrenIds) {
97 + foreach ($childrenIds as $parentId => $type) {
98 + if ($userGroup->isObjectMember($objectType, $parentId) === true) {
99 + $objects[$parentId] = $type;
100 + }
82 101 }
83 102 }
84 103 }
85 104