Skip to content

Commit

Permalink
IBX-6773: Bookmarks for non-accessible contents cause exception
Browse files Browse the repository at this point in the history
  • Loading branch information
vidarl committed May 30, 2024
1 parent 98b7b50 commit 2668b73
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace eZ\Publish\API\Repository\Values\Content\Query\Criterion;

use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\Operator\Specifications;
use eZ\Publish\SPI\Repository\Values\Filter\FilteringCriterion;

/**
* A criterion that matches locations of bookmarks for a given user id.
*
*
* Supported operators:
* - EQ: matches against a unique user id
*/
class Bookmark extends Criterion implements FilteringCriterion
{
/**
* Creates a new Bookmark criterion.
*
* @param int $value UserID for which bookmarked locations must be matched against
*
* @throws \InvalidArgumentException if a non numeric id is given
* @throws \InvalidArgumentException if the value type doesn't match the operator
*/
public function __construct($value)
{
parent::__construct(null, null, $value);
}

public function getSpecifications(): array
{
return [
new Specifications(Operator::EQ, Specifications::FORMAT_SINGLE, Specifications::TYPE_INTEGER),
];
}
}
4 changes: 4 additions & 0 deletions eZ/Publish/Core/Persistence/Cache/BookmarkHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ function (Bookmark $bookmark) {
}

/**
* @deprecated Please use LocationService::find() and Criterion\Bookmark instead.
*
* {@inheritdoc}
*/
public function loadUserBookmarks(int $userId, int $offset = 0, int $limit = -1): array
Expand All @@ -99,6 +101,8 @@ public function loadUserBookmarks(int $userId, int $offset = 0, int $limit = -1)
}

/**
* @deprecated Please use LocationService::count() and Criterion\Bookmark instead.
*
* {@inheritdoc}
*/
public function countUserBookmarks(int $userId): int
Expand Down
4 changes: 4 additions & 0 deletions eZ/Publish/Core/Persistence/Legacy/Bookmark/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ abstract public function loadBookmarkDataByUserIdAndLocationId(int $userId, arra
/**
* Load data for all bookmarks owned by given $userId.
*
* @deprecated Please use LocationService::find() and Criterion\Bookmark instead.
*
* @param int $userId ID of user
* @param int $offset Offset to start listing from, 0 by default
* @param int $limit Limit for the listing. -1 by default (no limit)
Expand All @@ -55,6 +57,8 @@ abstract public function loadUserBookmarks(int $userId, int $offset = 0, int $li
/**
* Count bookmarks owned by given $userId.
*
* @deprecated Please use LocationService::count() and Criterion\Bookmark instead.
*
* @param int $userId ID of user
*
* @return int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ public function loadBookmarkDataByUserIdAndLocationId(int $userId, array $locati
}

/**
* @deprecated Please use LocationService::find() and Criterion\Bookmark instead.
*
* {@inheritdoc}
*/
public function loadUserBookmarks(int $userId, int $offset = 0, int $limit = -1): array
Expand All @@ -123,6 +125,8 @@ public function loadUserBookmarks(int $userId, int $offset = 0, int $limit = -1)
}

/**
* @deprecated Please use LocationService::count() and Criterion\Bookmark instead.
*
* {@inheritdoc}
*/
public function countUserBookmarks(int $userId): int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ public function loadBookmarkDataByUserIdAndLocationId(int $userId, array $locati
}
}

/**
* @deprecated Please use LocationService::find() and Criterion\Bookmark instead.
*
* @param int $userId
* @param int $offset
* @param int $limit
*
* @return array
*/
public function loadUserBookmarks(int $userId, int $offset = 0, int $limit = -1): array
{
try {
Expand All @@ -65,6 +74,13 @@ public function loadUserBookmarks(int $userId, int $offset = 0, int $limit = -1)
}
}

/**
* @deprecated Please use LocationService::count() and Criterion\Bookmark instead.
*
* @param int $userId
*
* @return int
*/
public function countUserBookmarks(int $userId): int
{
try {
Expand Down
4 changes: 4 additions & 0 deletions eZ/Publish/Core/Persistence/Legacy/Bookmark/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ public function loadByUserIdAndLocationId(int $userId, array $locationIds): arra
}

/**
* @deprecated Please use LocationService::find() and Criterion\Bookmark instead.
*
* {@inheritdoc}
*/
public function loadUserBookmarks(int $userId, int $offset = 0, int $limit = -1): array
Expand All @@ -84,6 +86,8 @@ public function loadUserBookmarks(int $userId, int $offset = 0, int $limit = -1)
}

/**
* @deprecated Please use LocationService::count() and Criterion\Bookmark instead.
*
* {@inheritdoc}
*/
public function countUserBookmarks(int $userId): int
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace eZ\Publish\Core\Persistence\Legacy\Filter\CriterionQueryBuilder\Location;

use Doctrine\DBAL\ParameterType;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\Bookmark;
use eZ\Publish\Core\Persistence\Legacy\Bookmark\Gateway\DoctrineDatabase;
use eZ\Publish\SPI\Persistence\Filter\Doctrine\FilteringQueryBuilder;
use eZ\Publish\SPI\Repository\Values\Filter\FilteringCriterion;

/**
* @internal for internal use by Repository Filtering
*/
final class BookmarkQueryBuilder extends BaseLocationCriterionQueryBuilder
{
public function accepts(FilteringCriterion $criterion): bool
{
return $criterion instanceof Bookmark;
}

public function buildQueryConstraint(
FilteringQueryBuilder $queryBuilder,
FilteringCriterion $criterion
): ?string {
$queryBuilder
->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
)
);
}
}
27 changes: 18 additions & 9 deletions eZ/Publish/Core/Repository/BookmarkService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}

Expand Down
4 changes: 4 additions & 0 deletions eZ/Publish/SPI/Persistence/Bookmark/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 2668b73

Please sign in to comment.