PluginProbe
User Access Manager / 2.1.6
User Access Manager v2.1.6
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.6, at src/Controller/Backend/ObjectController.php

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