Skip to content

Commit b2b1682

Browse files
committed
Fix author filtering for search entries
1 parent aa96198 commit b2b1682

2 files changed

Lines changed: 40 additions & 4 deletions

File tree

src/Http/Controllers/CP/Collections/QueriesAuthorEntries.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Statamic\Contracts\Query\Builder;
77
use Statamic\Facades\User;
88
use Statamic\Fields\Blueprint;
9+
use Statamic\Search\QueryBuilder as SearchQueryBuilder;
910

1011
trait QueriesAuthorEntries
1112
{
@@ -21,10 +22,20 @@ protected function queryAuthorEntries(Builder $query, Collection $collection): v
2122
// Exclude entries from other collections (for entries fieldtypes with multiple collections)
2223
->whereNotIn('collectionHandle', [$collection->handle()])
2324
// Include entries with blueprints where the current user is the author
24-
->orWhere(fn ($query) => $query
25-
->whereIn('blueprint', $blueprintsWithAuthor)
26-
->whereHas('author', fn ($query) => $query->where('id', User::current()->id()))
27-
)
25+
->orWhere(function ($query) use ($collection, $blueprintsWithAuthor) {
26+
$query->whereIn('blueprint', $blueprintsWithAuthor);
27+
28+
if ($query instanceof SearchQueryBuilder) {
29+
$ids = $collection->queryEntries()
30+
->whereHas('author', fn ($query) => $query->where('id', User::current()->id()))
31+
->pluck('id')
32+
->all();
33+
34+
return $query->whereIn('id', $ids);
35+
}
36+
37+
return $query->whereHas('author', fn ($query) => $query->where('id', User::current()->id()));
38+
})
2839
// Include entries with blueprints that don't have an author
2940
->orWhereIn('blueprint', $this->blueprintsWithoutAuthor($collection))
3041
);

tests/Feature/Entries/ViewEntryListingTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,31 @@ public function it_shows_entries_index()
4444
$this->assertEquals(['one', 'two', 'three'], $entries->pluck('slug')->all());
4545
}
4646

47+
#[Test]
48+
public function it_can_search_entries_without_viewing_other_authors_entries()
49+
{
50+
$handle = 'view-other-authors-search';
51+
52+
$this->setTestRole('view-own-entries', [
53+
'access cp',
54+
"view {$handle} entries",
55+
]);
56+
57+
$user = tap(User::make()->assignRole('view-own-entries'))->save();
58+
59+
Blueprint::make('with-author')
60+
->setNamespace("collections/{$handle}")
61+
->ensureField('author', ['type' => 'users'])
62+
->save();
63+
64+
tap(Collection::make($handle)->searchIndex('default'))->save();
65+
66+
$this
67+
->actingAs($user)
68+
->get(cp_route('collections.entries.index', ['collection' => $handle, 'search' => 'entry']))
69+
->assertOk();
70+
}
71+
4772
#[Test]
4873
public function it_shows_only_entries_in_index_for_sites_user_can_access()
4974
{

0 commit comments

Comments
 (0)