Skip to content

Commit e51988d

Browse files
committed
Allow URLs to be ignored by StageFront
1 parent 38174b0 commit e51988d

File tree

3 files changed

+62
-5
lines changed

3 files changed

+62
-5
lines changed

config/stagefront.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,13 @@
8787
*/
8888
'middleware' => env('STAGEFRONT_MIDDLEWARE', 'web'),
8989

90+
/**
91+
* The following URLs will be ignored by StageFront.
92+
* Access to these URLs will never be blocked.
93+
* You can use wildcards:
94+
* '/route/*' (this does not include '/route')
95+
*
96+
* Default: []
97+
*/
98+
'ignore_urls' => [],
9099
];

src/Middleware/RedirectIfStageFrontIsEnabled.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,35 @@ public function handle($request, Closure $next)
1818
{
1919
$disabled = ! config('stagefront.enabled', false);
2020
$unlocked = session('stagefront.unlocked', false);
21-
$stageFrontUrl = trim(config('stagefront.url'), '/');
21+
$stageFrontUrl = config('stagefront.url');
22+
$ignoredUrls = config('stagefront.ignore_urls', []);
23+
array_push($ignoredUrls, $stageFrontUrl);
2224

23-
if ($unlocked || $disabled || $request->is($stageFrontUrl)) {
25+
if ($unlocked || $disabled || $this->urlIsIgnored($request, $ignoredUrls)) {
2426
return $next($request);
2527
}
2628

2729
return redirect($stageFrontUrl);
2830
}
31+
32+
/**
33+
* Check if a URL should be ignored.
34+
*
35+
* @param \Illuminate\Http\Request $request
36+
* @param array $ignoredUrls
37+
*
38+
* @return bool
39+
*/
40+
protected function urlIsIgnored($request, $ignoredUrls)
41+
{
42+
foreach ($ignoredUrls as $url) {
43+
$url = trim($url, '/');
44+
45+
if ($request->is($url)) {
46+
return true;
47+
}
48+
}
49+
50+
return false;
51+
}
2952
}

tests/Feature/StageFrontTest.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ public function setUp()
2626
// It is disabled by default so nothing is loaded by default.
2727
$this->url = config('stagefront.url');
2828

29-
Route::get('/page', function () {
30-
return 'Some Page';
31-
})->middleware(config('stagefront.middleware'));
29+
$this->registerRoute('/page', 'Some Page');
3230
}
3331

3432
/** @test */
@@ -176,6 +174,20 @@ public function you_can_limit_which_database_users_have_access()
176174
])->assertRedirect($this->url)->assertSessionHasErrors('password');
177175
}
178176

177+
/** @test */
178+
public function urls_can_be_ignored_so_access_is_not_denied_by_stagefront()
179+
{
180+
$this->registerRoute('/public', 'Public');
181+
$this->registerRoute('/public/route', 'Route');
182+
183+
config()->set('stagefront.ignore_urls', ['/public/*']);
184+
185+
$this->enableStageFront();
186+
187+
$this->get('/public')->assertRedirect($this->url);
188+
$this->get('/public/route')->assertStatus(200)->assertSee('Route');
189+
}
190+
179191
/**
180192
* Tell Laravel we navigated to this intended URL and
181193
* got redirected to the login page so that
@@ -227,4 +239,17 @@ protected function enableStageFront()
227239
RedirectIfStageFrontIsEnabled::class
228240
);
229241
}
242+
243+
/**
244+
* Register a test route.
245+
*
246+
* @param string $url
247+
* @param string $text
248+
*/
249+
protected function registerRoute($url, $text)
250+
{
251+
Route::get($url, function () use ($text) {
252+
return $text;
253+
})->middleware(config('stagefront.middleware'));
254+
}
230255
}

0 commit comments

Comments
 (0)