Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to be compatible with CMS 5 #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
14 changes: 8 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@
"license": "MIT",

"require": {
"silverstripe/framework": "^4",
"silverstripe/admin": "^1.0",
"symbiote/silverstripe-gridfieldextensions": "^3",
"php": "^8.1",
"silverstripe/framework": "^5",
"silverstripe/admin": "^2",
"symbiote/silverstripe-gridfieldextensions": "^4",
"s1lentium/iptools": "^1.1",
"silverstripe/auditor": "^2"
"silverstripe/auditor": "^3",
"silverstripe/errorpage": "^2"
},

"require-dev": {
"phpunit/phpunit": "^5.7",
"squizlabs/php_codesniffer": "^3.0"
"phpunit/phpunit": "^9.5",
"squizlabs/php_codesniffer": "^3"
},

"autoload": {
Expand Down
26 changes: 13 additions & 13 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/silverstripe/framework/tests/bootstrap.php" colors="true">
<testsuite name="madmatt/silverstripe-iplists">
<directory>tests/</directory>
</testsuite>
<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src/</directory>
<exclude>
<directory suffix=".php">tests/</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/silverstripe/framework/tests/bootstrap.php" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage includeUncoveredFiles="true">
<include>
<directory suffix=".php">src/</directory>
</include>
<exclude>
<directory suffix=".php">tests/</directory>
</exclude>
</coverage>
<testsuite name="madmatt/silverstripe-iplists">
<directory>tests/</directory>
</testsuite>
</phpunit>
15 changes: 8 additions & 7 deletions src/Admin/IPListAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,28 @@

class IPListAdmin extends ModelAdmin
{
private static $menu_title = 'IP Lists';
private static string $menu_title = 'IP Lists';

private static $url_segment = 'ip-lists';
private static string $url_segment = 'ip-lists';

private static $menu_icon_class = 'font-icon-lock';
private static string $menu_icon_class = 'font-icon-lock';

private static $managed_models = [
private static array $managed_models = [
IPList::class,
IP::class
];

/**
* @inheritDoc
*/
public function getEditForm($id = null, $fields = null)
{
$form = parent::getEditForm($id, $fields);

if ($this->modelClass == IP::class) {
if ($this->modelClass === IP::class) {
$msg = '<div class="alert alert-warning"><strong>Caution:</strong> Removing IPs from this list will remove'
. ' them from all IP lists and the database.</div>';
$form->Fields()->insertBefore($this->sanitiseClassName(IP::class), LiteralField::create('IPHelper', $msg));


}

return $form;
Expand Down
12 changes: 6 additions & 6 deletions src/Middleware/IPListMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,41 @@
use Madmatt\IPLists\Service\IPListService;
use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Control\Middleware\HTTPMiddleware;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Injector\Injectable;

class IPListMiddleware implements HTTPMiddleware
{
use Configurable, Injectable;
use Configurable;
use Injectable;

/**
* @var bool Defaults to true, set this to false via YML config to globally disable the middleware
* @config
*/
private static $enabled = true;
private static bool $enabled = true;

/**
* @var bool Defaults to true, set this to false via YML config to disable the middleware on development enviroments
* @config
*/
private static $enabled_on_dev = true;
private static bool $enabled_on_dev = true;

/**
* @var bool Defaults to false, set this to true via YML config to enable the middleware on the command-line
* interface (cli). This isn't generally considered a good idea, and may be removed in future (note: this middleware
* extends *HTTPMiddleware*, so you'd only expect it to apply to HTTP requests but it also applies to cli).
* @config
*/
private static $enabled_on_cli = false;
private static bool $enabled_on_cli = false;

/**
* @var IPListService
*/
public $service;

private static $dependencies = [
private static array $dependencies = [
'service' => '%$' . IPListService::class
];

Expand Down
14 changes: 6 additions & 8 deletions src/Model/IP.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
namespace Madmatt\IPLists\Model;

use Exception;
use IPTools\Network;
use IPTools\Range;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\ORM\FieldType\DBVarchar;
use SilverStripe\ORM\ManyManyList;

/**
Expand All @@ -30,29 +28,29 @@
*/
class IP extends DataObject
{
private static $singular_name = 'IP';
private static string $singular_name = 'IP';

private static $table_name = 'IPLists_IP';
private static string $table_name = 'IPLists_IP';

private static $db = [
'AddressType' => 'Enum(array("' . self::TYPE_IP . '","' . self::TYPE_CIDR . '"), "' . self::TYPE_IP . '")',
'IP' => 'Varchar(45)', // IPv6 addresses can be up to 45 characters, if they include an IPv4 mapped IPv6 address
'Title' => 'Varchar(200)',
];

private static $belongs_many_many = [
private static array $belongs_many_many = [
'Lists' => IPList::class
];

private static $summary_fields = [
private static array $summary_fields = [
'Title' => 'Title',
'IP' => 'IP',
'AddressType' => 'AddressType',
'UsedInLists' => 'Used in...'
];

const TYPE_IP = 'IP';
const TYPE_CIDR = 'CIDR';
public const TYPE_IP = 'IP';
public const TYPE_CIDR = 'CIDR';

public function getCMSFields()
{
Expand Down
34 changes: 12 additions & 22 deletions src/Model/IPList.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,11 @@
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridField_ActionMenu;
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
use SilverStripe\Forms\GridField\GridFieldAddNewButton;
use SilverStripe\Forms\GridField\GridFieldButtonRow;
use SilverStripe\Forms\GridField\GridFieldConfig;
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;
use SilverStripe\Forms\GridField\GridFieldDataColumns;
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
use SilverStripe\Forms\GridField\GridFieldDetailForm;
use SilverStripe\Forms\GridField\GridFieldEditButton;
use SilverStripe\Forms\GridField\GridFieldFilterHeader;
use SilverStripe\Forms\GridField\GridFieldPageCount;
use SilverStripe\Forms\GridField\GridFieldPaginator;
use SilverStripe\Forms\GridField\GridFieldSortableHeader;
use SilverStripe\Forms\GridField\GridFieldToolbarHeader;
use SilverStripe\Forms\LiteralField;
use SilverStripe\Forms\NumericField;
Expand All @@ -30,11 +23,8 @@
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\ToggleCompositeField;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\ORM\FieldType\DBHTMLVarchar;
use Symbiote\GridFieldExtensions\GridFieldAddNewInlineButton;
use Symbiote\GridFieldExtensions\GridFieldEditableColumns;
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
use Symbiote\GridFieldExtensions\GridFieldTitleHeader;

/**
Expand All @@ -54,13 +44,13 @@
*/
class IPList extends DataObject
{
private static $singular_name = 'IP list';
private static string $singular_name = 'IP list';

private static $table_name = 'IPLists_IPList';
private static string $table_name = 'IPLists_IPList';

private static $default_sort = 'Priority DESC, ID ASC';
private static string $default_sort = 'Priority DESC, ID ASC';

private static $db = [
private static array $db = [
'Title' => 'Varchar(200)',
'Description' => 'Text',
'Enabled' => 'Boolean',
Expand All @@ -70,36 +60,36 @@ class IPList extends DataObject
'ProtectedRoutes' => 'Text',
];

private static $many_many = [
private static array $many_many = [
'IPs' => IP::class
];

private static $many_many_extraFields = [
private static array $many_many_extraFields = [
'IPs' => [
'Sort' => 'Int'
]
];

private static $defaults = [
private static array $defaults = [
'Enabled' => true,
'Priority' => 100,
'ListType' => self::LIST_TYPE_ALLOW,
'DenyMethod' => self::DENY_METHOD_404,
];

private static $summary_fields = [
private static array $summary_fields = [
'Title' => 'Title',
'Description' => 'Description',
'Enabled.Nice' => 'Is Enabled?',
'ProtectedRoutes' => 'Protected URLs',
'IPs.Count' => 'Number of IPs'
];

const LIST_TYPE_DENY = 'Deny';
const LIST_TYPE_ALLOW = 'Allow';
public const LIST_TYPE_DENY = 'Deny';
public const LIST_TYPE_ALLOW = 'Allow';

const DENY_METHOD_404 = 404;
const DENY_METHOD_400 = 400;
public const DENY_METHOD_404 = 404;
public const DENY_METHOD_400 = 400;

public function getCMSFields()
{
Expand Down
13 changes: 5 additions & 8 deletions src/Service/IPListService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use LogicException;
use Madmatt\IPLists\Model\IP;
use Madmatt\IPLists\Model\IPList;
use Monolog\Level;
use Monolog\Logger;
use PageController;
use SilverStripe\Control\Controller;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\Session;
Expand All @@ -22,7 +22,7 @@ class IPListService
*/
public $auditLogger;

private static $dependencies = [
private static array $dependencies = [
'auditLogger' => '%$AuditLogger'
];

Expand All @@ -39,10 +39,7 @@ class IPListService
public const IP_ACCESS_DENIED = 0;
public const IP_ACCESS_AMBIVALENT = -1;

/**
* @var array
*/
private $validLists = [];
private array $validLists = [];

/**
* Check all {@link IPList} objects to find any that match the given URL route. If one is found, confirm whether the
Expand Down Expand Up @@ -214,13 +211,13 @@ protected function log(int $allowOrDeny = -1, array $context = [])
switch ($allowOrDeny) {
case self::IP_ACCESS_ALLOWED:
$message = $successLogMsg;
$logLevel = Logger::DEBUG;
$logLevel = Level::Debug;
$msgType = 'success';
break;

case self::IP_ACCESS_DENIED:
$message = $failureLogMsg;
$logLevel = Logger::WARNING;
$logLevel = Level::Warning;
$msgType = 'failure';
break;

Expand Down
13 changes: 9 additions & 4 deletions tests/Model/IPListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,28 @@

use Madmatt\IPLists\Model\IPList;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\LiteralField;

class IPListTest extends SapphireTest
{
protected $usesDatabase = true;

public function testGetCMSFieldsIPHelpMessage()
public function testGetCMSFieldsIPHelpMessage(): void
{
$list = new IPList();
$list = new IPList([
'Title' => 'Title',
'ProtectedRoutes' => 'routes'
]);
$fields = $list->getCMSFields();

// IPHelpMessage should only appear if the list hasn't been saved yet, and should be replaced by the GridField
$this->assertTrue($fields->hasField('IPHelpMessage'));
$this->assertInstanceOf(LiteralField::class, $fields->fieldByName('IPHelpMessage'));
$this->assertFalse($fields->hasField('IPs'));

$list->write();
$fields = $list->getCMSFields();
$this->assertFalse($fields->hasField('IPHelpMessage'));
$this->assertTrue($fields->hasField('IPs'));
$this->assertInstanceOf(GridField::class, $fields->dataFieldByName('IPs'));
}
}
4 changes: 2 additions & 2 deletions tests/Model/IPTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class IPTest extends SapphireTest
{
public function testContainsIP()
public function testContainsIP(): void
{
$ip = new IP([
'IP' => '10.1.1.1',
Expand All @@ -22,7 +22,7 @@ public function testContainsIP()
$this->assertFalse($ip->contains('::1'));
}

public function testContainsCIDR()
public function testContainsCIDR(): void
{
$ip = new IP([
'IP' => '10.1.1.1/24',
Expand Down