Skip to content
This repository was archived by the owner on Dec 19, 2019. It is now read-only.

Commit 65fd603

Browse files
Merge pull request #3691 from magento-qwerty/2.3-bugfixes-040219
Fixed issues: - MAGETWO-95400: Incorrect send-friend feature flow - MAGETWO-96505: Fixed incorrect stacktrace displaying - MC-13633: File Read Configuration
2 parents 28760f2 + cd7f153 commit 65fd603

File tree

20 files changed

+418
-1348
lines changed

20 files changed

+418
-1348
lines changed

.htaccess

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
############################################
3131
## default index file
32+
## Specifies option, to use methods arguments in backtrace or not
33+
SetEnv MAGE_DEBUG_SHOW_ARGS 1
3234

3335
DirectoryIndex index.php
3436

.htaccess.sample

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
############################################
3131
## default index file
32+
## Specifies option, to use methods arguments in backtrace or not
33+
SetEnv MAGE_DEBUG_SHOW_ARGS 1
3234

3335
DirectoryIndex index.php
3436

app/bootstrap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* Environment initialization
99
*/
1010
error_reporting(E_ALL);
11+
stream_wrapper_unregister('phar');
1112
#ini_set('display_errors', 1);
1213

1314
/* PHP version validation */

app/code/Magento/SendFriend/Block/Send.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\SendFriend\Block;
77

8+
use Magento\Captcha\Block\Captcha;
89
use Magento\Customer\Model\Context;
910

1011
/**
@@ -170,6 +171,7 @@ public function setFormData($data)
170171
/**
171172
* Retrieve Current Product Id
172173
*
174+
* @SuppressWarnings(PHPMD.RequestAwareBlockMethod)
173175
* @return int
174176
*/
175177
public function getProductId()
@@ -180,6 +182,7 @@ public function getProductId()
180182
/**
181183
* Retrieve current category id for product
182184
*
185+
* @SuppressWarnings(PHPMD.RequestAwareBlockMethod)
183186
* @return int
184187
*/
185188
public function getCategoryId()
@@ -222,4 +225,24 @@ public function canSend()
222225
{
223226
return !$this->sendfriend->isExceedLimit();
224227
}
228+
229+
/**
230+
* @inheritdoc
231+
*/
232+
protected function _prepareLayout()
233+
{
234+
if (!$this->getChildBlock('captcha')) {
235+
$this->addChild(
236+
'captcha',
237+
Captcha::class,
238+
[
239+
'cacheable' => false,
240+
'after' => '-',
241+
'form_id' => 'product_sendtofriend_form',
242+
'image_width' => 230,
243+
'image_height' => 230
244+
]
245+
);
246+
}
247+
}
225248
}

app/code/Magento/SendFriend/Controller/Product/Send.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
*/
66
namespace Magento\SendFriend\Controller\Product;
77

8+
use Magento\Framework\App\Action\HttpGetActionInterface;
89
use Magento\Framework\Controller\ResultFactory;
910

10-
class Send extends \Magento\SendFriend\Controller\Product
11+
/**
12+
* Controller class. Represents rendering and request flow
13+
*/
14+
class Send extends \Magento\SendFriend\Controller\Product implements HttpGetActionInterface
1115
{
1216
/**
1317
* @var \Magento\Catalog\Model\Session

app/code/Magento/SendFriend/Controller/Product/Sendmail.php

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,18 @@
66

77
namespace Magento\SendFriend\Controller\Product;
88

9+
use Magento\Framework\App\Action\HttpPostActionInterface;
10+
use Magento\Framework\App\ObjectManager;
911
use Magento\Framework\Exception\NoSuchEntityException;
1012
use Magento\Framework\Controller\ResultFactory;
13+
use Magento\SendFriend\Model\CaptchaValidator;
1114

12-
class Sendmail extends \Magento\SendFriend\Controller\Product
15+
/**
16+
* Class Sendmail. Represents request flow logic of 'sendmail' feature
17+
*
18+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
19+
*/
20+
class Sendmail extends \Magento\SendFriend\Controller\Product implements HttpPostActionInterface
1321
{
1422
/**
1523
* @var \Magento\Catalog\Api\CategoryRepositoryInterface
@@ -22,13 +30,21 @@ class Sendmail extends \Magento\SendFriend\Controller\Product
2230
protected $catalogSession;
2331

2432
/**
33+
* @var CaptchaValidator
34+
*/
35+
private $captchaValidator;
36+
37+
/**
38+
* Sendmail class construct
39+
*
2540
* @param \Magento\Framework\App\Action\Context $context
2641
* @param \Magento\Framework\Registry $coreRegistry
2742
* @param \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
2843
* @param \Magento\SendFriend\Model\SendFriend $sendFriend
2944
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
3045
* @param \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository
3146
* @param \Magento\Catalog\Model\Session $catalogSession
47+
* @param CaptchaValidator|null $captchaValidator
3248
*/
3349
public function __construct(
3450
\Magento\Framework\App\Action\Context $context,
@@ -37,29 +53,27 @@ public function __construct(
3753
\Magento\SendFriend\Model\SendFriend $sendFriend,
3854
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
3955
\Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository,
40-
\Magento\Catalog\Model\Session $catalogSession
56+
\Magento\Catalog\Model\Session $catalogSession,
57+
CaptchaValidator $captchaValidator = null
4158
) {
4259
parent::__construct($context, $coreRegistry, $formKeyValidator, $sendFriend, $productRepository);
4360
$this->categoryRepository = $categoryRepository;
4461
$this->catalogSession = $catalogSession;
62+
$this->captchaValidator = $captchaValidator ?: ObjectManager::getInstance()->create(CaptchaValidator::class);
4563
}
4664

4765
/**
4866
* Send Email Post Action
4967
*
5068
* @return \Magento\Framework\Controller\ResultInterface
5169
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
70+
* @SuppressWarnings(PHPMD.NPathComplexity)
5271
*/
5372
public function execute()
5473
{
5574
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
5675
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
5776

58-
if (!$this->_formKeyValidator->validate($this->getRequest())) {
59-
$resultRedirect->setPath('sendfriend/product/send', ['_current' => true]);
60-
return $resultRedirect;
61-
}
62-
6377
$product = $this->_initProduct();
6478
$data = $this->getRequest()->getPostValue();
6579

@@ -89,6 +103,9 @@ public function execute()
89103

90104
try {
91105
$validate = $this->sendFriend->validate();
106+
107+
$this->captchaValidator->validateSending($this->getRequest());
108+
92109
if ($validate === true) {
93110
$this->sendFriend->send();
94111
$this->messageManager->addSuccess(__('The link to a friend was sent.'));
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\SendFriend\Model;
10+
11+
use Magento\Framework\App\RequestInterface;
12+
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\Captcha\Helper\Data;
14+
use Magento\Captcha\Model\DefaultModel;
15+
use Magento\Captcha\Observer\CaptchaStringResolver;
16+
use Magento\Authorization\Model\UserContextInterface;
17+
use Magento\Customer\Api\CustomerRepositoryInterface;
18+
19+
/**
20+
* Class CaptchaValidator. Performs captcha validation
21+
*/
22+
class CaptchaValidator
23+
{
24+
/**
25+
* @var Data
26+
*/
27+
private $captchaHelper;
28+
29+
/**
30+
* @var CaptchaStringResolver
31+
*/
32+
private $captchaStringResolver;
33+
34+
/**
35+
* @var UserContextInterface
36+
*/
37+
private $currentUser;
38+
39+
/**
40+
* @var CustomerRepositoryInterface
41+
*/
42+
private $customerRepository;
43+
44+
/**
45+
* CaptchaValidator constructor.
46+
*
47+
* @param Data $captchaHelper
48+
* @param CaptchaStringResolver $captchaStringResolver
49+
* @param UserContextInterface $currentUser
50+
* @param CustomerRepositoryInterface $customerRepository
51+
*/
52+
public function __construct(
53+
Data $captchaHelper,
54+
CaptchaStringResolver $captchaStringResolver,
55+
UserContextInterface $currentUser,
56+
CustomerRepositoryInterface $customerRepository
57+
) {
58+
$this->captchaHelper = $captchaHelper;
59+
$this->captchaStringResolver = $captchaStringResolver;
60+
$this->currentUser = $currentUser;
61+
$this->customerRepository = $customerRepository;
62+
}
63+
64+
/**
65+
* Entry point for captcha validation
66+
*
67+
* @param RequestInterface $request
68+
* @throws LocalizedException
69+
* @throws \Magento\Framework\Exception\NoSuchEntityException
70+
*/
71+
public function validateSending(RequestInterface $request): void
72+
{
73+
$this->validateCaptcha($request);
74+
}
75+
76+
/**
77+
* Validates captcha and triggers log attempt
78+
*
79+
* @param RequestInterface $request
80+
* @throws LocalizedException
81+
* @throws \Magento\Framework\Exception\NoSuchEntityException
82+
*/
83+
private function validateCaptcha(RequestInterface $request): void
84+
{
85+
$captchaTargetFormName = 'product_sendtofriend_form';
86+
/** @var DefaultModel $captchaModel */
87+
$captchaModel = $this->captchaHelper->getCaptcha($captchaTargetFormName);
88+
89+
if ($captchaModel->isRequired()) {
90+
$word = $this->captchaStringResolver->resolve(
91+
$request,
92+
$captchaTargetFormName
93+
);
94+
95+
$isCorrectCaptcha = $captchaModel->isCorrect($word);
96+
97+
if (!$isCorrectCaptcha) {
98+
$this->logCaptchaAttempt($captchaModel);
99+
throw new LocalizedException(__('Incorrect CAPTCHA'));
100+
}
101+
}
102+
103+
$this->logCaptchaAttempt($captchaModel);
104+
}
105+
106+
/**
107+
* Log captcha attempts
108+
*
109+
* @param DefaultModel $captchaModel
110+
* @throws LocalizedException
111+
* @throws \Magento\Framework\Exception\NoSuchEntityException
112+
*/
113+
private function logCaptchaAttempt(DefaultModel $captchaModel): void
114+
{
115+
$email = '';
116+
117+
if ($this->currentUser->getUserType() == UserContextInterface::USER_TYPE_CUSTOMER) {
118+
$email = $this->customerRepository->getById($this->currentUser->getUserId())->getEmail();
119+
}
120+
121+
$captchaModel->logAttempt($email);
122+
}
123+
}

app/code/Magento/SendFriend/Model/SendFriend.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ protected function _construct()
163163
}
164164

165165
/**
166-
* Send email.
166+
* Sends email to recipients
167167
*
168168
* @return $this
169169
* @throws CoreException

0 commit comments

Comments
 (0)