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

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

601 lines 17.1 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 $postId = $this->getRequestParameter('post', $this->getRequestParameter('attachment_id'));
352
353 if ($postId !== null) {
354 $this->dieOnNoAccess(ObjectHandler::GENERAL_POST_OBJECT_TYPE, $postId);
355 }
356
357 $tagId = $this->getRequestParameter('tag_ID');
358
359 if ($tagId !== null) {
360 $this->dieOnNoAccess(ObjectHandler::GENERAL_TERM_OBJECT_TYPE, $tagId);
361 }
362 }
363
364 /**
365 * @param array $data
366 * @param string $name
367 *
368 * @return null|string
369 */
370 private function getDateParameter(array $data, $name)
371 {
372 return (isset($data[$name]) === true && (string)$data[$name] !== '') ? (string)$data[$name] : null;
373 }
374
375 /**
376 * Returns the user groups by reference which should be add and removed from the object.
377 *
378 * @param string $objectType
379 * @param string $objectId
380 * @param array|null $addUserGroups
381 * @param array $removeUserGroups
382 */
383 private function getAddRemoveGroups($objectType, $objectId, &$addUserGroups, &$removeUserGroups)
384 {
385 if ($addUserGroups === null) {
386 $addUserGroups = (array)$this->getRequestParameter(self::DEFAULT_GROUPS_FORM_NAME, []);
387 }
388
389 $filteredUserGroupsForObject = $this->accessHandler->getFilteredUserGroupsForObject(
390 $objectType,
391 $objectId
392 );
393 $removeUserGroups = array_flip(array_keys($filteredUserGroupsForObject));
394 $bulkType = $this->getRequestParameter('uam_bulk_type');
395
396 if ($bulkType === self::BULK_REMOVE) {
397 $removeUserGroups = $addUserGroups;
398 $addUserGroups = [];
399 }
400 }
401
402 /**
403 * Updates the user groups for the given object.
404 *
405 * @param AbstractUserGroup[] $filteredUserGroups
406 * @param string $objectType
407 * @param string $objectId
408 * @param array $addUserGroups
409 * @param array $removeUserGroups
410 */
411 private function setUserGroups(
412 array $filteredUserGroups,
413 $objectType,
414 $objectId,
415 array $addUserGroups,
416 array $removeUserGroups
417 ) {
418 foreach ($filteredUserGroups as $groupId => $userGroup) {
419 if (isset($removeUserGroups[$groupId]) === true) {
420 $userGroup->removeObject($objectType, $objectId);
421 }
422
423 if (isset($addUserGroups[$groupId]['id']) === true
424 && (int)$addUserGroups[$groupId]['id'] === (int)$groupId
425 ) {
426 $userGroup->addObject(
427 $objectType,
428 $objectId,
429 $this->getDateParameter($addUserGroups[$groupId], 'fromDate'),
430 $this->getDateParameter($addUserGroups[$groupId], 'toDate')
431 );
432 }
433 }
434 }
435
436 /**
437 * Sets the dynamic user groups for the given object.
438 *
439 * @param string $objectType
440 * @param string $objectId
441 */
442 private function setDynamicGroups($objectType, $objectId)
443 {
444 $addDynamicUserGroups = $this->getRequestParameter(self::DEFAULT_DYNAMIC_GROUPS_FORM_NAME, []);
445
446 foreach ($addDynamicUserGroups as $dynamicUserGroupKey => $addDynamicUserGroup) {
447 $dynamicUserGroupData = explode('|', $dynamicUserGroupKey);
448
449 if (count($dynamicUserGroupData) === 2
450 && $addDynamicUserGroup['id'] === $dynamicUserGroupKey
451 ) {
452 $dynamicUserGroup = $this->userGroupFactory->createDynamicUserGroup(
453 $dynamicUserGroupData[0],
454 $dynamicUserGroupData[1]
455 );
456
457 $dynamicUserGroup->addObject(
458 $objectType,
459 $objectId,
460 $this->getDateParameter($addDynamicUserGroup, 'fromDate'),
461 $this->getDateParameter($addDynamicUserGroup, 'toDate')
462 );
463 }
464 }
465 }
466
467 /**
468 * Sets the default user groups for the given object.
469 *
470 * @param AbstractUserGroup[] $filteredUserGroups
471 * @param string $objectType
472 * @param string $objectId
473 */
474 private function setDefaultGroups(array $filteredUserGroups, $objectType, $objectId)
475 {
476 /**
477 * @var UserGroup[] $userGroupsToCheck
478 */
479 $userGroupsToCheck = array_diff_key($this->getUserGroups(), $filteredUserGroups);
480
481 foreach ($userGroupsToCheck as $userGroupToCheck) {
482 if ($userGroupToCheck->isDefaultGroupForObjectType($objectType, $fromTime, $toTime) === true) {
483 $userGroupToCheck->addObject(
484 $objectType,
485 $objectId,
486 $this->dateUtil->getDateFromTime($fromTime),
487 $this->dateUtil->getDateFromTime($toTime)
488 );
489 }
490 }
491 }
492
493 /**
494 * Saves the object data to the database.
495 *
496 * @param string $objectType The object type.
497 * @param string $objectId The id of the object.
498 * @param array $addUserGroups The new user groups for the object.
499 */
500 public function saveObjectData($objectType, $objectId, array $addUserGroups = null)
501 {
502 $isUpdateForm = (bool)$this->getRequestParameter(self::UPDATE_GROUPS_FORM_NAME, false) === true
503 || $this->getRequestParameter('uam_bulk_type') !== null;
504
505 $hasRights = $this->checkUserAccess() === true || $this->mainConfig->authorsCanAddPostsToGroups() === true;
506
507 if ($isUpdateForm === true && $hasRights === true) {
508 $filteredUserGroups = $this->accessHandler->getFilteredUserGroups();
509 $this->getAddRemoveGroups($objectType, $objectId, $addUserGroups, $removeUserGroups);
510 $this->setUserGroups($filteredUserGroups, $objectType, $objectId, $addUserGroups, $removeUserGroups);
511
512 if ($this->checkUserAccess() === true) {
513 $this->setDynamicGroups($objectType, $objectId);
514 } else {
515 $this->setDefaultGroups($filteredUserGroups, $objectType, $objectId);
516 }
517
518 $this->accessHandler->unsetUserGroupsForObject();
519 }
520 }
521
522 /**
523 * Removes the object data.
524 *
525 * @param string $objectType The object type.
526 * @param int $id The object id.
527 */
528 public function removeObjectData($objectType, $id)
529 {
530 $this->database->delete(
531 $this->database->getUserGroupToObjectTable(),
532 [
533 'object_id' => $id,
534 'object_type' => $objectType,
535 ],
536 [
537 '%d',
538 '%s'
539 ]
540 );
541 }
542
543 /**
544 * Returns the group selection form for pluggable objects.
545 *
546 * @param string $objectType The object type.
547 * @param string $objectId The id of the object.
548 * @param string $formName The formName.
549 * @param array $objectUserGroups If set we force this user groups for the object.
550 *
551 * @return string
552 */
553 public function showGroupSelectionForm(
554 $objectType,
555 $objectId,
556 $formName = null,
557 array $objectUserGroups = null
558 ) {
559 $this->setObjectInformation($objectType, $objectId, $objectUserGroups);
560
561 $this->groupsFromName = $formName;
562 $formContent = $this->getIncludeContents('GroupSelectionForm.php');
563 $this->groupsFromName = null;
564
565 return $formContent;
566 }
567
568 /**
569 * Returns the column for a pluggable object.
570 *
571 * @param string $objectType The object type.
572 * @param string $objectId The object id.
573 *
574 * @return string
575 */
576 public function getGroupColumn($objectType, $objectId)
577 {
578 $this->setObjectInformation($objectType, $objectId);
579 return $this->getIncludeContents('ObjectColumn.php');
580 }
581
582 /**
583 * Checks if the current object is a new object.
584 *
585 * @return bool
586 */
587 public function isNewObject()
588 {
589 if ($this->objectType !== null) {
590 $generalObjectType = $this->objectHandler->getGeneralObjectType($this->objectType);
591
592 return ($this->objectId === null
593 || ($generalObjectType === ObjectHandler::GENERAL_POST_OBJECT_TYPE &&
594 $this->getRequestParameter('action') !== 'edit')
595 );
596 }
597
598 return false;
599 }
600 }
601