Skip to content

Commit 7a774ec

Browse files
committed
Allow the blockade to expire with a config item 'expire'
1 parent 0d27e61 commit 7a774ec

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

config/blockade.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@
6060
*/
6161
'redirect' => env('BLOCKADE_REDIRECT', null),
6262

63+
/*
64+
|--------------------------------------------------------------------------
65+
| Redirect when blocked
66+
|--------------------------------------------------------------------------
67+
|
68+
| Providing this will redirect the visitor following any other rules until
69+
| this time has passed. Date must be parsable by the date() function.
70+
|
71+
*/
72+
'until' => env('BLOCKADE_UNTIL', null),
73+
6374
/*
6475
|--------------------------------------------------------------------------
6576
| Not-blocked list

src/IsBlocked.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Konsulting\Laravel\Blockade;
44

55
use Closure;
6+
use Carbon\Carbon;
67
use Illuminate\Http\Request;
78
use Illuminate\Session\Store as SessionStore;
89

@@ -46,6 +47,13 @@ class IsBlocked
4647
*/
4748
protected $allowMultipleCodes;
4849

50+
/**
51+
* The expiry date for the blockade
52+
*
53+
* @var Carbon|null
54+
*/
55+
protected $until;
56+
4957
public function __construct(SessionStore $session)
5058
{
5159
$this->session = $session;
@@ -54,6 +62,7 @@ public function __construct(SessionStore $session)
5462
$this->code = config('blockade.code', false);
5563
$this->exclude = config('blockade.not_blocked', []);
5664
$this->redirect = config('blockade.redirect', null);
65+
$this->until = config('blockade.until', null) ? Carbon::parse(config('blockade.until')) : null;
5766
$this->allowMultipleCodes = config('blockade.multiple_codes', false);
5867
}
5968

@@ -70,6 +79,10 @@ public function handle($request, Closure $next)
7079
return $next($request);
7180
}
7281

82+
if ($this->blockHasExpired()) {
83+
return $next($request);
84+
}
85+
7386
if ($request->exists($this->key)) {
7487
$this->session->put($this->key, $request->input($this->key));
7588

@@ -148,4 +161,16 @@ protected function shouldRedirectWhenBlocked()
148161
{
149162
return null !== $this->redirect;
150163
}
164+
165+
/**
166+
* Check if the block has expired
167+
*/
168+
protected function blockHasExpired()
169+
{
170+
if (! $this->until) {
171+
return false;
172+
}
173+
174+
return $this->until->isPast();
175+
}
151176
}

tests/BlockTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Konsulting\Laravel\Blockade;
44

5+
use Illuminate\Support\Carbon;
6+
57
class BlockTest extends BlockingTestCase
68
{
79
public function test_it_blocks_access_to_home()
@@ -30,6 +32,19 @@ public function test_it_doesnt_block_access_to_a_non_blocked_route()
3032
->see('Not Blocked');
3133
}
3234

35+
public function test_it_blocks_until_a_specified_time()
36+
{
37+
config()->set('blockade.until', Carbon::now()->addHour());
38+
39+
$this->visitRoute('home')
40+
->dontSee('Home');
41+
42+
Carbon::setTestNow(now()->addHours(2));
43+
44+
$this->visitRoute('home')
45+
->see('Home');
46+
}
47+
3348
/** @dataProvider passwordProvider */
3449
public function test_it_accepts_multiple_codes($code, $shouldPass, $enableMultipleCodes = true)
3550
{

0 commit comments

Comments
 (0)