Skip to content

Commit b401816

Browse files
author
Karel Mareš
committed
git init
0 parents  commit b401816

File tree

5 files changed

+198
-0
lines changed

5 files changed

+198
-0
lines changed

README.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Breadcrumb for Laravel 5
2+
3+
This package provide simple way to filter by IP addresses for your Laravel 5 application.
4+
5+
## Install
6+
7+
Via Composer
8+
9+
``` bash
10+
$ composer require mares29/laravel-ip-filter
11+
```
12+
13+
Laravel 5.5+ automaticly register service provider and set Alias thanks to auto-discovery. With lower laravel version add to **app.php**
14+
15+
``` php
16+
'providers' => [
17+
\Mares29\Breadcrumb\FilterIpServiceProvider::class,
18+
]
19+
```
20+
21+
## Usage
22+
23+
Export filter for IpFilter
24+
25+
``` terminal
26+
php artisan vendor:publish --provider="Mares29\IpFilter\FilterIpServiceProvider"
27+
```
28+
29+
Use one **black list** or **white list**. For example, allow acces only from ip address *127.0.0.1*.
30+
31+
``` php
32+
// White list - List of allowed IP addresses
33+
'allowed' => [
34+
'127.0.0.1'
35+
],
36+
37+
// Black list - List of denied IP addresses
38+
'denied' => [],
39+
```
40+
41+
Add middleware for Yours all web routes.
42+
43+
``` php
44+
protected function mapWebRoutes()
45+
{
46+
Route::middleware('web')
47+
->middleware('filterIp')
48+
->namespace($this->namespace)
49+
->group(base_path('routes/web.php'));
50+
}
51+
```
52+
or just for specific routes
53+
54+
``` php
55+
Route::get('/', function () {
56+
return view('welcome');
57+
})->middleware('filterIp');
58+
```
59+
60+
## Credits
61+
62+
- [Karel Mares][https://github.com/mares29]
63+
64+
## License
65+
66+
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
67+

composer.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "mares29/laravel-ip-filter",
3+
"description": "Filter request by IP addresses for Laravel",
4+
"keywords": [
5+
"mares29",
6+
"laravel",
7+
"filter-ip-address",
8+
"middleware"
9+
],
10+
"homepage": "https://github.com/mares29/laravel-ip-filter",
11+
"license": "MIT",
12+
"authors": [
13+
{
14+
"name": "Karel Mares",
15+
"email": "[email protected]",
16+
"homepage": "https://karelmares.cz",
17+
"role": "Developer"
18+
}
19+
],
20+
"autoload": {
21+
"psr-4": {
22+
"Mares29\\IpFilter\\": "src/"
23+
}
24+
},
25+
"extra": {
26+
"laravel": {
27+
"providers": [
28+
"Mares29\\IpFilter\\FilterIpServiceProvider"
29+
]
30+
}
31+
}
32+
}

config/ip-filter.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
return [
4+
/*
5+
|--------------------------------------------------------------------------
6+
| Ip Filter Config
7+
|--------------------------------------------------------------------------
8+
|
9+
| Use one of supported filter methods.
10+
|
11+
| Supported: "Black list", "White list"
12+
|
13+
*/
14+
15+
// White list - List of allowed IP addresses
16+
'allowed' => [],
17+
18+
// Black list - List of denied IP addresses
19+
'denied' => [],
20+
];

src/FilterIp.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Mares29\IpFilter;
4+
5+
use Closure;
6+
7+
class FilterIp
8+
{
9+
10+
/**
11+
* Handle an incoming request.
12+
*
13+
* @param \Illuminate\Http\Request $request
14+
* @param \Closure $next
15+
* @return mixed
16+
*/
17+
public function handle($request, Closure $next)
18+
{
19+
$whiteList = config('ip-filter.allowed', []);
20+
$blackList = config('ip-filter.denied', []);
21+
$ip = $request->ip();
22+
23+
if (
24+
(!count($whiteList) && !count($blackList)) ||
25+
count($whiteList) > 0 && in_array($ip, $whiteList) ||
26+
count($blackList) > 0 && !in_array($ip, $blackList)
27+
) {
28+
return $next($request);
29+
}
30+
}
31+
}

src/FilterIpServiceProvider.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Mares29\IpFilter;
4+
5+
use Illuminate\Routing\Router;
6+
use Illuminate\Support\ServiceProvider;
7+
8+
class FilterIpServiceProvider extends ServiceProvider
9+
{
10+
11+
/**
12+
* Bootstrap the application services.
13+
*
14+
* @param Router $router
15+
* @return void
16+
*/
17+
public function boot(Router $router)
18+
{
19+
$configPath = __DIR__ . '/../config/ip-filter.php';
20+
$this->publishes([$configPath => $this->getConfigPath()], 'config');
21+
$router->aliasMiddleware('filterIp', FilterIp::class);
22+
}
23+
24+
25+
26+
/**
27+
* Register the application services.
28+
*
29+
* @return void
30+
*/
31+
public function register()
32+
{
33+
$configPath = __DIR__ . '/../config/ip-filter.php';
34+
$this->mergeConfigFrom($configPath, 'ip-filter');
35+
}
36+
37+
38+
39+
/**
40+
* Get the config path
41+
*
42+
* @return string
43+
*/
44+
protected function getConfigPath()
45+
{
46+
return config_path('ip-filter.php');
47+
}
48+
}

0 commit comments

Comments
 (0)