Skip to content

Commit c0c4ebe

Browse files
committed
Add password protected pages (resolve #3)
1 parent e9d1cd6 commit c0c4ebe

File tree

5 files changed

+23
-4
lines changed

5 files changed

+23
-4
lines changed

src/Page.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public static function schema(Blueprint $table)
1919
{
2020
$table->string('slug');
2121
$table->string('title');
22+
$table->string('password')->nullable();
2223
$table->longText('content');
2324
}
2425

src/PageController.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ public function __invoke(string $page)
2121
}
2222

2323
if ($model = config('pages.model')::find($page)) {
24+
if ($model->password) {
25+
abort_unless(request()->query('password') === $model->password, 403);
26+
}
27+
2428
seo()
2529
->title($model->title)
2630
->description(Str::limit($model->content, 100));

tests/Feature/PagesTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,16 @@
7777
->assertSee('<meta property="og:title" content="Test page" />', false)
7878
->assertSee('<meta property="og:description" content="This is a test page" />', false);
7979
});
80+
81+
test('pages can be password-protected', function () {
82+
Page::create([
83+
'slug' => 'test',
84+
'title' => 'Test page',
85+
'password' => 'foo',
86+
'content' => 'This is a test page'
87+
]);
88+
89+
using($this)->get('/test')->assertForbidden();
90+
using($this)->get('/test?password=bar')->assertForbidden();
91+
using($this)->get('/test?password=foo')->assertSuccessful()->assertSee('This is a test page');
92+
});

tests/orbit/content/pages/example.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
slug: example
33
title: 'Test page'
4-
updated_at: 2021-08-06T02:25:15+00:00
5-
created_at: 2021-08-06T02:25:15+00:00
4+
updated_at: 2021-08-08T18:00:46+00:00
5+
created_at: 2021-08-08T18:00:46+00:00
66
---
77
This is a test page

tests/orbit/content/pages/test.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
---
22
slug: test
33
title: 'Test page'
4-
updated_at: 2021-08-06T02:25:15+00:00
5-
created_at: 2021-08-06T02:25:15+00:00
4+
password: foo
5+
updated_at: 2021-08-08T18:00:46+00:00
6+
created_at: 2021-08-08T18:00:46+00:00
67
---
78
This is a test page

0 commit comments

Comments
 (0)