PluginProbe
User Access Manager / 2.0.3
User Access Manager v2.0.3
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.3, at src/UserAccessManager/Controller/AdminObjectController.php

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