LoginPost.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Controller\Account;
  7. use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
  8. use Magento\Customer\Model\Account\Redirect as AccountRedirect;
  9. use Magento\Framework\App\Action\Context;
  10. use Magento\Customer\Model\Session;
  11. use Magento\Customer\Api\AccountManagementInterface;
  12. use Magento\Customer\Model\Url as CustomerUrl;
  13. use Magento\Framework\App\CsrfAwareActionInterface;
  14. use Magento\Framework\App\Request\InvalidRequestException;
  15. use Magento\Framework\App\RequestInterface;
  16. use Magento\Framework\Controller\Result\Redirect;
  17. use Magento\Framework\Exception\EmailNotConfirmedException;
  18. use Magento\Framework\Exception\AuthenticationException;
  19. use Magento\Framework\Data\Form\FormKey\Validator;
  20. use Magento\Framework\Exception\LocalizedException;
  21. use Magento\Framework\Exception\State\UserLockedException;
  22. use Magento\Framework\App\Config\ScopeConfigInterface;
  23. use Magento\Customer\Controller\AbstractAccount;
  24. use Magento\Framework\Phrase;
  25. /**
  26. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  27. */
  28. class LoginPost extends AbstractAccount implements CsrfAwareActionInterface, HttpPostActionInterface
  29. {
  30. /**
  31. * @var \Magento\Customer\Api\AccountManagementInterface
  32. */
  33. protected $customerAccountManagement;
  34. /**
  35. * @var \Magento\Framework\Data\Form\FormKey\Validator
  36. */
  37. protected $formKeyValidator;
  38. /**
  39. * @var AccountRedirect
  40. */
  41. protected $accountRedirect;
  42. /**
  43. * @var Session
  44. */
  45. protected $session;
  46. /**
  47. * @var ScopeConfigInterface
  48. */
  49. private $scopeConfig;
  50. /**
  51. * @var \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory
  52. */
  53. private $cookieMetadataFactory;
  54. /**
  55. * @var \Magento\Framework\Stdlib\Cookie\PhpCookieManager
  56. */
  57. private $cookieMetadataManager;
  58. /**
  59. * @param Context $context
  60. * @param Session $customerSession
  61. * @param AccountManagementInterface $customerAccountManagement
  62. * @param CustomerUrl $customerHelperData
  63. * @param Validator $formKeyValidator
  64. * @param AccountRedirect $accountRedirect
  65. */
  66. public function __construct(
  67. Context $context,
  68. Session $customerSession,
  69. AccountManagementInterface $customerAccountManagement,
  70. CustomerUrl $customerHelperData,
  71. Validator $formKeyValidator,
  72. AccountRedirect $accountRedirect
  73. ) {
  74. $this->session = $customerSession;
  75. $this->customerAccountManagement = $customerAccountManagement;
  76. $this->customerUrl = $customerHelperData;
  77. $this->formKeyValidator = $formKeyValidator;
  78. $this->accountRedirect = $accountRedirect;
  79. parent::__construct($context);
  80. }
  81. /**
  82. * Get scope config
  83. *
  84. * @return ScopeConfigInterface
  85. * @deprecated 100.0.10
  86. */
  87. private function getScopeConfig()
  88. {
  89. if (!($this->scopeConfig instanceof \Magento\Framework\App\Config\ScopeConfigInterface)) {
  90. return \Magento\Framework\App\ObjectManager::getInstance()->get(
  91. \Magento\Framework\App\Config\ScopeConfigInterface::class
  92. );
  93. } else {
  94. return $this->scopeConfig;
  95. }
  96. }
  97. /**
  98. * Retrieve cookie manager
  99. *
  100. * @deprecated 100.1.0
  101. * @return \Magento\Framework\Stdlib\Cookie\PhpCookieManager
  102. */
  103. private function getCookieManager()
  104. {
  105. if (!$this->cookieMetadataManager) {
  106. $this->cookieMetadataManager = \Magento\Framework\App\ObjectManager::getInstance()->get(
  107. \Magento\Framework\Stdlib\Cookie\PhpCookieManager::class
  108. );
  109. }
  110. return $this->cookieMetadataManager;
  111. }
  112. /**
  113. * Retrieve cookie metadata factory
  114. *
  115. * @deprecated 100.1.0
  116. * @return \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory
  117. */
  118. private function getCookieMetadataFactory()
  119. {
  120. if (!$this->cookieMetadataFactory) {
  121. $this->cookieMetadataFactory = \Magento\Framework\App\ObjectManager::getInstance()->get(
  122. \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory::class
  123. );
  124. }
  125. return $this->cookieMetadataFactory;
  126. }
  127. /**
  128. * @inheritDoc
  129. */
  130. public function createCsrfValidationException(
  131. RequestInterface $request
  132. ): ?InvalidRequestException {
  133. /** @var Redirect $resultRedirect */
  134. $resultRedirect = $this->resultRedirectFactory->create();
  135. $resultRedirect->setPath('*/*/');
  136. return new InvalidRequestException(
  137. $resultRedirect,
  138. [new Phrase('Invalid Form Key. Please refresh the page.')]
  139. );
  140. }
  141. /**
  142. * @inheritDoc
  143. */
  144. public function validateForCsrf(RequestInterface $request): ?bool
  145. {
  146. return null;
  147. }
  148. /**
  149. * Login post action
  150. *
  151. * @return \Magento\Framework\Controller\Result\Redirect
  152. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  153. */
  154. public function execute()
  155. {
  156. if ($this->session->isLoggedIn() || !$this->formKeyValidator->validate($this->getRequest())) {
  157. /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
  158. $resultRedirect = $this->resultRedirectFactory->create();
  159. $resultRedirect->setPath('*/*/');
  160. return $resultRedirect;
  161. }
  162. if ($this->getRequest()->isPost()) {
  163. $login = $this->getRequest()->getPost('login');
  164. if (!empty($login['username']) && !empty($login['password'])) {
  165. try {
  166. $customer = $this->customerAccountManagement->authenticate($login['username'], $login['password']);
  167. $this->session->setCustomerDataAsLoggedIn($customer);
  168. $this->session->regenerateId();
  169. if ($this->getCookieManager()->getCookie('mage-cache-sessid')) {
  170. $metadata = $this->getCookieMetadataFactory()->createCookieMetadata();
  171. $metadata->setPath('/');
  172. $this->getCookieManager()->deleteCookie('mage-cache-sessid', $metadata);
  173. }
  174. $redirectUrl = $this->accountRedirect->getRedirectCookie();
  175. if (!$this->getScopeConfig()->getValue('customer/startup/redirect_dashboard') && $redirectUrl) {
  176. $this->accountRedirect->clearRedirectCookie();
  177. $resultRedirect = $this->resultRedirectFactory->create();
  178. // URL is checked to be internal in $this->_redirect->success()
  179. $resultRedirect->setUrl($this->_redirect->success($redirectUrl));
  180. return $resultRedirect;
  181. }
  182. } catch (EmailNotConfirmedException $e) {
  183. $value = $this->customerUrl->getEmailConfirmationUrl($login['username']);
  184. $message = __(
  185. 'This account is not confirmed. <a href="%1">Click here</a> to resend confirmation email.',
  186. $value
  187. );
  188. } catch (UserLockedException $e) {
  189. $message = __(
  190. 'The account sign-in was incorrect or your account is disabled temporarily. '
  191. . 'Please wait and try again later.'
  192. );
  193. } catch (AuthenticationException $e) {
  194. $message = __(
  195. 'The account sign-in was incorrect or your account is disabled temporarily. '
  196. . 'Please wait and try again later.'
  197. );
  198. } catch (LocalizedException $e) {
  199. $message = $e->getMessage();
  200. } catch (\Exception $e) {
  201. // PA DSS violation: throwing or logging an exception here can disclose customer password
  202. $this->messageManager->addError(
  203. __('An unspecified error occurred. Please contact us for assistance.')
  204. );
  205. } finally {
  206. if (isset($message)) {
  207. $this->messageManager->addError($message);
  208. $this->session->setUsername($login['username']);
  209. }
  210. }
  211. } else {
  212. $this->messageManager->addError(__('A login and a password are required.'));
  213. }
  214. }
  215. return $this->accountRedirect->getRedirect();
  216. }
  217. }