Skip to content

Commit 1c6a30f

Browse files
Package files
1 parent c38f737 commit 1c6a30f

File tree

5 files changed

+120
-0
lines changed

5 files changed

+120
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# laravel-ip-restrictions
2+
23
Laravel middleware for whitelisting and blacklisting IP addresses per environment.

composer.json

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "arnehendriksen/laravel-ip-restrictions",
3+
"description": "Laravel middleware for whitelisting and blacklisting IP addresses per environment.",
4+
"keywords": ["laravel", "middleware", "whitelist", "blacklist"],
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Arne Hendriksen",
9+
"email": "[email protected]",
10+
"homepage": "https://arnehendriksen.com"
11+
}
12+
],
13+
"require": {
14+
"php": ">=7.1.0"
15+
},
16+
"autoload": {
17+
"psr-0": {
18+
"arnehendriksen\\LaravelIpRestrictions\\": "src/"
19+
}
20+
},
21+
"extra": {
22+
"laravel": {
23+
"providers": [
24+
"arnehendriksen\\LaravelIpRestrictions\\IpRestrictionsServiceProvider"
25+
]
26+
}
27+
},
28+
"minimum-stability": "stable"
29+
}

src/IpRestrictionsServiceProvider.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace arnehendriksen\LaravelIpRestrictions;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class IpRestrictionsServiceProvider extends ServiceProvider
8+
{
9+
public function boot()
10+
{
11+
$this->publishes([
12+
__DIR__.'/config/ip-restrictions.php' => config_path('ip-restrictions.php'),
13+
], 'ip-restrictions');
14+
}
15+
16+
public function register()
17+
{
18+
//
19+
}
20+
}

src/Middleware/IpRestrictions.php

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace arnehendriksen\LaravelIpRestrictions\Middleware;
4+
5+
use Closure;
6+
7+
class IpRestrictions
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+
$blacklist_all = config('ip-restrictions.blacklist.all');
21+
$blacklist_env = config('ip-restrictions.blacklist.'.config('app.env'));
22+
$whitelist_all = config('ip-restrictions.whitelist.all');
23+
$whitelist_env = config('ip-restrictions.whitelist.'.config('app.env'));
24+
25+
if (is_array($blacklist_all) && count($blacklist_all) > 0) {
26+
if (in_array($request->ip(), $blacklist_all)) {
27+
abort(403);
28+
}
29+
}
30+
if (is_array($blacklist_env) && count($blacklist_env) > 0) {
31+
if (in_array($request->ip(), $blacklist_env)) {
32+
abort(403);
33+
}
34+
}
35+
if (is_array($whitelist_all) && count($whitelist_all) > 0) {
36+
if (in_array($request->ip(), $whitelist_all)) {
37+
$allow = true;
38+
}
39+
}
40+
if (is_array($whitelist_env) && count($whitelist_env) > 0) {
41+
if (in_array($request->ip(), $whitelist_env)) {
42+
$allow = true;
43+
}
44+
}
45+
if (!$allow) {
46+
abort(403);
47+
}
48+
return $next($request);
49+
}
50+
}

src/config/ip-restrictions.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
return [
3+
4+
// IP addresses to be whitelisted per enviroment (as defined in config.env).
5+
'whitelist' => [
6+
'all' => [],
7+
'test' => [],
8+
'staging' => [],
9+
'production' => []
10+
],
11+
12+
// IP addresses to be blacklisted per enviroment (as defined in config.env).
13+
'blacklist' => [
14+
'all' => [],
15+
'test' => [],
16+
'staging' => [],
17+
'production' => []
18+
],
19+
20+
];

0 commit comments

Comments
 (0)