PluginProbe
User Access Manager / 2.1.9
User Access Manager v2.1.9
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 / Controller / Backend / ObjectController.php

ObjectController.php in User Access Manager 2.1.9, at src/Controller/Backend/ObjectController.php

617 lines 17.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ObjectController.php
4 *
5 * The ObjectController 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 namespace UserAccessManager\Controller\Backend;
16
17 use UserAccessManager\Access\AccessHandler;
18 use UserAccessManager\Cache\Cache;
19 use UserAccessManager\Config\MainConfig;
20 use UserAccessManager\Config\WordpressConfig;
21 use UserAccessManager\Controller\Controller;
22 use UserAccessManager\Database\Database;
23 use UserAccessManager\Object\ObjectHandler;
24 use UserAccessManager\ObjectMembership\MissingObjectMembershipHandlerException;
25 use UserAccessManager\UserGroup\AbstractUserGroup;
26 use UserAccessManager\UserGroup\AssignmentInformation;
27 use UserAccessManager\UserGroup\UserGroup;
28 use UserAccessManager\UserGroup\UserGroupFactory;
29 use UserAccessManager\User\UserHandler;
30 use UserAccessManager\UserGroup\UserGroupHandler;
31 use UserAccessManager\Util\DateUtil;
32 use UserAccessManager\Wrapper\Php;
33 use UserAccessManager\Wrapper\Wordpress;
34
35 /**
36 * Class ObjectController
37 *
38 * @package UserAccessManager\Controller
39 */
40 class ObjectController extends Controller
41 {
42 const COLUMN_NAME = 'uam_access';
43 const BULK_REMOVE = 'remove';
44 const DEFAULT_GROUPS_FORM_NAME = 'uam_user_groups';
45 const DEFAULT_DYNAMIC_GROUPS_FORM_NAME = 'uam_dynamic_user_groups';
46 const UPDATE_GROUPS_FORM_NAME = 'uam_update_groups';
47
48 /**
49 * @var MainConfig
50 */
51 protected $mainConfig;
52
53 /**
54 * @var Database
55 */
56 protected $database;
57
58 /**
59 * @var Cache
60 */
61 protected $cache;
62
63 /**
64 * @var DateUtil
65 */
66 protected $dateUtil;
67
68 /**
69 * @var ObjectHandler
70 */
71 protected $objectHandler;
72
73 /**
74 * @var UserHandler
75 */
76 protected $userHandler;
77
78 /**
79 * @var UserGroupHandler
80 */
81 protected $userGroupHandler;
82
83 /**
84 * @var AccessHandler
85 */
86 protected $accessHandler;
87
88 /**
89 * @var UserGroupFactory
90 */
91 protected $userGroupFactory;
92
93 /**
94 * @var null|string
95 */
96 protected $groupsFromName = null;
97
98 /**
99 * @var null|string
100 */
101 protected $objectType = null;
102
103 /**
104 * @var null|string
105 */
106 protected $objectId = null;
107
108 /**
109 * @var AbstractUserGroup[]
110 */
111 protected $objectUserGroups = [];
112
113 /**
114 * @var int
115 */
116 protected $userGroupDiff = 0;
117
118 /**
119 * ObjectController constructor.
120 *
121 * @param Php $php
122 * @param Wordpress $wordpress
123 * @param WordpressConfig $wordpressConfig
124 * @param MainConfig $mainConfig
125 * @param Database $database
126 * @param DateUtil $dateUtil
127 * @param Cache $cache
128 * @param ObjectHandler $objectHandler
129 * @param UserHandler $userHandler
130 * @param UserGroupHandler $userGroupHandler
131 * @param AccessHandler $accessHandler
132 * @param UserGroupFactory $userGroupFactory
133 */
134 public function __construct(
135 Php $php,
136 Wordpress $wordpress,
137 WordpressConfig $wordpressConfig,
138 MainConfig $mainConfig,
139 Database $database,
140 DateUtil $dateUtil,
141 Cache $cache,
142 ObjectHandler $objectHandler,
143 UserHandler $userHandler,
144 UserGroupHandler $userGroupHandler,
145 AccessHandler $accessHandler,
146 UserGroupFactory $userGroupFactory
147 ) {
148 parent::__construct($php, $wordpress, $wordpressConfig);
149 $this->mainConfig = $mainConfig;
150 $this->database = $database;
151 $this->cache = $cache;
152 $this->dateUtil = $dateUtil;
153 $this->objectHandler = $objectHandler;
154 $this->userHandler = $userHandler;
155 $this->userGroupHandler = $userGroupHandler;
156 $this->accessHandler = $accessHandler;
157 $this->userGroupFactory = $userGroupFactory;
158 }
159
160 /**
161 * Sets the current object type, the object id and the user groups.
162 *
163 * @param string $objectType
164 * @param string $objectId
165 * @param array $objectUserGroups
166 */
167 protected function setObjectInformation($objectType, $objectId, array $objectUserGroups = null)
168 {
169 $this->objectType = $objectType;
170 $this->objectId = $objectId;
171 $this->userGroupDiff = 0;
172
173 if ($objectUserGroups === null && $objectId !== null) {
174 $objectUserGroups = $this->userGroupHandler->getFilteredUserGroupsForObject($objectType, $objectId, true);
175 $fullObjectUserGroups = $this->userGroupHandler->getUserGroupsForObject($objectType, $objectId, true);
176 $this->userGroupDiff = count($fullObjectUserGroups) - count($objectUserGroups);
177 }
178
179 $this->objectUserGroups = (array)$objectUserGroups;
180 }
181
182 /**
183 * Returns the default groups form name.
184 *
185 * @return string
186 */
187 public function getGroupsFormName()
188 {
189 return ($this->groupsFromName !== null) ? (string)$this->groupsFromName : self::DEFAULT_GROUPS_FORM_NAME;
190 }
191
192 /**
193 * Returns the current object type.
194 *
195 * @return string
196 */
197 public function getObjectType()
198 {
199 return $this->objectType;
200 }
201
202 /**
203 * Returns the current object id.
204 *
205 * @return string
206 */
207 public function getObjectId()
208 {
209 return $this->objectId;
210 }
211
212 /**
213 * Returns the current object user groups.
214 *
215 * @return AbstractUserGroup[]
216 */
217 public function getObjectUserGroups()
218 {
219 return $this->objectUserGroups;
220 }
221
222 /**
223 * Returns the user group count diff.
224 *
225 * @return int
226 */
227 public function getUserGroupDiff()
228 {
229 return $this->userGroupDiff;
230 }
231
232 /**
233 * Returns all available user groups.
234 *
235 * @return AbstractUserGroup[]
236 */
237 public function getUserGroups()
238 {
239 return $this->userGroupHandler->getFullUserGroups();
240 }
241
242 /**
243 * Returns the filtered user groups.
244 *
245 * @return AbstractUserGroup[]
246 */
247 public function getFilteredUserGroups()
248 {
249 return $this->userGroupHandler->getFilteredUserGroups();
250 }
251
252 /**
253 * Returns the date util.
254 *
255 * @return DateUtil
256 */
257 public function getDateUtil()
258 {
259 return $this->dateUtil;
260 }
261
262 /**
263 * Checks if the current user is an admin.
264 *
265 * @return bool
266 */
267 public function isCurrentUserAdmin()
268 {
269 if ($this->objectType === ObjectHandler::GENERAL_USER_OBJECT_TYPE
270 && $this->objectId !== null
271 ) {
272 return $this->userHandler->userIsAdmin($this->objectId);
273 }
274
275 return false;
276 }
277
278 /**
279 * Returns the wordpress role names.
280 *
281 * @return array
282 */
283 public function getRoleNames()
284 {
285 $roles = $this->wordpress->getRoles();
286 return $roles->role_names;
287 }
288
289 /**
290 * Returns all object types.
291 *
292 * @return array
293 */
294 public function getAllObjectTypes()
295 {
296 return $this->objectHandler->getAllObjectTypes();
297 }
298
299 /**
300 * Checks the user access.
301 *
302 * @return bool
303 */
304 public function checkUserAccess()
305 {
306 return $this->userHandler->checkUserAccess(UserHandler::MANAGE_USER_GROUPS_CAPABILITY);
307 }
308
309 /**
310 * Returns the recursive object membership.
311 *
312 * @param $userGroup
313 *
314 * @return array
315 */
316 public function getRecursiveMembership(AbstractUserGroup $userGroup)
317 {
318 $recursiveMembership = [];
319 $objectType = $this->getObjectType();
320 $objectId = $this->getObjectId();
321 $recursiveMembershipForObject = $userGroup->getRecursiveMembershipForObject($objectType, $objectId);
322
323 /**
324 * @var AssignmentInformation[] $assignmentInformation
325 */
326 foreach ($recursiveMembershipForObject as $recursiveType => $assignmentInformation) {
327 foreach ($assignmentInformation as $objectId => $information) {
328 try {
329 $membershipHandler = $this->objectHandler->getObjectMembershipHandler($information->getType());
330 $typeName = $membershipHandler->getGeneralObjectType();
331 $objectName = $membershipHandler->getObjectName($objectId, $typeName);
332 $recursiveMembership[$typeName][$objectId] = $objectName;
333 } catch (MissingObjectMembershipHandlerException $exception) {
334 // Do nothing
335 }
336 }
337 }
338
339 return $recursiveMembership;
340 }
341
342 /**
343 * Checks the access and dies if the user has no access.
344 *
345 * @param string $objectType
346 * @param string $objectId
347 */
348 private function dieOnNoAccess($objectType, $objectId)
349 {
350 if ($this->accessHandler->checkObjectAccess($objectType, $objectId) === false) {
351 $this->wordpress->wpDie(TXT_UAM_NO_RIGHTS_MESSAGE, TXT_UAM_NO_RIGHTS_TITLE, ['response' => 403]);
352 }
353 }
354
355 /**
356 * Shows the error if the user has no rights to edit the content.
357 */
358 public function checkRightsToEditContent()
359 {
360 $postIdParameter = $this->getRequestParameter('post', $this->getRequestParameter('attachment_id'));
361
362 if ($postIdParameter !== null) {
363 $postIds = is_array($postIdParameter) === false ? [$postIdParameter] : $postIdParameter;
364
365 foreach ($postIds as $postId) {
366 $this->dieOnNoAccess(ObjectHandler::GENERAL_POST_OBJECT_TYPE, $postId);
367 }
368 }
369
370 $tagId = $this->getRequestParameter('tag_ID');
371
372 if ($tagId !== null) {
373 $this->dieOnNoAccess(ObjectHandler::GENERAL_TERM_OBJECT_TYPE, $tagId);
374 }
375 }
376
377 /**
378 * @param array $data
379 * @param string $name
380 *
381 * @return null|string
382 */
383 private function getDateParameter(array $data, $name)
384 {
385 $isValid = isset($data[$name]['date']) === true && isset($data[$name]['time']) === true
386 && (string)$data[$name]['date'] !== '' && (string)$data[$name]['time'] !== '';
387
388 return ($isValid === true) ? (string)$data[$name]['date'].'T'.$data[$name]['time'] : null;
389 }
390
391 /**
392 * Returns the user groups by reference which should be add and removed from the object.
393 *
394 * @param string $objectType
395 * @param string $objectId
396 * @param array|null $addUserGroups
397 * @param array $removeUserGroups
398 */
399 private function getAddRemoveGroups($objectType, $objectId, &$addUserGroups, &$removeUserGroups)
400 {
401 if ($addUserGroups === null) {
402 $addUserGroups = (array)$this->getRequestParameter(self::DEFAULT_GROUPS_FORM_NAME, []);
403 }
404
405 $filteredUserGroupsForObject = $this->userGroupHandler->getFilteredUserGroupsForObject(
406 $objectType,
407 $objectId
408 );
409 $removeUserGroups = array_flip(array_keys($filteredUserGroupsForObject));
410 $bulkType = $this->getRequestParameter('uam_bulk_type');
411
412 if ($bulkType === self::BULK_REMOVE) {
413 $removeUserGroups = $addUserGroups;
414 $addUserGroups = [];
415 }
416 }
417
418 /**
419 * Updates the user groups for the given object.
420 *
421 * @param AbstractUserGroup[] $filteredUserGroups
422 * @param string $objectType
423 * @param string $objectId
424 * @param array $addUserGroups
425 * @param array $removeUserGroups
426 */
427 private function setUserGroups(
428 array $filteredUserGroups,
429 $objectType,
430 $objectId,
431 array $addUserGroups,
432 array $removeUserGroups
433 ) {
434 foreach ($filteredUserGroups as $groupId => $userGroup) {
435 if (isset($removeUserGroups[$groupId]) === true) {
436 $userGroup->removeObject($objectType, $objectId);
437 }
438
439 if (isset($addUserGroups[$groupId]['id']) === true
440 && (int)$addUserGroups[$groupId]['id'] === (int)$groupId
441 ) {
442 $userGroup->addObject(
443 $objectType,
444 $objectId,
445 $this->getDateParameter($addUserGroups[$groupId], 'fromDate'),
446 $this->getDateParameter($addUserGroups[$groupId], 'toDate')
447 );
448 }
449 }
450 }
451
452 /**
453 * Sets the dynamic user groups for the given object.
454 *
455 * @param string $objectType
456 * @param string $objectId
457 */
458 private function setDynamicGroups($objectType, $objectId)
459 {
460 $addDynamicUserGroups = $this->getRequestParameter(self::DEFAULT_DYNAMIC_GROUPS_FORM_NAME, []);
461
462 foreach ($addDynamicUserGroups as $dynamicUserGroupKey => $addDynamicUserGroup) {
463 $dynamicUserGroupData = explode('|', $dynamicUserGroupKey);
464
465 if (count($dynamicUserGroupData) === 2
466 && $addDynamicUserGroup['id'] === $dynamicUserGroupKey
467 ) {
468 $dynamicUserGroup = $this->userGroupFactory->createDynamicUserGroup(
469 $dynamicUserGroupData[0],
470 $dynamicUserGroupData[1]
471 );
472
473 $dynamicUserGroup->addObject(
474 $objectType,
475 $objectId,
476 $this->getDateParameter($addDynamicUserGroup, 'fromDate'),
477 $this->getDateParameter($addDynamicUserGroup, 'toDate')
478 );
479 }
480 }
481 }
482
483 /**
484 * Sets the default user groups for the given object.
485 *
486 * @param AbstractUserGroup[] $filteredUserGroups
487 * @param string $objectType
488 * @param string $objectId
489 */
490 private function setDefaultGroups(array $filteredUserGroups, $objectType, $objectId)
491 {
492 /**
493 * @var UserGroup[] $userGroupsToCheck
494 */
495 $userGroupsToCheck = array_diff_key($this->getUserGroups(), $filteredUserGroups);
496
497 foreach ($userGroupsToCheck as $userGroupToCheck) {
498 if ($userGroupToCheck->isDefaultGroupForObjectType($objectType, $fromTime, $toTime) === true) {
499 $userGroupToCheck->addObject(
500 $objectType,
501 $objectId,
502 $this->dateUtil->getDateFromTime($fromTime),
503 $this->dateUtil->getDateFromTime($toTime)
504 );
505 }
506 }
507 }
508
509 /**
510 * Saves the object data to the database.
511 *
512 * @param string $objectType The object type.
513 * @param string $objectId The id of the object.
514 * @param array $addUserGroups The new user groups for the object.
515 */
516 public function saveObjectData($objectType, $objectId, array $addUserGroups = null)
517 {
518 $isUpdateForm = (bool)$this->getRequestParameter(self::UPDATE_GROUPS_FORM_NAME, false) === true
519 || $this->getRequestParameter('uam_bulk_type') !== null;
520
521 $hasRights = $this->checkUserAccess() === true || $this->mainConfig->authorsCanAddPostsToGroups() === true;
522
523 if ($isUpdateForm === true && $hasRights === true) {
524 $filteredUserGroups = $this->userGroupHandler->getFilteredUserGroups();
525 $this->getAddRemoveGroups($objectType, $objectId, $addUserGroups, $removeUserGroups);
526 $this->setUserGroups($filteredUserGroups, $objectType, $objectId, $addUserGroups, $removeUserGroups);
527
528 if ($this->checkUserAccess() === true) {
529 $this->setDynamicGroups($objectType, $objectId);
530 } else {
531 $this->setDefaultGroups($filteredUserGroups, $objectType, $objectId);
532 }
533
534 $this->userGroupHandler->unsetUserGroupsForObject();
535 }
536 }
537
538 /**
539 * Removes the object data.
540 *
541 * @param string $objectType The object type.
542 * @param int $id The object id.
543 */
544 public function removeObjectData($objectType, $id)
545 {
546 $this->database->delete(
547 $this->database->getUserGroupToObjectTable(),
548 [
549 'object_id' => $id,
550 'object_type' => $objectType,
551 ],
552 [
553 '%d',
554 '%s'
555 ]
556 );
557 }
558
559 /**
560 * Returns the group selection form for pluggable objects.
561 *
562 * @param string $objectType The object type.
563 * @param string $objectId The id of the object.
564 * @param string $formName The formName.
565 * @param array $objectUserGroups If set we force this user groups for the object.
566 *
567 * @return string
568 */
569 public function showGroupSelectionForm(
570 $objectType,
571 $objectId,
572 $formName = null,
573 array $objectUserGroups = null
574 ) {
575 $this->setObjectInformation($objectType, $objectId, $objectUserGroups);
576
577 $this->groupsFromName = $formName;
578 $formContent = $this->getIncludeContents('GroupSelectionForm.php');
579 $this->groupsFromName = null;
580
581 return $formContent;
582 }
583
584 /**
585 * Returns the column for a pluggable object.
586 *
587 * @param string $objectType The object type.
588 * @param string $objectId The object id.
589 *
590 * @return string
591 */
592 public function getGroupColumn($objectType, $objectId)
593 {
594 $this->setObjectInformation($objectType, $objectId);
595 return $this->getIncludeContents('ObjectColumn.php');
596 }
597
598 /**
599 * Checks if the current object is a new object.
600 *
601 * @return bool
602 */
603 public function isNewObject()
604 {
605 if ($this->objectType !== null) {
606 $generalObjectType = $this->objectHandler->getGeneralObjectType($this->objectType);
607
608 return ($this->objectId === null
609 || ($generalObjectType === ObjectHandler::GENERAL_POST_OBJECT_TYPE &&
610 $this->getRequestParameter('action') !== 'edit')
611 );
612 }
613
614 return false;
615 }
616 }
617