PluginProbe
User Access Manager / 2.2.11
User Access Manager v2.2.11
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
user-access-manager / src / ObjectMembership / ObjectMembershipHandler.php

ObjectMembershipHandler.php in User Access Manager 2.2.11, at src/ObjectMembership/ObjectMembershipHandler.php

176 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MembershipHandler.php
4 *
5 * The MembershipHandler 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 */
15
16 declare(strict_types=1);
17
18 namespace UserAccessManager\ObjectMembership;
19
20 use Exception;
21 use UserAccessManager\UserGroup\AbstractUserGroup;
22 use UserAccessManager\UserGroup\AssignmentInformation;
23 use UserAccessManager\UserGroup\AssignmentInformationFactory;
24
25 /**
26 * Class MembershipHandler
27 *
28 * @package UserAccessManager\UserGroup
29 */
30 abstract class ObjectMembershipHandler
31 {
32 /**
33 * @var AssignmentInformationFactory
34 */
35 protected $assignmentInformationFactory;
36
37 /**
38 * @var string
39 */
40 protected $generalObjectType;
41
42 /**
43 * MembershipHandler constructor.
44 * @param AssignmentInformationFactory $assignmentInformationFactory
45 * @throws Exception
46 */
47 public function __construct(
48 AssignmentInformationFactory $assignmentInformationFactory
49 ) {
50 if ($this->generalObjectType === null) {
51 throw new MissingObjectTypeException('Missing general object type, Object type must be set.');
52 }
53
54 $this->assignmentInformationFactory = $assignmentInformationFactory;
55 }
56
57 /**
58 * Returns the object name.
59 * @param int|string $objectId
60 * @param string $typeName
61 * @return int|string
62 */
63 abstract public function getObjectName($objectId, string &$typeName = '');
64
65 /**
66 * Returns the general object type.
67 * @return string
68 */
69 public function getGeneralObjectType(): string
70 {
71 return $this->generalObjectType;
72 }
73
74 /**
75 * Returns the handled objects.
76 * @return array
77 */
78 public function getHandledObjects(): array
79 {
80 return [$this->generalObjectType => $this->generalObjectType];
81 }
82
83 /**
84 * Returns if the object is handled by this handler.
85 * @param $objectType
86 * @return bool
87 */
88 public function handlesObject($objectType): bool
89 {
90 $objectTypes = $this->getHandledObjects();
91 return isset($objectTypes[$objectType]);
92 }
93
94 /**
95 * Assigns recursive membership to the assignment information object.
96 * @param null|AssignmentInformation $assignmentInformation
97 * @param array $recursiveMembership
98 */
99 protected function assignRecursiveMembership(
100 ?AssignmentInformation &$assignmentInformation,
101 array $recursiveMembership
102 ) {
103 if ($assignmentInformation === null) {
104 $assignmentInformation = $this->assignmentInformationFactory->createAssignmentInformation();
105 }
106
107 $assignmentInformation->setRecursiveMembership($recursiveMembership);
108 }
109
110 /**
111 * Checks for the access and assigns the recursive membership array if access.
112 * @param bool $isMember
113 * @param array $recursiveMembership
114 * @param null|AssignmentInformation $assignmentInformation
115 * @return bool
116 */
117 protected function checkAccessWithRecursiveMembership(
118 bool $isMember,
119 array $recursiveMembership,
120 ?AssignmentInformation &$assignmentInformation
121 ): bool {
122 if ($isMember === true || count($recursiveMembership) > 0) {
123 $this->assignRecursiveMembership($assignmentInformation, $recursiveMembership);
124 return true;
125 }
126
127 return false;
128 }
129
130 /**
131 * Returns the assigned objects as array map.
132 * @param AbstractUserGroup $userGroup
133 * @param string $objectType
134 * @return array
135 */
136 protected function getSimpleAssignedObjects(AbstractUserGroup $userGroup, string $objectType): array
137 {
138 $objects = $userGroup->getAssignedObjects($objectType);
139
140 return array_map(
141 function (AssignmentInformation $element) {
142 return $element->getType();
143 },
144 $objects
145 );
146 }
147
148 /**
149 * Checks if the object is a member of the user group.
150 * @param AbstractUserGroup $userGroup
151 * @param bool $lockRecursive
152 * @param int|string $objectId
153 * @param null|AssignmentInformation $assignmentInformation
154 * @return bool
155 */
156 abstract public function isMember(
157 AbstractUserGroup $userGroup,
158 bool $lockRecursive,
159 $objectId,
160 ?AssignmentInformation &$assignmentInformation = null
161 ): bool;
162
163 /**
164 * Returns the full objects.
165 * @param AbstractUserGroup $userGroup
166 * @param bool $lockRecursive
167 * @param null $objectType
168 * @return array
169 */
170 abstract public function getFullObjects(
171 AbstractUserGroup $userGroup,
172 bool $lockRecursive,
173 $objectType = null
174 ): array;
175 }
176