PluginProbe
User Access Manager / 2.1.4
User Access Manager v2.1.4
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.4, at src/Controller/Backend/ObjectController.php

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