Skip to content

Commit

Permalink
Extract N98_Guillotine into an independent module.
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Schröer committed Jun 9, 2017
0 parents commit 3837270
Show file tree
Hide file tree
Showing 21 changed files with 682 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Api/FilterSettingsResolverInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* @copyright Copyright (c) 1999-2017 netz98 GmbH (http://www.netz98.de)
*
* @see PROJECT_LICENSE.txt
*/

namespace N98\Guillotine\Api;

/**
* Interface FilterSettingsResolverInterface
*
* @api
*/
interface FilterSettingsResolverInterface
{
const SCOPE_CONFIG_WHITELIST_PATTERNS = 'n98_headless/guillotine/whitelist_patterns';

/**
* @return string[]
*/
public function execute();
}
24 changes: 24 additions & 0 deletions Api/RequestFilterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* @copyright Copyright (c) 1999-2017 netz98 GmbH (http://www.netz98.de)
*
* @see PROJECT_LICENSE.txt
*/

namespace N98\Guillotine\Api;

/**
* Interface RequestFilterInterface
*
* @api
*/
interface RequestFilterInterface
{
/**
* @param string $requestPath
*
* @return void
* @throws \N98\Guillotine\Exception\NotAllowedException
*/
public function execute($requestPath);
}
31 changes: 31 additions & 0 deletions Exception/NotAllowedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* @copyright Copyright (c) 1999-2017 netz98 GmbH (http://www.netz98.de)
*
* @see PROJECT_LICENSE.txt
*/

namespace N98\Guillotine\Exception;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Phrase;

/**
* Class NotAllowedException
*
* @api
*/
class NotAllowedException extends LocalizedException
{
const MSG_BLOCKED_DEFAULT = 'The requested page is blocked.';

/**
* @param Phrase|null $msg
*
* @return \N98\Guillotine\Exception\NotAllowedException
*/
public static function notAllowed($msg = null)
{
return new self(new Phrase($msg ?: self::MSG_BLOCKED_DEFAULT));
}
}
46 changes: 46 additions & 0 deletions Observer/FrontendBlacklistFilterObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* @copyright Copyright (c) 1999-2017 netz98 GmbH (http://www.netz98.de)
*
* @see PROJECT_LICENSE.txt
*/

namespace N98\Guillotine\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use N98\Guillotine\Api\RequestFilterInterface;

/**
* Class FrontendBlacklistFilterObserver
*/
class FrontendBlacklistFilterObserver implements ObserverInterface
{
/**
* @var \N98\Guillotine\Api\RequestFilterInterface
*/
private $requestFilter;

/**
* FrontendBlacklistFilterObserver constructor.
*
* @param \N98\Guillotine\Api\RequestFilterInterface $requestFilter
*/
public function __construct(RequestFilterInterface $requestFilter)
{
$this->requestFilter = $requestFilter;
}

/**
* @param Observer $observer
*
* @return void
*/
public function execute(Observer $observer)
{
$request = $observer->getEvent()->getDataByKey('request');
/* @var $request \Magento\Framework\App\Request\Http */

$this->requestFilter->execute($request->getPathInfo());
}
}
20 changes: 20 additions & 0 deletions PROJECT_LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2017 netz98 GmbH

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# N98_Guillotine

A simple but useful module to disable frontend requests to implement a headless approch.

## Installation

```bash

# Install module using composer
composer require n98/headless-guillotine

# Enable the module
bin/magento module:enable N98_Guillotine

# Upgrade Magento
bin/magento setup:upgrade

```

## Configuration

To add or edit a whitelist entry, simply navigate to the system configuration and modify the patterns to match your needs:

![Screenshot](./doc/configuration.png)
50 changes: 50 additions & 0 deletions Service/FilterSettingsResolverService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* @copyright Copyright (c) 1999-2017 netz98 GmbH (http://www.netz98.de)
*
* @see PROJECT_LICENSE.txt
*/

namespace N98\Guillotine\Service;

use Magento\Framework\App\Config\ScopeConfigInterface;
use N98\Guillotine\Api\FilterSettingsResolverInterface;

/**
* Class FilterSettingsResolverService
*/
class FilterSettingsResolverService implements FilterSettingsResolverInterface
{
/**
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
private $scopeConfig;

/**
* FilterSettingsResolverService constructor.
*
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
*/
public function __construct(ScopeConfigInterface $scopeConfig)
{
$this->scopeConfig = $scopeConfig;
}

/**
* @return string[]
*/
public function execute()
{
$data = array_map(
'trim',
explode(
"\n",
(string)$this->scopeConfig->getValue(self::SCOPE_CONFIG_WHITELIST_PATTERNS)
)
);

return array_values(
array_filter($data)
);
}
}
52 changes: 52 additions & 0 deletions Service/RequestFilterService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* @copyright Copyright (c) 1999-2017 netz98 GmbH (http://www.netz98.de)
*
* @see PROJECT_LICENSE.txt
*/

namespace N98\Guillotine\Service;

use N98\Guillotine\Api\FilterSettingsResolverInterface;
use N98\Guillotine\Api\RequestFilterInterface;
use N98\Guillotine\Exception\NotAllowedException;

/**
* Class RequestFilterService
*/
class RequestFilterService implements RequestFilterInterface
{
/**
* @var \N98\Guillotine\Api\FilterSettingsResolverInterface
*/
private $filterSettingsResolver;

/**
* RequestFilterService constructor.
*
* @param \N98\Guillotine\Api\FilterSettingsResolverInterface $filterSettingsResolver
*/
public function __construct(FilterSettingsResolverInterface $filterSettingsResolver)
{
$this->filterSettingsResolver = $filterSettingsResolver;
}

/**
* @param string $requestPath
*
* @return void
* @throws \N98\Guillotine\Exception\NotAllowedException
*/
public function execute($requestPath)
{
$whitelistPatterns = $this->filterSettingsResolver->execute();
foreach ($whitelistPatterns as $pattern) {
$regex = sprintf('#%s#', trim($pattern));
if (preg_match($regex, $requestPath, $matches)) {
return;
}
}

throw NotAllowedException::notAllowed();
}
}
45 changes: 45 additions & 0 deletions Tests/Unit/Exception/NotAllowedExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* @copyright Copyright (c) 1999-2017 netz98 GmbH (http://www.netz98.de)
*
* @see PROJECT_LICENSE.txt
*/

namespace N98\Guillotine\Tests\Unit\Exception;

use Magento\Framework\Phrase;
use N98\Guillotine\Exception\NotAllowedException;

/**
* Class NotAllowedExceptionTest
*
* @covers \N98\Guillotine\Exception\NotAllowedException
*/
class NotAllowedExceptionTest extends \PHPUnit_Framework_TestCase
{
/**
* @param string|null $msg
*
* @dataProvider dataProvider
*/
public function test($msg)
{
try {
throw NotAllowedException::notAllowed($msg);
} catch (NotAllowedException $exception) {
$this->assertAttributeInstanceOf(Phrase::class, 'phrase', $exception);
$this->assertEquals($msg ?: NotAllowedException::MSG_BLOCKED_DEFAULT, $exception->getRawMessage());
}
}

/**
* @return array[]
*/
public function dataProvider()
{
return [
[null],
['This is a test with a custom message.'],
];
}
}
72 changes: 72 additions & 0 deletions Tests/Unit/Observer/FrontendBlacklistFilterObserverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* @copyright Copyright (c) 1999-2017 netz98 GmbH (http://www.netz98.de)
*
* @see PROJECT_LICENSE.txt
*/

namespace N98\Guillotine\Tests\Unit\Observer;

use Magento\Framework\Event;
use Magento\Framework\Event\Observer;
use N98\Guillotine\Api\RequestFilterInterface;
use N98\Guillotine\Observer\FrontendBlacklistFilterObserver;
use PHPUnit_Framework_MockObject_MockObject as Mock;

/**
* Class FrontendBlacklistFilterObserverTest
*
* @covers \N98\Guillotine\Observer\FrontendBlacklistFilterObserver
*/
class FrontendBlacklistFilterObserverTest extends \PHPUnit_Framework_TestCase
{
const TEST_PATH = '/test/path';

public function test()
{
$filterMock = $this->getMockBuilder(RequestFilterInterface::class)
->getMock();
/* @var $filterMock RequestFilterInterface|Mock */

$filterMock
->expects($this->once())
->method('execute')
->with($this->equalTo(self::TEST_PATH));

$observerMock = $this->getMockBuilder(Observer::class)
->disableOriginalConstructor()
->setMethods(['getEvent'])
->getMock();
/* @var $observerMock Observer|Mock */

$eventMock = $this->getMockBuilder(Event::class)
->disableOriginalConstructor()
->setMethods(['getDataByKey'])
->getMock();
/* @var $eventMock Event|Mock */

$observerMock
->expects($this->once())
->method('getEvent')
->willReturn($eventMock);

$requestMock = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
->disableOriginalConstructor()
->setMethods(['getPathInfo'])
->getMock();

$requestMock
->expects($this->once())
->method('getPathInfo')
->willReturn(self::TEST_PATH);

$eventMock
->expects($this->once())
->method('getDataByKey')
->with($this->equalTo('request'))
->willReturn($requestMock);

$observer = new FrontendBlacklistFilterObserver($filterMock);
$observer->execute($observerMock);
}
}
Loading

0 comments on commit 3837270

Please sign in to comment.