PluginProbe
User Access Manager / 2.1.11
User Access Manager v2.1.11
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
← All changes | src/Controller/Backend/ObjectController.php +211 -91 2.3.17 → 2.1.11 View file →
@@ -1,5 +1,5 @@
1 -<?php /** @noinspection PhpCastIsUnnecessaryInspection */
1 +<?php
2 2 /**
3 3 * ObjectController.php
4 4 *
5 5 * The ObjectController class file.
@@ -11,14 +11,10 @@
11 11 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2
12 12 * @version SVN: $id$
13 13 * @link http://wordpress.org/extend/plugins/user-access-manager/
14 14 */
15 -
16 -declare(strict_types=1);
17 -
18 15 namespace UserAccessManager\Controller\Backend;
19 16
20 -use Exception;
21 17 use UserAccessManager\Access\AccessHandler;
22 18 use UserAccessManager\Config\MainConfig;
23 19 use UserAccessManager\Config\WordpressConfig;
24 20 use UserAccessManager\Controller\Controller;
@@ -24,16 +20,15 @@
24 20 use UserAccessManager\Controller\Controller;
25 21 use UserAccessManager\Database\Database;
26 22 use UserAccessManager\Object\ObjectHandler;
27 23 use UserAccessManager\ObjectMembership\MissingObjectMembershipHandlerException;
28 -use UserAccessManager\User\UserHandler;
29 24 use UserAccessManager\UserGroup\AbstractUserGroup;
30 25 use UserAccessManager\UserGroup\AssignmentInformation;
31 26 use UserAccessManager\UserGroup\DynamicUserGroup;
32 27 use UserAccessManager\UserGroup\UserGroupAssignmentException;
33 28 use UserAccessManager\UserGroup\UserGroupAssignmentHandler;
29 +use UserAccessManager\User\UserHandler;
34 30 use UserAccessManager\UserGroup\UserGroupHandler;
35 -use UserAccessManager\UserGroup\UserGroupTypeException;
36 31 use UserAccessManager\Util\DateUtil;
37 32 use UserAccessManager\Wrapper\Php;
38 33 use UserAccessManager\Wrapper\Wordpress;
39 34
@@ -43,83 +38,165 @@
43 38 * @package UserAccessManager\Controller
44 39 */
45 40 class ObjectController extends Controller
46 41 {
47 - public const COLUMN_NAME = 'uam_access';
48 - public const BULK_ADD = 'add';
49 - public const BULK_REMOVE = 'remove';
50 - public const BULK_OVERWRITE = 'overwrite';
51 - public const DEFAULT_GROUPS_FORM_NAME = 'uam_user_groups';
52 - public const DEFAULT_DYNAMIC_GROUPS_FORM_NAME = 'uam_dynamic_user_groups';
53 - public const UPDATE_GROUPS_FORM_NAME = 'uam_update_groups';
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';
54 47
55 - protected ObjectInformation $objectInformation;
48 + /**
49 + * @var MainConfig
50 + */
51 + protected $mainConfig;
56 52
57 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 + /**
58 94 * @var null|string
59 95 */
60 - protected ?string $groupsFromName = null;
96 + protected $groupsFromName = null;
61 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 + */
62 114 public function __construct(
63 115 Php $php,
64 116 Wordpress $wordpress,
65 117 WordpressConfig $wordpressConfig,
66 - protected MainConfig $mainConfig,
67 - protected Database $database,
68 - protected DateUtil $dateUtil,
69 - protected ObjectHandler $objectHandler,
70 - protected UserHandler $userHandler,
71 - protected UserGroupHandler $userGroupHandler,
72 - protected UserGroupAssignmentHandler $userGroupAssignmentHandler,
73 - protected AccessHandler $accessHandler,
118 + MainConfig $mainConfig,
119 + Database $database,
120 + DateUtil $dateUtil,
121 + ObjectHandler $objectHandler,
122 + UserHandler $userHandler,
123 + UserGroupHandler $userGroupHandler,
124 + UserGroupAssignmentHandler $userGroupAssignmentHandler,
125 + AccessHandler $accessHandler,
74 126 ObjectInformationFactory $objectInformationFactory
75 127 ) {
76 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;
77 137 $this->objectInformation = $objectInformationFactory->createObjectInformation();
78 138 }
79 139
80 140 /**
81 - * @throws UserGroupTypeException
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
82 146 */
83 - protected function setObjectInformation(
84 - string $objectType,
85 - int|string|null $objectId,
86 - ?array $objectUserGroups = null
87 - ): void {
147 + protected function setObjectInformation($objectType, $objectId, array $objectUserGroups = null)
148 + {
88 149 $userGroupDiff = 0;
89 150
90 151 if ($objectUserGroups === null && $objectId !== null) {
91 152 $objectUserGroups = $this->userGroupHandler->getFilteredUserGroupsForObject($objectType, $objectId, true);
92 153 $fullObjectUserGroups = $this->userGroupHandler->getUserGroupsForObject($objectType, $objectId, true);
93 - $userGroupDiff = count((array) $fullObjectUserGroups) - count((array) $objectUserGroups);
154 + $userGroupDiff = count($fullObjectUserGroups) - count($objectUserGroups);
94 155 }
95 156
96 157 $this->objectInformation->setObjectType($objectType)
97 158 ->setObjectId($objectId)
98 - ->setObjectUserGroups((array) $objectUserGroups)
159 + ->setObjectUserGroups((array)$objectUserGroups)
99 160 ->setUserGroupDiff($userGroupDiff);
100 161 }
101 162
102 - public function getObjectInformation(): ObjectInformation
163 + /**
164 + * Returns the object information.
165 + *
166 + * @return ObjectInformation
167 + */
168 + public function getObjectInformation()
103 169 {
104 170 return $this->objectInformation;
105 171 }
106 172
107 - public function getGroupsFormName(): string
173 + /**
174 + * Returns the default groups form name.
175 + *
176 + * @return string
177 + */
178 + public function getGroupsFormName()
108 179 {
109 - return ($this->groupsFromName !== null) ? (string) $this->groupsFromName : self::DEFAULT_GROUPS_FORM_NAME;
180 + return ($this->groupsFromName !== null) ? (string)$this->groupsFromName : self::DEFAULT_GROUPS_FORM_NAME;
110 181 }
111 182
112 183 /**
113 - * @return AbstractUserGroup[]
114 - * @throws UserGroupTypeException
184 + * Returns the filtered user groups.
185 + *
186 + * @return AbstractUserGroup[]
115 187 */
116 - public function getFilteredUserGroups(): array
188 + public function getFilteredUserGroups()
117 189 {
118 190 return $this->userGroupHandler->getFilteredUserGroups();
119 191 }
120 192
121 - public function sortUserGroups(array &$userGroups): void
193 + /**
194 + * Sorts the user groups.
195 + *
196 + * @param array $userGroups
197 + */
198 + public function sortUserGroups(array &$userGroups)
122 199 {
123 200 uasort(
124 201 $userGroups,
125 202 function (
@@ -125,14 +202,14 @@
125 202 function (
126 203 AbstractUserGroup $userGroupOne,
127 204 AbstractUserGroup $userGroupTwo
128 205 ) {
129 - $notLoggedInUserGroupId = DynamicUserGroup::USER_TYPE . '|' . DynamicUserGroup::NOT_LOGGED_IN_USER_ID;
206 + $notLoggedInUserGroupId = DynamicUserGroup::USER_TYPE.'|'.DynamicUserGroup::NOT_LOGGED_IN_USER_ID;
130 207
131 208 if ($userGroupOne->getId() === $notLoggedInUserGroupId) {
132 209 return 1;
133 210 } elseif ($userGroupTwo->getId() === $notLoggedInUserGroupId) {
134 - return -1;
211 + return 0;
135 212 }
136 213
137 214 return strnatcasecmp($userGroupOne->getName(), $userGroupTwo->getName());
138 215 }
@@ -138,14 +215,24 @@
138 215 }
139 216 );
140 217 }
141 218
142 - public function getDateUtil(): DateUtil
219 + /**
220 + * Returns the date util.
221 + *
222 + * @return DateUtil
223 + */
224 + public function getDateUtil()
143 225 {
144 226 return $this->dateUtil;
145 227 }
146 228
147 - public function isCurrentUserAdmin(): bool
229 + /**
230 + * Checks if the current user is an admin.
231 + *
232 + * @return bool
233 + */
234 + public function isCurrentUserAdmin()
148 235 {
149 236 if ($this->objectInformation->getObjectType() === ObjectHandler::GENERAL_USER_OBJECT_TYPE
150 237 && $this->objectInformation->getObjectId() !== null
151 238 ) {
@@ -154,23 +241,37 @@
154 241
155 242 return false;
156 243 }
157 244
158 - public function getRoleNames(): array
245 + /**
246 + * Returns the wordpress role names.
247 + *
248 + * @return array
249 + */
250 + public function getRoleNames()
159 251 {
160 252 $roles = $this->wordpress->getRoles();
161 253 return $roles->role_names;
162 254 }
163 255
164 - public function checkUserAccess(): bool
256 + /**
257 + * Checks the user access.
258 + *
259 + * @return bool
260 + */
261 + public function checkUserAccess()
165 262 {
166 263 return $this->userHandler->checkUserAccess(UserHandler::MANAGE_USER_GROUPS_CAPABILITY);
167 264 }
168 265
169 266 /**
170 - * @throws Exception
267 + * Returns the recursive object membership.
268 + *
269 + * @param $userGroup
270 + *
271 + * @return array
171 272 */
172 - public function getRecursiveMembership(AbstractUserGroup $userGroup): array
273 + public function getRecursiveMembership(AbstractUserGroup $userGroup)
173 274 {
174 275 $recursiveMembership = [];
175 276 $objectType = $this->objectInformation->getObjectType();
176 277 $objectId = $this->objectInformation->getObjectId();
@@ -178,9 +279,9 @@
178 279
179 280 /**
180 281 * @var AssignmentInformation[] $assignmentInformation
181 282 */
182 - foreach ($recursiveMembershipForObject as $assignmentInformation) {
283 + foreach ($recursiveMembershipForObject as $recursiveType => $assignmentInformation) {
183 284 foreach ($assignmentInformation as $objectId => $information) {
184 285 try {
185 286 $membershipHandler = $this->objectHandler->getObjectMembershipHandler($information->getType());
186 287 $typeName = $membershipHandler->getGeneralObjectType();
@@ -185,9 +286,10 @@
185 286 $membershipHandler = $this->objectHandler->getObjectMembershipHandler($information->getType());
186 287 $typeName = $membershipHandler->getGeneralObjectType();
187 288 $objectName = $membershipHandler->getObjectName($objectId, $typeName);
188 289 $recursiveMembership[$typeName][$objectId] = $objectName;
189 - } catch (MissingObjectMembershipHandlerException) {
290 + } catch (MissingObjectMembershipHandlerException $exception) {
291 + // Do nothing
190 292 }
191 293 }
192 294 }
193 295
@@ -194,11 +296,14 @@
194 296 return $recursiveMembership;
195 297 }
196 298
197 299 /**
198 - * @throws UserGroupTypeException
300 + * Checks the access and dies if the user has no access.
301 + *
302 + * @param string $objectType
303 + * @param string $objectId
199 304 */
200 - private function dieOnNoAccess(string $objectType, int|string|null $objectId): void
305 + private function dieOnNoAccess($objectType, $objectId)
201 306 {
202 307 if ($this->accessHandler->checkObjectAccess($objectType, $objectId) === false) {
203 308 $this->wordpress->wpDie(TXT_UAM_NO_RIGHTS_MESSAGE, TXT_UAM_NO_RIGHTS_TITLE, ['response' => 403]);
204 309 }
@@ -204,11 +309,11 @@
204 309 }
205 310 }
206 311
207 312 /**
208 - * @throws UserGroupTypeException
313 + * Shows the error if the user has no rights to edit the content.
209 314 */
210 - public function checkRightsToEditContent(): void
315 + public function checkRightsToEditContent()
211 316 {
212 317 $postIdParameter = $this->getRequestParameter('post', $this->getRequestParameter('attachment_id'));
213 318
214 319 if ($postIdParameter !== null) {
@@ -226,50 +331,45 @@
226 331 }
227 332 }
228 333
229 334 /**
230 - * @throws UserGroupTypeException
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
231 341 */
232 - private function getAddRemoveGroups(
233 - string $objectType,
234 - int|string|null $objectId,
235 - ?array &$addUserGroups = [],
236 - ?array &$removeUserGroups = []
237 - ): void {
238 - $groupsToChange = (array) $this->getRequestParameter(self::DEFAULT_GROUPS_FORM_NAME, []);
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 +
239 348 $filteredUserGroupsForObject = $this->userGroupHandler->getFilteredUserGroupsForObject(
240 349 $objectType,
241 350 $objectId
242 351 );
243 -
244 - $addUserGroups = $addUserGroups ?? $groupsToChange;
245 352 $removeUserGroups = array_flip(array_keys($filteredUserGroupsForObject));
246 353 $bulkType = $this->getRequestParameter('uam_bulk_type');
247 354
248 - if ($bulkType === self::BULK_ADD) {
249 - $addUserGroups = $groupsToChange;
250 - $removeUserGroups = [];
251 - } elseif ($bulkType === self::BULK_REMOVE) {
355 + if ($bulkType === self::BULK_REMOVE) {
356 + $removeUserGroups = $addUserGroups;
252 357 $addUserGroups = [];
253 - $removeUserGroups = array_filter(
254 - $groupsToChange,
255 - function (array $group) {
256 - return isset($group['id']);
257 - }
258 - );
259 358 }
260 359 }
261 360
262 361 /**
263 - * @throws UserGroupTypeException
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.
264 368 */
265 - public function saveObjectData(
266 - string $objectType,
267 - int|string|null $objectId,
268 - ?array $addUserGroups = null,
269 - bool $force = false
270 - ): void {
271 - $isUpdateForm = (bool) $this->getRequestParameter(self::UPDATE_GROUPS_FORM_NAME, false) === true
369 + public function saveObjectData($objectType, $objectId, array $addUserGroups = null, $force = false)
370 + {
371 + $isUpdateForm = (bool)$this->getRequestParameter(self::UPDATE_GROUPS_FORM_NAME, false) === true
272 372 || $this->getRequestParameter('uam_bulk_type') !== null;
273 373
274 374 $hasRights = $this->checkUserAccess() === true || $this->mainConfig->authorsCanAddPostsToGroups() === true;
275 375
@@ -289,9 +389,15 @@
289 389 }
290 390 }
291 391 }
292 392
293 - public function removeObjectData(string $objectType, int|string|null $id): void
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)
294 400 {
295 401 $this->database->delete(
296 402 $this->database->getUserGroupToObjectTable(),
297 403 [
@@ -305,16 +411,23 @@
305 411 );
306 412 }
307 413
308 414 /**
309 - * @throws UserGroupTypeException
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
310 423 */
311 424 public function showGroupSelectionForm(
312 - string $objectType,
313 - int|string|null $objectId,
425 + $objectType,
426 + $objectId,
314 427 $formName = null,
315 - ?array $objectUserGroups = null
316 - ): string {
428 + array $objectUserGroups = null
429 + ) {
317 430 $this->setObjectInformation($objectType, $objectId, $objectUserGroups);
318 431
319 432 $this->groupsFromName = $formName;
320 433 $formContent = $this->getIncludeContents('GroupSelectionForm.php');
@@ -323,11 +436,16 @@
323 436 return $formContent;
324 437 }
325 438
326 439 /**
327 - * @throws UserGroupTypeException
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
328 446 */
329 - public function getGroupColumn(string $objectType, int|string|null $objectId): string
447 + public function getGroupColumn($objectType, $objectId)
330 448 {
331 449 $this->setObjectInformation($objectType, $objectId);
332 450 return $this->getIncludeContents('ObjectColumn.php');
333 451 }
@@ -332,11 +450,13 @@
332 450 return $this->getIncludeContents('ObjectColumn.php');
333 451 }
334 452
335 453 /**
336 - * @throws Exception
454 + * Checks if the current object is a new object.
455 + *
456 + * @return bool
337 457 */
338 - public function isNewObject(): bool
458 + public function isNewObject()
339 459 {
340 460 $objectType = $this->objectInformation->getObjectType();
341 461
342 462 if ($objectType !== null) {