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