-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENGCOM-3139: My Account > Change account information and Newsletter s…
…ubscription #162
- Loading branch information
Showing
31 changed files
with
1,396 additions
and
440 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
app/code/Magento/CustomerGraphQl/Model/Customer/ChangeSubscriptionStatus.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CustomerGraphQl\Model\Customer; | ||
|
||
use Magento\Newsletter\Model\SubscriberFactory; | ||
|
||
/** | ||
* Change subscription status. Subscribe OR unsubscribe if required | ||
*/ | ||
class ChangeSubscriptionStatus | ||
{ | ||
/** | ||
* @var SubscriberFactory | ||
*/ | ||
private $subscriberFactory; | ||
|
||
/** | ||
* @param SubscriberFactory $subscriberFactory | ||
*/ | ||
public function __construct( | ||
SubscriberFactory $subscriberFactory | ||
) { | ||
$this->subscriberFactory = $subscriberFactory; | ||
} | ||
|
||
/** | ||
* Change subscription status. Subscribe OR unsubscribe if required | ||
* | ||
* @param int $customerId | ||
* @param bool $subscriptionStatus | ||
* @return void | ||
*/ | ||
public function execute(int $customerId, bool $subscriptionStatus): void | ||
{ | ||
$subscriber = $this->subscriberFactory->create()->loadByCustomerId($customerId); | ||
|
||
if ($subscriptionStatus === true && !$subscriber->isSubscribed()) { | ||
$this->subscriberFactory->create()->subscribeCustomerById($customerId); | ||
} elseif ($subscriptionStatus === false && $subscriber->isSubscribed()) { | ||
$this->subscriberFactory->create()->unsubscribeCustomerById($customerId); | ||
} | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
app/code/Magento/CustomerGraphQl/Model/Customer/CheckCustomerAccount.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CustomerGraphQl\Model\Customer; | ||
|
||
use Magento\Authorization\Model\UserContextInterface; | ||
use Magento\Customer\Api\AccountManagementInterface; | ||
use Magento\Customer\Api\CustomerRepositoryInterface; | ||
use Magento\Customer\Model\AuthenticationInterface; | ||
use Magento\Framework\Exception\NoSuchEntityException; | ||
use Magento\Framework\GraphQl\Exception\GraphQlAuthenticationException; | ||
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException; | ||
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; | ||
|
||
/** | ||
* Check customer account | ||
*/ | ||
class CheckCustomerAccount | ||
{ | ||
/** | ||
* @var AuthenticationInterface | ||
*/ | ||
private $authentication; | ||
|
||
/** | ||
* @var CustomerRepositoryInterface | ||
*/ | ||
private $customerRepository; | ||
|
||
/** | ||
* @var AccountManagementInterface | ||
*/ | ||
private $accountManagement; | ||
|
||
/** | ||
* @param AuthenticationInterface $authentication | ||
* @param CustomerRepositoryInterface $customerRepository | ||
* @param AccountManagementInterface $accountManagement | ||
*/ | ||
public function __construct( | ||
AuthenticationInterface $authentication, | ||
CustomerRepositoryInterface $customerRepository, | ||
AccountManagementInterface $accountManagement | ||
) { | ||
$this->authentication = $authentication; | ||
$this->customerRepository = $customerRepository; | ||
$this->accountManagement = $accountManagement; | ||
} | ||
|
||
/** | ||
* Check customer account | ||
* | ||
* @param int|null $customerId | ||
* @param int|null $customerType | ||
* @return void | ||
* @throws GraphQlAuthorizationException | ||
* @throws GraphQlNoSuchEntityException | ||
* @throws GraphQlAuthenticationException | ||
*/ | ||
public function execute(?int $customerId, ?int $customerType): void | ||
{ | ||
if (true === $this->isCustomerGuest($customerId, $customerType)) { | ||
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.')); | ||
} | ||
|
||
try { | ||
$this->customerRepository->getById($customerId); | ||
} catch (NoSuchEntityException $e) { | ||
throw new GraphQlNoSuchEntityException( | ||
__('Customer with id "%customer_id" does not exist.', ['customer_id' => $customerId]), | ||
$e | ||
); | ||
} | ||
|
||
if (true === $this->authentication->isLocked($customerId)) { | ||
throw new GraphQlAuthenticationException(__('The account is locked.')); | ||
} | ||
|
||
$confirmationStatus = $this->accountManagement->getConfirmationStatus($customerId); | ||
if ($confirmationStatus === AccountManagementInterface::ACCOUNT_CONFIRMATION_REQUIRED) { | ||
throw new GraphQlAuthenticationException(__("This account isn't confirmed. Verify and try again.")); | ||
} | ||
} | ||
|
||
/** | ||
* Checking if current customer is guest | ||
* | ||
* @param int|null $customerId | ||
* @param int|null $customerType | ||
* @return bool | ||
*/ | ||
private function isCustomerGuest(?int $customerId, ?int $customerType): bool | ||
{ | ||
if (null === $customerId || null === $customerType) { | ||
return true; | ||
} | ||
return 0 === (int)$customerId || (int)$customerType === UserContextInterface::USER_TYPE_GUEST; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
app/code/Magento/CustomerGraphQl/Model/Customer/CheckCustomerPassword.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CustomerGraphQl\Model\Customer; | ||
|
||
use Magento\Customer\Model\AuthenticationInterface; | ||
use Magento\Framework\Exception\InvalidEmailOrPasswordException; | ||
use Magento\Framework\GraphQl\Exception\GraphQlAuthenticationException; | ||
|
||
/** | ||
* Check customer password | ||
*/ | ||
class CheckCustomerPassword | ||
{ | ||
/** | ||
* @var AuthenticationInterface | ||
*/ | ||
private $authentication; | ||
|
||
/** | ||
* @param AuthenticationInterface $authentication | ||
*/ | ||
public function __construct( | ||
AuthenticationInterface $authentication | ||
) { | ||
$this->authentication = $authentication; | ||
} | ||
|
||
/** | ||
* Check customer password | ||
* | ||
* @param string $password | ||
* @param int $customerId | ||
* @throws GraphQlAuthenticationException | ||
*/ | ||
public function execute(string $password, int $customerId) | ||
{ | ||
try { | ||
$this->authentication->authenticate($customerId, $password); | ||
} catch (InvalidEmailOrPasswordException $e) { | ||
throw new GraphQlAuthenticationException( | ||
__('The password doesn\'t match this account. Verify the password and try again.') | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.