UserGroupHandler.php
10.4 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
<?php
/**
* UserGroupHandler.php
*
* The UserGroupHandler 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\UserGroup;
use Exception;
use UserAccessManager\Config\MainConfig;
use UserAccessManager\Config\WordpressConfig;
use UserAccessManager\Database\Database;
use UserAccessManager\Object\ObjectHandler;
use UserAccessManager\User\UserHandler;
use UserAccessManager\Wrapper\Wordpress;
use WP_User;
/**
* Class UserGroupHandler
*
* @package UserAccessManager\UserGroup
*/
class UserGroupHandler
{
/**
* @var Wordpress
*/
private $wordpress;
/**
* @var WordpressConfig
*/
private $wordpressConfig;
/**
* @var MainConfig
*/
private $mainConfig;
/**
* @var Database
*/
private $database;
/**
* @var ObjectHandler
*/
private $objectHandler;
/**
* @var UserHandler
*/
private $userHandler;
/**
* @var UserGroupFactory
*/
private $userGroupFactory;
/**
* @var null|UserGroup[]
*/
private $userGroups = null;
/**
* @var null|DynamicUserGroup[]
*/
private $dynamicUserGroups = null;
/**
* @var null|AbstractUserGroup[]
*/
private $userGroupsForUser = null;
/**
* @var array
*/
private $objectUserGroups = [];
public function __construct(
Wordpress $wordpress,
WordpressConfig $wordpressConfig,
MainConfig $mainConfig,
Database $database,
ObjectHandler $objectHandler,
UserHandler $userHandler,
UserGroupFactory $userGroupFactory
) {
$this->wordpress = $wordpress;
$this->wordpressConfig = $wordpressConfig;
$this->mainConfig = $mainConfig;
$this->database = $database;
$this->objectHandler = $objectHandler;
$this->userHandler = $userHandler;
$this->userGroupFactory = $userGroupFactory;
}
/**
* Returns all user groups.
* @return UserGroup[]
* @throws UserGroupTypeException
*/
public function getUserGroups(): ?array
{
if ($this->userGroups === null) {
$this->userGroups = [];
$query = "SELECT ID FROM {$this->database->getUserGroupTable()}";
$userGroups = (array) $this->database->getResults($query);
foreach ($userGroups as $userGroup) {
$group = $this->userGroupFactory->createUserGroup($userGroup->ID);
$this->userGroups[$group->getId()] = $group;
}
}
return $this->userGroups;
}
/**
* Returns all dynamic user groups.
* @return null|DynamicUserGroup[]
* @throws UserGroupTypeException
*/
public function getDynamicUserGroups(): ?array
{
if ($this->dynamicUserGroups === null) {
$this->dynamicUserGroups = [];
$notLoggedInUserGroup = $this->userGroupFactory->createDynamicUserGroup(
DynamicUserGroup::USER_TYPE,
DynamicUserGroup::NOT_LOGGED_IN_USER_ID
);
$this->dynamicUserGroups[$notLoggedInUserGroup->getId()] = $notLoggedInUserGroup;
$userGroupTypes = implode('\', \'', [DynamicUserGroup::ROLE_TYPE, DynamicUserGroup::USER_TYPE]);
$query = "SELECT group_id AS id, group_type AS type
FROM {$this->database->getUserGroupToObjectTable()}
WHERE group_type IN ('{$userGroupTypes}')
GROUP BY group_type, group_id";
$dynamicUserGroups = (array) $this->database->getResults($query);
foreach ($dynamicUserGroups as $dynamicUserGroup) {
$group = $this->userGroupFactory->createDynamicUserGroup(
$dynamicUserGroup->type,
$dynamicUserGroup->id
);
$this->dynamicUserGroups[$group->getId()] = $group;
}
}
return $this->dynamicUserGroups;
}
/**
* Returns the full user groups
* @return AbstractUserGroup[]
* @throws UserGroupTypeException
*/
public function getFullUserGroups(): ?array
{
return $this->getUserGroups() + $this->getDynamicUserGroups();
}
/**
* Returns the user groups filtered by the user user groups.
* @return AbstractUserGroup[]
* @throws UserGroupTypeException
*/
public function getFilteredUserGroups(): array
{
$userGroups = $this->getFullUserGroups();
$userUserGroups = $this->getUserGroupsForUser() + $this->getDynamicUserGroups();
return array_intersect_key($userGroups, $userUserGroups);
}
/**
* Adds a user group.
* @param UserGroup $userGroup
* @throws UserGroupTypeException
*/
public function addUserGroup(UserGroup $userGroup)
{
$this->getUserGroups();
$this->userGroups[$userGroup->getId()] = $userGroup;
}
/**
* Deletes a user group.
* @param string|int $userGroupId
* @return bool
* @throws UserGroupTypeException
* @throws Exception
*/
public function deleteUserGroup($userGroupId): bool
{
$userGroups = $this->getUserGroups();
if (isset($userGroups[$userGroupId])
&& $userGroups[$userGroupId]->delete() === true
) {
unset($this->userGroups[$userGroupId]);
return true;
}
return false;
}
/**
* Returns the user groups for the given object.
* @param string $objectType The object type.
* @param int|string $objectId The id of the object.
* @param bool $ignoreDates If true we ignore the dates for the object assignment.
* @return AbstractUserGroup[]
* @throws UserGroupTypeException
* @throws Exception
*/
public function getUserGroupsForObject(string $objectType, $objectId, bool $ignoreDates = false): array
{
if ($this->objectHandler->isValidObjectType($objectType) === false) {
return [];
}
if (isset($this->objectUserGroups[$ignoreDates][$objectType][$objectId]) === false) {
$objectUserGroups = [];
$userGroups = $this->getFullUserGroups();
foreach ($userGroups as $userGroup) {
$userGroup->setIgnoreDates($ignoreDates);
if ($userGroup->isObjectMember($objectType, $objectId) === true) {
$objectUserGroups[$userGroup->getId()] = $userGroup;
}
}
$this->objectUserGroups[$ignoreDates][$objectType][$objectId] = $objectUserGroups;
}
return $this->objectUserGroups[$ignoreDates][$objectType][$objectId];
}
/**
* Unset the object user groups.
*/
public function unsetUserGroupsForObject()
{
$this->objectUserGroups = [];
}
/**
* Checks if the current user is in the ip range or if the user group is public.
* @param UserGroup $userGroup
* @return bool
*/
private function checkUserGroupAccess(UserGroup $userGroup): bool
{
$extraIpHeader = $this->mainConfig->getExtraIpHeader();
$userIp = $extraIpHeader !== null ?
$_SERVER[$extraIpHeader] ?? ($_SERVER['REMOTE_ADDR'] ?? '') :
$_SERVER['REMOTE_ADDR'] ?? '';
return $this->userHandler->isIpInRange($userIp, $userGroup->getIpRangeArray())
|| $this->wordpressConfig->atAdminPanel() === false && $userGroup->getReadAccess() === 'all'
|| $this->wordpressConfig->atAdminPanel() === true && $userGroup->getWriteAccess() === 'all';
}
/**
* Assigns the dynamic user groups to the user user groups.
* @param WP_User $currentUser
* @param array $userGroupsForUser
* @throws UserGroupTypeException
*/
private function assignDynamicUserGroupsForUser(WP_User $currentUser, array &$userGroupsForUser)
{
$userUserGroup = $this->userGroupFactory->createDynamicUserGroup(
DynamicUserGroup::USER_TYPE,
$currentUser->ID
);
$userGroupsForUser[$userUserGroup->getId()] = $userUserGroup;
$roles = $this->userHandler->getUserRole($currentUser);
foreach ($roles as $role) {
$group = $this->userGroupFactory->createDynamicUserGroup(
DynamicUserGroup::ROLE_TYPE,
$role
);
$userGroupsForUser[$group->getId()] = $group;
}
}
/**
* Returns the user groups for the user.
* @return AbstractUserGroup[]|null
* @throws UserGroupTypeException
*/
public function getUserGroupsForUser(): ?array
{
if ($this->userHandler->checkUserAccess(UserHandler::MANAGE_USER_GROUPS_CAPABILITY) === true) {
return $this->getUserGroups();
}
if ($this->userGroupsForUser === null) {
$currentUser = $this->wordpress->getCurrentUser();
$userGroupsForUser = $this->getUserGroupsForObject(
ObjectHandler::GENERAL_USER_OBJECT_TYPE,
$currentUser->ID
);
$this->assignDynamicUserGroupsForUser($currentUser, $userGroupsForUser);
$userGroups = $this->getUserGroups();
foreach ($userGroups as $userGroup) {
if (isset($userGroupsForUser[$userGroup->getId()]) === false
&& $this->checkUserGroupAccess($userGroup) === true
) {
$userGroupsForUser[$userGroup->getId()] = $userGroup;
}
}
$this->userGroupsForUser = $userGroupsForUser;
}
return $this->userGroupsForUser;
}
/**
* Returns the user groups for the object filtered by the user user groups.
* @param string $objectType
* @param string|int $objectId
* @param bool $ignoreDates
* @return AbstractUserGroup[]
* @throws UserGroupTypeException
*/
public function getFilteredUserGroupsForObject(string $objectType, $objectId, bool $ignoreDates = false): array
{
$userGroups = $this->getUserGroupsForObject($objectType, $objectId, $ignoreDates);
$userUserGroups = $this->getUserGroupsForUser() + $this->getDynamicUserGroups();
return array_intersect_key($userGroups, $userUserGroups);
}
}