Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor/
composer.lock
.phpunit.result.cache
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# cakephp3-ip-filter
restrict access by ip address for CakePHP3 Component
restrict access by ip address for CakePHP4 Component

## Installation

Expand Down
11 changes: 5 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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-set": "^2.1"
},
"require-dev": {
"phpunit/phpunit": "^5.7|^6.0"
"phpunit/phpunit": "^8.5"
},
"autoload": {
"psr-4": {
Expand Down
9 changes: 4 additions & 5 deletions src/Controller/Component/IpFilterComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
}
Expand All @@ -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);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/TestCase/Controller/Component/IpFilterComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class IpFilterComponentTest extends TestCase
*
* @return void
*/
public function setUp()
public function setUp(): void
{
parent::setUp();
$this->_oSERVER = $_SERVER;
Expand All @@ -53,7 +53,7 @@ public function setUp()
*
* @return void
*/
public function tearDown()
public function tearDown(): void
{
parent::tearDown();

Expand Down Expand Up @@ -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');
}
}
6 changes: 4 additions & 2 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);