77namespace Magento \Newsletter \Controller \Subscriber ;
88
99use Magento \Customer \Api \AccountManagementInterface as CustomerAccountManagement ;
10+ use Magento \Customer \Api \CustomerRepositoryInterface ;
1011use Magento \Customer \Model \Session ;
1112use Magento \Customer \Model \Url as CustomerUrl ;
1213use Magento \Framework \App \Action \Context ;
@@ -47,6 +48,11 @@ class NewAction extends SubscriberController implements HttpPostActionInterface
4748 */
4849 private $ subscriptionManager ;
4950
51+ /**
52+ * @var CustomerRepositoryInterface
53+ */
54+ private $ customerRepository ;
55+
5056 /**
5157 * Initialize dependencies.
5258 *
@@ -57,7 +63,8 @@ class NewAction extends SubscriberController implements HttpPostActionInterface
5763 * @param CustomerUrl $customerUrl
5864 * @param CustomerAccountManagement $customerAccountManagement
5965 * @param SubscriptionManagerInterface $subscriptionManager
60- * @param EmailValidator $emailValidator
66+ * @param EmailValidator|null $emailValidator
67+ * @param CustomerRepositoryInterface|null $customerRepository
6168 */
6269 public function __construct (
6370 Context $ context ,
@@ -67,11 +74,14 @@ public function __construct(
6774 CustomerUrl $ customerUrl ,
6875 CustomerAccountManagement $ customerAccountManagement ,
6976 SubscriptionManagerInterface $ subscriptionManager ,
70- EmailValidator $ emailValidator = null
77+ EmailValidator $ emailValidator = null ,
78+ CustomerRepositoryInterface $ customerRepository = null
7179 ) {
7280 $ this ->customerAccountManagement = $ customerAccountManagement ;
7381 $ this ->subscriptionManager = $ subscriptionManager ;
7482 $ this ->emailValidator = $ emailValidator ?: ObjectManager::getInstance ()->get (EmailValidator::class);
83+ $ this ->customerRepository = $ customerRepository ?: ObjectManager::getInstance ()
84+ ->get (CustomerRepositoryInterface::class);
7585 parent ::__construct (
7686 $ context ,
7787 $ subscriberFactory ,
@@ -165,7 +175,8 @@ public function execute()
165175 }
166176
167177 $ storeId = (int )$ this ->_storeManager ->getStore ()->getId ();
168- $ currentCustomerId = $ this ->getSessionCustomerId ($ email );
178+ $ currentCustomerId = $ this ->getCustomerId ($ email , $ websiteId );
179+
169180 $ subscriber = $ currentCustomerId
170181 ? $ this ->subscriptionManager ->subscribeCustomer ($ currentCustomerId , $ storeId )
171182 : $ this ->subscriptionManager ->subscribe ($ email , $ storeId );
@@ -182,28 +193,26 @@ public function execute()
182193 }
183194 /** @var Redirect $redirect */
184195 $ redirect = $ this ->resultFactory ->create (ResultFactory::TYPE_REDIRECT );
196+ // phpcs:ignore Magento2.Legacy.ObsoleteResponse
185197 $ redirectUrl = $ this ->_redirect ->getRedirectUrl ();
186198 return $ redirect ->setUrl ($ redirectUrl );
187199 }
188200
189201 /**
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
191203 *
192204 * @param string $email
205+ * @param int $websiteId
193206 * @return int|null
194207 */
195- private function getSessionCustomerId (string $ email ): ?int
208+ private function getCustomerId (string $ email, int $ websiteId ): ?int
196209 {
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 ) {
198214 return null ;
199215 }
200-
201- $ customer = $ this ->_customerSession ->getCustomerDataObject ();
202- if ($ customer ->getEmail () !== $ email ) {
203- return null ;
204- }
205-
206- return (int )$ this ->_customerSession ->getId ();
207216 }
208217
209218 /**
0 commit comments