From f5ec85651f5241200c51a3add47377a79201f5ca Mon Sep 17 00:00:00 2001 From: Orkhan Ahmadov Date: Wed, 18 Sep 2019 18:06:38 +0200 Subject: [PATCH] WIP --- .gitignore | 2 ++ composer.json | 2 ++ src/IpMiddleware.php | 29 +++++++++++++++++++++++++++-- tests/IpMiddlewareTest.php | 31 +++++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 tests/IpMiddlewareTest.php diff --git a/.gitignore b/.gitignore index 3929e4b..495ca71 100755 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ docs vendor coverage .idea +.php_cs.cache +.phpunit.result.cache diff --git a/composer.json b/composer.json index 309a65c..1bfc6fe 100755 --- a/composer.json +++ b/composer.json @@ -21,6 +21,8 @@ ], "require": { "php": "^7.1", + "illuminate/container": "5.5.*|5.8.*|^6.0", + "illuminate/contracts": "5.5.*|5.8.*|^6.0", "illuminate/http": "5.5.*|5.8.*|^6.0", "illuminate/support": "5.5.*|5.8.*|^6.0" }, diff --git a/src/IpMiddleware.php b/src/IpMiddleware.php index a2bf0f0..eb88917 100644 --- a/src/IpMiddleware.php +++ b/src/IpMiddleware.php @@ -3,22 +3,47 @@ namespace Orkhanahmadov\LaravelIpMiddleware; use Closure; +use Illuminate\Contracts\Config\Repository; +use Illuminate\Contracts\Foundation\Application; +use Illuminate\Http\Request; use Illuminate\Http\Response; use Illuminate\Support\Arr; class IpMiddleware { + /** + * @var Application + */ + private $application; + /** + * @var Repository + */ + private $config; + + /** + * IpMiddleware constructor. + * @param Application $application + * @param Repository $config + */ + public function __construct(Application $application, Repository $config) + { + $this->application = $application; + $this->config = $config; + } + /** * Handle an incoming request. * - * @param \Illuminate\Http\Request $request + * @param Request $request * @param Closure $next * @param array $allowedIp * @return mixed */ public function handle($request, Closure $next, ...$allowedIp) { - if (!app()->environment(config('ip-middleware.ignore_environments')) && !in_array($request->ip(), Arr::flatten($allowedIp))) { + if (! $this->application->environment($this->config->get('ip-middleware.ignore_environments')) && + !in_array($request->ip(), Arr::flatten($allowedIp)) + ) { abort(Response::HTTP_FORBIDDEN); } diff --git a/tests/IpMiddlewareTest.php b/tests/IpMiddlewareTest.php new file mode 100644 index 0000000..85d50ea --- /dev/null +++ b/tests/IpMiddlewareTest.php @@ -0,0 +1,31 @@ +withoutExceptionHandling(); + $this->expectException(HttpException::class); + $middleware = $this->app->make(IpMiddleware::class); + $request = Request::create('/', 'GET', [], [], [], ['REMOTE_ADDR' => '1.1.1.0']); + + $middleware->handle($request, function () { + }, '1.1.1.1'); + } + + public function testPasses() + { + $this->withoutExceptionHandling(); + $middleware = new IpMiddleware(); + $request = Request::create('/', 'GET', [], [], [], ['REMOTE_ADDR' => '1.1.1.1']); + + $middleware->handle($request, function () { + }, '1.1.1.1'); + } +}