Skip to content

Commit 2057ddf

Browse files
author
Valeriy Nayda
committed
GraphQL-55: [Mutations] My Account > Change account information
1 parent 9200a16 commit 2057ddf

File tree

20 files changed

+1236
-574
lines changed

20 files changed

+1236
-574
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CustomerGraphQl\Model\Customer;
9+
10+
use Magento\Newsletter\Model\SubscriberFactory;
11+
12+
/**
13+
* Change subscription status. Subscribe OR unsubscribe if required
14+
*/
15+
class ChangeSubscriptionStatus
16+
{
17+
/**
18+
* @var SubscriberFactory
19+
*/
20+
private $subscriberFactory;
21+
22+
/**
23+
* @param SubscriberFactory $subscriberFactory
24+
*/
25+
public function __construct(
26+
SubscriberFactory $subscriberFactory
27+
) {
28+
$this->subscriberFactory = $subscriberFactory;
29+
}
30+
31+
/**
32+
* Change subscription status. Subscribe OR unsubscribe if required
33+
*
34+
* @param int $customerId
35+
* @param bool $subscriptionStatus
36+
* @return void
37+
*/
38+
public function execute(int $customerId, bool $subscriptionStatus): void
39+
{
40+
$subscriber = $this->subscriberFactory->create()->loadByCustomerId($customerId);
41+
42+
if ($subscriptionStatus === true && !$subscriber->isSubscribed()) {
43+
$this->subscriberFactory->create()->subscribeCustomerById($customerId);
44+
} elseif ($subscriptionStatus === false && $subscriber->isSubscribed()) {
45+
$this->subscriberFactory->create()->unsubscribeCustomerById($customerId);
46+
}
47+
}
48+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CustomerGraphQl\Model\Customer;
9+
10+
use Magento\Authorization\Model\UserContextInterface;
11+
use Magento\Customer\Api\AccountManagementInterface;
12+
use Magento\Customer\Api\CustomerRepositoryInterface;
13+
use Magento\Customer\Model\AuthenticationInterface;
14+
use Magento\Framework\Exception\NoSuchEntityException;
15+
use Magento\Framework\GraphQl\Exception\GraphQlAuthenticationException;
16+
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
17+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
18+
19+
/**
20+
* Check customer account
21+
*/
22+
class CheckCustomerAccount
23+
{
24+
/**
25+
* @var AuthenticationInterface
26+
*/
27+
private $authentication;
28+
29+
/**
30+
* @var CustomerRepositoryInterface
31+
*/
32+
private $customerRepository;
33+
34+
/**
35+
* @var AccountManagementInterface
36+
*/
37+
private $accountManagement;
38+
39+
/**
40+
* @param AuthenticationInterface $authentication
41+
* @param CustomerRepositoryInterface $customerRepository
42+
* @param AccountManagementInterface $accountManagement
43+
*/
44+
public function __construct(
45+
AuthenticationInterface $authentication,
46+
CustomerRepositoryInterface $customerRepository,
47+
AccountManagementInterface $accountManagement
48+
) {
49+
$this->authentication = $authentication;
50+
$this->customerRepository = $customerRepository;
51+
$this->accountManagement = $accountManagement;
52+
}
53+
54+
/**
55+
* Check customer account
56+
*
57+
* @param int|null $customerId
58+
* @param int|null $customerType
59+
* @return void
60+
* @throws GraphQlAuthorizationException
61+
* @throws GraphQlNoSuchEntityException
62+
* @throws GraphQlAuthenticationException
63+
*/
64+
public function execute(?int $customerId, ?int $customerType): void
65+
{
66+
if (true === $this->isCustomerGuest($customerId, $customerType)) {
67+
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
68+
}
69+
70+
try {
71+
$this->customerRepository->getById($customerId);
72+
} catch (NoSuchEntityException $e) {
73+
throw new GraphQlNoSuchEntityException(
74+
__('Customer with id "%customer_id" does not exist.', ['customer_id' => $customerId]),
75+
$e
76+
);
77+
}
78+
79+
if (true === $this->authentication->isLocked($customerId)) {
80+
throw new GraphQlAuthenticationException(__('The account is locked.'));
81+
}
82+
83+
$confirmationStatus = $this->accountManagement->getConfirmationStatus($customerId);
84+
if ($confirmationStatus === AccountManagementInterface::ACCOUNT_CONFIRMATION_REQUIRED) {
85+
throw new GraphQlAuthenticationException(__("This account isn't confirmed. Verify and try again."));
86+
}
87+
}
88+
89+
/**
90+
* Checking if current customer is guest
91+
*
92+
* @param int|null $customerId
93+
* @param int|null $customerType
94+
* @return bool
95+
*/
96+
private function isCustomerGuest(?int $customerId, ?int $customerType): bool
97+
{
98+
if (null === $customerId || null === $customerType) {
99+
return true;
100+
}
101+
return 0 === (int)$customerId || (int)$customerType === UserContextInterface::USER_TYPE_GUEST;
102+
}
103+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CustomerGraphQl\Model\Customer;
9+
10+
use Magento\Customer\Model\AuthenticationInterface;
11+
use Magento\Framework\Exception\InvalidEmailOrPasswordException;
12+
use Magento\Framework\GraphQl\Exception\GraphQlAuthenticationException;
13+
14+
/**
15+
* Check customer password
16+
*/
17+
class CheckCustomerPassword
18+
{
19+
/**
20+
* @var AuthenticationInterface
21+
*/
22+
private $authentication;
23+
24+
/**
25+
* @param AuthenticationInterface $authentication
26+
*/
27+
public function __construct(
28+
AuthenticationInterface $authentication
29+
) {
30+
$this->authentication = $authentication;
31+
}
32+
33+
/**
34+
* Check customer password
35+
*
36+
* @param string $password
37+
* @param int $customerId
38+
* @throws GraphQlAuthenticationException
39+
*/
40+
public function execute(string $password, int $customerId)
41+
{
42+
try {
43+
$this->authentication->authenticate($customerId, $password);
44+
} catch (InvalidEmailOrPasswordException $e) {
45+
throw new GraphQlAuthenticationException(
46+
__('The password doesn\'t match this account. Verify the password and try again.')
47+
);
48+
}
49+
}
50+
}

app/code/Magento/CustomerGraphQl/Model/Resolver/Customer/CustomerDataProvider.php renamed to app/code/Magento/CustomerGraphQl/Model/Customer/CustomerDataProvider.php

Lines changed: 23 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,15 @@
55
*/
66
declare(strict_types=1);
77

8-
namespace Magento\CustomerGraphQl\Model\Resolver\Customer;
8+
namespace Magento\CustomerGraphQl\Model\Customer;
99

1010
use Magento\Customer\Api\CustomerRepositoryInterface;
1111
use Magento\Framework\Exception\LocalizedException;
1212
use Magento\Framework\Exception\NoSuchEntityException;
13+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1314
use Magento\Framework\Serialize\SerializerInterface;
1415
use Magento\Framework\Webapi\ServiceOutputProcessor;
1516
use Magento\Customer\Api\Data\CustomerInterface;
16-
use Magento\Customer\Model\CustomerRegistry;
17-
use Magento\Framework\Encryption\EncryptorInterface as Encryptor;
1817

1918
/**
2019
* Customer field data provider, used for GraphQL request processing.
@@ -31,42 +30,24 @@ class CustomerDataProvider
3130
*/
3231
private $serviceOutputProcessor;
3332

34-
/**
35-
* @var CustomerRegistry
36-
*/
37-
protected $customerRegistry;
38-
3933
/**
4034
* @var SerializerInterface
4135
*/
42-
private $jsonSerializer;
36+
private $serializer;
4337

4438
/**
45-
* @var Encryptor
46-
*/
47-
protected $encryptor;
48-
49-
/**
50-
* CustomerDataProvider constructor.
51-
*
5239
* @param CustomerRepositoryInterface $customerRepository
5340
* @param ServiceOutputProcessor $serviceOutputProcessor
54-
* @param SerializerInterface $jsonSerializer
55-
* @param CustomerRegistry $customerRegistry
56-
* @param Encryptor $encryptor
41+
* @param SerializerInterface $serializer
5742
*/
5843
public function __construct(
5944
CustomerRepositoryInterface $customerRepository,
6045
ServiceOutputProcessor $serviceOutputProcessor,
61-
SerializerInterface $jsonSerializer,
62-
CustomerRegistry $customerRegistry,
63-
Encryptor $encryptor
46+
SerializerInterface $serializer
6447
) {
6548
$this->customerRepository = $customerRepository;
6649
$this->serviceOutputProcessor = $serviceOutputProcessor;
67-
$this->jsonSerializer = $jsonSerializer;
68-
$this->customerRegistry = $customerRegistry;
69-
$this->encryptor = $encryptor;
50+
$this->serializer = $serializer;
7051
}
7152

7253
/**
@@ -79,39 +60,41 @@ public function __construct(
7960
public function getCustomerById(int $customerId): array
8061
{
8162
try {
82-
$customerObject = $this->customerRepository->getById($customerId);
63+
$customer = $this->customerRepository->getById($customerId);
8364
} catch (NoSuchEntityException $e) {
84-
// No error should be thrown, null result should be returned
85-
return [];
65+
throw new GraphQlNoSuchEntityException(
66+
__('Customer id "%customer_id" does not exist.', ['customer_id' => $customerId]),
67+
$e
68+
);
8669
}
87-
return $this->processCustomer($customerObject);
70+
return $this->processCustomer($customer);
8871
}
8972

9073
/**
9174
* Transform single customer data from object to in array format
9275
*
93-
* @param CustomerInterface $customerObject
76+
* @param CustomerInterface $customer
9477
* @return array
9578
*/
96-
private function processCustomer(CustomerInterface $customerObject): array
79+
private function processCustomer(CustomerInterface $customer): array
9780
{
98-
$customer = $this->serviceOutputProcessor->process(
99-
$customerObject,
81+
$customerData = $this->serviceOutputProcessor->process(
82+
$customer,
10083
CustomerRepositoryInterface::class,
10184
'get'
10285
);
103-
if (isset($customer['extension_attributes'])) {
104-
$customer = array_merge($customer, $customer['extension_attributes']);
86+
if (isset($customerData['extension_attributes'])) {
87+
$customerData = array_merge($customerData, $customerData['extension_attributes']);
10588
}
10689
$customAttributes = [];
107-
if (isset($customer['custom_attributes'])) {
108-
foreach ($customer['custom_attributes'] as $attribute) {
90+
if (isset($customerData['custom_attributes'])) {
91+
foreach ($customerData['custom_attributes'] as $attribute) {
10992
$isArray = false;
11093
if (is_array($attribute['value'])) {
11194
$isArray = true;
11295
foreach ($attribute['value'] as $attributeValue) {
11396
if (is_array($attributeValue)) {
114-
$customAttributes[$attribute['attribute_code']] = $this->jsonSerializer->serialize(
97+
$customAttributes[$attribute['attribute_code']] = $this->serializer->serialize(
11598
$attribute['value']
11699
);
117100
continue;
@@ -126,22 +109,8 @@ private function processCustomer(CustomerInterface $customerObject): array
126109
$customAttributes[$attribute['attribute_code']] = $attribute['value'];
127110
}
128111
}
129-
$customer = array_merge($customer, $customAttributes);
112+
$customerData = array_merge($customerData, $customAttributes);
130113

131-
return $customer;
132-
}
133-
134-
/**
135-
* Checking if password for customer is correct
136-
*
137-
* @param string $password
138-
* @param int $customerId
139-
* @return bool
140-
* @throws NoSuchEntityException
141-
*/
142-
public function isPasswordCorrect(string $password, int $customerId)
143-
{
144-
$hash = $this->customerRegistry->retrieveSecureData($customerId)->getPasswordHash();
145-
return $this->encryptor->validateHash($password, $hash);
114+
return $customerData;
146115
}
147116
}

0 commit comments

Comments
 (0)