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

[FEATURE] Implements method to resolve siteKey and siteSecretKey variants based on conditions evaluated using Expression Language. #14

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
37 changes: 35 additions & 2 deletions Classes/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace StudioMitte\FriendlyCaptcha;

use Symfony\Component\ExpressionLanguage\SyntaxError;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\ExpressionLanguage\Resolver;
use TYPO3\CMS\Core\Site\Entity\Site;
use TYPO3\CMS\Core\Utility\GeneralUtility;

Expand All @@ -28,8 +30,16 @@ public function __construct(Site $site = null)
return;
}
$siteConfiguration = $site->getConfiguration();
$this->siteKey = trim($siteConfiguration['friendlycaptcha_site_key'] ?? '');
$this->siteSecretKey = trim($siteConfiguration['friendlycaptcha_secret_key'] ?? '');

$this->siteKey = $this->resolveValueWithVariants(
trim($siteConfiguration['friendlycaptcha_site_key'] ?? ''),
$siteConfiguration['friendlycaptcha_site_key_variants'] ?? null
);
$this->siteSecretKey = $this->resolveValueWithVariants(
trim($siteConfiguration['friendlycaptcha_secret_key'] ?? ''),
$siteConfiguration['friendlycaptcha_secret_key_variants'] ?? null,
);

$this->puzzleUrl = trim($siteConfiguration['friendlycaptcha_puzzle_url'] ?? '');
$this->verifyUrl = trim($siteConfiguration['friendlycaptcha_verify_url'] ?? '');
$this->jsPath = trim($siteConfiguration['friendlycaptcha_js_path'] ?? '');
Expand Down Expand Up @@ -76,4 +86,27 @@ public function hasSkipDevValidation(): bool
{
return Environment::getContext()->isDevelopment() && $this->skipDevValidation;
}

protected function resolveValueWithVariants(string $value, ?array $variants): string
{
if (!empty($variants)) {
$expressionLanguageResolver = GeneralUtility::makeInstance(
Resolver::class,
'site',
[]
);
foreach ($variants as $variant) {
try {
if ($expressionLanguageResolver->evaluate($variant['condition'])) {
$value = trim($variant['value']);
break;
}
} catch (SyntaxError $e) {
// silently fail and do not evaluate
// no logger here, as Site is currently cached and serialized
}
}
}
return $value;
}
}