Skip to content

Commit 2090148

Browse files
dimitriBouteilleDimitri BOUTEILLE
andauthored
Add trusted IP feature (#51)
* 💎 Add whitelist feature * Use trusted ips keyword --------- Co-authored-by: Dimitri BOUTEILLE <dimitri.bouteille@reflet-digital.com>
1 parent f0f8482 commit 2090148

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

Model/Config.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class Config
2121

2222
public const CONFIG_PATH_ENDPOINT = 'imi_friendly_captcha/general/endpoint';
2323

24+
public const CONFIG_PATH_TRUSTED_IPS = 'imi_friendly_captcha/general/trusted_ips';
25+
2426
public const CONFIG_PATH_ENABLED_FRONTEND = 'imi_friendly_captcha/frontend/enabled';
2527

2628
public const CONFIG_PATH_ENABLED_FRONTEND_LOGIN = 'imi_friendly_captcha/frontend/enabled_login';
@@ -261,4 +263,19 @@ public function isEnabledFrontendSendFriend(): bool
261263
ScopeInterface::SCOPE_WEBSITE
262264
);
263265
}
266+
267+
/**
268+
* Returns trusted IPS
269+
*
270+
* @return array<string>
271+
*/
272+
public function getTrustedIps(): array
273+
{
274+
$config = $this->scopeConfig->getValue(static::CONFIG_PATH_TRUSTED_IPS);
275+
if ($config === null || $config === '') {
276+
return [];
277+
}
278+
279+
return array_map('trim', explode(',', (string)$config));
280+
}
264281
}

Model/Validate.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Framework\HTTP\Client\CurlFactory;
1414
use Magento\Framework\Serialize\Serializer\Json;
1515
use Psr\Log\LoggerInterface;
16+
use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;
1617

1718
class Validate implements ValidateInterface
1819
{
@@ -43,23 +44,31 @@ class Validate implements ValidateInterface
4344
private $serializer;
4445

4546
/**
46-
* Validate constructor.
47+
* @var
48+
*/
49+
private $remoteAddress;
50+
51+
/**
52+
* Validate Constructor
4753
*
4854
* @param LoggerInterface $logger
4955
* @param Config $config
5056
* @param CurlFactory $curlFactory
5157
* @param Json $serializer
58+
* @param RemoteAddress $remoteAddress
5259
*/
5360
public function __construct(
5461
LoggerInterface $logger,
5562
Config $config,
5663
CurlFactory $curlFactory,
57-
Json $serializer
64+
Json $serializer,
65+
RemoteAddress $remoteAddress
5866
) {
5967
$this->logger = $logger;
6068
$this->config = $config;
6169
$this->curlFactory = $curlFactory;
6270
$this->serializer = $serializer;
71+
$this->remoteAddress = $remoteAddress;
6372
}
6473

6574
/**
@@ -71,6 +80,12 @@ public function __construct(
7180
*/
7281
public function validate(string $friendlyCaptchaSolution): bool
7382
{
83+
$ips = $this->config->getTrustedIps();
84+
$clientIp = $this->remoteAddress->getRemoteAddress();
85+
if ($ips !== [] && in_array((string)$clientIp, $ips, true)) {
86+
return true;
87+
}
88+
7489
$parameters = [
7590
self::PARAMETER_SOLUTION => $friendlyCaptchaSolution,
7691
self::PARAMETER_SECRET => $this->config->getApikey(),

etc/adminhtml/system.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@
5050
</depends>
5151
<validate>required-entry validate-url validate-no-html-tags</validate>
5252
</field>
53+
54+
<field id="trusted_ips" translate="label comment tooltip" type="textarea" sortOrder="33"
55+
showInDefault="1" showInWebsite="0" showInStore="0" canRestore="1">
56+
<label>Trusted IPS</label>
57+
<tooltip>Friendly captcha will be disabled for the specified IP addresses</tooltip>
58+
<comment><![CDATA[For example: <strong>192.168.1.2</strong> </br>Each IP must be seperated by comma]]></comment>
59+
</field>
5360
</group>
5461
<group id="frontend" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1"
5562
showInStore="1">

0 commit comments

Comments
 (0)