PluginProbe
User Access Manager / 2.2.3
User Access Manager v2.2.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
← All changes | src/Controller/Backend/ObjectController.php +173 -54 2.3.162.2.3 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.
@@ -43,49 +43,114 @@
43 43 * @package UserAccessManager\Controller
44 44 */
45 45 class ObjectController extends Controller
46 46 {
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';
47 + const COLUMN_NAME = 'uam_access';
48 + const BULK_REMOVE = 'remove';
49 + const DEFAULT_GROUPS_FORM_NAME = 'uam_user_groups';
50 + const DEFAULT_DYNAMIC_GROUPS_FORM_NAME = 'uam_dynamic_user_groups';
51 + const UPDATE_GROUPS_FORM_NAME = 'uam_update_groups';
54 52
55 - protected ObjectInformation $objectInformation;
53 + /**
54 + * @var MainConfig
55 + */
56 + protected $mainConfig;
56 57
57 58 /**
59 + * @var Database
60 + */
61 + protected $database;
62 +
63 + /**
64 + * @var DateUtil
65 + */
66 + protected $dateUtil;
67 +
68 + /**
69 + * @var ObjectHandler
70 + */
71 + protected $objectHandler;
72 +
73 + /**
74 + * @var UserHandler
75 + */
76 + protected $userHandler;
77 +
78 + /**
79 + * @var UserGroupHandler
80 + */
81 + protected $userGroupHandler;
82 +
83 + /**
84 + * @var AccessHandler
85 + */
86 + protected $accessHandler;
87 +
88 + /**
89 + * @var UserGroupAssignmentHandler
90 + */
91 + protected $userGroupAssignmentHandler;
92 +
93 + /**
94 + * @var ObjectInformation
95 + */
96 + protected $objectInformation;
97 +
98 + /**
58 99 * @var null|string
59 100 */
60 - protected ?string $groupsFromName = null;
101 + protected $groupsFromName = null;
61 102
103 + /**
104 + * ObjectController constructor.
105 + * @param Php $php
106 + * @param Wordpress $wordpress
107 + * @param WordpressConfig $wordpressConfig
108 + * @param MainConfig $mainConfig
109 + * @param Database $database
110 + * @param DateUtil $dateUtil
111 + * @param ObjectHandler $objectHandler
112 + * @param UserHandler $userHandler
113 + * @param UserGroupHandler $userGroupHandler
114 + * @param UserGroupAssignmentHandler $userGroupAssignmentHandler
115 + * @param AccessHandler $accessHandler
116 + * @param ObjectInformationFactory $objectInformationFactory
117 + */
62 118 public function __construct(
63 119 Php $php,
64 120 Wordpress $wordpress,
65 121 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,
122 + MainConfig $mainConfig,
123 + Database $database,
124 + DateUtil $dateUtil,
125 + ObjectHandler $objectHandler,
126 + UserHandler $userHandler,
127 + UserGroupHandler $userGroupHandler,
128 + UserGroupAssignmentHandler $userGroupAssignmentHandler,
129 + AccessHandler $accessHandler,
74 130 ObjectInformationFactory $objectInformationFactory
75 131 ) {
76 132 parent::__construct($php, $wordpress, $wordpressConfig);
133 + $this->mainConfig = $mainConfig;
134 + $this->database = $database;
135 + $this->dateUtil = $dateUtil;
136 + $this->objectHandler = $objectHandler;
137 + $this->userHandler = $userHandler;
138 + $this->userGroupHandler = $userGroupHandler;
139 + $this->userGroupAssignmentHandler = $userGroupAssignmentHandler;
140 + $this->accessHandler = $accessHandler;
77 141 $this->objectInformation = $objectInformationFactory->createObjectInformation();
78 142 }
79 143
80 144 /**
145 + * Sets the current object type, the object id and the user groups.
146 + * @param string $objectType
147 + * @param int|string|null $objectId
148 + * @param array|null $objectUserGroups
81 149 * @throws UserGroupTypeException
82 150 */
83 - protected function setObjectInformation(
84 - string $objectType,
85 - int|string|null $objectId,
86 - ?array $objectUserGroups = null
87 - ): void {
151 + protected function setObjectInformation(string $objectType, $objectId, ?array $objectUserGroups = null)
152 + {
88 153 $userGroupDiff = 0;
89 154
90 155 if ($objectUserGroups === null && $objectId !== null) {
91 156 $objectUserGroups = $this->userGroupHandler->getFilteredUserGroupsForObject($objectType, $objectId, true);
@@ -98,13 +163,21 @@
98 163 ->setObjectUserGroups((array) $objectUserGroups)
99 164 ->setUserGroupDiff($userGroupDiff);
100 165 }
101 166
167 + /**
168 + * Returns the object information.
169 + * @return ObjectInformation
170 + */
102 171 public function getObjectInformation(): ObjectInformation
103 172 {
104 173 return $this->objectInformation;
105 174 }
106 175
176 + /**
177 + * Returns the default groups form name.
178 + * @return string
179 + */
107 180 public function getGroupsFormName(): string
108 181 {
109 182 return ($this->groupsFromName !== null) ? (string) $this->groupsFromName : self::DEFAULT_GROUPS_FORM_NAME;
110 183 }
@@ -109,9 +182,10 @@
109 182 return ($this->groupsFromName !== null) ? (string) $this->groupsFromName : self::DEFAULT_GROUPS_FORM_NAME;
110 183 }
111 184
112 185 /**
113 - * @return AbstractUserGroup[]
186 + * Returns the filtered user groups.
187 + * @return AbstractUserGroup[]
114 188 * @throws UserGroupTypeException
115 189 */
116 190 public function getFilteredUserGroups(): array
117 191 {
@@ -117,9 +191,13 @@
117 191 {
118 192 return $this->userGroupHandler->getFilteredUserGroups();
119 193 }
120 194
121 - public function sortUserGroups(array &$userGroups): void
195 + /**
196 + * Sorts the user groups.
197 + * @param array $userGroups
198 + */
199 + public function sortUserGroups(array &$userGroups)
122 200 {
123 201 uasort(
124 202 $userGroups,
125 203 function (
@@ -138,13 +216,21 @@
138 216 }
139 217 );
140 218 }
141 219
220 + /**
221 + * Returns the date util.
222 + * @return DateUtil
223 + */
142 224 public function getDateUtil(): DateUtil
143 225 {
144 226 return $this->dateUtil;
145 227 }
146 228
229 + /**
230 + * Checks if the current user is an admin.
231 + * @return bool
232 + */
147 233 public function isCurrentUserAdmin(): bool
148 234 {
149 235 if ($this->objectInformation->getObjectType() === ObjectHandler::GENERAL_USER_OBJECT_TYPE
150 236 && $this->objectInformation->getObjectId() !== null
@@ -154,8 +240,12 @@
154 240
155 241 return false;
156 242 }
157 243
244 + /**
245 + * Returns the wordpress role names.
246 + * @return array
247 + */
158 248 public function getRoleNames(): array
159 249 {
160 250 $roles = $this->wordpress->getRoles();
161 251 return $roles->role_names;
@@ -160,8 +250,12 @@
160 250 $roles = $this->wordpress->getRoles();
161 251 return $roles->role_names;
162 252 }
163 253
254 + /**
255 + * Checks the user access.
256 + * @return bool
257 + */
164 258 public function checkUserAccess(): bool
165 259 {
166 260 return $this->userHandler->checkUserAccess(UserHandler::MANAGE_USER_GROUPS_CAPABILITY);
167 261 }
@@ -166,9 +260,13 @@
166 260 return $this->userHandler->checkUserAccess(UserHandler::MANAGE_USER_GROUPS_CAPABILITY);
167 261 }
168 262
169 263 /**
264 + * Returns the recursive object membership.
265 + * @param AbstractUserGroup $userGroup
266 + * @return array
170 267 * @throws Exception
268 + * @throws Exception
171 269 */
172 270 public function getRecursiveMembership(AbstractUserGroup $userGroup): array
173 271 {
174 272 $recursiveMembership = [];
@@ -178,9 +276,9 @@
178 276
179 277 /**
180 278 * @var AssignmentInformation[] $assignmentInformation
181 279 */
182 - foreach ($recursiveMembershipForObject as $assignmentInformation) {
280 + foreach ($recursiveMembershipForObject as $recursiveType => $assignmentInformation) {
183 281 foreach ($assignmentInformation as $objectId => $information) {
184 282 try {
185 283 $membershipHandler = $this->objectHandler->getObjectMembershipHandler($information->getType());
186 284 $typeName = $membershipHandler->getGeneralObjectType();
@@ -185,9 +283,10 @@
185 283 $membershipHandler = $this->objectHandler->getObjectMembershipHandler($information->getType());
186 284 $typeName = $membershipHandler->getGeneralObjectType();
187 285 $objectName = $membershipHandler->getObjectName($objectId, $typeName);
188 286 $recursiveMembership[$typeName][$objectId] = $objectName;
189 - } catch (MissingObjectMembershipHandlerException) {
287 + } catch (MissingObjectMembershipHandlerException $exception) {
288 + // Do nothing
190 289 }
191 290 }
192 291 }
193 292
@@ -194,11 +293,14 @@
194 293 return $recursiveMembership;
195 294 }
196 295
197 296 /**
297 + * Checks the access and dies if the user has no access.
298 + * @param string $objectType
299 + * @param int|string $objectId
198 300 * @throws UserGroupTypeException
199 301 */
200 - private function dieOnNoAccess(string $objectType, int|string|null $objectId): void
302 + private function dieOnNoAccess(string $objectType, $objectId)
201 303 {
202 304 if ($this->accessHandler->checkObjectAccess($objectType, $objectId) === false) {
203 305 $this->wordpress->wpDie(TXT_UAM_NO_RIGHTS_MESSAGE, TXT_UAM_NO_RIGHTS_TITLE, ['response' => 403]);
204 306 }
@@ -204,11 +306,12 @@
204 306 }
205 307 }
206 308
207 309 /**
310 + * Shows the error if the user has no rights to edit the content.
208 311 * @throws UserGroupTypeException
209 312 */
210 - public function checkRightsToEditContent(): void
313 + public function checkRightsToEditContent()
211 314 {
212 315 $postIdParameter = $this->getRequestParameter('post', $this->getRequestParameter('attachment_id'));
213 316
214 317 if ($postIdParameter !== null) {
@@ -226,49 +329,48 @@
226 329 }
227 330 }
228 331
229 332 /**
333 + * Returns the user groups by reference which should be add and removed from the object.
334 + * @param string $objectType
335 + * @param int|string $objectId
336 + * @param array|null $addUserGroups
337 + * @param array|null $removeUserGroups
230 338 * @throws UserGroupTypeException
231 339 */
232 340 private function getAddRemoveGroups(
233 341 string $objectType,
234 - int|string|null $objectId,
342 + $objectId,
235 343 ?array &$addUserGroups = [],
236 344 ?array &$removeUserGroups = []
237 - ): void {
238 - $groupsToChange = (array) $this->getRequestParameter(self::DEFAULT_GROUPS_FORM_NAME, []);
345 + ) {
346 + if ($addUserGroups === null) {
347 + $addUserGroups = (array) $this->getRequestParameter(self::DEFAULT_GROUPS_FORM_NAME, []);
348 + }
349 +
239 350 $filteredUserGroupsForObject = $this->userGroupHandler->getFilteredUserGroupsForObject(
240 351 $objectType,
241 352 $objectId
242 353 );
243 -
244 - $addUserGroups = $addUserGroups ?? $groupsToChange;
245 354 $removeUserGroups = array_flip(array_keys($filteredUserGroupsForObject));
246 355 $bulkType = $this->getRequestParameter('uam_bulk_type');
247 356
248 - if ($bulkType === self::BULK_ADD) {
249 - $addUserGroups = $groupsToChange;
250 - $removeUserGroups = [];
251 - } elseif ($bulkType === self::BULK_REMOVE) {
357 + if ($bulkType === self::BULK_REMOVE) {
358 + $removeUserGroups = $addUserGroups;
252 359 $addUserGroups = [];
253 - $removeUserGroups = array_filter(
254 - $groupsToChange,
255 - function (array $group) {
256 - return isset($group['id']);
257 - }
258 - );
259 360 }
260 361 }
261 362
262 363 /**
364 + * Saves the object data to the database.
365 + * @param string $objectType The object type.
366 + * @param int|string|null $objectId The id of the object.
367 + * @param array|null $addUserGroups The new user groups for the object.
368 + * @param bool $force If true we force the assignment.
263 369 * @throws UserGroupTypeException
264 370 */
265 - public function saveObjectData(
266 - string $objectType,
267 - int|string|null $objectId,
268 - ?array $addUserGroups = null,
269 - bool $force = false
270 - ): void {
371 + public function saveObjectData(string $objectType, $objectId, ?array $addUserGroups = null, $force = false)
372 + {
271 373 $isUpdateForm = (bool) $this->getRequestParameter(self::UPDATE_GROUPS_FORM_NAME, false) === true
272 374 || $this->getRequestParameter('uam_bulk_type') !== null;
273 375
274 376 $hasRights = $this->checkUserAccess() === true || $this->mainConfig->authorsCanAddPostsToGroups() === true;
@@ -289,9 +391,14 @@
289 391 }
290 392 }
291 393 }
292 394
293 - public function removeObjectData(string $objectType, int|string|null $id): void
395 + /**
396 + * Removes the object data.
397 + * @param string $objectType The object type.
398 + * @param int|string $id The object id.
399 + */
400 + public function removeObjectData(string $objectType, $id)
294 401 {
295 402 $this->database->delete(
296 403 $this->database->getUserGroupToObjectTable(),
297 404 [
@@ -305,15 +412,21 @@
305 412 );
306 413 }
307 414
308 415 /**
416 + * Returns the group selection form for pluggable objects.
417 + * @param string $objectType The object type.
418 + * @param int|string $objectId The id of the object.
419 + * @param null $formName The formName.
420 + * @param array|null $objectUserGroups If set we force this user groups for the object.
421 + * @return string
309 422 * @throws UserGroupTypeException
310 423 */
311 424 public function showGroupSelectionForm(
312 425 string $objectType,
313 - int|string|null $objectId,
426 + $objectId,
314 427 $formName = null,
315 - ?array $objectUserGroups = null
428 + array $objectUserGroups = null
316 429 ): string {
317 430 $this->setObjectInformation($objectType, $objectId, $objectUserGroups);
318 431
319 432 $this->groupsFromName = $formName;
@@ -323,11 +436,15 @@
323 436 return $formContent;
324 437 }
325 438
326 439 /**
440 + * Returns the column for a pluggable object.
441 + * @param string $objectType The object type.
442 + * @param int|string $objectId The object id.
443 + * @return string
327 444 * @throws UserGroupTypeException
328 445 */
329 - public function getGroupColumn(string $objectType, int|string|null $objectId): string
446 + public function getGroupColumn(string $objectType, $objectId): string
330 447 {
331 448 $this->setObjectInformation($objectType, $objectId);
332 449 return $this->getIncludeContents('ObjectColumn.php');
333 450 }
@@ -332,8 +449,10 @@
332 449 return $this->getIncludeContents('ObjectColumn.php');
333 450 }
334 451
335 452 /**
453 + * Checks if the current object is a new object.
454 + * @return bool
336 455 * @throws Exception
337 456 */
338 457 public function isNewObject(): bool
339 458 {