/** * 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->_getSession()->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->_getSession()->setCustomerDataAsLoggedIn($customer); $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)); }
/** * @magentoDataFixture Magento/Customer/_files/inactive_customer.php * @magentoAppArea frontend * @expectedException \Magento\Framework\Exception\State\InvalidTransitionException */ public function testActivateCustomerAlreadyActive() { /** @var \Magento\Customer\Model\Customer $customerModel */ $customerModel = $this->objectManager->create('Magento\\Customer\\Model\\Customer'); $customerModel->load(1); $key = $customerModel->getConfirmation(); $this->accountManagement->activate($customerModel->getEmail(), $key); // activate it one more time to produce an exception $this->accountManagement->activate($customerModel->getEmail(), $key); }
/** * Confirm customer account by id and confirmation key * * @return \Magento\Framework\Controller\Result\Redirect * @throws \Exception */ public function execute() { /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */ $resultRedirect = $this->resultRedirectFactory->create(); if ($this->_getSession()->isLoggedIn()) { $resultRedirect->setPath('*/*/'); return $resultRedirect; } $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->_getSession()->setCustomerDataAsLoggedIn($customer); $this->messageManager->addSuccess($this->getSuccessMessage()); $resultRedirect->setUrl($this->getSuccessRedirect()); return $resultRedirect; }