Skip to content

magento/magento2#: Captcha. Improvement. Replace deprecated addError with addErrorMessage. #24340

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

Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\App\ObjectManager;

/**
* Class CheckContactUsFormObserver
*/
class CheckContactUsFormObserver implements ObserverInterface
{
/**
Expand Down Expand Up @@ -76,7 +79,7 @@ public function execute(\Magento\Framework\Event\Observer $observer)
/** @var \Magento\Framework\App\Action\Action $controller */
$controller = $observer->getControllerAction();
if (!$captcha->isCorrect($this->captchaStringResolver->resolve($controller->getRequest(), $formId))) {
$this->messageManager->addError(__('Incorrect CAPTCHA.'));
$this->messageManager->addErrorMessage(__('Incorrect CAPTCHA.'));
$this->getDataPersistor()->set($formId, $controller->getRequest()->getPostValue());
$this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
$this->redirect->redirect($controller->getResponse(), 'contact/index/index');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

use Magento\Framework\Event\ObserverInterface;

/**
* Class CheckForgotpasswordObserver
*/
class CheckForgotpasswordObserver implements ObserverInterface
{
/**
Expand Down Expand Up @@ -69,7 +72,7 @@ public function execute(\Magento\Framework\Event\Observer $observer)
/** @var \Magento\Framework\App\Action\Action $controller */
$controller = $observer->getControllerAction();
if (!$captchaModel->isCorrect($this->captchaStringResolver->resolve($controller->getRequest(), $formId))) {
$this->messageManager->addError(__('Incorrect CAPTCHA'));
$this->messageManager->addErrorMessage(__('Incorrect CAPTCHA'));
$this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
$this->redirect->redirect($controller->getResponse(), '*/*/forgotpassword');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

use Magento\Framework\Event\ObserverInterface;

/**
* Class CheckUserCreateObserver
*
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
*/
class CheckUserCreateObserver implements ObserverInterface
{
/**
Expand Down Expand Up @@ -86,7 +91,7 @@ public function execute(\Magento\Framework\Event\Observer $observer)
/** @var \Magento\Framework\App\Action\Action $controller */
$controller = $observer->getControllerAction();
if (!$captchaModel->isCorrect($this->captchaStringResolver->resolve($controller->getRequest(), $formId))) {
$this->messageManager->addError(__('Incorrect CAPTCHA'));
$this->messageManager->addErrorMessage(__('Incorrect CAPTCHA'));
$this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
$this->_session->setCustomerFormData($controller->getRequest()->getPostValue());
$url = $this->_urlManager->getUrl('*/*/create', ['_nosecret' => true]);
Expand Down
14 changes: 7 additions & 7 deletions app/code/Magento/Captcha/Observer/CheckUserEditObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@
use Magento\Framework\App\Config\ScopeConfigInterface;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* Class CheckUserEditObserver
*
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
*/
class CheckUserEditObserver implements ObserverInterface
{
/**
* Form ID
*/
const FORM_ID = 'user_edit';

/**
Expand Down Expand Up @@ -96,7 +95,8 @@ public function __construct(
* Check Captcha On Forgot Password Page
*
* @param \Magento\Framework\Event\Observer $observer
* @return $this
* @return $this|void
* @throws \Magento\Framework\Exception\SessionException
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
Expand All @@ -119,9 +119,9 @@ public function execute(\Magento\Framework\Event\Observer $observer)
'The account is locked. Please wait and try again or contact %1.',
$this->scopeConfig->getValue('contact/email/recipient_email')
);
$this->messageManager->addError($message);
$this->messageManager->addErrorMessage($message);
}
$this->messageManager->addError(__('Incorrect CAPTCHA'));
$this->messageManager->addErrorMessage(__('Incorrect CAPTCHA'));
$this->actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
$this->redirect->redirect($controller->getResponse(), '*/*/edit');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

use Magento\Framework\Event\ObserverInterface;

/**
* Class CheckUserForgotPasswordBackendObserver
*
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
*/
class CheckUserForgotPasswordBackendObserver implements ObserverInterface
{
/**
Expand Down Expand Up @@ -76,7 +81,7 @@ public function execute(\Magento\Framework\Event\Observer $observer)
) {
$this->_session->setEmail((string)$controller->getRequest()->getPost('email'));
$this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
$this->messageManager->addError(__('Incorrect CAPTCHA'));
$this->messageManager->addErrorMessage(__('Incorrect CAPTCHA'));
$controller->getResponse()->setRedirect(
$controller->getUrl('*/*/forgotpassword', ['_nosecret' => true])
);
Expand Down
10 changes: 6 additions & 4 deletions app/code/Magento/Captcha/Observer/CheckUserLoginObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

namespace Magento\Captcha\Observer;

use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Model\AuthenticationInterface;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Customer\Api\CustomerRepositoryInterface;

/**
* Check captcha on user login page observer.
Expand Down Expand Up @@ -64,6 +64,8 @@ class CheckUserLoginObserver implements ObserverInterface
protected $authentication;

/**
* CheckUserLoginObserver constructor.
*
* @param \Magento\Captcha\Helper\Data $helper
* @param \Magento\Framework\App\ActionFlag $actionFlag
* @param \Magento\Framework\Message\ManagerInterface $messageManager
Expand Down Expand Up @@ -125,8 +127,7 @@ private function getAuthentication()
* Check captcha on user login page
*
* @param \Magento\Framework\Event\Observer $observer
* @throws NoSuchEntityException
* @return $this
* @return $this|void
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
Expand All @@ -143,10 +144,11 @@ public function execute(\Magento\Framework\Event\Observer $observer)
try {
$customer = $this->getCustomerRepository()->get($login);
$this->getAuthentication()->processAuthenticationFailure($customer->getId());
// phpcs:ignore Magento2.CodeAnalysis.EmptyBlock
} catch (NoSuchEntityException $e) {
//do nothing as customer existence is validated later in authenticate method
}
$this->messageManager->addError(__('Incorrect CAPTCHA'));
$this->messageManager->addErrorMessage(__('Incorrect CAPTCHA'));
$this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
$this->_session->setUsername($login);
$beforeUrl = $this->_session->getBeforeAuthUrl();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ protected function setUp()
$this->messageManagerMock = $this->createMock(\Magento\Framework\Message\ManagerInterface::class);
$this->redirectMock = $this->createMock(\Magento\Framework\App\Response\RedirectInterface::class);
$this->captchaStringResolverMock = $this->createMock(\Magento\Captcha\Observer\CaptchaStringResolver::class);
$this->sessionMock = $this->createPartialMock(\Magento\Framework\Session\SessionManager::class, ['addError']);
$this->sessionMock = $this->createPartialMock(
\Magento\Framework\Session\SessionManager::class,
['addErrorMessage']
);
$this->dataPersistorMock = $this->getMockBuilder(\Magento\Framework\App\Request\DataPersistorInterface::class)
->getMockForAbstractClass();

Expand Down Expand Up @@ -116,7 +119,7 @@ public function testCheckContactUsFormWhenCaptchaIsRequiredAndValid()
$this->helperMock->expects($this->any())
->method('getCaptcha')
->with($formId)->willReturn($this->captchaMock);
$this->sessionMock->expects($this->never())->method('addError');
$this->sessionMock->expects($this->never())->method('addErrorMessage');

$this->checkContactUsFormObserver->execute(
new \Magento\Framework\Event\Observer(['controller_action' => $controller])
Expand Down Expand Up @@ -163,7 +166,7 @@ public function testCheckContactUsFormRedirectsCustomerWithWarningMessageWhenCap
->method('getCaptcha')
->with($formId)
->willReturn($this->captchaMock);
$this->messageManagerMock->expects($this->once())->method('addError')->with($warningMessage);
$this->messageManagerMock->expects($this->once())->method('addErrorMessage')->with($warningMessage);
$this->actionFlagMock->expects($this->once())
->method('set')
->with('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function testCheckForgotpasswordRedirects()
)->will(
$this->returnValue($this->_captcha)
);
$this->_messageManager->expects($this->once())->method('addError')->with($warningMessage);
$this->_messageManager->expects($this->once())->method('addErrorMessage')->with($warningMessage);
$this->_actionFlag->expects(
$this->once()
)->method(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public function testCheckUserCreateRedirectsError()
)->will(
$this->returnValue($this->_captcha)
);
$this->_messageManager->expects($this->once())->method('addError')->with($warningMessage);
$this->_messageManager->expects($this->once())->method('addErrorMessage')->with($warningMessage);
$this->_actionFlag->expects(
$this->once()
)->method(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public function testExecute()

$message = __('The account is locked. Please wait and try again or contact %1.', $email);
$this->messageManagerMock->expects($this->exactly(2))
->method('addError')
->method('addErrorMessage')
->withConsecutive([$message], [__('Incorrect CAPTCHA')]);

$this->actionFlagMock->expects($this->once())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public function testExecute()
->with($customerId);

$this->messageManagerMock->expects($this->once())
->method('addError')
->method('addErrorMessage')
->with(__('Incorrect CAPTCHA'));

$this->actionFlagMock->expects($this->once())
Expand Down
21 changes: 13 additions & 8 deletions app/code/Magento/Customer/Controller/Account/Confirmation.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Customer\Controller\Account;

use Magento\Customer\Api\AccountManagementInterface;
use Magento\Customer\Controller\AbstractAccount;
use Magento\Customer\Model\Session;
use Magento\Customer\Model\Url;
use Magento\Framework\App\Action\HttpGetActionInterface as HttpGetActionInterface;
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
use Magento\Framework\App\Action\Context;
use Magento\Customer\Model\Session;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\State\InvalidTransitionException;
use Magento\Framework\View\Result\PageFactory;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Customer\Api\AccountManagementInterface;
use Magento\Framework\Exception\State\InvalidTransitionException;

class Confirmation extends \Magento\Customer\Controller\AbstractAccount
/**
* Class Confirmation. Send confirmation link to specified email
*/
class Confirmation extends AbstractAccount implements HttpGetActionInterface, HttpPostActionInterface
{
/**
* @var \Magento\Store\Model\StoreManagerInterface
Expand Down Expand Up @@ -91,11 +96,11 @@ public function execute()
$email,
$this->storeManager->getStore()->getWebsiteId()
);
$this->messageManager->addSuccess(__('Please check your email for confirmation key.'));
$this->messageManager->addSuccessMessage(__('Please check your email for confirmation key.'));
} catch (InvalidTransitionException $e) {
$this->messageManager->addSuccess(__('This email does not require confirmation.'));
$this->messageManager->addSuccessMessage(__('This email does not require confirmation.'));
} catch (\Exception $e) {
$this->messageManager->addException($e, __('Wrong email.'));
$this->messageManager->addExceptionMessage($e, __('Wrong email.'));
$resultRedirect->setPath('*/*/*', ['email' => $email, '_secure' => true]);
return $resultRedirect;
}
Expand Down
29 changes: 20 additions & 9 deletions app/code/Magento/Customer/Model/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,9 @@ public function getSharingConfig()
public function authenticate($login, $password)
{
$this->loadByEmail($login);
if ($this->getConfirmation() && $this->isConfirmationRequired()) {
if ($this->getConfirmation() &&
$this->accountConfirmation->isConfirmationRequired($this->getWebsiteId(), $this->getId(), $this->getEmail())
) {
throw new EmailNotConfirmedException(
__("This account isn't confirmed. Verify and try again.")
);
Expand All @@ -415,8 +417,9 @@ public function authenticate($login, $password)
/**
* Load customer by email
*
* @param string $customerEmail
* @return $this
* @param string $customerEmail
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function loadByEmail($customerEmail)
{
Expand All @@ -427,8 +430,9 @@ public function loadByEmail($customerEmail)
/**
* Change customer password
*
* @param string $newPassword
* @return $this
* @param string $newPassword
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function changePassword($newPassword)
{
Expand All @@ -440,6 +444,7 @@ public function changePassword($newPassword)
* Get full customer name
*
* @return string
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getName()
{
Expand All @@ -462,8 +467,9 @@ public function getName()
/**
* Add address to address collection
*
* @param Address $address
* @return $this
* @param Address $address
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function addAddress(Address $address)
{
Expand All @@ -487,6 +493,7 @@ public function getAddressById($addressId)
*
* @param int $addressId
* @return Address
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getAddressItemById($addressId)
{
Expand All @@ -506,7 +513,8 @@ public function getAddressCollection()
/**
* Customer addresses collection
*
* @return \Magento\Customer\Model\ResourceModel\Address\Collection
* @return ResourceModel\Address\Collection
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getAddressesCollection()
{
Expand Down Expand Up @@ -538,6 +546,7 @@ public function getAddresses()
* Retrieve all customer attributes
*
* @return Attribute[]
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function getAttributes()
{
Expand Down Expand Up @@ -591,7 +600,8 @@ public function hashPassword($password, $salt = true)
* Validate password with salted hash
*
* @param string $password
* @return boolean
* @return bool
* @throws \Exception
*/
public function validatePassword($password)
{
Expand Down Expand Up @@ -805,6 +815,7 @@ public function isConfirmationRequired()
*/
public function getRandomConfirmationKey()
{
// phpcs:ignore Magento2.Security.InsecureFunction
return md5(uniqid());
}

Expand Down
Loading