-
Notifications
You must be signed in to change notification settings - Fork 154
My Account > Change account information and Newsletter subscription #162
Changes from all commits
09dc09c
ef7f579
8963811
d0dd90f
b691d84
d759867
a1dd683
cc8d328
9200a16
2057ddf
20e3393
661988b
706af91
a1c4d33
0cb378b
6e6b5c3
6f73325
7ca6097
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
} | ||
} | ||
} |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why customer type is needed here? For guests $customerID will be 0. |
||
{ | ||
if (true === $this->isCustomerGuest($customerId, $customerType)) { | ||
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Usually it is better to use 'is not' instead of 'isn't' in error message. |
||
} | ||
|
||
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; | ||
} | ||
} |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should use nouns as class names. Please consider changing it to CustomerPasswordVerifier or CustomerAuthenticator. Please fix the names of all affected variables which hold this object as well. |
||
{ | ||
/** | ||
* @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.') | ||
); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does not make sense to load records from DB if the next two conditions fail.