From a73588e725dda5c78ba87de238ee8dba2a9e1a48 Mon Sep 17 00:00:00 2001 From: Tomasz Kryszan Date: Thu, 17 Oct 2024 10:20:53 +0200 Subject: [PATCH] Added UsersWithPermissionInfoToContentItemMapper --- ...sWithPermissionInfoToContentItemMapper.php | 122 ++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 src/lib/User/Mapper/UsersWithPermissionInfoToContentItemMapper.php diff --git a/src/lib/User/Mapper/UsersWithPermissionInfoToContentItemMapper.php b/src/lib/User/Mapper/UsersWithPermissionInfoToContentItemMapper.php new file mode 100644 index 0000000000..018b93b092 --- /dev/null +++ b/src/lib/User/Mapper/UsersWithPermissionInfoToContentItemMapper.php @@ -0,0 +1,122 @@ +userService = $userService; + $this->permissionResolver = $permissionResolver; + } + + /** + * @param array<\Ibexa\Contracts\Core\Repository\Values\ValueObject> $targets + * + * @phpstan-return TUserData + * + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\BadStateException + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException + */ + public function mapSearchResults( + SearchResult $searchResult, + ContentInfo $contentInfo, + string $module, + string $function, + array $targets = [] + ): array { + $currentUserReference = $this->permissionResolver->getCurrentUserReference(); + + $results = $this->groupByPermissions($searchResult, $contentInfo, $module, $function, $targets); + + $this->permissionResolver->setCurrentUserReference($currentUserReference); + + return $results; + } + + /** + * @param array<\Ibexa\Contracts\Core\Repository\Values\ValueObject> $targets + * + * @phpstan-return TUserData + * + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\BadStateException + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException + */ + private function groupByPermissions( + SearchResult $searchResult, + ContentInfo $contentInfo, + string $module, + string $function, + array $targets = [] + ): array { + $results = [ + 'access' => [], + 'no_access' => [], + ]; + + foreach ($searchResult as $result) { + /** @var \Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo $userContentInfo */ + $userContentInfo = $result->valueObject; + + $user = $this->userService->loadUser($userContentInfo->getId()); + $userReference = new UserReference($user->getUserId()); + $userData = $this->getUserData($user); + + $this->permissionResolver->setCurrentUserReference($userReference); + + if ($this->permissionResolver->canUser($module, $function, $contentInfo, $targets)) { + $results['access'][] = $userData; + } else { + $results['no_access'][] = $userData; + } + } + + return $results; + } + + /** + * @return array{ + * name: string, + * email: string, + * } + */ + private function getUserData(User $user): array + { + return [ + 'name' => $user->getName() ?? $user->getLogin(), + 'email' => $user->email, + ]; + } +}