@@ -250,6 +250,77 @@ public function you_can_limit_which_database_users_have_access_using_an_array()
250250 ])->assertRedirect ($ this ->url )->assertSessionHasErrors ('password ' );
251251 }
252252
253+ /** @test */
254+ public function it_allows_access_to_whitelisted_ips_only ()
255+ {
256+ $ this ->url = Config::get ('stagefront.url ' );
257+ $ this ->registerRoute ('/page ' , 'Some Page ' );
258+
259+ $ this ->enableStageFront ();
260+ $ this ->setIntendedUrl ('/page ' );
261+
262+ Config::set ('stagefront.ip_whitelist ' , ' 0.0.0.0 , 1.1.1.1 ' );
263+ Config::set ('stagefront.ip_whitelist_only ' , true );
264+ Config::set ('stagefront.ip_whitelist_require_login ' , false );
265+
266+ $ this ->get ('/page ' , ['REMOTE_ADDR ' => '1.2.3.4 ' ])
267+ ->assertStatus (403 );
268+
269+ $ this ->get ('/page ' , ['REMOTE_ADDR ' => '1.1.1.1 ' ])
270+ ->assertOk ();
271+ }
272+
273+ /** @test */
274+ public function it_allows_access_to_whitelisted_ips_only_with_required_login ()
275+ {
276+ $ this ->url = Config::get ('stagefront.url ' );
277+ $ this ->registerRoute ('/page ' , 'Some Page ' );
278+
279+ $ this ->enableStageFront ();
280+ $ this ->setIntendedUrl ('/page ' );
281+
282+ Config::set ('stagefront.login ' , 'tester ' );
283+ Config::set ('stagefront.password ' , 'p4ssw0rd ' );
284+ Config::set ('stagefront.ip_whitelist ' , ' 0.0.0.0 , 1.1.1.1 ' );
285+ Config::set ('stagefront.ip_whitelist_only ' , true );
286+ Config::set ('stagefront.ip_whitelist_require_login ' , true );
287+
288+ $ this ->get ('/page ' , ['REMOTE_ADDR ' => '1.2.3.4 ' ])
289+ ->assertStatus (403 );
290+
291+ $ this ->get ('/page ' , ['REMOTE_ADDR ' => '1.1.1.1 ' ])
292+ ->assertRedirect ($ this ->url );
293+
294+ $ response = $ this ->submitForm ([
295+ 'login ' => 'tester ' ,
296+ 'password ' => 'p4ssw0rd ' ,
297+ ], ['REMOTE_ADDR ' => '1.1.1.1 ' ]);
298+
299+ $ response ->assertRedirect ('/page ' );
300+ }
301+
302+ /** @test */
303+ public function it_allows_instant_access_to_whitelisted_ips_and_password_access_to_other_ips ()
304+ {
305+ $ this ->url = Config::get ('stagefront.url ' );
306+ $ this ->registerRoute ('/page ' , 'Some Page ' );
307+
308+ $ this ->enableStageFront ();
309+ $ this ->setIntendedUrl ('/page ' );
310+
311+ Config::set ('stagefront.login ' , 'tester ' );
312+ Config::set ('stagefront.password ' , 'p4ssw0rd ' );
313+ Config::set ('stagefront.ip_whitelist ' , ' 0.0.0.0 , 1.1.1.1 ' );
314+ Config::set ('stagefront.ip_whitelist_only ' , false );
315+ Config::set ('stagefront.ip_whitelist_require_login ' , false );
316+
317+ $ this ->get ('/page ' , ['REMOTE_ADDR ' => '1.2.3.4 ' ])
318+ ->assertRedirect ($ this ->url );
319+
320+ $ this ->get ('/page ' , ['REMOTE_ADDR ' => '1.1.1.1 ' ])
321+ ->assertOk ();
322+ }
323+
253324 /** @test */
254325 public function urls_can_be_ignored_so_access_is_not_denied_by_stagefront ()
255326 {
@@ -267,6 +338,26 @@ public function urls_can_be_ignored_so_access_is_not_denied_by_stagefront()
267338 $ this ->get ('/public/route ' )->assertStatus (200 )->assertSee ('Route ' );
268339 }
269340
341+ /** @test */
342+ public function ignored_urls_can_be_accessed_by_non_whitelisted_ips ()
343+ {
344+ $ this ->url = Config::get ('stagefront.url ' );
345+ $ this ->registerRoute ('/page ' , 'Some Page ' );
346+
347+ $ this ->registerRoute ('/public ' , 'Public ' );
348+ $ this ->registerRoute ('/public/route ' , 'Route ' );
349+
350+ Config::set ('stagefront.ignore_urls ' , ['/public/* ' ]);
351+ Config::set ('stagefront.ip_whitelist ' , '0.0.0.0 ' );
352+ Config::set ('stagefront.ip_whitelist_only ' , true );
353+ Config::set ('stagefront.ip_whitelist_require_login ' , false );
354+
355+ $ this ->enableStageFront ();
356+
357+ $ this ->get ('/public ' , ['REMOTE_ADDR ' => '1.2.3.4 ' ])->assertStatus (403 );
358+ $ this ->get ('/public/route ' , ['REMOTE_ADDR ' => '1.2.3.4 ' ])->assertStatus (200 )->assertSee ('Route ' );
359+ }
360+
270361 /** @test */
271362 public function it_throttles_login_attempts ()
272363 {
@@ -334,16 +425,16 @@ protected function setIntendedUrl($url)
334425 *
335426 * @return \Illuminate\Foundation\Testing\TestResponse
336427 */
337- protected function submitForm (array $ credentials )
428+ protected function submitForm (array $ credentials, $ headers = [] )
338429 {
339- $ response = $ this -> post ( $ this -> url , $ credentials , [
430+ $ headers += [
340431 // Since we're calling routes directly,
341432 // we need to fake the referring page
342433 // so that redirect()->back() will work.
343- 'HTTP_REFERER ' => $ this ->url
344- ]) ;
434+ 'HTTP_REFERER ' => $ this ->url ,
435+ ];
345436
346- return $ response ;
437+ return $ this -> post ( $ this -> url , $ credentials , $ headers ) ;
347438 }
348439
349440 /**
0 commit comments