diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..bd0ddd7 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,16 @@ +; This file is for unifying the coding style for different editors and IDEs. +; More information at http://editorconfig.org + +root = false + +[*] +indent_style = space +indent_size = 4 +charset = "utf-8" +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true + +[*.yml] +indent_style = space +indent_size = 2 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3d7e132 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +vendor/ +composer.lock +.phpunit.result.cache diff --git a/README.md b/README.md index 871a9df..471f130 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # cakephp3-ip-filter -restrict access by ip address for CakePHP3 Component +restrict access by ip address for CakePHP4 Component ## Installation diff --git a/composer.json b/composer.json index 2b8db40..f5e7735 100644 --- a/composer.json +++ b/composer.json @@ -1,15 +1,14 @@ - { "name": "tyrellsys/cakephp3-ip-filter", - "description": "restrict access by ip address for CakePHP3", + "description": "restrict access by ip address for CakePHP4", "type": "cakephp-plugin", "require": { - "php": ">=5.6", - "cakephp/cakephp": "~3.6", - "jalle19/php-whitelist-check": "^1.0" + "php": ">=7.2", + "cakephp/cakephp": "^4.0", + "wikimedia/ip-utils": "5.0.0" }, "require-dev": { - "phpunit/phpunit": "^5.7|^6.0" + "phpunit/phpunit": "^8.5" }, "autoload": { "psr-4": { diff --git a/src/Controller/Component/IpFilterComponent.php b/src/Controller/Component/IpFilterComponent.php index 2de8761..bda7ad6 100644 --- a/src/Controller/Component/IpFilterComponent.php +++ b/src/Controller/Component/IpFilterComponent.php @@ -4,7 +4,7 @@ use Cake\Controller\Component; use Cake\Controller\ComponentRegistry; use Cake\Http\Exception\ForbiddenException; -use Whitelist\Check; +use Wikimedia\IPSet; /** * IpFilter component @@ -28,9 +28,8 @@ class IpFilterComponent extends Component */ public function check($ip = null) { - $checker = new Check(); if (is_null($ip)) { - $request = clone $this->request; + $request = clone $this->getController()->getRequest(); $request->trustProxy = filter_var($this->getConfig('trustProxy', true), FILTER_VALIDATE_BOOLEAN); $ip = $request->clientIP(); } @@ -40,9 +39,9 @@ public function check($ip = null) $whitelist = explode(",", $whitelist); } - $checker->whitelist($whitelist); + $ipset = new IPSet($whitelist); - return $checker->check($ip); + return $ipset->match($ip); } /** diff --git a/tests/TestCase/Controller/Component/IpFilterComponentTest.php b/tests/TestCase/Controller/Component/IpFilterComponentTest.php index 0dd6e72..0a61b5a 100644 --- a/tests/TestCase/Controller/Component/IpFilterComponentTest.php +++ b/tests/TestCase/Controller/Component/IpFilterComponentTest.php @@ -27,7 +27,7 @@ class IpFilterComponentTest extends TestCase * * @return void */ - public function setUp() + public function setUp(): void { parent::setUp(); $this->_oSERVER = $_SERVER; @@ -53,7 +53,7 @@ public function setUp() * * @return void */ - public function tearDown() + public function tearDown(): void { parent::tearDown(); @@ -121,11 +121,11 @@ public function testCheckNoTrustProxy($ip) /** * test checkOrFail method - * @expectedException \Cake\Http\Exception\ForbiddenException * @return void */ public function testCheckOrFail() { + $this->expectException(\Cake\Http\Exception\ForbiddenException::class); $this->Controller->IpFilter->checkOrFail('127.0.0.1'); } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 3834ac3..8a107ce 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -19,8 +19,10 @@ $root = $findRoot(__FILE__); unset($findRoot); -require $root . '/vendor/autoload.php'; +require_once 'vendor/cakephp/cakephp/src/basics.php'; -require $root . '/config/bootstrap.php'; +require $root . '/vendor/autoload.php'; $_SERVER['PHP_SELF'] = '/'; + +error_reporting(E_ALL);