Skip to content

Commit 558a5ee

Browse files
[5.x] Added the ability for Protectors to allow static caching (#11542)
Co-authored-by: Simon Geoghegan <[email protected]>
1 parent 260dfff commit 558a5ee

File tree

5 files changed

+93
-1
lines changed

5 files changed

+93
-1
lines changed

src/Auth/Protect/Protection.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ public function protect()
6969
->protect();
7070
}
7171

72+
public function cacheable()
73+
{
74+
return $this->driver()
75+
->cacheable();
76+
}
77+
7278
protected function url()
7379
{
7480
return URL::tidy(request()->fullUrl());

src/Auth/Protect/Protectors/Protector.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,9 @@ public function setConfig($config)
3636

3737
return $this;
3838
}
39+
40+
public function cacheable()
41+
{
42+
return false;
43+
}
3944
}

src/Http/Responses/DataResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ protected function protect()
9595

9696
$protection->protect();
9797

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

tests/Auth/Protect/ProtectionTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,46 @@ public function protect()
147147
$this->assertTrue($state->protected);
148148
}
149149

150+
#[Test]
151+
public function protector_driver_allows_static_caching()
152+
{
153+
config(['statamic.protect.default' => 'test']);
154+
config(['statamic.protect.schemes.test' => [
155+
'driver' => 'test',
156+
]]);
157+
158+
app(ProtectorManager::class)->extend('test', function ($app) {
159+
return new class() extends Protector
160+
{
161+
public function protect()
162+
{
163+
//
164+
}
165+
166+
public function cacheable()
167+
{
168+
return true;
169+
}
170+
};
171+
});
172+
173+
$this->assertTrue($this->protection->cacheable());
174+
$this->assertTrue($this->protection->driver()->cacheable());
175+
}
176+
177+
#[Test]
178+
public function protector_driver_disallows_static_caching()
179+
{
180+
config(['statamic.protect.default' => 'logged_in']);
181+
config(['statamic.protect.schemes.logged_in' => [
182+
'driver' => 'auth',
183+
'form_url' => '/login',
184+
]]);
185+
186+
$this->assertFalse($this->protection->cacheable());
187+
$this->assertFalse($this->protection->driver()->cacheable());
188+
}
189+
150190
private function createEntryWithScheme($scheme)
151191
{
152192
return EntryFactory::id('test')

tests/FrontendTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use Illuminate\Support\Facades\Event;
1212
use PHPUnit\Framework\Attributes\DataProvider;
1313
use PHPUnit\Framework\Attributes\Test;
14+
use Statamic\Auth\Protect\ProtectorManager;
15+
use Statamic\Auth\Protect\Protectors\Protector;
1416
use Statamic\Events\ResponseCreated;
1517
use Statamic\Facades\Blueprint;
1618
use Statamic\Facades\Cascade;
@@ -373,6 +375,45 @@ public function header_is_added_to_protected_responses()
373375
->assertHeader('X-Statamic-Protected', true);
374376
}
375377

378+
#[Test]
379+
public function header_is_not_added_to_cacheable_protected_responses()
380+
{
381+
// config(['statamic.protect.default' => 'test']);
382+
config(['statamic.protect.schemes.test' => [
383+
'driver' => 'test',
384+
]]);
385+
386+
app(ProtectorManager::class)->extend('test', function ($app) {
387+
return new class() extends Protector
388+
{
389+
public function protect()
390+
{
391+
//
392+
}
393+
394+
public function cacheable()
395+
{
396+
return true;
397+
}
398+
};
399+
});
400+
401+
$page = $this->createPage('about');
402+
403+
$this
404+
->get('/about')
405+
->assertOk()
406+
->assertHeaderMissing('X-Statamic-Protected');
407+
408+
$page->set('protect', 'test')->save();
409+
410+
$this
411+
->actingAs(User::make())
412+
->get('/about')
413+
->assertOk()
414+
->assertHeaderMissing('X-Statamic-Protected');
415+
}
416+
376417
#[Test]
377418
public function key_variables_key_added()
378419
{

0 commit comments

Comments
 (0)