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