From 2668b73ba9a5c5a1c03f896dbffb1af2afa670fa Mon Sep 17 00:00:00 2001 From: Vidar Langseid Date: Fri, 24 May 2024 15:46:47 +0200 Subject: [PATCH] IBX-6773: Bookmarks for non-accessible contents cause exception --- .../Content/Query/Criterion/Bookmark.php | 43 +++++++++++++++++ .../Persistence/Cache/BookmarkHandler.php | 4 ++ .../Persistence/Legacy/Bookmark/Gateway.php | 4 ++ .../Bookmark/Gateway/DoctrineDatabase.php | 4 ++ .../Bookmark/Gateway/ExceptionConversion.php | 16 +++++++ .../Persistence/Legacy/Bookmark/Handler.php | 4 ++ .../Location/BookmarkQueryBuilder.php | 47 +++++++++++++++++++ .../Core/Repository/BookmarkService.php | 27 +++++++---- .../SPI/Persistence/Bookmark/Handler.php | 4 ++ 9 files changed, 144 insertions(+), 9 deletions(-) create mode 100644 eZ/Publish/API/Repository/Values/Content/Query/Criterion/Bookmark.php create mode 100644 eZ/Publish/Core/Persistence/Legacy/Filter/CriterionQueryBuilder/Location/BookmarkQueryBuilder.php diff --git a/eZ/Publish/API/Repository/Values/Content/Query/Criterion/Bookmark.php b/eZ/Publish/API/Repository/Values/Content/Query/Criterion/Bookmark.php new file mode 100644 index 0000000000..faf57568af --- /dev/null +++ b/eZ/Publish/API/Repository/Values/Content/Query/Criterion/Bookmark.php @@ -0,0 +1,43 @@ +joinOnce( + 'location', + DoctrineDatabase::TABLE_BOOKMARKS, + 'bookmark', + 'location.node_id = bookmark.node_id' + ); + + return $queryBuilder->expr()->eq( + 'bookmark.user_id', + $queryBuilder->createNamedParameter( + (int)$criterion->value[0], + ParameterType::INTEGER + ) + ); + } +} diff --git a/eZ/Publish/Core/Repository/BookmarkService.php b/eZ/Publish/Core/Repository/BookmarkService.php index 05d5d08cde..d720a1b86b 100644 --- a/eZ/Publish/Core/Repository/BookmarkService.php +++ b/eZ/Publish/Core/Repository/BookmarkService.php @@ -13,8 +13,11 @@ use eZ\Publish\API\Repository\Repository as RepositoryInterface; use eZ\Publish\API\Repository\Values\Bookmark\BookmarkList; use eZ\Publish\API\Repository\Values\Content\Location; +use eZ\Publish\API\Repository\Values\Content\Query; +use eZ\Publish\API\Repository\Values\Content\Query\Criterion; +use eZ\Publish\API\Repository\Values\Content\Query\SortClause; +use eZ\Publish\API\Repository\Values\Filter\Filter; use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException; -use eZ\Publish\SPI\Persistence\Bookmark\Bookmark; use eZ\Publish\SPI\Persistence\Bookmark\CreateStruct; use eZ\Publish\SPI\Persistence\Bookmark\Handler as BookmarkHandler; @@ -97,16 +100,22 @@ public function loadBookmarks(int $offset = 0, int $limit = 25): BookmarkList { $currentUserId = $this->getCurrentUserId(); - $list = new BookmarkList(); - $list->totalCount = $this->bookmarkHandler->countUserBookmarks($currentUserId); - if ($list->totalCount > 0) { - $bookmarks = $this->bookmarkHandler->loadUserBookmarks($currentUserId, $offset, $limit); - - $list->items = array_map(function (Bookmark $bookmark) { - return $this->repository->getLocationService()->loadLocation($bookmark->locationId); - }, $bookmarks); + $filter = new Filter(); + try { + $filter + ->withCriterion(new Criterion\Bookmark($currentUserId)) + ->withSortClause(new SortClause\DatePublished(Query::SORT_ASC)) + ->sliceBy($limit, $offset); + + $result = $this->repository->getlocationService()->find($filter, []); + } catch (\eZ\Publish\API\Repository\Exceptions\BadStateException $e) { + return new BookmarkList(); } + $list = new BookmarkList(); + $list->totalCount = $result->totalCount; + $list->items = $result->locations; + return $list; } diff --git a/eZ/Publish/SPI/Persistence/Bookmark/Handler.php b/eZ/Publish/SPI/Persistence/Bookmark/Handler.php index c335f40297..8a515d187c 100644 --- a/eZ/Publish/SPI/Persistence/Bookmark/Handler.php +++ b/eZ/Publish/SPI/Persistence/Bookmark/Handler.php @@ -41,6 +41,8 @@ public function loadByUserIdAndLocationId(int $userId, array $locationIds): arra /** * Loads bookmarks owned by user. * + * @deprecated Please use LocationService::find() and Criterion\Bookmark instead. + * * @param int $userId * @param int $offset the start offset for paging * @param int $limit the number of bookmarked locations returned @@ -52,6 +54,8 @@ public function loadUserBookmarks(int $userId, int $offset = 0, int $limit = -1) /** * Count bookmarks owned by user. * + * @deprecated Please use LocationService::count() and Criterion\Bookmark instead. + * * @param int $userId * * @return int