-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathConfirmation.php
133 lines (119 loc) · 4.18 KB
/
Confirmation.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Customer\Controller\Account;
use Magento\Customer\Api\AccountManagementInterface;
use Magento\Customer\Controller\AbstractAccount;
use Magento\Customer\Model\Session;
use Magento\Customer\Model\Url;
use Magento\Framework\App\Action\HttpGetActionInterface as HttpGetActionInterface;
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\State\InvalidTransitionException;
use Magento\Framework\View\Result\Page;
use Magento\Framework\View\Result\PageFactory;
use Magento\Store\Model\StoreManagerInterface;
/**
* Send confirmation link to specified email
*/
class Confirmation extends AbstractAccount implements HttpGetActionInterface, HttpPostActionInterface
{
/**
* @var StoreManagerInterface
*/
protected $storeManager;
/**
* @var AccountManagementInterface
*/
protected $customerAccountManagement;
/**
* @var Session
*/
protected $session;
/**
* @var PageFactory
*/
protected $resultPageFactory;
/**
* @var Url
*/
private $customerUrl;
/**
* @param Context $context
* @param Session $customerSession
* @param PageFactory $resultPageFactory
* @param StoreManagerInterface $storeManager
* @param AccountManagementInterface $customerAccountManagement
* @param Url|null $customerUrl
*/
public function __construct(
Context $context,
Session $customerSession,
PageFactory $resultPageFactory,
StoreManagerInterface $storeManager,
AccountManagementInterface $customerAccountManagement,
?Url $customerUrl = null
) {
$this->session = $customerSession;
$this->resultPageFactory = $resultPageFactory;
$this->storeManager = $storeManager;
$this->customerAccountManagement = $customerAccountManagement;
$this->customerUrl = $customerUrl ?: ObjectManager::getInstance()->get(Url::class);
parent::__construct($context);
}
/**
* Send confirmation link to specified email
*
* @return Redirect|Page
* @throws LocalizedException
*/
public function execute()
{
if ($this->session->isLoggedIn()) {
return $this->getRedirect('*/*/');
}
$email = $this->getRequest()->getPost('email');
if ($email) {
try {
$this->customerAccountManagement->resendConfirmation(
$email,
$this->storeManager->getStore()->getWebsiteId()
);
$this->messageManager->addSuccessMessage(__('Please check your email for confirmation key.'));
return $this->getRedirect('*/*/index', ['_secure' => true]);
} catch (InvalidTransitionException $e) {
$this->messageManager->addSuccessMessage(__('This email does not require confirmation.'));
return $this->getRedirect('*/*/index', ['_secure' => true]);
} catch (NoSuchEntityException $e) {
$this->messageManager->addErrorMessage(__('Wrong email.'));
} catch (LocalizedException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
}
}
$resultPage = $this->resultPageFactory->create();
$resultPage->getLayout()->getBlock('accountConfirmation')
->setEmail($email)
->setLoginUrl($this->customerUrl->getLoginUrl());
return $resultPage;
}
/**
* Returns redirect object
*
* @param string $path
* @param array $params
* @return Redirect
*/
private function getRedirect(string $path, array $params = []): Redirect
{
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath($path, $params);
return $resultRedirect;
}
}