Skip to content

Commit ff46e89

Browse files
committed
LYNX-363: Implement getting configurations for reCAPTCHA in GraphQL
1 parent 937cd5a commit ff46e89

File tree

13 files changed

+699
-32
lines changed

13 files changed

+699
-32
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*
6+
* NOTICE: All information contained herein is, and remains
7+
* the property of Adobe and its suppliers, if any. The intellectual
8+
* and technical concepts contained herein are proprietary to Adobe
9+
* and its suppliers and are protected by all applicable intellectual
10+
* property laws, including trade secret and copyright laws.
11+
* Dissemination of this information or reproduction of this material
12+
* is strictly forbidden unless prior written permission is obtained from
13+
* Adobe.
14+
*/
15+
declare(strict_types=1);
16+
17+
namespace Magento\ReCaptchaVersion2Checkbox\Model;
18+
19+
use Magento\Framework\ObjectManager\ResetAfterRequestInterface;
20+
use Magento\ReCaptchaVersion2Checkbox\Model\Frontend\UiConfigProvider;
21+
use Magento\ReCaptchaWebapiGraphQl\Model\Adapter\ReCaptchaConfigInterface;
22+
23+
class Config implements ReCaptchaConfigInterface, ResetAfterRequestInterface
24+
{
25+
/**
26+
* @var $minimumScore
27+
*/
28+
private ?float $minimumScore = null;
29+
30+
/**
31+
* @var array
32+
*/
33+
private array $uiConfig = [];
34+
35+
/**
36+
* @param UiConfigProvider $uiConfigProvider
37+
*/
38+
public function __construct(
39+
private readonly UiConfigProvider $uiConfigProvider,
40+
) {
41+
}
42+
43+
/**
44+
* Get front-end's UI configurations
45+
*
46+
* @return array
47+
*/
48+
public function getUiConfig(): array
49+
{
50+
if (empty($this->uiConfig)) {
51+
$this->uiConfig = $this->uiConfigProvider->get();
52+
}
53+
return $this->uiConfig;
54+
}
55+
56+
/**
57+
* Get website's Google API public key
58+
*
59+
* @return string
60+
*/
61+
public function getWebsiteKey(): string
62+
{
63+
return $this->getUiConfig()['rendering']['sitekey'];
64+
}
65+
66+
/**
67+
* Get configured captcha's theme
68+
*
69+
* @return string
70+
*/
71+
public function getTheme(): string
72+
{
73+
return $this->getUiConfig()['rendering']['theme'];
74+
}
75+
76+
/**
77+
* Get code of language to send notifications
78+
*
79+
* @return string
80+
*/
81+
public function getLanguageCode(): string
82+
{
83+
return $this->getUiConfig()['rendering']['hl'];
84+
}
85+
86+
/**
87+
* "I am not a robot" captcha does not provide configurable minimum score setting
88+
*
89+
* @return null
90+
*/
91+
public function getMinimumScore()
92+
{
93+
return $this->minimumScore;
94+
}
95+
96+
/**
97+
* ReCaptcha V2 does not provide configurable badge_position setting
98+
*
99+
* @return string
100+
*/
101+
public function getBadgePosition(): string
102+
{
103+
return '';
104+
}
105+
106+
/**
107+
* @inheritDoc
108+
*/
109+
public function _resetState(): void
110+
{
111+
$this->uiConfig = [];
112+
$this->minimumScore = null;
113+
}
114+
}

ReCaptchaVersion2Checkbox/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"magento/framework": "*",
77
"magento/module-store": "*",
88
"magento/module-re-captcha-ui": "*",
9-
"magento/module-re-captcha-validation-api": "*"
9+
"magento/module-re-captcha-validation-api": "*",
10+
"magento/module-re-captcha-webapi-graph-ql": "*"
1011
},
1112
"suggest": {
1213
"magento/module-config": "*",
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*
6+
* NOTICE: All information contained herein is, and remains
7+
* the property of Adobe and its suppliers, if any. The intellectual
8+
* and technical concepts contained herein are proprietary to Adobe
9+
* and its suppliers and are protected by all applicable intellectual
10+
* property laws, including trade secret and copyright laws.
11+
* Dissemination of this information or reproduction of this material
12+
* is strictly forbidden unless prior written permission is obtained from
13+
* Adobe.
14+
*/
15+
declare(strict_types=1);
16+
17+
namespace Magento\ReCaptchaVersion2Invisible\Model;
18+
19+
use Magento\Framework\ObjectManager\ResetAfterRequestInterface;
20+
use Magento\ReCaptchaVersion2Invisible\Model\Frontend\UiConfigProvider;
21+
use Magento\ReCaptchaWebapiGraphQl\Model\Adapter\ReCaptchaConfigInterface;
22+
23+
class Config implements ReCaptchaConfigInterface, ResetAfterRequestInterface
24+
{
25+
/**
26+
* @var array
27+
*/
28+
private array $uiConfig = [];
29+
30+
/**
31+
* @var $minimumScore
32+
*/
33+
private ?float $minimumScore = null;
34+
35+
/**
36+
* @param UiConfigProvider $uiConfigProvider
37+
*/
38+
public function __construct(
39+
private readonly UiConfigProvider $uiConfigProvider,
40+
) {
41+
}
42+
43+
/**
44+
* Get website's Google API public key
45+
*
46+
* @return string
47+
*/
48+
public function getWebsiteKey(): string
49+
{
50+
return $this->getUiConfig()['rendering']['sitekey'];
51+
}
52+
53+
/**
54+
* ReCaptcha V2 Invisible does not provide configurable minimum score setting
55+
*
56+
* @return null
57+
*/
58+
public function getMinimumScore()
59+
{
60+
return $this->minimumScore;
61+
}
62+
63+
/**
64+
* Get configured captcha's badge position
65+
*
66+
* @return string
67+
*/
68+
public function getBadgePosition(): string
69+
{
70+
return $this->getUiConfig()['rendering']['badge'];
71+
}
72+
73+
/**
74+
* Get configured captcha's theme
75+
*
76+
* @return string
77+
*/
78+
public function getTheme(): string
79+
{
80+
return $this->getUiConfig()['rendering']['theme'];
81+
}
82+
83+
/**
84+
* Get code of language to send notifications
85+
*
86+
* @return string
87+
*/
88+
public function getLanguageCode(): string
89+
{
90+
return $this->getUiConfig()['rendering']['hl'];
91+
}
92+
93+
/**
94+
* Get front-end's UI configurations
95+
*
96+
* @return array
97+
*/
98+
public function getUiConfig(): array
99+
{
100+
if (empty($this->uiConfig)) {
101+
$this->uiConfig = $this->uiConfigProvider->get();
102+
}
103+
return $this->uiConfig;
104+
}
105+
106+
/**
107+
* @inheritDoc
108+
*/
109+
public function _resetState(): void
110+
{
111+
$this->uiConfig = [];
112+
$this->minimumScore = null;
113+
}
114+
}

ReCaptchaVersion2Invisible/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"magento/framework": "*",
77
"magento/module-store": "*",
88
"magento/module-re-captcha-ui": "*",
9-
"magento/module-re-captcha-validation-api": "*"
9+
"magento/module-re-captcha-validation-api": "*",
10+
"magento/module-re-captcha-webapi-graph-ql": "*"
1011
},
1112
"suggest": {
1213
"magento/module-config": "*",

ReCaptchaVersion3Invisible/Model/Config.php

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
/**
43
* Copyright © Magento, Inc. All rights reserved.
54
* See COPYING.txt for license details.
@@ -12,8 +11,9 @@
1211
use Magento\ReCaptchaValidationApi\Api\Data\ValidationConfigInterface;
1312
use Magento\ReCaptchaVersion3Invisible\Model\Frontend\UiConfigProvider;
1413
use Magento\ReCaptchaVersion3Invisible\Model\Frontend\ValidationConfigProvider;
14+
use Magento\ReCaptchaWebapiGraphQl\Model\Adapter\ReCaptchaConfigInterface;
1515

16-
class Config implements ResetAfterRequestInterface
16+
class Config implements ReCaptchaConfigInterface, ResetAfterRequestInterface
1717
{
1818
/**
1919
* @var string|null
@@ -35,21 +35,11 @@ class Config implements ResetAfterRequestInterface
3535
*/
3636
private ?string $languageCode = null;
3737

38-
/**
39-
* @var UiConfigProvider
40-
*/
41-
private UiConfigProvider $uiConfigProvider;
42-
4338
/**
4439
* @var array
4540
*/
4641
private array $uiConfig = [];
4742

48-
/**
49-
* @var ValidationConfigProvider
50-
*/
51-
private ValidationConfigProvider $validationConfigProvider;
52-
5343
/**
5444
* @var ValidationConfigInterface|null
5545
*/
@@ -66,13 +56,11 @@ class Config implements ResetAfterRequestInterface
6656
* @param array $formTypes
6757
*/
6858
public function __construct(
69-
UiConfigProvider $uiConfigProvider,
70-
ValidationConfigProvider $validationConfigProvider,
59+
private readonly UiConfigProvider $uiConfigProvider,
60+
private readonly ValidationConfigProvider $validationConfigProvider,
7161
array $formTypes = []
7262
) {
7363
$this->formTypes = $formTypes;
74-
$this->uiConfigProvider = $uiConfigProvider;
75-
$this->validationConfigProvider = $validationConfigProvider;
7664
}
7765

7866
/**
@@ -91,12 +79,16 @@ public function getWebsiteKey(): string
9179
/**
9280
* Get configured minimum score value
9381
*
94-
* @return float
82+
* @return float|null
9583
*/
96-
public function getMinimumScore(): float
84+
public function getMinimumScore(): float|null
9785
{
9886
if (!$this->minimumScore) {
99-
$this->minimumScore = $this->validationConfig->getExtensionAttributes()->getScoreThreshold();
87+
$validationProvider = $this->validationConfigProvider->get();
88+
if ($validationProvider->getExtensionAttributes() === null) {
89+
return $this->minimumScore;
90+
}
91+
$this->minimumScore = $validationProvider->getExtensionAttributes()->getScoreThreshold();
10092
}
10193
return $this->minimumScore;
10294
}
@@ -114,6 +106,16 @@ public function getBadgePosition(): string
114106
return $this->badgePosition;
115107
}
116108

109+
/**
110+
* Get configured captcha's theme
111+
*
112+
* @return string
113+
*/
114+
public function getTheme(): string
115+
{
116+
return $this->getUiConfig()['rendering']['theme'];
117+
}
118+
117119
/**
118120
* Get code of language to send notifications
119121
*
@@ -155,7 +157,7 @@ public function getValidationConfig(): ValidationConfigInterface
155157
*
156158
* @return array
157159
*/
158-
private function getUiConfig(): array
160+
public function getUiConfig(): array
159161
{
160162
if (empty($this->uiConfig)) {
161163
$this->uiConfig = $this->uiConfigProvider->get();
@@ -168,8 +170,11 @@ private function getUiConfig(): array
168170
*/
169171
public function _resetState(): void
170172
{
171-
$this->websiteKey = null;
172173
$this->uiConfig = [];
174+
$this->websiteKey = null;
175+
$this->minimumScore = null;
176+
$this->languageCode = null;
177+
$this->badgePosition = null;
173178
$this->validationConfig = null;
174179
}
175180
}

ReCaptchaVersion3Invisible/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"magento/framework": "*",
77
"magento/module-store": "*",
88
"magento/module-re-captcha-ui": "*",
9-
"magento/module-re-captcha-validation-api": "*"
9+
"magento/module-re-captcha-validation-api": "*",
10+
"magento/module-re-captcha-webapi-graph-ql": "*"
1011
},
1112
"suggest": {
1213
"magento/module-config": "*",

0 commit comments

Comments
 (0)