Skip to content

Commit

Permalink
GraphQL-55: [Mutations] My Account > Change account information
Browse files Browse the repository at this point in the history
  • Loading branch information
Valeriy Nayda committed Oct 8, 2018
1 parent 9200a16 commit 2057ddf
Show file tree
Hide file tree
Showing 20 changed files with 1,236 additions and 574 deletions.
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
{
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;
}
}
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.')
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
*/
declare(strict_types=1);

namespace Magento\CustomerGraphQl\Model\Resolver\Customer;
namespace Magento\CustomerGraphQl\Model\Customer;

use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Framework\Webapi\ServiceOutputProcessor;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Customer\Model\CustomerRegistry;
use Magento\Framework\Encryption\EncryptorInterface as Encryptor;

/**
* Customer field data provider, used for GraphQL request processing.
Expand All @@ -31,42 +30,24 @@ class CustomerDataProvider
*/
private $serviceOutputProcessor;

/**
* @var CustomerRegistry
*/
protected $customerRegistry;

/**
* @var SerializerInterface
*/
private $jsonSerializer;
private $serializer;

/**
* @var Encryptor
*/
protected $encryptor;

/**
* CustomerDataProvider constructor.
*
* @param CustomerRepositoryInterface $customerRepository
* @param ServiceOutputProcessor $serviceOutputProcessor
* @param SerializerInterface $jsonSerializer
* @param CustomerRegistry $customerRegistry
* @param Encryptor $encryptor
* @param SerializerInterface $serializer
*/
public function __construct(
CustomerRepositoryInterface $customerRepository,
ServiceOutputProcessor $serviceOutputProcessor,
SerializerInterface $jsonSerializer,
CustomerRegistry $customerRegistry,
Encryptor $encryptor
SerializerInterface $serializer
) {
$this->customerRepository = $customerRepository;
$this->serviceOutputProcessor = $serviceOutputProcessor;
$this->jsonSerializer = $jsonSerializer;
$this->customerRegistry = $customerRegistry;
$this->encryptor = $encryptor;
$this->serializer = $serializer;
}

/**
Expand All @@ -79,39 +60,41 @@ public function __construct(
public function getCustomerById(int $customerId): array
{
try {
$customerObject = $this->customerRepository->getById($customerId);
$customer = $this->customerRepository->getById($customerId);
} catch (NoSuchEntityException $e) {
// No error should be thrown, null result should be returned
return [];
throw new GraphQlNoSuchEntityException(
__('Customer id "%customer_id" does not exist.', ['customer_id' => $customerId]),
$e
);
}
return $this->processCustomer($customerObject);
return $this->processCustomer($customer);
}

/**
* Transform single customer data from object to in array format
*
* @param CustomerInterface $customerObject
* @param CustomerInterface $customer
* @return array
*/
private function processCustomer(CustomerInterface $customerObject): array
private function processCustomer(CustomerInterface $customer): array
{
$customer = $this->serviceOutputProcessor->process(
$customerObject,
$customerData = $this->serviceOutputProcessor->process(
$customer,
CustomerRepositoryInterface::class,
'get'
);
if (isset($customer['extension_attributes'])) {
$customer = array_merge($customer, $customer['extension_attributes']);
if (isset($customerData['extension_attributes'])) {
$customerData = array_merge($customerData, $customerData['extension_attributes']);
}
$customAttributes = [];
if (isset($customer['custom_attributes'])) {
foreach ($customer['custom_attributes'] as $attribute) {
if (isset($customerData['custom_attributes'])) {
foreach ($customerData['custom_attributes'] as $attribute) {
$isArray = false;
if (is_array($attribute['value'])) {
$isArray = true;
foreach ($attribute['value'] as $attributeValue) {
if (is_array($attributeValue)) {
$customAttributes[$attribute['attribute_code']] = $this->jsonSerializer->serialize(
$customAttributes[$attribute['attribute_code']] = $this->serializer->serialize(
$attribute['value']
);
continue;
Expand All @@ -126,22 +109,8 @@ private function processCustomer(CustomerInterface $customerObject): array
$customAttributes[$attribute['attribute_code']] = $attribute['value'];
}
}
$customer = array_merge($customer, $customAttributes);
$customerData = array_merge($customerData, $customAttributes);

return $customer;
}

/**
* Checking if password for customer is correct
*
* @param string $password
* @param int $customerId
* @return bool
* @throws NoSuchEntityException
*/
public function isPasswordCorrect(string $password, int $customerId)
{
$hash = $this->customerRegistry->retrieveSecureData($customerId)->getPasswordHash();
return $this->encryptor->validateHash($password, $hash);
return $customerData;
}
}
Loading

0 comments on commit 2057ddf

Please sign in to comment.