PluginProbe
User Access Manager / 2.0.11
User Access Manager v2.0.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 / UserAccessManager / Controller / AdminObjectController.php

AdminObjectController.php in User Access Manager 2.0.11, at src/UserAccessManager/Controller/AdminObjectController.php

761 lines 21.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * AdminObjectController.php
4 *
5 * The AdminObjectController 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;
16
17 use UserAccessManager\AccessHandler\AccessHandler;
18 use UserAccessManager\Config\Config;
19 use UserAccessManager\Database\Database;
20 use UserAccessManager\ObjectHandler\ObjectHandler;
21 use UserAccessManager\UserGroup\UserGroup;
22 use UserAccessManager\Wrapper\Php;
23 use UserAccessManager\Wrapper\Wordpress;
24
25 /**
26 * Class AdminObjectController
27 *
28 * @package UserAccessManager\Controller
29 */
30 class AdminObjectController extends Controller
31 {
32 const COLUMN_NAME = 'uam_access';
33 const BULK_REMOVE = 'remove';
34 const DEFAULT_GROUPS_FORM_NAME = 'uam_user_groups';
35 const UPDATE_GROUPS_FORM_NAME = 'uam_update_groups';
36
37 /**
38 * @var Database
39 */
40 private $database;
41
42 /**
43 * @var ObjectHandler
44 */
45 private $objectHandler;
46
47 /**
48 * @var AccessHandler
49 */
50 private $accessHandler;
51
52 /**
53 * @var null|string
54 */
55 private $groupsFromName = null;
56
57 /**
58 * @var null|string
59 */
60 private $objectType = null;
61
62 /**
63 * @var null|string
64 */
65 private $objectId = null;
66
67 /**
68 * @var UserGroup[]
69 */
70 private $objectUserGroups = [];
71
72 /**
73 * @var int
74 */
75 private $userGroupDiff = 0;
76
77 /**
78 * AdminObjectController constructor.
79 *
80 * @param Php $php
81 * @param Wordpress $wordpress
82 * @param Config $config
83 * @param Database $database
84 * @param ObjectHandler $objectHandler
85 * @param AccessHandler $accessHandler
86 */
87 public function __construct(
88 Php $php,
89 Wordpress $wordpress,
90 Config $config,
91 Database $database,
92 ObjectHandler $objectHandler,
93 AccessHandler $accessHandler
94 ) {
95 parent::__construct($php, $wordpress, $config);
96 $this->database = $database;
97 $this->objectHandler = $objectHandler;
98 $this->accessHandler = $accessHandler;
99 }
100
101 /**
102 * Sets the current object type, the object id and the user groups.
103 *
104 * @param string $objectType
105 * @param string $objectId
106 * @param array $objectUserGroups
107 */
108 private function setObjectInformation($objectType, $objectId, array $objectUserGroups = null)
109 {
110 $this->objectType = $objectType;
111 $this->objectId = $objectId;
112
113 if ($objectUserGroups === null) {
114 $objectUserGroups = $this->accessHandler->getFilteredUserGroupsForObject($objectType, $objectId);
115 $fullObjectUserGroups = $this->accessHandler->getUserGroupsForObject($objectType, $objectId);
116 $this->userGroupDiff = count($fullObjectUserGroups) - count($objectUserGroups);
117 } else {
118 $this->userGroupDiff = 0;
119 }
120
121 $this->objectUserGroups = $objectUserGroups;
122 }
123
124 /**
125 * Returns the default groups form name.
126 *
127 * @return string
128 */
129 public function getGroupsFromName()
130 {
131 return ($this->groupsFromName !== null) ? (string)$this->groupsFromName : self::DEFAULT_GROUPS_FORM_NAME;
132 }
133
134 /**
135 * Returns the current object type.
136 *
137 * @return string
138 */
139 public function getObjectType()
140 {
141 return $this->objectType;
142 }
143
144 /**
145 * Returns the current object id.
146 *
147 * @return string
148 */
149 public function getObjectId()
150 {
151 return $this->objectId;
152 }
153
154 /**
155 * Returns the current object user groups.
156 *
157 * @return UserGroup[]
158 */
159 public function getObjectUserGroups()
160 {
161 return $this->objectUserGroups;
162 }
163
164 /**
165 * Returns the user group count diff.
166 *
167 * @return int
168 */
169 public function getUserGroupDiff()
170 {
171 return $this->userGroupDiff;
172 }
173
174 /**
175 * Returns all available user groups.
176 *
177 * @return UserGroup[]
178 */
179 public function getUserGroups()
180 {
181 return $this->accessHandler->getUserGroups();
182 }
183
184 /**
185 * Returns the filtered user groups.
186 *
187 * @return UserGroup[]
188 */
189 public function getFilteredUserGroups()
190 {
191 return $this->accessHandler->getFilteredUserGroups();
192 }
193
194 /**
195 * Checks if the current user is an admin.
196 *
197 * @return bool
198 */
199 public function isCurrentUserAdmin()
200 {
201 if ($this->objectType === ObjectHandler::GENERAL_USER_OBJECT_TYPE
202 && $this->objectId !== null
203 ) {
204 return $this->accessHandler->userIsAdmin($this->objectId);
205 }
206
207 return false;
208 }
209
210 /**
211 * Returns the wordpress role names.
212 *
213 * @return array
214 */
215 public function getRoleNames()
216 {
217 $roles = $this->wordpress->getRoles();
218 return $roles->role_names;
219 }
220
221 /**
222 * Returns all object types.
223 *
224 * @return array
225 */
226 public function getAllObjectTypes()
227 {
228 return $this->objectHandler->getAllObjectTypes();
229 }
230
231 /**
232 * Checks the user access.
233 *
234 * @return bool
235 */
236 public function checkUserAccess()
237 {
238 return $this->accessHandler->checkUserAccess();
239 }
240
241 /**
242 * Returns the recursive object membership.
243 *
244 * @param $userGroup
245 *
246 * @return array
247 */
248 public function getRecursiveMembership(UserGroup $userGroup)
249 {
250 $recursiveMembership = [];
251 $objectId = $this->getObjectId();
252 $objectType = $this->getObjectType();
253 $roles = $this->getRoleNames();
254 $recursiveMembershipForObject = $userGroup->getRecursiveMembershipForObject($objectType, $objectId);
255
256 foreach ($recursiveMembershipForObject as $recursiveType => $objectIds) {
257 foreach ($objectIds as $objectId) {
258 $objectName = $objectId;
259 $typeName = $this->objectHandler->getGeneralObjectType($recursiveType);
260
261 if ($typeName === ObjectHandler::GENERAL_ROLE_OBJECT_TYPE) {
262 $objectName = isset($roles[$objectId]) ? $roles[$objectId] : $objectId;
263 } elseif ($typeName === ObjectHandler::GENERAL_USER_OBJECT_TYPE) {
264 $user = $this->objectHandler->getUser($objectId);
265 $objectName = ($user !== false) ? $user->display_name : $objectId;
266 } elseif ($typeName === ObjectHandler::GENERAL_TERM_OBJECT_TYPE) {
267 $term = $this->objectHandler->getTerm($objectId);
268
269 if ($term !== false) {
270 $taxonomy = $this->wordpress->getTaxonomy($term->taxonomy);
271 $typeName = ($taxonomy !== false) ? $taxonomy->labels->name : $typeName;
272 $objectName = $term->name;
273 }
274 } elseif ($typeName === ObjectHandler::GENERAL_POST_OBJECT_TYPE) {
275 $post = $this->objectHandler->getPost($objectId);
276
277 if ($post !== false) {
278 $postTypeObject = $this->wordpress->getPostTypeObject($post->post_type);
279 $typeName = ($postTypeObject !== null) ? $postTypeObject->labels->name : $typeName;
280 $objectName = $post->post_title;
281 }
282 } elseif ($this->objectHandler->isPluggableObject($recursiveType) === true) {
283 $pluggableObject = $this->objectHandler->getPluggableObject($recursiveType);
284 $typeName = $pluggableObject->getObjectType();
285 $objectName = $pluggableObject->getObjectName($objectId);
286 }
287
288 if ($typeName !== null) {
289 $recursiveMembership[$typeName][$objectId] = $objectName;
290 }
291 }
292 }
293
294 return $recursiveMembership;
295 }
296
297 /**
298 * Shows the error if the user has no rights to edit the content.
299 */
300 public function checkRightsToEditContent()
301 {
302 $noRights = false;
303
304 $postId = $this->getRequestParameter('post');
305 $postId = is_numeric($postId) === false ? $this->getRequestParameter('attachment_id') : $postId;
306
307 if (is_numeric($postId) === true) {
308 $post = $this->objectHandler->getPost($postId);
309
310 if ($post !== false) {
311 $noRights = !$this->accessHandler->checkObjectAccess($post->post_type, $post->ID);
312 }
313 }
314
315 $tagId = $this->getRequestParameter('tag_ID');
316
317 if ($noRights === false && is_numeric($tagId)) {
318 $noRights = !$this->accessHandler->checkObjectAccess(ObjectHandler::GENERAL_TERM_OBJECT_TYPE, $tagId);
319 }
320
321 if ($noRights === true) {
322 $this->wordpress->wpDie(TXT_UAM_NO_RIGHTS_MESSAGE, TXT_UAM_NO_RIGHTS_TITLE, ['response' => 403]);
323 }
324 }
325
326
327 /*
328 * Meta functions
329 */
330
331 /**
332 * Saves the object data to the database.
333 *
334 * @param string $objectType The object type.
335 * @param integer $objectId The _iId of the object.
336 * @param UserGroup[] $userGroups The new user groups for the object.
337 */
338 private function saveObjectData($objectType, $objectId, array $userGroups = null)
339 {
340 $isUpdateForm = (bool)$this->getRequestParameter(self::UPDATE_GROUPS_FORM_NAME, false) === true
341 || $this->getRequestParameter('uam_bulk_type') !== null;
342
343 $hasRights = $this->accessHandler->checkUserAccess('manage_user_groups') === true
344 || $this->config->authorsCanAddPostsToGroups() === true;
345
346 if ($isUpdateForm === true && $hasRights === true) {
347 if ($userGroups === null) {
348 $updateGroups = $this->getRequestParameter(self::DEFAULT_GROUPS_FORM_NAME, []);
349 $userGroups = (is_array($updateGroups) === true) ? $updateGroups : [];
350 }
351
352 $addUserGroups = array_flip($userGroups);
353 $filteredUserGroupsForObject = $this->accessHandler->getFilteredUserGroupsForObject(
354 $objectType,
355 $objectId
356 );
357 $removeUserGroups = array_flip(array_keys($filteredUserGroupsForObject));
358 $filteredUserGroups = $this->accessHandler->getFilteredUserGroups();
359 $bulkType = $this->getRequestParameter('uam_bulk_type');
360
361 if ($bulkType === self::BULK_REMOVE) {
362 $removeUserGroups = $addUserGroups;
363 $addUserGroups = [];
364 }
365
366 foreach ($filteredUserGroups as $groupId => $userGroup) {
367 if (isset($removeUserGroups[$groupId]) === true) {
368 $userGroup->removeObject($objectType, $objectId);
369 }
370
371 if (isset($addUserGroups[$groupId]) === true) {
372 $userGroup->addObject($objectType, $objectId);
373 }
374
375 $userGroup->save();
376 }
377
378 $this->accessHandler->unsetUserGroupsForObject();
379 }
380 }
381
382 /**
383 * Removes the object data.
384 *
385 * @param string $objectType The object type.
386 * @param int $id The object id.
387 */
388 private function removeObjectData($objectType, $id)
389 {
390 $this->database->delete(
391 $this->database->getUserGroupToObjectTable(),
392 [
393 'object_id' => $id,
394 'object_type' => $objectType,
395 ],
396 [
397 '%d',
398 '%s'
399 ]
400 );
401 }
402
403 /*
404 * Functions for the post actions.
405 */
406
407 /**
408 * The function for the manage_posts_columns and
409 * the manage_pages_columns filter.
410 *
411 * @param array $defaults The table headers.
412 *
413 * @return array
414 */
415 public function addPostColumnsHeader($defaults)
416 {
417 $defaults[self::COLUMN_NAME] = TXT_UAM_COLUMN_ACCESS;
418 return $defaults;
419 }
420
421 /**
422 * The function for the manage_users_custom_column action.
423 *
424 * @param string $columnName The column name.
425 * @param integer $id The id.
426 */
427 public function addPostColumn($columnName, $id)
428 {
429 if ($columnName === self::COLUMN_NAME) {
430 $post = $this->objectHandler->getPost($id);
431 $this->setObjectInformation($post->post_type, $post->ID);
432 echo $this->getIncludeContents('ObjectColumn.php');
433 }
434 }
435
436 /**
437 * The function for the uma_post_access meta box.
438 *
439 * @param object $post The post.
440 */
441 public function editPostContent($post)
442 {
443 if ($post instanceof \WP_Post) {
444 $this->setObjectInformation($post->post_type, $post->ID);
445 }
446
447 echo $this->getIncludeContents('PostEditForm.php');
448 }
449
450 /**
451 * Adds the bulk edit form.
452 *
453 * @param $columnName
454 */
455 public function addBulkAction($columnName)
456 {
457 if ($columnName === self::COLUMN_NAME) {
458 echo $this->getIncludeContents('BulkEditForm.php');
459 }
460 }
461
462 /**
463 * The function for the save_post action.
464 *
465 * @param mixed $postParam The post id or a array of a post.
466 */
467 public function savePostData($postParam)
468 {
469 $postId = (is_array($postParam) === true) ? $postParam['ID'] : $postParam;
470 $post = $this->objectHandler->getPost($postId);
471 $postType = $post->post_type;
472 $postId = $post->ID;
473
474 if ($postType === 'revision') {
475 $postId = $post->post_parent;
476 $parentPost = $this->objectHandler->getPost($postId);
477 $postType = $parentPost->post_type;
478 }
479
480 $this->saveObjectData($postType, $postId);
481 }
482
483 /**
484 * The function for the attachment_fields_to_save filter.
485 * We have to use this because the attachment actions work
486 * not in the way we need.
487 *
488 * @param array $attachment The attachment id.
489 *
490 * @return array
491 */
492 public function saveAttachmentData($attachment)
493 {
494 $this->savePostData($attachment['ID']);
495
496 return $attachment;
497 }
498
499 /**
500 * The function for the wp_ajax_save_attachment_compat filter.
501 */
502 public function saveAjaxAttachmentData()
503 {
504 $attachmentId = $this->getRequestParameter('id');
505 $userGroups = $this->getRequestParameter(self::DEFAULT_GROUPS_FORM_NAME);
506
507 $this->saveObjectData(
508 ObjectHandler::GENERAL_POST_OBJECT_TYPE,
509 $attachmentId,
510 $userGroups
511 );
512 }
513
514 /**
515 * The function for the delete_post action.
516 *
517 * @param integer $postId The post id.
518 */
519 public function removePostData($postId)
520 {
521 $post = $this->objectHandler->getPost($postId);
522 $this->removeObjectData($post->post_type, $postId);
523 }
524
525 /**
526 * The function for the media_meta action.
527 *
528 * @param array $formFields The meta.
529 * @param \WP_Post $post The post.
530 *
531 * @return string
532 */
533 public function showMediaFile(array $formFields, $post = null)
534 {
535 $attachmentId = $this->getRequestParameter('attachment_id');
536
537 if ($attachmentId !== null) {
538 $post = $this->objectHandler->getPost($attachmentId);
539 }
540
541 if ($post instanceof \WP_Post) {
542 $this->setObjectInformation($post->post_type, $post->ID);
543 }
544
545 $formFields[self::DEFAULT_GROUPS_FORM_NAME] =[
546 'label' => TXT_UAM_SET_UP_USER_GROUPS,
547 'input' => 'editFrom',
548 'editFrom' => $this->getIncludeContents('MediaAjaxEditForm.php')
549 ];
550
551 return $formFields;
552 }
553
554 /*
555 * Functions for the user actions.
556 */
557
558 /**
559 * The function for the manage_users_columns filter.
560 *
561 * @param array $defaults The table headers.
562 *
563 * @return array
564 */
565 public function addUserColumnsHeader($defaults)
566 {
567 $defaults[self::COLUMN_NAME] = TXT_UAM_COLUMN_USER_GROUPS;
568 return $defaults;
569 }
570
571 /**
572 * The function for the manage_users_custom_column action.
573 *
574 * @param string $return The normal return value.
575 * @param string $columnName The column name.
576 * @param integer $id The id.
577 *
578 * @return string|null
579 */
580 public function addUserColumn($return, $columnName, $id)
581 {
582 if ($columnName === self::COLUMN_NAME) {
583 $this->setObjectInformation(ObjectHandler::GENERAL_USER_OBJECT_TYPE, $id);
584 $return .= $this->getIncludeContents('UserColumn.php');
585 }
586
587 return $return;
588 }
589
590 /**
591 * The function for the edit_user_profile action.
592 */
593 public function showUserProfile()
594 {
595 $userId = $this->getRequestParameter('user_id');
596
597 if ($userId !== null) {
598 $this->setObjectInformation(ObjectHandler::GENERAL_USER_OBJECT_TYPE, $userId);
599 }
600
601 echo $this->getIncludeContents('UserProfileEditForm.php');
602 }
603
604 /**
605 * The function for the profile_update action.
606 *
607 * @param integer $userId The user id.
608 */
609 public function saveUserData($userId)
610 {
611 $this->saveObjectData(ObjectHandler::GENERAL_USER_OBJECT_TYPE, $userId);
612 }
613
614 /**
615 * The function for the delete_user action.
616 *
617 * @param integer $userId The user id.
618 */
619 public function removeUserData($userId)
620 {
621 $this->removeObjectData(ObjectHandler::GENERAL_USER_OBJECT_TYPE, $userId);
622 }
623
624
625 /*
626 * Functions for the term actions.
627 */
628
629 /**
630 * The function for the manage_categories_columns filter.
631 *
632 * @param array $defaults The table headers.
633 *
634 * @return array
635 */
636 public function addTermColumnsHeader($defaults)
637 {
638 $defaults[self::COLUMN_NAME] = TXT_UAM_COLUMN_ACCESS;
639 return $defaults;
640 }
641
642 /**
643 * The function for the manage_categories_custom_column action.
644 *
645 * @param string $content Content for the column. Multiple filter calls are possible, so we need to append.
646 * @param string $columnName The column name.
647 * @param integer $id The id.
648 *
649 * @return string $content with content appended for self::COLUMN_NAME column
650 */
651 public function addTermColumn($content, $columnName, $id)
652 {
653 if ($columnName === self::COLUMN_NAME) {
654 $this->setObjectInformation(ObjectHandler::GENERAL_TERM_OBJECT_TYPE, $id);
655 $content .= $this->getIncludeContents('ObjectColumn.php');
656 }
657
658 return $content;
659 }
660
661 /**
662 * The function for the edit_{term}_form action.
663 *
664 * @param \WP_Term $term The term.
665 */
666 public function showTermEditForm($term)
667 {
668 if ($term instanceof \WP_Term) {
669 $this->setObjectInformation($term->taxonomy, $term->term_id);
670 }
671
672 echo $this->getIncludeContents('TermEditForm.php');
673 }
674
675 /**
676 * The function for the edit_{term} action.
677 *
678 * @param integer $termId The term id.
679 */
680 public function saveTermData($termId)
681 {
682 $this->saveObjectData(ObjectHandler::GENERAL_TERM_OBJECT_TYPE, $termId);
683 }
684
685 /**
686 * The function for the delete_{term} action.
687 *
688 * @param integer $termId The id of the term.
689 */
690 public function removeTermData($termId)
691 {
692 $this->removeObjectData(ObjectHandler::GENERAL_TERM_OBJECT_TYPE, $termId);
693 }
694
695 /*
696 * Functions for the pluggable object actions.
697 */
698
699 /**
700 * The function for the pluggable save action.
701 *
702 * @param string $objectType The name of the pluggable object.
703 * @param integer $objectId The pluggable object id.
704 * @param UserGroup[] $userGroups The user groups for the object.
705 */
706 public function savePluggableObjectData($objectType, $objectId, $userGroups = null)
707 {
708 $this->saveObjectData($objectType, $objectId, $userGroups);
709 }
710
711 /**
712 * The function for the pluggable remove action.
713 *
714 * @param string $objectName The name of the pluggable object.
715 * @param integer $objectId The pluggable object id.
716 */
717 public function removePluggableObjectData($objectName, $objectId)
718 {
719 $this->removeObjectData($objectName, $objectId);
720 }
721
722 /**
723 * Returns the group selection form for pluggable objects.
724 *
725 * @param string $objectType The object type.
726 * @param string $objectId The id of the object.
727 * @param string $formName The formName.
728 * @param array $objectUserGroups If set we force this user groups for the object.
729 *
730 * @return string
731 */
732 public function showPluggableGroupSelectionForm(
733 $objectType,
734 $objectId,
735 $formName = null,
736 array $objectUserGroups = null
737 ) {
738 $this->setObjectInformation($objectType, $objectId, $objectUserGroups);
739
740 $this->groupsFromName = $formName;
741 $formContent = $this->getIncludeContents('GroupSelectionForm.php');
742 $this->groupsFromName = null;
743
744 return $formContent;
745 }
746
747 /**
748 * Returns the column for a pluggable object.
749 *
750 * @param string $objectType The object type.
751 * @param string $objectId The object id.
752 *
753 * @return string
754 */
755 public function getPluggableColumn($objectType, $objectId)
756 {
757 $this->setObjectInformation($objectType, $objectId);
758 return $this->getIncludeContents('ObjectColumn.php');
759 }
760 }
761