Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions resources/lang/en/permissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
'publish_{collection}_entries_desc' => 'Ability to change from draft to published and vice versa',
'reorder_{collection}_entries' => 'Reorder entries',
'reorder_{collection}_entries_desc' => 'Enables drag and drop reordering',
'view_other_authors_{collection}_entries' => "View other authors' entries",
'edit_other_authors_{collection}_entries' => "Edit other authors' entries",
'publish_other_authors_{collection}_entries' => "Manage publish state of other authors' entries",
'delete_other_authors_{collection}_entries' => "Delete other authors' entries",
Expand Down
8 changes: 5 additions & 3 deletions src/Auth/CorePermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,11 @@ protected function registerCollections()
$this->permission('delete {collection} entries'),
$this->permission('publish {collection} entries'),
$this->permission('reorder {collection} entries'),
$this->permission('edit other authors {collection} entries')->children([
$this->permission('publish other authors {collection} entries'),
$this->permission('delete other authors {collection} entries'),
$this->permission('view other authors {collection} entries')->children([
$this->permission('edit other authors {collection} entries')->children([
$this->permission('publish other authors {collection} entries'),
$this->permission('delete other authors {collection} entries'),
]),
]),
]),
])->replacements('collection', function () {
Expand Down
14 changes: 14 additions & 0 deletions src/Fieldtypes/Entries.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,20 @@ public function getIndexItems($request)
$query->whereIn('blueprint', $blueprints);
}

collect($this->getConfiguredCollections())
->map(fn ($handle) => Collection::findByHandle($handle))
->filter(fn ($collection) => User::current()->cant('view-other-authors-entries', [EntryContract::class, $collection]))
->each(function ($collection) use ($query) {
$blueprintsWithoutAuthor = $collection->entryBlueprints()
->filter(fn ($blueprint) => ! $blueprint->hasField('author'))
->map->handle()->all();

$query->where(fn ($query) => $query
->whereIn('blueprint', $blueprintsWithoutAuthor)
->orWhere('author', User::current()->id())
);
});

$this->activeFilterBadges = $this->queryFilters($query, $filters, $this->getSelectionFilterContext());

if ($sort = $this->getSortColumn($request)) {
Expand Down
16 changes: 15 additions & 1 deletion src/Http/Controllers/CP/Collections/CollectionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,24 @@ private function collections()
|| User::current()->can('view', $collection)
&& $collection->sites()->contains(Site::selected()->handle());
})->map(function ($collection) {
$entriesCount = $collection->queryEntries()
->where('site', Site::selected())
->when(User::current()->cant('view-other-authors-entries', [EntryContract::class, $collection]), function ($query) use ($collection) {
$blueprintsWithoutAuthor = $collection->entryBlueprints()
->filter(fn ($blueprint) => ! $blueprint->hasField('author'))
->map->handle()->all();

$query->where(fn ($query) => $query
->whereIn('blueprint', $blueprintsWithoutAuthor)
->orWhere('author', User::current()->id())
);
})
->count();

return [
'id' => $collection->handle(),
'title' => $collection->title(),
'entries' => $collection->queryEntries()->where('site', Site::selected())->count(),
'entries' => $entriesCount,
'edit_url' => $collection->editUrl(),
'delete_url' => $collection->deleteUrl(),
'entries_url' => cp_route('collections.show', $collection->handle()),
Expand Down
12 changes: 12 additions & 0 deletions src/Http/Controllers/CP/Collections/EntriesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ protected function indexQuery($collection)
$query->whereIn('site', Site::authorized()->map->handle()->all());
}

if (User::current()->cant('view-other-authors-entries', [EntryContract::class, $collection])) {
// Mirror the behavior of the hasAnotherAuthor() method in the EntryPolicy.
$blueprintsWithoutAuthor = $collection->entryBlueprints()
->filter(fn ($blueprint) => ! $blueprint->hasField('author'))
->map->handle()->all();

$query->where(fn ($query) => $query
->whereIn('blueprint', $blueprintsWithoutAuthor)
->orWhere('author', User::current()->id())
);
}

return $query;
}

Expand Down
9 changes: 9 additions & 0 deletions src/Policies/EntryPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public function view($user, $entry)
return false;
}

if ($this->hasAnotherAuthor($user, $entry)) {
return $user->hasPermission("view other authors {$entry->collectionHandle()} entries");
}

return $this->edit($user, $entry)
|| $user->hasPermission("view {$entry->collectionHandle()} entries");
}
Expand All @@ -49,6 +53,11 @@ public function edit($user, $entry)
return $user->hasPermission("edit {$entry->collectionHandle()} entries");
}

public function viewOtherAuthorsEntries($user, $collection)
{
return $user->hasPermission("view other authors {$collection->handle()} entries");
}

public function editOtherAuthorsEntries($user, $collection, $blueprint = null)
{
$blueprint = $blueprint ?? $collection->entryBlueprint();
Expand Down