|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Captcha\Plugin; |
| 9 | + |
| 10 | +use Magento\Captcha\Helper\Data as HelperCaptcha; |
| 11 | +use Magento\Customer\Block\Account\AuthenticationPopup; |
| 12 | +use Magento\Customer\Model\Context; |
| 13 | +use Magento\Framework\App\Http\Context as HttpContext; |
| 14 | + |
| 15 | +/** |
| 16 | + * Check need captcha for authentication popup |
| 17 | + */ |
| 18 | +class CheckCaptchaOnStorefront |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @var HelperCaptcha |
| 22 | + */ |
| 23 | + protected $helper; |
| 24 | + |
| 25 | + /** |
| 26 | + * Customer session |
| 27 | + * |
| 28 | + * @var HttpContext |
| 29 | + */ |
| 30 | + protected $httpContext; |
| 31 | + |
| 32 | + /** |
| 33 | + * CheckCaptchaOnStorefront constructor |
| 34 | + * |
| 35 | + * @param HelperCaptcha $helper |
| 36 | + * @param HttpContext $httpContext |
| 37 | + */ |
| 38 | + public function __construct( |
| 39 | + HelperCaptcha $helper, |
| 40 | + HttpContext $httpContext |
| 41 | + ) { |
| 42 | + $this->helper = $helper; |
| 43 | + $this->httpContext = $httpContext; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Remove template when loggin or disable captcha storefront |
| 48 | + * |
| 49 | + * @param AuthenticationPopup $subject |
| 50 | + * @param string $result |
| 51 | + * @return string |
| 52 | + * |
| 53 | + * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
| 54 | + */ |
| 55 | + public function afterGetTemplate( |
| 56 | + AuthenticationPopup $subject, |
| 57 | + $result |
| 58 | + ) { |
| 59 | + if ($this->isLoggedIn() || !$this->helper->getConfig('enable')) { |
| 60 | + return ''; |
| 61 | + } |
| 62 | + |
| 63 | + return $result; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Is logged in |
| 68 | + * |
| 69 | + * @return bool |
| 70 | + */ |
| 71 | + private function isLoggedIn() |
| 72 | + { |
| 73 | + return $this->httpContext->getValue(Context::CONTEXT_AUTH); |
| 74 | + } |
| 75 | +} |
0 commit comments