Confirm.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Customer\Controller\Account;
  8. use Magento\Customer\Model\Url;
  9. use Magento\Framework\App\Action\Context;
  10. use Magento\Customer\Model\Session;
  11. use Magento\Framework\App\Config\ScopeConfigInterface;
  12. use Magento\Store\Model\StoreManagerInterface;
  13. use Magento\Customer\Api\AccountManagementInterface;
  14. use Magento\Customer\Api\CustomerRepositoryInterface;
  15. use Magento\Customer\Helper\Address;
  16. use Magento\Framework\UrlFactory;
  17. use Magento\Framework\Exception\StateException;
  18. use Magento\Store\Model\ScopeInterface;
  19. use Magento\Framework\Controller\ResultFactory;
  20. /**
  21. * Class Confirm
  22. *
  23. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  24. */
  25. class Confirm extends \Magento\Customer\Controller\AbstractAccount
  26. {
  27. /**
  28. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  29. */
  30. protected $scopeConfig;
  31. /**
  32. * @var \Magento\Store\Model\StoreManagerInterface
  33. */
  34. protected $storeManager;
  35. /**
  36. * @var \Magento\Customer\Api\AccountManagementInterface
  37. */
  38. protected $customerAccountManagement;
  39. /**
  40. * @var \Magento\Customer\Api\CustomerRepositoryInterface
  41. */
  42. protected $customerRepository;
  43. /**
  44. * @var \Magento\Customer\Helper\Address
  45. */
  46. protected $addressHelper;
  47. /**
  48. * @var \Magento\Framework\UrlInterface
  49. */
  50. protected $urlModel;
  51. /**
  52. * @var Session
  53. */
  54. protected $session;
  55. /**
  56. * @var \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory
  57. */
  58. private $cookieMetadataFactory;
  59. /**
  60. * @var \Magento\Framework\Stdlib\Cookie\PhpCookieManager
  61. */
  62. private $cookieMetadataManager;
  63. /**
  64. * @param Context $context
  65. * @param Session $customerSession
  66. * @param ScopeConfigInterface $scopeConfig
  67. * @param StoreManagerInterface $storeManager
  68. * @param AccountManagementInterface $customerAccountManagement
  69. * @param CustomerRepositoryInterface $customerRepository
  70. * @param Address $addressHelper
  71. * @param UrlFactory $urlFactory
  72. */
  73. public function __construct(
  74. Context $context,
  75. Session $customerSession,
  76. ScopeConfigInterface $scopeConfig,
  77. StoreManagerInterface $storeManager,
  78. AccountManagementInterface $customerAccountManagement,
  79. CustomerRepositoryInterface $customerRepository,
  80. Address $addressHelper,
  81. UrlFactory $urlFactory
  82. ) {
  83. $this->session = $customerSession;
  84. $this->scopeConfig = $scopeConfig;
  85. $this->storeManager = $storeManager;
  86. $this->customerAccountManagement = $customerAccountManagement;
  87. $this->customerRepository = $customerRepository;
  88. $this->addressHelper = $addressHelper;
  89. $this->urlModel = $urlFactory->create();
  90. parent::__construct($context);
  91. }
  92. /**
  93. * Retrieve cookie manager
  94. *
  95. * @deprecated 101.0.0
  96. * @return \Magento\Framework\Stdlib\Cookie\PhpCookieManager
  97. */
  98. private function getCookieManager()
  99. {
  100. if (!$this->cookieMetadataManager) {
  101. $this->cookieMetadataManager = \Magento\Framework\App\ObjectManager::getInstance()->get(
  102. \Magento\Framework\Stdlib\Cookie\PhpCookieManager::class
  103. );
  104. }
  105. return $this->cookieMetadataManager;
  106. }
  107. /**
  108. * Retrieve cookie metadata factory
  109. *
  110. * @deprecated 101.0.0
  111. * @return \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory
  112. */
  113. private function getCookieMetadataFactory()
  114. {
  115. if (!$this->cookieMetadataFactory) {
  116. $this->cookieMetadataFactory = \Magento\Framework\App\ObjectManager::getInstance()->get(
  117. \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory::class
  118. );
  119. }
  120. return $this->cookieMetadataFactory;
  121. }
  122. /**
  123. * Confirm customer account by id and confirmation key
  124. *
  125. * @return \Magento\Framework\Controller\Result\Redirect
  126. */
  127. public function execute()
  128. {
  129. /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
  130. $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
  131. if ($this->session->isLoggedIn()) {
  132. $resultRedirect->setPath('*/*/');
  133. return $resultRedirect;
  134. }
  135. try {
  136. $customerId = $this->getRequest()->getParam('id', false);
  137. $key = $this->getRequest()->getParam('key', false);
  138. if (empty($customerId) || empty($key)) {
  139. throw new \Exception(__('Bad request.'));
  140. }
  141. // log in and send greeting email
  142. $customerEmail = $this->customerRepository->getById($customerId)->getEmail();
  143. $customer = $this->customerAccountManagement->activate($customerEmail, $key);
  144. $this->session->setCustomerDataAsLoggedIn($customer);
  145. if ($this->getCookieManager()->getCookie('mage-cache-sessid')) {
  146. $metadata = $this->getCookieMetadataFactory()->createCookieMetadata();
  147. $metadata->setPath('/');
  148. $this->getCookieManager()->deleteCookie('mage-cache-sessid', $metadata);
  149. }
  150. $this->messageManager->addSuccess($this->getSuccessMessage());
  151. $resultRedirect->setUrl($this->getSuccessRedirect());
  152. return $resultRedirect;
  153. } catch (StateException $e) {
  154. $this->messageManager->addException($e, __('This confirmation key is invalid or has expired.'));
  155. } catch (\Exception $e) {
  156. $this->messageManager->addException($e, __('There was an error confirming the account'));
  157. }
  158. $url = $this->urlModel->getUrl('*/*/index', ['_secure' => true]);
  159. return $resultRedirect->setUrl($this->_redirect->error($url));
  160. }
  161. /**
  162. * Retrieve success message
  163. *
  164. * @return string
  165. */
  166. protected function getSuccessMessage()
  167. {
  168. if ($this->addressHelper->isVatValidationEnabled()) {
  169. if ($this->addressHelper->getTaxCalculationAddressType() == Address::TYPE_SHIPPING) {
  170. // @codingStandardsIgnoreStart
  171. $message = __(
  172. 'If you are a registered VAT customer, please click <a href="%1">here</a> to enter your shipping address for proper VAT calculation.',
  173. $this->urlModel->getUrl('customer/address/edit')
  174. );
  175. // @codingStandardsIgnoreEnd
  176. } else {
  177. // @codingStandardsIgnoreStart
  178. $message = __(
  179. 'If you are a registered VAT customer, please click <a href="%1">here</a> to enter your billing address for proper VAT calculation.',
  180. $this->urlModel->getUrl('customer/address/edit')
  181. );
  182. // @codingStandardsIgnoreEnd
  183. }
  184. } else {
  185. $message = __('Thank you for registering with %1.', $this->storeManager->getStore()->getFrontendName());
  186. }
  187. return $message;
  188. }
  189. /**
  190. * Retrieve success redirect URL
  191. *
  192. * @return string
  193. */
  194. protected function getSuccessRedirect()
  195. {
  196. $backUrl = $this->getRequest()->getParam('back_url', false);
  197. $redirectToDashboard = $this->scopeConfig->isSetFlag(
  198. Url::XML_PATH_CUSTOMER_STARTUP_REDIRECT_TO_DASHBOARD,
  199. ScopeInterface::SCOPE_STORE
  200. );
  201. if (!$redirectToDashboard && $this->session->getBeforeAuthUrl()) {
  202. $successUrl = $this->session->getBeforeAuthUrl(true);
  203. } else {
  204. $successUrl = $this->urlModel->getUrl('*/*/index', ['_secure' => true]);
  205. }
  206. return $this->_redirect->success($backUrl ? $backUrl : $successUrl);
  207. }
  208. }