ObjectController.php
15.2 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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
<?php
/**
* ObjectController.php
*
* The ObjectController 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\Controller\Backend;
use Exception;
use UserAccessManager\Access\AccessHandler;
use UserAccessManager\Config\MainConfig;
use UserAccessManager\Config\WordpressConfig;
use UserAccessManager\Controller\Controller;
use UserAccessManager\Database\Database;
use UserAccessManager\Object\ObjectHandler;
use UserAccessManager\ObjectMembership\MissingObjectMembershipHandlerException;
use UserAccessManager\User\UserHandler;
use UserAccessManager\UserGroup\AbstractUserGroup;
use UserAccessManager\UserGroup\AssignmentInformation;
use UserAccessManager\UserGroup\DynamicUserGroup;
use UserAccessManager\UserGroup\UserGroupAssignmentException;
use UserAccessManager\UserGroup\UserGroupAssignmentHandler;
use UserAccessManager\UserGroup\UserGroupHandler;
use UserAccessManager\UserGroup\UserGroupTypeException;
use UserAccessManager\Util\DateUtil;
use UserAccessManager\Wrapper\Php;
use UserAccessManager\Wrapper\Wordpress;
/**
* Class ObjectController
*
* @package UserAccessManager\Controller
*/
class ObjectController extends Controller
{
const COLUMN_NAME = 'uam_access';
const BULK_ADD = 'add';
const BULK_REMOVE = 'remove';
const BULK_OVERWRITE = 'overwrite';
const DEFAULT_GROUPS_FORM_NAME = 'uam_user_groups';
const DEFAULT_DYNAMIC_GROUPS_FORM_NAME = 'uam_dynamic_user_groups';
const UPDATE_GROUPS_FORM_NAME = 'uam_update_groups';
/**
* @var MainConfig
*/
protected $mainConfig;
/**
* @var Database
*/
protected $database;
/**
* @var DateUtil
*/
protected $dateUtil;
/**
* @var ObjectHandler
*/
protected $objectHandler;
/**
* @var UserHandler
*/
protected $userHandler;
/**
* @var UserGroupHandler
*/
protected $userGroupHandler;
/**
* @var AccessHandler
*/
protected $accessHandler;
/**
* @var UserGroupAssignmentHandler
*/
protected $userGroupAssignmentHandler;
/**
* @var ObjectInformation
*/
protected $objectInformation;
/**
* @var null|string
*/
protected $groupsFromName = null;
/**
* ObjectController constructor.
* @param Php $php
* @param Wordpress $wordpress
* @param WordpressConfig $wordpressConfig
* @param MainConfig $mainConfig
* @param Database $database
* @param DateUtil $dateUtil
* @param ObjectHandler $objectHandler
* @param UserHandler $userHandler
* @param UserGroupHandler $userGroupHandler
* @param UserGroupAssignmentHandler $userGroupAssignmentHandler
* @param AccessHandler $accessHandler
* @param ObjectInformationFactory $objectInformationFactory
*/
public function __construct(
Php $php,
Wordpress $wordpress,
WordpressConfig $wordpressConfig,
MainConfig $mainConfig,
Database $database,
DateUtil $dateUtil,
ObjectHandler $objectHandler,
UserHandler $userHandler,
UserGroupHandler $userGroupHandler,
UserGroupAssignmentHandler $userGroupAssignmentHandler,
AccessHandler $accessHandler,
ObjectInformationFactory $objectInformationFactory
) {
parent::__construct($php, $wordpress, $wordpressConfig);
$this->mainConfig = $mainConfig;
$this->database = $database;
$this->dateUtil = $dateUtil;
$this->objectHandler = $objectHandler;
$this->userHandler = $userHandler;
$this->userGroupHandler = $userGroupHandler;
$this->userGroupAssignmentHandler = $userGroupAssignmentHandler;
$this->accessHandler = $accessHandler;
$this->objectInformation = $objectInformationFactory->createObjectInformation();
}
/**
* Sets the current object type, the object id and the user groups.
* @param string $objectType
* @param int|string|null $objectId
* @param array|null $objectUserGroups
* @throws UserGroupTypeException
*/
protected function setObjectInformation(string $objectType, $objectId, ?array $objectUserGroups = null)
{
$userGroupDiff = 0;
if ($objectUserGroups === null && $objectId !== null) {
$objectUserGroups = $this->userGroupHandler->getFilteredUserGroupsForObject($objectType, $objectId, true);
$fullObjectUserGroups = $this->userGroupHandler->getUserGroupsForObject($objectType, $objectId, true);
$userGroupDiff = count((array) $fullObjectUserGroups) - count((array) $objectUserGroups);
}
$this->objectInformation->setObjectType($objectType)
->setObjectId($objectId)
->setObjectUserGroups((array) $objectUserGroups)
->setUserGroupDiff($userGroupDiff);
}
/**
* Returns the object information.
* @return ObjectInformation
*/
public function getObjectInformation(): ObjectInformation
{
return $this->objectInformation;
}
/**
* Returns the default groups form name.
* @return string
*/
public function getGroupsFormName(): string
{
return ($this->groupsFromName !== null) ? (string) $this->groupsFromName : self::DEFAULT_GROUPS_FORM_NAME;
}
/**
* Returns the filtered user groups.
* @return AbstractUserGroup[]
* @throws UserGroupTypeException
*/
public function getFilteredUserGroups(): array
{
return $this->userGroupHandler->getFilteredUserGroups();
}
/**
* Sorts the user groups.
* @param array $userGroups
*/
public function sortUserGroups(array &$userGroups)
{
uasort(
$userGroups,
function (
AbstractUserGroup $userGroupOne,
AbstractUserGroup $userGroupTwo
) {
$notLoggedInUserGroupId = DynamicUserGroup::USER_TYPE . '|' . DynamicUserGroup::NOT_LOGGED_IN_USER_ID;
if ($userGroupOne->getId() === $notLoggedInUserGroupId) {
return 1;
} elseif ($userGroupTwo->getId() === $notLoggedInUserGroupId) {
return -1;
}
return strnatcasecmp($userGroupOne->getName(), $userGroupTwo->getName());
}
);
}
/**
* Returns the date util.
* @return DateUtil
*/
public function getDateUtil(): DateUtil
{
return $this->dateUtil;
}
/**
* Checks if the current user is an admin.
* @return bool
*/
public function isCurrentUserAdmin(): bool
{
if ($this->objectInformation->getObjectType() === ObjectHandler::GENERAL_USER_OBJECT_TYPE
&& $this->objectInformation->getObjectId() !== null
) {
return $this->userHandler->userIsAdmin($this->objectInformation->getObjectId());
}
return false;
}
/**
* Returns the wordpress role names.
* @return array
*/
public function getRoleNames(): array
{
$roles = $this->wordpress->getRoles();
return $roles->role_names;
}
/**
* Checks the user access.
* @return bool
*/
public function checkUserAccess(): bool
{
return $this->userHandler->checkUserAccess(UserHandler::MANAGE_USER_GROUPS_CAPABILITY);
}
/**
* Returns the recursive object membership.
* @param AbstractUserGroup $userGroup
* @return array
* @throws Exception
* @throws Exception
*/
public function getRecursiveMembership(AbstractUserGroup $userGroup): array
{
$recursiveMembership = [];
$objectType = $this->objectInformation->getObjectType();
$objectId = $this->objectInformation->getObjectId();
$recursiveMembershipForObject = $userGroup->getRecursiveMembershipForObject($objectType, $objectId);
/**
* @var AssignmentInformation[] $assignmentInformation
*/
foreach ($recursiveMembershipForObject as $recursiveType => $assignmentInformation) {
foreach ($assignmentInformation as $objectId => $information) {
try {
$membershipHandler = $this->objectHandler->getObjectMembershipHandler($information->getType());
$typeName = $membershipHandler->getGeneralObjectType();
$objectName = $membershipHandler->getObjectName($objectId, $typeName);
$recursiveMembership[$typeName][$objectId] = $objectName;
} catch (MissingObjectMembershipHandlerException $exception) {
// Do nothing
}
}
}
return $recursiveMembership;
}
/**
* Checks the access and dies if the user has no access.
* @param string $objectType
* @param int|string $objectId
* @throws UserGroupTypeException
*/
private function dieOnNoAccess(string $objectType, $objectId)
{
if ($this->accessHandler->checkObjectAccess($objectType, $objectId) === false) {
$this->wordpress->wpDie(TXT_UAM_NO_RIGHTS_MESSAGE, TXT_UAM_NO_RIGHTS_TITLE, ['response' => 403]);
}
}
/**
* Shows the error if the user has no rights to edit the content.
* @throws UserGroupTypeException
*/
public function checkRightsToEditContent()
{
$postIdParameter = $this->getRequestParameter('post', $this->getRequestParameter('attachment_id'));
if ($postIdParameter !== null) {
$postIds = is_array($postIdParameter) === false ? [$postIdParameter] : $postIdParameter;
foreach ($postIds as $postId) {
$this->dieOnNoAccess(ObjectHandler::GENERAL_POST_OBJECT_TYPE, $postId);
}
}
$tagId = $this->getRequestParameter('tag_ID');
if ($tagId !== null) {
$this->dieOnNoAccess(ObjectHandler::GENERAL_TERM_OBJECT_TYPE, $tagId);
}
}
/**
* Returns the user groups by reference which should be add and removed from the object.
* @param string $objectType
* @param int|string $objectId
* @param array|null $addUserGroups
* @param array|null $removeUserGroups
* @throws UserGroupTypeException
*/
private function getAddRemoveGroups(
string $objectType,
$objectId,
?array &$addUserGroups = [],
?array &$removeUserGroups = []
) {
$groupsToChange = (array) $this->getRequestParameter(self::DEFAULT_GROUPS_FORM_NAME, []);
$filteredUserGroupsForObject = $this->userGroupHandler->getFilteredUserGroupsForObject(
$objectType,
$objectId
);
$addUserGroups = $addUserGroups ?? $groupsToChange;
$removeUserGroups = array_flip(array_keys($filteredUserGroupsForObject));
$bulkType = $this->getRequestParameter('uam_bulk_type');
if ($bulkType === self::BULK_ADD) {
$addUserGroups = $groupsToChange;
$removeUserGroups = [];
} elseif ($bulkType === self::BULK_REMOVE) {
$addUserGroups = [];
$removeUserGroups = array_filter(
$groupsToChange,
function (array $group) {
return isset($group['id']);
}
);
}
}
/**
* Saves the object data to the database.
* @param string $objectType The object type.
* @param int|string|null $objectId The id of the object.
* @param array|null $addUserGroups The new user groups for the object.
* @param bool $force If true we force the assignment.
* @throws UserGroupTypeException
*/
public function saveObjectData(string $objectType, $objectId, ?array $addUserGroups = null, $force = false)
{
$isUpdateForm = (bool) $this->getRequestParameter(self::UPDATE_GROUPS_FORM_NAME, false) === true
|| $this->getRequestParameter('uam_bulk_type') !== null;
$hasRights = $this->checkUserAccess() === true || $this->mainConfig->authorsCanAddPostsToGroups() === true;
if ($isUpdateForm === true && $hasRights === true || $force === true) {
$this->getAddRemoveGroups($objectType, $objectId, $addUserGroups, $removeUserGroups);
try {
$this->userGroupAssignmentHandler->assignObjectToUserGroups(
$objectType,
$objectId,
$addUserGroups,
$removeUserGroups,
$this->getRequestParameter(self::DEFAULT_DYNAMIC_GROUPS_FORM_NAME, [])
);
} catch (UserGroupAssignmentException $exception) {
$this->addErrorMessage(sprintf(TXT_UAM_ERROR, $exception->getMessage()));
}
}
}
/**
* Removes the object data.
* @param string $objectType The object type.
* @param int|string $id The object id.
*/
public function removeObjectData(string $objectType, $id)
{
$this->database->delete(
$this->database->getUserGroupToObjectTable(),
[
'object_id' => $id,
'object_type' => $objectType,
],
[
'%d',
'%s'
]
);
}
/**
* Returns the group selection form for pluggable objects.
* @param string $objectType The object type.
* @param int|string $objectId The id of the object.
* @param null $formName The formName.
* @param array|null $objectUserGroups If set we force this user groups for the object.
* @return string
* @throws UserGroupTypeException
*/
public function showGroupSelectionForm(
string $objectType,
$objectId,
$formName = null,
array $objectUserGroups = null
): string {
$this->setObjectInformation($objectType, $objectId, $objectUserGroups);
$this->groupsFromName = $formName;
$formContent = $this->getIncludeContents('GroupSelectionForm.php');
$this->groupsFromName = null;
return $formContent;
}
/**
* Returns the column for a pluggable object.
* @param string $objectType The object type.
* @param int|string $objectId The object id.
* @return string
* @throws UserGroupTypeException
*/
public function getGroupColumn(string $objectType, $objectId): string
{
$this->setObjectInformation($objectType, $objectId);
return $this->getIncludeContents('ObjectColumn.php');
}
/**
* Checks if the current object is a new object.
* @return bool
* @throws Exception
*/
public function isNewObject(): bool
{
$objectType = $this->objectInformation->getObjectType();
if ($objectType !== null) {
$generalObjectType = $this->objectHandler->getGeneralObjectType($objectType);
return ($this->objectInformation->getObjectId() === null
|| ($generalObjectType === ObjectHandler::GENERAL_POST_OBJECT_TYPE &&
$this->getRequestParameter('action') !== 'edit')
);
}
return false;
}
}