Skip to content

Commit 4748d83

Browse files
Separated whitelist/blacklist middleware, added helper functions
1 parent 1c6a30f commit 4748d83

File tree

4 files changed

+100
-50
lines changed

4 files changed

+100
-50
lines changed

src/IpRestrictions.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php namespace arnehendriksen\LaravelIpRestrictions;
2+
3+
class IpRestrictions
4+
{
5+
/**
6+
* Returns whether the given (or current) IP is whitelisted.
7+
*
8+
* @param null $ip
9+
* @return bool
10+
*/
11+
public static function whitelisted($ip = null)
12+
{
13+
$ip = ($ip ?: request()->ip());
14+
$all = config('ip-restrictions.whitelist.all', []);
15+
$env = config('ip-restrictions.whitelist.'.config('app.env'), []);
16+
$list = array_merge($all, $env);
17+
return in_array($ip, $list);
18+
}
19+
20+
/**
21+
* Returns whether the given (or current) IP is blacklisted.
22+
*
23+
* @param null $ip
24+
* @return bool
25+
*/
26+
public static function blacklisted($ip = null)
27+
{
28+
$ip = ($ip ?: request()->ip());
29+
$all = config('ip-restrictions.blacklist.all', []);
30+
$env = config('ip-restrictions.blacklist.'.config('app.env'), []);
31+
$list = array_merge($all, $env);
32+
return in_array($ip, $list);
33+
}
34+
}

src/Middleware/Blacklist.php

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace arnehendriksen\LaravelIpRestrictions\Middleware;
4+
5+
use Closure;
6+
7+
class Blacklist
8+
{
9+
/**
10+
* Handle an incoming request.
11+
*
12+
* @param \Illuminate\Http\Request $request
13+
* @param \Closure $next
14+
* @return mixed
15+
*/
16+
public function handle($request, Closure $next)
17+
{
18+
$allow = true;
19+
20+
$blacklist_all = config('ip-restrictions.blacklist.all', []);
21+
$blacklist_env = config('ip-restrictions.blacklist.'.config('app.env'), []);
22+
$blacklist = array_merge($blacklist_all, $blacklist_env);
23+
24+
if (in_array($request->ip(), $blacklist)) {
25+
$allow = false;
26+
}
27+
28+
if (!$allow) {
29+
abort(403);
30+
}
31+
return $next($request);
32+
}
33+
}

src/Middleware/IpRestrictions.php

-50
This file was deleted.

src/Middleware/Whitelist.php

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace arnehendriksen\LaravelIpRestrictions\Middleware;
4+
5+
use Closure;
6+
7+
class Whitelist
8+
{
9+
/**
10+
* Handle an incoming request.
11+
*
12+
* @param \Illuminate\Http\Request $request
13+
* @param \Closure $next
14+
* @return mixed
15+
*/
16+
public function handle($request, Closure $next)
17+
{
18+
$allow = false;
19+
20+
$whitelist_all = config('ip-restrictions.whitelist.all', []);
21+
$whitelist_env = config('ip-restrictions.whitelist.'.config('app.env'), []);
22+
$whitelist = array_merge($whitelist_all, $whitelist_env);
23+
24+
if (in_array($request->ip(), $whitelist)) {
25+
$allow = true;
26+
}
27+
28+
if (!$allow) {
29+
abort(403);
30+
}
31+
return $next($request);
32+
}
33+
}

0 commit comments

Comments
 (0)