PluginProbe
User Access Manager / 2.1.12
User Access Manager v2.1.12
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.12, at src/Controller/Backend/ObjectController.php

474 lines 14.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\Config\MainConfig;
19 use UserAccessManager\Config\WordpressConfig;
20 use UserAccessManager\Controller\Controller;
21 use UserAccessManager\Database\Database;
22 use UserAccessManager\Object\ObjectHandler;
23 use UserAccessManager\ObjectMembership\MissingObjectMembershipHandlerException;
24 use UserAccessManager\UserGroup\AbstractUserGroup;
25 use UserAccessManager\UserGroup\AssignmentInformation;
26 use UserAccessManager\UserGroup\DynamicUserGroup;
27 use UserAccessManager\UserGroup\UserGroupAssignmentException;
28 use UserAccessManager\UserGroup\UserGroupAssignmentHandler;
29 use UserAccessManager\User\UserHandler;
30 use UserAccessManager\UserGroup\UserGroupHandler;
31 use UserAccessManager\Util\DateUtil;
32 use UserAccessManager\Wrapper\Php;
33 use UserAccessManager\Wrapper\Wordpress;
34
35 /**
36 * Class ObjectController
37 *
38 * @package UserAccessManager\Controller
39 */
40 class ObjectController extends Controller
41 {
42 const COLUMN_NAME = 'uam_access';
43 const BULK_REMOVE = 'remove';
44 const DEFAULT_GROUPS_FORM_NAME = 'uam_user_groups';
45 const DEFAULT_DYNAMIC_GROUPS_FORM_NAME = 'uam_dynamic_user_groups';
46 const UPDATE_GROUPS_FORM_NAME = 'uam_update_groups';
47
48 /**
49 * @var MainConfig
50 */
51 protected $mainConfig;
52
53 /**
54 * @var Database
55 */
56 protected $database;
57
58 /**
59 * @var DateUtil
60 */
61 protected $dateUtil;
62
63 /**
64 * @var ObjectHandler
65 */
66 protected $objectHandler;
67
68 /**
69 * @var UserHandler
70 */
71 protected $userHandler;
72
73 /**
74 * @var UserGroupHandler
75 */
76 protected $userGroupHandler;
77
78 /**
79 * @var AccessHandler
80 */
81 protected $accessHandler;
82
83 /**
84 * @var UserGroupAssignmentHandler
85 */
86 protected $userGroupAssignmentHandler;
87
88 /**
89 * @var ObjectInformation
90 */
91 protected $objectInformation;
92
93 /**
94 * @var null|string
95 */
96 protected $groupsFromName = null;
97
98 /**
99 * ObjectController constructor.
100 *
101 * @param Php $php
102 * @param Wordpress $wordpress
103 * @param WordpressConfig $wordpressConfig
104 * @param MainConfig $mainConfig
105 * @param Database $database
106 * @param DateUtil $dateUtil
107 * @param ObjectHandler $objectHandler
108 * @param UserHandler $userHandler
109 * @param UserGroupHandler $userGroupHandler
110 * @param UserGroupAssignmentHandler $userGroupAssignmentHandler
111 * @param AccessHandler $accessHandler
112 * @param ObjectInformationFactory $objectInformationFactory
113 */
114 public function __construct(
115 Php $php,
116 Wordpress $wordpress,
117 WordpressConfig $wordpressConfig,
118 MainConfig $mainConfig,
119 Database $database,
120 DateUtil $dateUtil,
121 ObjectHandler $objectHandler,
122 UserHandler $userHandler,
123 UserGroupHandler $userGroupHandler,
124 UserGroupAssignmentHandler $userGroupAssignmentHandler,
125 AccessHandler $accessHandler,
126 ObjectInformationFactory $objectInformationFactory
127 ) {
128 parent::__construct($php, $wordpress, $wordpressConfig);
129 $this->mainConfig = $mainConfig;
130 $this->database = $database;
131 $this->dateUtil = $dateUtil;
132 $this->objectHandler = $objectHandler;
133 $this->userHandler = $userHandler;
134 $this->userGroupHandler = $userGroupHandler;
135 $this->userGroupAssignmentHandler = $userGroupAssignmentHandler;
136 $this->accessHandler = $accessHandler;
137 $this->objectInformation = $objectInformationFactory->createObjectInformation();
138 }
139
140 /**
141 * Sets the current object type, the object id and the user groups.
142 *
143 * @param string $objectType
144 * @param string $objectId
145 * @param array $objectUserGroups
146 */
147 protected function setObjectInformation($objectType, $objectId, array $objectUserGroups = null)
148 {
149 $userGroupDiff = 0;
150
151 if ($objectUserGroups === null && $objectId !== null) {
152 $objectUserGroups = $this->userGroupHandler->getFilteredUserGroupsForObject($objectType, $objectId, true);
153 $fullObjectUserGroups = $this->userGroupHandler->getUserGroupsForObject($objectType, $objectId, true);
154 $userGroupDiff = count($fullObjectUserGroups) - count($objectUserGroups);
155 }
156
157 $this->objectInformation->setObjectType($objectType)
158 ->setObjectId($objectId)
159 ->setObjectUserGroups((array)$objectUserGroups)
160 ->setUserGroupDiff($userGroupDiff);
161 }
162
163 /**
164 * Returns the object information.
165 *
166 * @return ObjectInformation
167 */
168 public function getObjectInformation()
169 {
170 return $this->objectInformation;
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 filtered user groups.
185 *
186 * @return AbstractUserGroup[]
187 */
188 public function getFilteredUserGroups()
189 {
190 return $this->userGroupHandler->getFilteredUserGroups();
191 }
192
193 /**
194 * Sorts the user groups.
195 *
196 * @param array $userGroups
197 */
198 public function sortUserGroups(array &$userGroups)
199 {
200 uasort(
201 $userGroups,
202 function (
203 AbstractUserGroup $userGroupOne,
204 AbstractUserGroup $userGroupTwo
205 ) {
206 $notLoggedInUserGroupId = DynamicUserGroup::USER_TYPE.'|'.DynamicUserGroup::NOT_LOGGED_IN_USER_ID;
207
208 if ($userGroupOne->getId() === $notLoggedInUserGroupId) {
209 return 1;
210 } elseif ($userGroupTwo->getId() === $notLoggedInUserGroupId) {
211 return 0;
212 }
213
214 return strnatcasecmp($userGroupOne->getName(), $userGroupTwo->getName());
215 }
216 );
217 }
218
219 /**
220 * Returns the date util.
221 *
222 * @return DateUtil
223 */
224 public function getDateUtil()
225 {
226 return $this->dateUtil;
227 }
228
229 /**
230 * Checks if the current user is an admin.
231 *
232 * @return bool
233 */
234 public function isCurrentUserAdmin()
235 {
236 if ($this->objectInformation->getObjectType() === ObjectHandler::GENERAL_USER_OBJECT_TYPE
237 && $this->objectInformation->getObjectId() !== null
238 ) {
239 return $this->userHandler->userIsAdmin($this->objectInformation->getObjectId());
240 }
241
242 return false;
243 }
244
245 /**
246 * Returns the wordpress role names.
247 *
248 * @return array
249 */
250 public function getRoleNames()
251 {
252 $roles = $this->wordpress->getRoles();
253 return $roles->role_names;
254 }
255
256 /**
257 * Checks the user access.
258 *
259 * @return bool
260 */
261 public function checkUserAccess()
262 {
263 return $this->userHandler->checkUserAccess(UserHandler::MANAGE_USER_GROUPS_CAPABILITY);
264 }
265
266 /**
267 * Returns the recursive object membership.
268 *
269 * @param $userGroup
270 *
271 * @return array
272 */
273 public function getRecursiveMembership(AbstractUserGroup $userGroup)
274 {
275 $recursiveMembership = [];
276 $objectType = $this->objectInformation->getObjectType();
277 $objectId = $this->objectInformation->getObjectId();
278 $recursiveMembershipForObject = $userGroup->getRecursiveMembershipForObject($objectType, $objectId);
279
280 /**
281 * @var AssignmentInformation[] $assignmentInformation
282 */
283 foreach ($recursiveMembershipForObject as $recursiveType => $assignmentInformation) {
284 foreach ($assignmentInformation as $objectId => $information) {
285 try {
286 $membershipHandler = $this->objectHandler->getObjectMembershipHandler($information->getType());
287 $typeName = $membershipHandler->getGeneralObjectType();
288 $objectName = $membershipHandler->getObjectName($objectId, $typeName);
289 $recursiveMembership[$typeName][$objectId] = $objectName;
290 } catch (MissingObjectMembershipHandlerException $exception) {
291 // Do nothing
292 }
293 }
294 }
295
296 return $recursiveMembership;
297 }
298
299 /**
300 * Checks the access and dies if the user has no access.
301 *
302 * @param string $objectType
303 * @param string $objectId
304 */
305 private function dieOnNoAccess($objectType, $objectId)
306 {
307 if ($this->accessHandler->checkObjectAccess($objectType, $objectId) === false) {
308 $this->wordpress->wpDie(TXT_UAM_NO_RIGHTS_MESSAGE, TXT_UAM_NO_RIGHTS_TITLE, ['response' => 403]);
309 }
310 }
311
312 /**
313 * Shows the error if the user has no rights to edit the content.
314 */
315 public function checkRightsToEditContent()
316 {
317 $postIdParameter = $this->getRequestParameter('post', $this->getRequestParameter('attachment_id'));
318
319 if ($postIdParameter !== null) {
320 $postIds = is_array($postIdParameter) === false ? [$postIdParameter] : $postIdParameter;
321
322 foreach ($postIds as $postId) {
323 $this->dieOnNoAccess(ObjectHandler::GENERAL_POST_OBJECT_TYPE, $postId);
324 }
325 }
326
327 $tagId = $this->getRequestParameter('tag_ID');
328
329 if ($tagId !== null) {
330 $this->dieOnNoAccess(ObjectHandler::GENERAL_TERM_OBJECT_TYPE, $tagId);
331 }
332 }
333
334 /**
335 * Returns the user groups by reference which should be add and removed from the object.
336 *
337 * @param string $objectType
338 * @param string $objectId
339 * @param array|null $addUserGroups
340 * @param array $removeUserGroups
341 */
342 private function getAddRemoveGroups($objectType, $objectId, &$addUserGroups, &$removeUserGroups)
343 {
344 if ($addUserGroups === null) {
345 $addUserGroups = (array)$this->getRequestParameter(self::DEFAULT_GROUPS_FORM_NAME, []);
346 }
347
348 $filteredUserGroupsForObject = $this->userGroupHandler->getFilteredUserGroupsForObject(
349 $objectType,
350 $objectId
351 );
352 $removeUserGroups = array_flip(array_keys($filteredUserGroupsForObject));
353 $bulkType = $this->getRequestParameter('uam_bulk_type');
354
355 if ($bulkType === self::BULK_REMOVE) {
356 $removeUserGroups = $addUserGroups;
357 $addUserGroups = [];
358 }
359 }
360
361 /**
362 * Saves the object data to the database.
363 *
364 * @param string $objectType The object type.
365 * @param string $objectId The id of the object.
366 * @param array $addUserGroups The new user groups for the object.
367 * @param bool $force If true we force the assignment.
368 */
369 public function saveObjectData($objectType, $objectId, array $addUserGroups = null, $force = false)
370 {
371 $isUpdateForm = (bool)$this->getRequestParameter(self::UPDATE_GROUPS_FORM_NAME, false) === true
372 || $this->getRequestParameter('uam_bulk_type') !== null;
373
374 $hasRights = $this->checkUserAccess() === true || $this->mainConfig->authorsCanAddPostsToGroups() === true;
375
376 if ($isUpdateForm === true && $hasRights === true || $force === true) {
377 $this->getAddRemoveGroups($objectType, $objectId, $addUserGroups, $removeUserGroups);
378
379 try {
380 $this->userGroupAssignmentHandler->assignObjectToUserGroups(
381 $objectType,
382 $objectId,
383 $addUserGroups,
384 $removeUserGroups,
385 $this->getRequestParameter(self::DEFAULT_DYNAMIC_GROUPS_FORM_NAME, [])
386 );
387 } catch (UserGroupAssignmentException $exception) {
388 $this->addErrorMessage(sprintf(TXT_UAM_ERROR, $exception->getMessage()));
389 }
390 }
391 }
392
393 /**
394 * Removes the object data.
395 *
396 * @param string $objectType The object type.
397 * @param int $id The object id.
398 */
399 public function removeObjectData($objectType, $id)
400 {
401 $this->database->delete(
402 $this->database->getUserGroupToObjectTable(),
403 [
404 'object_id' => $id,
405 'object_type' => $objectType,
406 ],
407 [
408 '%d',
409 '%s'
410 ]
411 );
412 }
413
414 /**
415 * Returns the group selection form for pluggable objects.
416 *
417 * @param string $objectType The object type.
418 * @param string $objectId The id of the object.
419 * @param string $formName The formName.
420 * @param array $objectUserGroups If set we force this user groups for the object.
421 *
422 * @return string
423 */
424 public function showGroupSelectionForm(
425 $objectType,
426 $objectId,
427 $formName = null,
428 array $objectUserGroups = null
429 ) {
430 $this->setObjectInformation($objectType, $objectId, $objectUserGroups);
431
432 $this->groupsFromName = $formName;
433 $formContent = $this->getIncludeContents('GroupSelectionForm.php');
434 $this->groupsFromName = null;
435
436 return $formContent;
437 }
438
439 /**
440 * Returns the column for a pluggable object.
441 *
442 * @param string $objectType The object type.
443 * @param string $objectId The object id.
444 *
445 * @return string
446 */
447 public function getGroupColumn($objectType, $objectId)
448 {
449 $this->setObjectInformation($objectType, $objectId);
450 return $this->getIncludeContents('ObjectColumn.php');
451 }
452
453 /**
454 * Checks if the current object is a new object.
455 *
456 * @return bool
457 */
458 public function isNewObject()
459 {
460 $objectType = $this->objectInformation->getObjectType();
461
462 if ($objectType !== null) {
463 $generalObjectType = $this->objectHandler->getGeneralObjectType($objectType);
464
465 return ($this->objectInformation->getObjectId() === null
466 || ($generalObjectType === ObjectHandler::GENERAL_POST_OBJECT_TYPE &&
467 $this->getRequestParameter('action') !== 'edit')
468 );
469 }
470
471 return false;
472 }
473 }
474