Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
orkhanahmadov committed Sep 18, 2019
1 parent aa1c635 commit f5ec856
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ docs
vendor
coverage
.idea
.php_cs.cache
.phpunit.result.cache
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
29 changes: 27 additions & 2 deletions src/IpMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
31 changes: 31 additions & 0 deletions tests/IpMiddlewareTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Orkhanahmadov\LaravelIpMiddleware\Tests;

use Illuminate\Http\Request;
use Orkhanahmadov\LaravelIpMiddleware\IpMiddleware;
use Symfony\Component\HttpKernel\Exception\HttpException;

class IpMiddlewareTest extends TestCase
{
public function testFails()
{
$this->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');
}
}

0 comments on commit f5ec856

Please sign in to comment.