Skip to content

[5.x] Added the ability for Protectors to allow static caching #11212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
15 changes: 15 additions & 0 deletions config/protect.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@

'default' => null,

/*
|--------------------------------------------------------------------------
| Static caching.
|--------------------------------------------------------------------------
|
| Default setting for whether or not to allow protected pages in the static
| cache.
|
*/

'cacheable' => null,

/*
|--------------------------------------------------------------------------
| Protection Schemes
Expand All @@ -33,6 +45,9 @@
'ip_address' => [
'driver' => 'ip_address',
'allowed' => ['127.0.0.1'],
// NOTE: Set `cacheable` to true/false to allow static caching for
// this driver.
// 'cacheable' => null,
],

'logged_in' => [
Expand Down
6 changes: 6 additions & 0 deletions src/Auth/Protect/Protection.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ public function protect()
->protect();
}

public function cacheable()
{
return $this->driver()
->cacheable();
}

protected function url()
{
return URL::tidy(request()->fullUrl());
Expand Down
5 changes: 5 additions & 0 deletions src/Auth/Protect/Protectors/Protector.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@ public function setConfig($config)

return $this;
}

public function cacheable()
{
return $this->config['cacheable'] ?? config('statamic.protect.cacheable', false);
}
}
2 changes: 1 addition & 1 deletion src/Http/Responses/DataResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ protected function protect()

$protection->protect();

if ($protection->scheme()) {
if ($protection->scheme() && ! $protection->cacheable()) {
$this->headers['X-Statamic-Protected'] = true;
}

Expand Down
39 changes: 39 additions & 0 deletions tests/Auth/Protect/ProtectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,45 @@ public function protect()
$this->assertTrue($state->protected);
}

#[Test]
public function default_configuration_allows_static_caching()
{
config(['statamic.protect.default' => 'logged_in']);
config(['statamic.protect.cacheable' => true]);

$this->assertTrue($this->protection->cacheable());
}

#[Test]
public function driver_allows_static_caching()
{
config(['statamic.protect.default' => 'logged_in']);
config(['statamic.protect.schemes.logged_in' => [
'driver' => 'auth',
'form_url' => '/login',
'cacheable' => true,
]]);

$this->assertTrue($this->protection->cacheable());
$this->assertTrue($this->protection->driver()->cacheable());
}

#[Test]
public function driver_disallows_static_caching()
{
config(['statamic.protect.cacheable' => true]);
config(['statamic.protect.default' => 'logged_in']);
config(['statamic.protect.schemes.logged_in' => [
'driver' => 'auth',
'form_url' => '/login',
'cacheable' => false,
]]);

$this->assertTrue(config('statamic.protect.cacheable'));
$this->assertFalse($this->protection->cacheable());
$this->assertFalse($this->protection->driver()->cacheable());
}

private function createEntryWithScheme($scheme)
{
return EntryFactory::id('test')
Expand Down
42 changes: 42 additions & 0 deletions tests/FrontendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,48 @@ public function header_is_added_to_protected_responses()
->assertHeader('X-Statamic-Protected', true);
}

#[Test]
public function header_is_not_added_to_cacheable_protected_responses()
{
config()->set('statamic.protect.cacheable', true);

$page = $this->createPage('about');

$this
->get('/about')
->assertOk()
->assertHeaderMissing('X-Statamic-Protected');

$page->set('protect', 'logged_in')->save();

$this
->actingAs(User::make())
->get('/about')
->assertOk()
->assertHeaderMissing('X-Statamic-Protected');
}

#[Test]
public function header_is_not_added_to_cacheable_protected_responses_for_driver()
{
config()->set('statamic.protect.schemes.logged_in.cacheable', true);

$page = $this->createPage('about');

$this
->get('/about')
->assertOk()
->assertHeaderMissing('X-Statamic-Protected');

$page->set('protect', 'logged_in')->save();

$this
->actingAs(User::make())
->get('/about')
->assertOk()
->assertHeaderMissing('X-Statamic-Protected');
}

#[Test]
public function key_variables_key_added()
{
Expand Down
Loading