objectInformation = $objectInformationFactory->createObjectInformation(); } /** * @throws UserGroupTypeException */ protected function setObjectInformation( string $objectType, int|string|null $objectId, ?array $objectUserGroups = null ): void { $userGroupDiff = 0; if ($objectUserGroups === null && $objectId !== null) { $objectUserGroups = $this->userGroupHandler->getFilteredUserGroupsForObject($objectType, $objectId, true); $fullObjectUserGroups = $this->userGroupHandler->getUserGroupsForObject($objectType, $objectId, true); $userGroupDiff = count((array) $fullObjectUserGroups) - count((array) $objectUserGroups); } $this->objectInformation->setObjectType($objectType) ->setObjectId($objectId) ->setObjectUserGroups($objectUserGroups ?? []) ->setUserGroupDiff($userGroupDiff); } public function getObjectInformation(): ObjectInformation { return $this->objectInformation; } public function getGroupsFormName(): string { return $this->groupsFromName ?? self::DEFAULT_GROUPS_FORM_NAME; } /** * @return AbstractUserGroup[] * @throws UserGroupTypeException */ public function getFilteredUserGroups(): array { return $this->userGroupHandler->getFilteredUserGroups(); } public function sortUserGroups(array &$userGroups): void { $notLoggedInUserGroupId = DynamicUserGroup::USER_TYPE . '|' . DynamicUserGroup::NOT_LOGGED_IN_USER_ID; uasort( $userGroups, function ( AbstractUserGroup $userGroupOne, AbstractUserGroup $userGroupTwo ) use ($notLoggedInUserGroupId) { if ($userGroupOne->getId() === $notLoggedInUserGroupId) { return 1; } if ($userGroupTwo->getId() === $notLoggedInUserGroupId) { return -1; } return strnatcasecmp($userGroupOne->getName(), $userGroupTwo->getName()); } ); } public function getDateUtil(): DateUtil { return $this->dateUtil; } public function isCurrentUserAdmin(): bool { if ($this->objectInformation->getObjectType() === ObjectHandler::GENERAL_USER_OBJECT_TYPE && $this->objectInformation->getObjectId() !== null ) { return $this->userHandler->userIsAdmin($this->objectInformation->getObjectId()); } return false; } public function getRoleNames(): array { $roles = $this->wordpress->getRoles(); return $roles->role_names; } public function checkUserAccess(): bool { return $this->userHandler->checkUserAccess(UserHandler::MANAGE_USER_GROUPS_CAPABILITY); } /** * @throws Exception */ public function getRecursiveMembership(AbstractUserGroup $userGroup): array { $recursiveMembership = []; $recursiveMembershipForObject = $userGroup->getRecursiveMembershipForObject( $this->objectInformation->getObjectType(), $this->objectInformation->getObjectId() ); /** * @var AssignmentInformation[] $assignmentInformation */ foreach ($recursiveMembershipForObject as $assignmentInformation) { foreach ($assignmentInformation as $membershipObjectId => $information) { try { $membershipHandler = $this->objectHandler->getObjectMembershipHandler($information->getType()); $typeName = $membershipHandler->getGeneralObjectType(); $recursiveMembership[$typeName][$membershipObjectId] = $membershipHandler->getObjectName( $membershipObjectId, $typeName ); } catch (MissingObjectMembershipHandlerException) { } } } return $recursiveMembership; } /** * @throws UserGroupTypeException */ private function dieOnNoAccess(string $objectType, int|string|null $objectId): void { if ($this->accessHandler->checkObjectAccess($objectType, $objectId) === false) { $this->wordpress->wpDie(TXT_UAM_NO_RIGHTS_MESSAGE, TXT_UAM_NO_RIGHTS_TITLE, ['response' => 403]); } } /** * @throws UserGroupTypeException */ public function checkRightsToEditContent(): void { $postIdParameter = $this->getRequestParameter('post', $this->getRequestParameter('attachment_id')); if ($postIdParameter !== null) { foreach ((array) $postIdParameter as $postId) { $this->dieOnNoAccess(ObjectHandler::GENERAL_POST_OBJECT_TYPE, $postId); } } $tagId = $this->getRequestParameter('tag_ID'); if ($tagId !== null) { $this->dieOnNoAccess(ObjectHandler::GENERAL_TERM_OBJECT_TYPE, $tagId); } } /** * @throws UserGroupTypeException */ private function getAddRemoveGroups( string $objectType, int|string|null $objectId, ?array &$addUserGroups = [], ?array &$removeUserGroups = [] ): void { $groupsToChange = (array) $this->getRequestParameter(self::DEFAULT_GROUPS_FORM_NAME, []); $filteredUserGroupsForObject = $this->userGroupHandler->getFilteredUserGroupsForObject( $objectType, $objectId ); $addUserGroups = $addUserGroups ?? $groupsToChange; $removeUserGroups = array_flip(array_keys($filteredUserGroupsForObject)); $bulkType = $this->getRequestParameter('uam_bulk_type'); if ($bulkType === self::BULK_ADD) { $addUserGroups = $groupsToChange; $removeUserGroups = []; } elseif ($bulkType === self::BULK_REMOVE) { $addUserGroups = []; $removeUserGroups = array_filter( $groupsToChange, function (array $group) { return isset($group['id']); } ); } } /** * @throws UserGroupTypeException */ public function saveObjectData( string $objectType, int|string|null $objectId, ?array $addUserGroups = null, bool $force = false ): void { $isUpdateForm = (bool) $this->getRequestParameter(self::UPDATE_GROUPS_FORM_NAME, false) === true || $this->getRequestParameter('uam_bulk_type') !== null; $hasRights = $this->checkUserAccess() === true || $this->mainConfig->authorsCanAddPostsToGroups() === true; if (($isUpdateForm === true && $hasRights === true) === false && $force === false) { return; } $this->getAddRemoveGroups($objectType, $objectId, $addUserGroups, $removeUserGroups); try { $this->userGroupAssignmentHandler->assignObjectToUserGroups( $objectType, $objectId, $addUserGroups, $removeUserGroups, $this->getRequestParameter(self::DEFAULT_DYNAMIC_GROUPS_FORM_NAME, []) ); } catch (UserGroupAssignmentException $exception) { $this->addErrorMessage(sprintf(TXT_UAM_ERROR, $exception->getMessage())); } } public function removeObjectData(string $objectType, int|string|null $id): void { $this->database->delete( $this->database->getUserGroupToObjectTable(), [ 'object_id' => $id, 'object_type' => $objectType, ], [ '%d', '%s' ] ); } /** * @throws UserGroupTypeException */ public function showGroupSelectionForm( string $objectType, int|string|null $objectId, ?string $formName = null, ?array $objectUserGroups = null ): string { $this->setObjectInformation($objectType, $objectId, $objectUserGroups); $this->groupsFromName = $formName; $formContent = $this->getIncludeContents('GroupSelectionForm.php'); $this->groupsFromName = null; return $formContent; } /** * @throws UserGroupTypeException */ public function getGroupColumn(string $objectType, int|string|null $objectId): string { $this->setObjectInformation($objectType, $objectId); return $this->getIncludeContents('ObjectColumn.php'); } /** * @throws Exception */ public function isNewObject(): bool { $objectType = $this->objectInformation->getObjectType(); if ($objectType === null) { return false; } $generalObjectType = $this->objectHandler->getGeneralObjectType($objectType); return $this->objectInformation->getObjectId() === null || ($generalObjectType === ObjectHandler::GENERAL_POST_OBJECT_TYPE && $this->getRequestParameter('action') !== 'edit'); } }