Skip to content

Commit 5dce98f

Browse files
committed
Add configurable authorisation gate for previewing unpublished pages
1 parent bfb80b6 commit 5dce98f

File tree

6 files changed

+25
-7
lines changed

6 files changed

+25
-7
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ So you can achieve the following statuses by setting these dates in the past or
282282
- **Draft** - Page exists but not visible to public users
283283
- **Published** - Page is live and accessible via URL
284284
- **Scheduled** - Automatically publish/unpublish at specific times
285+
- **Preview** - Unpublished pages can be previewed, but are shielded by [a configurable authorisation gate](./documentation/configuration.md#page-resource-configuration).
285286

286287
Use the `published()` scope in your queries to show only published content:
287288

config/filament-flexible-content-block-pages.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,12 @@
130130
/*
131131
| The authorisation gate to show the undeletable toggle on the edit page.
132132
| The value should be the name of the gate to execute `Gate::allows($gateName, User $user, Page $page)`
133+
|
134+
| undeletable: to be able to toggle the undeletable switch after the page is created.
135+
| view_unpublished_pages: to be able to view pages on the website that are not published.
133136
*/
134-
'undeletable' => 'change_undeletable',
137+
'undeletable' => 'changeUndeletable',
138+
'view_unpublished_pages' => 'viewUnpublishedPages',
135139
],
136140

137141
/*

documentation/configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ Configure various features and options for the page resource. These settings con
165165
- **enable_replicate_action_on_table**: Shows the replicate action in the table
166166
- **navigation_sort**: Controls the order of the page resource in the Filament navigation menu
167167
- **gates.undeletable**: The authorisation gate to allow the deletable toggle to be shown on the page edit page.
168+
- **gates.view_unpublished_pages**: The authorisation gate to allow viewing unpublished pages on the website. This is useful for content editors to preview.
168169
- **page_tree.max_depth**: The maximum allowed page depth in the tree hierarchy of pages. First, enable the page tree.
169170

170171
## CMS Panel Configuration

src/FilamentFlexibleContentBlockPagesConfig.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,11 +349,25 @@ public function getUndeletableGate(string $modelClass): ?string
349349
return $this->packageConfig("page_resource.{$modelClass}.gates.undeletable");
350350
}
351351

352+
/**
353+
* @param class-string<Model> $modelClass
354+
*/
355+
public function getViewUnpublishedPagesGate(string $modelClass): ?string
356+
{
357+
return $this->packageConfig("page_resource.{$modelClass}.gates.view_unpublished_pages");
358+
}
359+
360+
/**
361+
* @param class-string<Model> $modelClass
362+
*/
352363
public function getPageTreeMaximumDepth(string $modelClass): int
353364
{
354365
return $this->packageConfig("page_resource.{$modelClass}.page_tree.max_depth", 2);
355366
}
356367

368+
/**
369+
* @param class-string<Model> $modelClass
370+
*/
357371
public function getPageNavigationSort(string $modelClass): ?int
358372
{
359373
return $this->packageConfig("page_resource.{$modelClass}.navigation_sort");

src/Form/Components/UndeletableToggle.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,7 @@ public static function create(?string $label = null): static
3737
$gate = FilamentFlexibleContentBlockPages::config()->getUndeletableGate($modelClass);
3838

3939
// If no gate set, no authorisation is wanted
40-
if ($gate === null) {
41-
return true;
42-
}
43-
44-
return Gate::allows($gate, $record);
40+
return (!$gate) || Gate::allows($gate, $record);
4541
});
4642
}
4743

src/Http/Controllers/PageController.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Foundation\Validation\ValidatesRequests;
77
use Illuminate\Http\Response;
88
use Illuminate\Support\Facades\Auth;
9+
use Illuminate\Support\Facades\Gate;
910
use Statikbe\FilamentFlexibleContentBlockPages\Facades\FilamentFlexibleContentBlockPages;
1011
use Statikbe\FilamentFlexibleContentBlockPages\Models\Page;
1112

@@ -18,7 +19,8 @@ class PageController extends AbstractSeoPageController
1819
public function index(Page $page)
1920
{
2021
// check if page is published:
21-
if (! Auth::user() || ! Auth::user()->can('viewUnpublishedPages')) {
22+
$viewUnpublishedPagesGate = FilamentFlexibleContentBlockPages::config()->getViewUnpublishedPagesGate($page::class);
23+
if (! Auth::user() || !($viewUnpublishedPagesGate && Gate::allows($viewUnpublishedPagesGate, $page))) {
2224
if (! $page->isPublished()) {
2325
SEOMeta::setRobots('noindex');
2426
abort(Response::HTTP_GONE);

0 commit comments

Comments
 (0)