From fc5337190391bd2dffa26b814c98a8958fd0bcec Mon Sep 17 00:00:00 2001 From: Tomasz Kryszan Date: Mon, 9 Dec 2024 10:13:46 +0100 Subject: [PATCH] Fixed UsersWithPermissionInfoMapper --- .../Mapper/UsersWithPermissionInfoMapper.php | 34 +++++++++++++------ 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/src/lib/Permission/Mapper/UsersWithPermissionInfoMapper.php b/src/lib/Permission/Mapper/UsersWithPermissionInfoMapper.php index c80fb27387..0a117ce4a8 100644 --- a/src/lib/Permission/Mapper/UsersWithPermissionInfoMapper.php +++ b/src/lib/Permission/Mapper/UsersWithPermissionInfoMapper.php @@ -10,6 +10,7 @@ use Ibexa\Contracts\AdminUi\Values\PermissionCheckContext; use Ibexa\Contracts\Core\Repository\PermissionResolver; +use Ibexa\Contracts\Core\Repository\Repository; use Ibexa\Contracts\Core\Repository\UserService; use Ibexa\Contracts\Core\Repository\Values\Content\Search\SearchResult; use Ibexa\Contracts\Core\Repository\Values\User\User; @@ -30,14 +31,18 @@ final class UsersWithPermissionInfoMapper { private PermissionResolver $permissionResolver; + private Repository $repository; + private UserService $userService; public function __construct( - UserService $userService, - PermissionResolver $permissionResolver + PermissionResolver $permissionResolver, + Repository $repository, + UserService $userService ) { - $this->userService = $userService; $this->permissionResolver = $permissionResolver; + $this->repository = $repository; + $this->userService = $userService; } /** @@ -55,11 +60,11 @@ public function mapSearchResults( ): array { $currentUserReference = $this->permissionResolver->getCurrentUserReference(); - $results = $this->groupByPermissions($searchResult, $permissionContext, $module, $function); - - $this->permissionResolver->setCurrentUserReference($currentUserReference); - - return $results; + try { + return $this->groupByPermissions($searchResult, $permissionContext, $module, $function); + } finally { + $this->permissionResolver->setCurrentUserReference($currentUserReference); + } } /** @@ -83,8 +88,7 @@ private function groupByPermissions( foreach ($searchResult as $result) { /** @var \Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo $userContentInfo */ $userContentInfo = $result->valueObject; - $user = $this->userService->loadUser($userContentInfo->getId()); - + $user = $this->loadUser($userContentInfo->getId()); $userReference = new UserReference($user->getUserId()); $this->permissionResolver->setCurrentUserReference($userReference); @@ -102,6 +106,16 @@ private function groupByPermissions( return $results; } + /** + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException + */ + private function loadUser(int $userId): User + { + return $this->repository->sudo( + fn (): User => $this->userService->loadUser($userId) + ); + } + /** * @phpstan-return TUserData */