Login.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Controller\Ajax;
  7. use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
  8. use Magento\Customer\Api\AccountManagementInterface;
  9. use Magento\Framework\Exception\EmailNotConfirmedException;
  10. use Magento\Framework\Exception\InvalidEmailOrPasswordException;
  11. use Magento\Framework\App\ObjectManager;
  12. use Magento\Customer\Model\Account\Redirect as AccountRedirect;
  13. use Magento\Framework\App\Config\ScopeConfigInterface;
  14. use Magento\Framework\Exception\LocalizedException;
  15. use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
  16. use Magento\Framework\Stdlib\CookieManagerInterface;
  17. /**
  18. * Login controller
  19. *
  20. * @method \Magento\Framework\App\RequestInterface getRequest()
  21. * @method \Magento\Framework\App\Response\Http getResponse()
  22. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  23. */
  24. class Login extends \Magento\Framework\App\Action\Action implements HttpPostActionInterface
  25. {
  26. /**
  27. * @var \Magento\Customer\Model\Session
  28. */
  29. protected $customerSession;
  30. /**
  31. * @var AccountManagementInterface
  32. */
  33. protected $customerAccountManagement;
  34. /**
  35. * @var \Magento\Framework\Json\Helper\Data $helper
  36. */
  37. protected $helper;
  38. /**
  39. * @var \Magento\Framework\Controller\Result\JsonFactory
  40. */
  41. protected $resultJsonFactory;
  42. /**
  43. * @var \Magento\Framework\Controller\Result\RawFactory
  44. */
  45. protected $resultRawFactory;
  46. /**
  47. * @var AccountRedirect
  48. */
  49. protected $accountRedirect;
  50. /**
  51. * @var ScopeConfigInterface
  52. */
  53. protected $scopeConfig;
  54. /**
  55. * @var CookieManagerInterface
  56. */
  57. private $cookieManager;
  58. /**
  59. * @var CookieMetadataFactory
  60. */
  61. private $cookieMetadataFactory;
  62. /**
  63. * Initialize Login controller
  64. *
  65. * @param \Magento\Framework\App\Action\Context $context
  66. * @param \Magento\Customer\Model\Session $customerSession
  67. * @param \Magento\Framework\Json\Helper\Data $helper
  68. * @param AccountManagementInterface $customerAccountManagement
  69. * @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
  70. * @param \Magento\Framework\Controller\Result\RawFactory $resultRawFactory
  71. * @param CookieManagerInterface $cookieManager
  72. * @param CookieMetadataFactory $cookieMetadataFactory
  73. */
  74. public function __construct(
  75. \Magento\Framework\App\Action\Context $context,
  76. \Magento\Customer\Model\Session $customerSession,
  77. \Magento\Framework\Json\Helper\Data $helper,
  78. AccountManagementInterface $customerAccountManagement,
  79. \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
  80. \Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
  81. CookieManagerInterface $cookieManager = null,
  82. CookieMetadataFactory $cookieMetadataFactory = null
  83. ) {
  84. parent::__construct($context);
  85. $this->customerSession = $customerSession;
  86. $this->helper = $helper;
  87. $this->customerAccountManagement = $customerAccountManagement;
  88. $this->resultJsonFactory = $resultJsonFactory;
  89. $this->resultRawFactory = $resultRawFactory;
  90. $this->cookieManager = $cookieManager ?:
  91. ObjectManager::getInstance()->get(CookieManagerInterface::class);
  92. $this->cookieMetadataFactory = $cookieMetadataFactory ?:
  93. ObjectManager::getInstance()->get(CookieMetadataFactory::class);
  94. }
  95. /**
  96. * Get account redirect.
  97. *
  98. * @deprecated 100.0.10
  99. * @return AccountRedirect
  100. */
  101. protected function getAccountRedirect()
  102. {
  103. if (!is_object($this->accountRedirect)) {
  104. $this->accountRedirect = ObjectManager::getInstance()->get(AccountRedirect::class);
  105. }
  106. return $this->accountRedirect;
  107. }
  108. /**
  109. * Account redirect setter for unit tests.
  110. *
  111. * @deprecated 100.0.10
  112. * @param AccountRedirect $value
  113. * @return void
  114. */
  115. public function setAccountRedirect($value)
  116. {
  117. $this->accountRedirect = $value;
  118. }
  119. /**
  120. * Initializes config dependency.
  121. *
  122. * @deprecated 100.0.10
  123. * @return ScopeConfigInterface
  124. */
  125. protected function getScopeConfig()
  126. {
  127. if (!is_object($this->scopeConfig)) {
  128. $this->scopeConfig = ObjectManager::getInstance()->get(ScopeConfigInterface::class);
  129. }
  130. return $this->scopeConfig;
  131. }
  132. /**
  133. * Sets config dependency.
  134. *
  135. * @deprecated 100.0.10
  136. * @param ScopeConfigInterface $value
  137. * @return void
  138. */
  139. public function setScopeConfig($value)
  140. {
  141. $this->scopeConfig = $value;
  142. }
  143. /**
  144. * Login registered users and initiate a session.
  145. *
  146. * Expects a POST. ex for JSON {"username":"user@magento.com", "password":"userpassword"}
  147. *
  148. * @return \Magento\Framework\Controller\ResultInterface
  149. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  150. */
  151. public function execute()
  152. {
  153. $credentials = null;
  154. $httpBadRequestCode = 400;
  155. /** @var \Magento\Framework\Controller\Result\Raw $resultRaw */
  156. $resultRaw = $this->resultRawFactory->create();
  157. try {
  158. $credentials = $this->helper->jsonDecode($this->getRequest()->getContent());
  159. } catch (\Exception $e) {
  160. return $resultRaw->setHttpResponseCode($httpBadRequestCode);
  161. }
  162. if (!$credentials || $this->getRequest()->getMethod() !== 'POST' || !$this->getRequest()->isXmlHttpRequest()) {
  163. return $resultRaw->setHttpResponseCode($httpBadRequestCode);
  164. }
  165. $response = [
  166. 'errors' => false,
  167. 'message' => __('Login successful.')
  168. ];
  169. try {
  170. $customer = $this->customerAccountManagement->authenticate(
  171. $credentials['username'],
  172. $credentials['password']
  173. );
  174. $this->customerSession->setCustomerDataAsLoggedIn($customer);
  175. $this->customerSession->regenerateId();
  176. $redirectRoute = $this->getAccountRedirect()->getRedirectCookie();
  177. if ($this->cookieManager->getCookie('mage-cache-sessid')) {
  178. $metadata = $this->cookieMetadataFactory->createCookieMetadata();
  179. $metadata->setPath('/');
  180. $this->cookieManager->deleteCookie('mage-cache-sessid', $metadata);
  181. }
  182. if (!$this->getScopeConfig()->getValue('customer/startup/redirect_dashboard') && $redirectRoute) {
  183. $response['redirectUrl'] = $this->_redirect->success($redirectRoute);
  184. $this->getAccountRedirect()->clearRedirectCookie();
  185. }
  186. } catch (LocalizedException $e) {
  187. $response = [
  188. 'errors' => true,
  189. 'message' => $e->getMessage(),
  190. ];
  191. } catch (\Exception $e) {
  192. $response = [
  193. 'errors' => true,
  194. 'message' => __('Invalid login or password.'),
  195. ];
  196. }
  197. /** @var \Magento\Framework\Controller\Result\Json $resultJson */
  198. $resultJson = $this->resultJsonFactory->create();
  199. return $resultJson->setData($response);
  200. }
  201. }