7
7
namespace Magento \Newsletter \Controller \Subscriber ;
8
8
9
9
use Magento \Customer \Api \AccountManagementInterface as CustomerAccountManagement ;
10
+ use Magento \Customer \Api \CustomerRepositoryInterface ;
10
11
use Magento \Customer \Model \Session ;
11
12
use Magento \Customer \Model \Url as CustomerUrl ;
12
13
use Magento \Framework \App \Action \Context ;
@@ -47,6 +48,11 @@ class NewAction extends SubscriberController implements HttpPostActionInterface
47
48
*/
48
49
private $ subscriptionManager ;
49
50
51
+ /**
52
+ * @var CustomerRepositoryInterface
53
+ */
54
+ private $ customerRepository ;
55
+
50
56
/**
51
57
* Initialize dependencies.
52
58
*
@@ -57,7 +63,8 @@ class NewAction extends SubscriberController implements HttpPostActionInterface
57
63
* @param CustomerUrl $customerUrl
58
64
* @param CustomerAccountManagement $customerAccountManagement
59
65
* @param SubscriptionManagerInterface $subscriptionManager
60
- * @param EmailValidator $emailValidator
66
+ * @param EmailValidator|null $emailValidator
67
+ * @param CustomerRepositoryInterface|null $customerRepository
61
68
*/
62
69
public function __construct (
63
70
Context $ context ,
@@ -67,11 +74,14 @@ public function __construct(
67
74
CustomerUrl $ customerUrl ,
68
75
CustomerAccountManagement $ customerAccountManagement ,
69
76
SubscriptionManagerInterface $ subscriptionManager ,
70
- EmailValidator $ emailValidator = null
77
+ EmailValidator $ emailValidator = null ,
78
+ CustomerRepositoryInterface $ customerRepository = null
71
79
) {
72
80
$ this ->customerAccountManagement = $ customerAccountManagement ;
73
81
$ this ->subscriptionManager = $ subscriptionManager ;
74
82
$ this ->emailValidator = $ emailValidator ?: ObjectManager::getInstance ()->get (EmailValidator::class);
83
+ $ this ->customerRepository = $ customerRepository ?: ObjectManager::getInstance ()
84
+ ->get (CustomerRepositoryInterface::class);
75
85
parent ::__construct (
76
86
$ context ,
77
87
$ subscriberFactory ,
@@ -165,7 +175,8 @@ public function execute()
165
175
}
166
176
167
177
$ storeId = (int )$ this ->_storeManager ->getStore ()->getId ();
168
- $ currentCustomerId = $ this ->getSessionCustomerId ($ email );
178
+ $ currentCustomerId = $ this ->getCustomerId ($ email , $ websiteId );
179
+
169
180
$ subscriber = $ currentCustomerId
170
181
? $ this ->subscriptionManager ->subscribeCustomer ($ currentCustomerId , $ storeId )
171
182
: $ this ->subscriptionManager ->subscribe ($ email , $ storeId );
@@ -182,28 +193,26 @@ public function execute()
182
193
}
183
194
/** @var Redirect $redirect */
184
195
$ redirect = $ this ->resultFactory ->create (ResultFactory::TYPE_REDIRECT );
196
+ // phpcs:ignore Magento2.Legacy.ObsoleteResponse
185
197
$ redirectUrl = $ this ->_redirect ->getRedirectUrl ();
186
198
return $ redirect ->setUrl ($ redirectUrl );
187
199
}
188
200
189
201
/**
190
- * Get customer id from session if he is owner of the email
202
+ * Check if customer with provided email exists and return its id
191
203
*
192
204
* @param string $email
205
+ * @param int $websiteId
193
206
* @return int|null
194
207
*/
195
- private function getSessionCustomerId (string $ email ): ?int
208
+ private function getCustomerId (string $ email, int $ websiteId ): ?int
196
209
{
197
- if (!$ this ->_customerSession ->isLoggedIn ()) {
210
+ try {
211
+ $ customer = $ this ->customerRepository ->get ($ email , $ websiteId );
212
+ return (int )$ customer ->getId ();
213
+ } catch (\Magento \Framework \Exception \NoSuchEntityException $ e ) {
198
214
return null ;
199
215
}
200
-
201
- $ customer = $ this ->_customerSession ->getCustomerDataObject ();
202
- if ($ customer ->getEmail () !== $ email ) {
203
- return null ;
204
- }
205
-
206
- return (int )$ this ->_customerSession ->getId ();
207
216
}
208
217
209
218
/**
0 commit comments