Skip to content

Commit 042c998

Browse files
committed
IBX-6773: Bookmarks for non-accessible contents cause exception
1 parent 3151472 commit 042c998

File tree

9 files changed

+146
-9
lines changed

9 files changed

+146
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) Ibexa AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace eZ\Publish\API\Repository\Values\Content\Query\Criterion;
10+
11+
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
12+
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\Operator\Specifications;
13+
use eZ\Publish\SPI\Repository\Values\Filter\FilteringCriterion;
14+
15+
/**
16+
* A criterion that matches content based on its parent location id.
17+
*
18+
* Own location id is done using {@see LocationId}
19+
*
20+
* Supported operators:
21+
* - IN: matches against a list of location ids
22+
* - EQ: matches against a unique location id
23+
*/
24+
class Bookmark extends Criterion implements FilteringCriterion
25+
{
26+
/**
27+
* Creates a new ParentLocationId criterion.
28+
*
29+
* @param int|int[] $value One or more locationId parent locations must be matched against
30+
*
31+
* @throws \InvalidArgumentException if a non numeric id is given
32+
* @throws \InvalidArgumentException if the value type doesn't match the operator
33+
*/
34+
public function __construct($value)
35+
{
36+
parent::__construct(null, null, $value);
37+
}
38+
39+
public function getSpecifications(): array
40+
{
41+
return [
42+
new Specifications(Operator::EQ, Specifications::FORMAT_SINGLE, Specifications::TYPE_INTEGER),
43+
];
44+
}
45+
}

eZ/Publish/Core/Persistence/Cache/BookmarkHandler.php

+4
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ function (Bookmark $bookmark) {
8585
}
8686

8787
/**
88+
* @deprecated Please use LocationService::find() and Criterion\Bookmark instead.
89+
*
8890
* {@inheritdoc}
8991
*/
9092
public function loadUserBookmarks(int $userId, int $offset = 0, int $limit = -1): array
@@ -99,6 +101,8 @@ public function loadUserBookmarks(int $userId, int $offset = 0, int $limit = -1)
99101
}
100102

101103
/**
104+
* @deprecated Please use LocationService::count() and Criterion\Bookmark instead.
105+
*
102106
* {@inheritdoc}
103107
*/
104108
public function countUserBookmarks(int $userId): int

eZ/Publish/Core/Persistence/Legacy/Bookmark/Gateway.php

+4
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ abstract public function loadBookmarkDataByUserIdAndLocationId(int $userId, arra
4444
/**
4545
* Load data for all bookmarks owned by given $userId.
4646
*
47+
* @deprecated Please use LocationService::find() and Criterion\Bookmark instead.
48+
*
4749
* @param int $userId ID of user
4850
* @param int $offset Offset to start listing from, 0 by default
4951
* @param int $limit Limit for the listing. -1 by default (no limit)
@@ -55,6 +57,8 @@ abstract public function loadUserBookmarks(int $userId, int $offset = 0, int $li
5557
/**
5658
* Count bookmarks owned by given $userId.
5759
*
60+
* @deprecated Please use LocationService::count() and Criterion\Bookmark instead.
61+
*
5862
* @param int $userId ID of user
5963
*
6064
* @return int

eZ/Publish/Core/Persistence/Legacy/Bookmark/Gateway/DoctrineDatabase.php

+4
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ public function loadBookmarkDataByUserIdAndLocationId(int $userId, array $locati
101101
}
102102

103103
/**
104+
* @deprecated Please use LocationService::find() and Criterion\Bookmark instead.
105+
*
104106
* {@inheritdoc}
105107
*/
106108
public function loadUserBookmarks(int $userId, int $offset = 0, int $limit = -1): array
@@ -123,6 +125,8 @@ public function loadUserBookmarks(int $userId, int $offset = 0, int $limit = -1)
123125
}
124126

125127
/**
128+
* @deprecated Please use LocationService::count() and Criterion\Bookmark instead.
129+
*
126130
* {@inheritdoc}
127131
*/
128132
public function countUserBookmarks(int $userId): int

eZ/Publish/Core/Persistence/Legacy/Bookmark/Gateway/ExceptionConversion.php

+16
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ public function loadBookmarkDataByUserIdAndLocationId(int $userId, array $locati
5656
}
5757
}
5858

59+
/**
60+
* @deprecated Please use LocationService::find() and Criterion\Bookmark instead.
61+
*
62+
* @param int $userId
63+
* @param int $offset
64+
* @param int $limit
65+
*
66+
* @return array
67+
*/
5968
public function loadUserBookmarks(int $userId, int $offset = 0, int $limit = -1): array
6069
{
6170
try {
@@ -65,6 +74,13 @@ public function loadUserBookmarks(int $userId, int $offset = 0, int $limit = -1)
6574
}
6675
}
6776

77+
/**
78+
* @deprecated Please use LocationService::count() and Criterion\Bookmark instead.
79+
*
80+
* @param int $userId
81+
*
82+
* @return int
83+
*/
6884
public function countUserBookmarks(int $userId): int
6985
{
7086
try {

eZ/Publish/Core/Persistence/Legacy/Bookmark/Handler.php

+4
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ public function loadByUserIdAndLocationId(int $userId, array $locationIds): arra
7474
}
7575

7676
/**
77+
* @deprecated Please use LocationService::find() and Criterion\Bookmark instead.
78+
*
7779
* {@inheritdoc}
7880
*/
7981
public function loadUserBookmarks(int $userId, int $offset = 0, int $limit = -1): array
@@ -84,6 +86,8 @@ public function loadUserBookmarks(int $userId, int $offset = 0, int $limit = -1)
8486
}
8587

8688
/**
89+
* @deprecated Please use LocationService::count() and Criterion\Bookmark instead.
90+
*
8791
* {@inheritdoc}
8892
*/
8993
public function countUserBookmarks(int $userId): int
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) Ibexa AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace eZ\Publish\Core\Persistence\Legacy\Filter\CriterionQueryBuilder\Location;
10+
11+
use Doctrine\DBAL\ParameterType;
12+
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\Bookmark;
13+
use eZ\Publish\Core\Persistence\Legacy\Bookmark\Gateway\DoctrineDatabase;
14+
use eZ\Publish\SPI\Persistence\Filter\Doctrine\FilteringQueryBuilder;
15+
use eZ\Publish\SPI\Repository\Values\Filter\FilteringCriterion;
16+
17+
/**
18+
* @internal for internal use by Repository Filtering
19+
*/
20+
final class BookmarkQueryBuilder extends BaseLocationCriterionQueryBuilder
21+
{
22+
public function accepts(FilteringCriterion $criterion): bool
23+
{
24+
return $criterion instanceof Bookmark;
25+
}
26+
27+
public function buildQueryConstraint(
28+
FilteringQueryBuilder $queryBuilder,
29+
FilteringCriterion $criterion
30+
): ?string {
31+
$queryBuilder
32+
->joinOnce(
33+
'location',
34+
DoctrineDatabase::TABLE_BOOKMARKS,
35+
'bookmark',
36+
'location.node_id = bookmark.node_id'
37+
);
38+
39+
return $queryBuilder->expr()->eq(
40+
'bookmark.user_id',
41+
$queryBuilder->createNamedParameter(
42+
(int)$criterion->value[0],
43+
ParameterType::INTEGER
44+
)
45+
);
46+
}
47+
}

eZ/Publish/Core/Repository/BookmarkService.php

+18-9
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@
1313
use eZ\Publish\API\Repository\Repository as RepositoryInterface;
1414
use eZ\Publish\API\Repository\Values\Bookmark\BookmarkList;
1515
use eZ\Publish\API\Repository\Values\Content\Location;
16+
use eZ\Publish\API\Repository\Values\Content\Query;
17+
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
18+
use eZ\Publish\API\Repository\Values\Content\Query\SortClause;
19+
use eZ\Publish\API\Repository\Values\Filter\Filter;
1620
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
17-
use eZ\Publish\SPI\Persistence\Bookmark\Bookmark;
1821
use eZ\Publish\SPI\Persistence\Bookmark\CreateStruct;
1922
use eZ\Publish\SPI\Persistence\Bookmark\Handler as BookmarkHandler;
2023

@@ -97,16 +100,22 @@ public function loadBookmarks(int $offset = 0, int $limit = 25): BookmarkList
97100
{
98101
$currentUserId = $this->getCurrentUserId();
99102

100-
$list = new BookmarkList();
101-
$list->totalCount = $this->bookmarkHandler->countUserBookmarks($currentUserId);
102-
if ($list->totalCount > 0) {
103-
$bookmarks = $this->bookmarkHandler->loadUserBookmarks($currentUserId, $offset, $limit);
104-
105-
$list->items = array_map(function (Bookmark $bookmark) {
106-
return $this->repository->getLocationService()->loadLocation($bookmark->locationId);
107-
}, $bookmarks);
103+
$filter = new Filter();
104+
try {
105+
$filter
106+
->withCriterion(new Criterion\Bookmark($currentUserId))
107+
->withSortClause(new SortClause\DatePublished(Query::SORT_ASC))
108+
->sliceBy($limit, $offset);
109+
110+
$result = $this->repository->getlocationService()->find($filter, []);
111+
} catch (\eZ\Publish\API\Repository\Exceptions\BadStateException $e) {
112+
return new BookmarkList();
108113
}
109114

115+
$list = new BookmarkList();
116+
$list->totalCount = $result->totalCount;
117+
$list->items = $result->locations;
118+
110119
return $list;
111120
}
112121

eZ/Publish/SPI/Persistence/Bookmark/Handler.php

+4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public function loadByUserIdAndLocationId(int $userId, array $locationIds): arra
4141
/**
4242
* Loads bookmarks owned by user.
4343
*
44+
* @deprecated Please use LocationService::find() and Criterion\Bookmark instead.
45+
*
4446
* @param int $userId
4547
* @param int $offset the start offset for paging
4648
* @param int $limit the number of bookmarked locations returned
@@ -52,6 +54,8 @@ public function loadUserBookmarks(int $userId, int $offset = 0, int $limit = -1)
5254
/**
5355
* Count bookmarks owned by user.
5456
*
57+
* @deprecated Please use LocationService::count() and Criterion\Bookmark instead.
58+
*
5559
* @param int $userId
5660
*
5761
* @return int

0 commit comments

Comments
 (0)