-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #216 from magento-extensibility/MAGETWO-15665-IP-V…
…alues [Extensibility] MAGETWO-15665: Ip values validation fix
- Loading branch information
Showing
4 changed files
with
161 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
app/code/Magento/Developer/Model/Config/Backend/AllowedIps.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
/** | ||
* Copyright © 2015 Magento. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\Developer\Model\Config\Backend; | ||
|
||
/** | ||
* Backend model for validating ip addresses entered in Developer Client Restrictions | ||
* | ||
* Class AllowedIps | ||
*/ | ||
class AllowedIps extends \Magento\Framework\App\Config\Value | ||
{ | ||
/** | ||
* @var \Magento\Framework\Message\ManagerInterface | ||
*/ | ||
private $messageManager; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param \Magento\Framework\Model\Context $context | ||
* @param \Magento\Framework\Registry $registry | ||
* @param \Magento\Framework\App\Config\ScopeConfigInterface $config | ||
* @param \Magento\Framework\Message\ManagerInterface $messageManager | ||
* @param \Magento\Framework\Model\Resource\AbstractResource $resource | ||
* @param \Magento\Framework\Data\Collection\Db $resourceCollection | ||
* @param array $data | ||
*/ | ||
public function __construct( | ||
\Magento\Framework\Model\Context $context, | ||
\Magento\Framework\Registry $registry, | ||
\Magento\Framework\App\Config\ScopeConfigInterface $config, | ||
\Magento\Framework\Message\ManagerInterface $messageManager, | ||
\Magento\Framework\Model\Resource\AbstractResource $resource = null, | ||
\Magento\Framework\Data\Collection\Db $resourceCollection = null, | ||
array $data = [] | ||
) { | ||
$this->messageManager = $messageManager; | ||
parent::__construct($context, $registry, $config, $resource, $resourceCollection, $data); | ||
} | ||
|
||
/** | ||
* Validate ip addresses before save | ||
* | ||
* @return $this | ||
*/ | ||
public function beforeSave() | ||
{ | ||
$allowedIpsRaw = $this->getValue(); | ||
$noticeMsgArray = []; | ||
$allowedIpsArray = []; | ||
|
||
if (empty($allowedIpsRaw)) { | ||
return parent::beforeSave(); | ||
} | ||
|
||
$dataArray = explode(',', $allowedIpsRaw); | ||
foreach ($dataArray as $data) { | ||
if (filter_var(trim($data), FILTER_VALIDATE_IP)) { | ||
$allowedIpsArray[] = $data; | ||
} else { | ||
$noticeMsgArray[] = $data; | ||
} | ||
} | ||
|
||
$noticeMsg = implode(',', $noticeMsgArray); | ||
if (!empty($noticeMsgArray)) { | ||
$this->messageManager->addNotice( | ||
__( | ||
__('The following invalid values cannot be saved: %values', ['values' => $noticeMsg]) | ||
) | ||
); | ||
} | ||
|
||
$this->setValue(implode(',', $allowedIpsArray)); | ||
return parent::beforeSave(); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
app/code/Magento/Developer/Test/Unit/Model/Config/Backend/AllowedIpsTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
/** | ||
* Copyright © 2015 Magento. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\Developer\Test\Unit\Model\Config\Backend; | ||
|
||
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; | ||
|
||
/** | ||
* Class AllowedIpsTest | ||
*/ | ||
class AllowedIpsTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var \Magento\Developer\Model\Config\Backend\AllowedIps | ||
*/ | ||
protected $model; | ||
|
||
protected function setUp() | ||
{ | ||
$contextMock = $this->getMockBuilder('\Magento\Framework\Model\Context') | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
$eventMangerMock = $this->getMockBuilder('\Magento\Framework\Event\ManagerInterface') | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
$contextMock->expects($this->any()) | ||
->method('getEventDispatcher') | ||
->willReturn($eventMangerMock); | ||
|
||
$objectManagerHelper = new ObjectManagerHelper($this); | ||
|
||
$this->model = $objectManagerHelper->getObject( | ||
'Magento\Developer\Model\Config\Backend\AllowedIps', | ||
[ | ||
'context' => $contextMock, | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* @param string $value | ||
* @param string $expected | ||
* @dataProvider beforeSaveDataProvider | ||
* @return void | ||
*/ | ||
public function testBeforeSave($value, $expected) | ||
{ | ||
$this->assertNull($this->model->getValue()); | ||
$this->model->setValue($value); | ||
$this->model->beforeSave(); | ||
$this->assertEquals($expected, trim($this->model->getValue())); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function beforeSaveDataProvider() | ||
{ | ||
return [ | ||
[ '', '' ], | ||
[ ', 10.64.206.85, 10. 64.85.206 ', '10.64.206.85' ], | ||
[ '10.64.206.85, 10.64.1a.x, ,,', '10.64.206.85' ], | ||
[ ' ,, 10.64.206.85, 10.49.206.85 , ', '10.64.206.85, 10.49.206.85' ], | ||
[ '2001:db8:0:1234:0:567:8:1, ', '2001:db8:0:1234:0:567:8:1' ], /* valid IPV6 address */ | ||
[ '2001:0cb8:25a3:04c1:1324:8a2b:0471:8221', '2001:0cb8:25a3:04c1:1324:8a2b:0471:8221'], | ||
[ '255.255.255.255', '255.255.255.255'], /* valid private ip */ | ||
[ '127.0.0.1, ::1', '127.0.0.1, ::1'], /* valid reserved ip */ | ||
['*[789bo88n=], 12.34.56.78,[,q 049cq9840@@', '12.34.56.78'] | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters