session = $customerSession; $this->scopeConfig = $scopeConfig; $this->storeManager = $storeManager; $this->customerAccountManagement = $customerAccountManagement; $this->customerRepository = $customerRepository; $this->addressHelper = $addressHelper; $this->urlModel = $urlFactory->create(); parent::__construct($context); } /** * Retrieve cookie manager * * @deprecated 101.0.0 * @return \Magento\Framework\Stdlib\Cookie\PhpCookieManager */ private function getCookieManager() { if (!$this->cookieMetadataManager) { $this->cookieMetadataManager = \Magento\Framework\App\ObjectManager::getInstance()->get( \Magento\Framework\Stdlib\Cookie\PhpCookieManager::class ); } return $this->cookieMetadataManager; } /** * Retrieve cookie metadata factory * * @deprecated 101.0.0 * @return \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory */ private function getCookieMetadataFactory() { if (!$this->cookieMetadataFactory) { $this->cookieMetadataFactory = \Magento\Framework\App\ObjectManager::getInstance()->get( \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory::class ); } return $this->cookieMetadataFactory; } /** * Confirm customer account by id and confirmation key * * @return \Magento\Framework\Controller\Result\Redirect */ public function execute() { /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */ $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT); if ($this->session->isLoggedIn()) { $resultRedirect->setPath('*/*/'); return $resultRedirect; } try { $customerId = $this->getRequest()->getParam('id', false); $key = $this->getRequest()->getParam('key', false); if (empty($customerId) || empty($key)) { throw new \Exception(__('Bad request.')); } // log in and send greeting email $customerEmail = $this->customerRepository->getById($customerId)->getEmail(); $customer = $this->customerAccountManagement->activate($customerEmail, $key); $this->session->setCustomerDataAsLoggedIn($customer); if ($this->getCookieManager()->getCookie('mage-cache-sessid')) { $metadata = $this->getCookieMetadataFactory()->createCookieMetadata(); $metadata->setPath('/'); $this->getCookieManager()->deleteCookie('mage-cache-sessid', $metadata); } $this->messageManager->addSuccess($this->getSuccessMessage()); $resultRedirect->setUrl($this->getSuccessRedirect()); return $resultRedirect; } catch (StateException $e) { $this->messageManager->addException($e, __('This confirmation key is invalid or has expired.')); } catch (\Exception $e) { $this->messageManager->addException($e, __('There was an error confirming the account')); } $url = $this->urlModel->getUrl('*/*/index', ['_secure' => true]); return $resultRedirect->setUrl($this->_redirect->error($url)); } /** * Retrieve success message * * @return string */ protected function getSuccessMessage() { if ($this->addressHelper->isVatValidationEnabled()) { if ($this->addressHelper->getTaxCalculationAddressType() == Address::TYPE_SHIPPING) { // @codingStandardsIgnoreStart $message = __( 'If you are a registered VAT customer, please click here to enter your shipping address for proper VAT calculation.', $this->urlModel->getUrl('customer/address/edit') ); // @codingStandardsIgnoreEnd } else { // @codingStandardsIgnoreStart $message = __( 'If you are a registered VAT customer, please click here to enter your billing address for proper VAT calculation.', $this->urlModel->getUrl('customer/address/edit') ); // @codingStandardsIgnoreEnd } } else { $message = __('Thank you for registering with %1.', $this->storeManager->getStore()->getFrontendName()); } return $message; } /** * Retrieve success redirect URL * * @return string */ protected function getSuccessRedirect() { $backUrl = $this->getRequest()->getParam('back_url', false); $redirectToDashboard = $this->scopeConfig->isSetFlag( Url::XML_PATH_CUSTOMER_STARTUP_REDIRECT_TO_DASHBOARD, ScopeInterface::SCOPE_STORE ); if (!$redirectToDashboard && $this->session->getBeforeAuthUrl()) { $successUrl = $this->session->getBeforeAuthUrl(true); } else { $successUrl = $this->urlModel->getUrl('*/*/index', ['_secure' => true]); } return $this->_redirect->success($backUrl ? $backUrl : $successUrl); } }