Skip to content

Commit

Permalink
magento#486: Add customer account validation in Quote operations
Browse files Browse the repository at this point in the history
  • Loading branch information
naydav committed Jun 14, 2019
1 parent 8893cb7 commit 1bf1312
Show file tree
Hide file tree
Showing 17 changed files with 23 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function __construct(
* Get customer
*
* @param int $customerId
* @return void
* @return CustomerInterface
* @throws GraphQlAuthenticationException
* @throws GraphQlAuthorizationException
* @throws GraphQlInputException
Expand Down
58 changes: 0 additions & 58 deletions app/code/Magento/CustomerGraphQl/Model/Customer/GetCustomer.php

This file was deleted.

14 changes: 2 additions & 12 deletions app/code/Magento/CustomerGraphQl/Model/Resolver/ChangePassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Magento\Customer\Api\AccountManagementInterface;
use Magento\CustomerGraphQl\Model\Customer\CheckCustomerPassword;
use Magento\CustomerGraphQl\Model\Customer\ExtractCustomerData;
use Magento\CustomerGraphQl\Model\Customer\GetCustomer;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
Expand All @@ -23,11 +22,6 @@
*/
class ChangePassword implements ResolverInterface
{
/**
* @var GetCustomer
*/
private $getCustomer;

/**
* @var CheckCustomerPassword
*/
Expand All @@ -44,18 +38,15 @@ class ChangePassword implements ResolverInterface
private $extractCustomerData;

/**
* @param GetCustomer $getCustomer
* @param CheckCustomerPassword $checkCustomerPassword
* @param AccountManagementInterface $accountManagement
* @param ExtractCustomerData $extractCustomerData
*/
public function __construct(
GetCustomer $getCustomer,
CheckCustomerPassword $checkCustomerPassword,
AccountManagementInterface $accountManagement,
ExtractCustomerData $extractCustomerData
) {
$this->getCustomer = $getCustomer;
$this->checkCustomerPassword = $checkCustomerPassword;
$this->accountManagement = $accountManagement;
$this->extractCustomerData = $extractCustomerData;
Expand All @@ -71,7 +62,8 @@ public function resolve(
array $value = null,
array $args = null
) {
if (true === $context->isGuest()) {
$customer = $context->getExtensionAttributes()->getCustomer();
if (null === $customer) {
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
}

Expand All @@ -83,9 +75,7 @@ public function resolve(
throw new GraphQlInputException(__('Specify the "newPassword" value.'));
}

$customer = $this->getCustomer->execute($context->getUserId());
$customerId = (int)$customer->getId();

$this->checkCustomerPassword->execute($args['currentPassword'], $customerId);

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function resolve(
array $value = null,
array $args = null
) {
if (true === $context->isGuest()) {
if (null === $context->getExtensionAttributes()->getCustomer()) {
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
}

Expand Down
16 changes: 3 additions & 13 deletions app/code/Magento/CustomerGraphQl/Model/Resolver/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace Magento\CustomerGraphQl\Model\Resolver;

use Magento\CustomerGraphQl\Model\Customer\GetCustomer;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\CustomerGraphQl\Model\Customer\ExtractCustomerData;
Expand All @@ -19,25 +18,17 @@
*/
class Customer implements ResolverInterface
{
/**
* @var GetCustomer
*/
private $getCustomer;

/**
* @var ExtractCustomerData
*/
private $extractCustomerData;

/**
* @param GetCustomer $getCustomer
* @param ExtractCustomerData $extractCustomerData
*/
public function __construct(
GetCustomer $getCustomer,
ExtractCustomerData $extractCustomerData
) {
$this->getCustomer = $getCustomer;
$this->extractCustomerData = $extractCustomerData;
}

Expand All @@ -51,12 +42,11 @@ public function resolve(
array $value = null,
array $args = null
) {
if (true === $context->isGuest()) {
$customer = $context->getExtensionAttributes()->getCustomer();

if (null === $customer) {
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
}

$customer = $this->getCustomer->execute($context->getUserId());

return $this->extractCustomerData->execute($customer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function resolve(
array $value = null,
array $args = null
) {
if (true === $context->isGuest()) {
if (null === $context->getExtensionAttributes()->getCustomer()) {
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function resolve(
array $value = null,
array $args = null
) {
if (true === $context->isGuest()) {
if (null === $context->getExtensionAttributes()->getCustomer()) {
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

namespace Magento\CustomerGraphQl\Model\Resolver;

use Magento\CustomerGraphQl\Model\Customer\GetCustomer;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
Expand Down Expand Up @@ -43,7 +43,7 @@ public function resolve(
array $value = null,
array $args = null
) {
if (true === $context->isGuest()) {
if (null === $context->getExtensionAttributes()->getCustomer()) {
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function resolve(
array $value = null,
array $args = null
) {
if (true === $context->isGuest()) {
if (null === $context->getExtensionAttributes()->getCustomer()) {
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
}

Expand Down
13 changes: 2 additions & 11 deletions app/code/Magento/CustomerGraphQl/Model/Resolver/UpdateCustomer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace Magento\CustomerGraphQl\Model\Resolver;

use Magento\CustomerGraphQl\Model\Customer\GetCustomer;
use Magento\CustomerGraphQl\Model\Customer\UpdateCustomerAccount;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
Expand All @@ -21,11 +20,6 @@
*/
class UpdateCustomer implements ResolverInterface
{
/**
* @var GetCustomer
*/
private $getCustomer;

/**
* @var UpdateCustomerAccount
*/
Expand All @@ -37,16 +31,13 @@ class UpdateCustomer implements ResolverInterface
private $extractCustomerData;

/**
* @param GetCustomer $getCustomer
* @param UpdateCustomerAccount $updateCustomerAccount
* @param ExtractCustomerData $extractCustomerData
*/
public function __construct(
GetCustomer $getCustomer,
UpdateCustomerAccount $updateCustomerAccount,
ExtractCustomerData $extractCustomerData
) {
$this->getCustomer = $getCustomer;
$this->updateCustomerAccount = $updateCustomerAccount;
$this->extractCustomerData = $extractCustomerData;
}
Expand All @@ -61,15 +52,15 @@ public function resolve(
array $value = null,
array $args = null
) {
if (true === $context->isGuest()) {
$customer = $context->getExtensionAttributes()->getCustomer();
if (null === $customer) {
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
}

if (!isset($args['input']) || !is_array($args['input']) || empty($args['input'])) {
throw new GraphQlInputException(__('"input" value should be specified'));
}

$customer = $this->getCustomer->execute($context->getUserId());
$this->updateCustomerAccount->execute($customer, $args['input']);

$data = $this->extractCustomerData->execute($customer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function resolve(
array $value = null,
array $args = null
) {
if (true === $context->isGuest()) {
if (null === $context->getExtensionAttributes()->getCustomer()) {
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function resolve(
array $value = null,
array $args = null
) {
if (true === $context->isGuest()) {
if (null === $context->getExtensionAttributes()->getCustomer()) {
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace Magento\QuoteGraphQl\Model\Cart;

use Magento\CustomerGraphQl\Model\Customer\GetCustomer;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
Expand All @@ -24,28 +23,20 @@ class SetBillingAddressOnCart
*/
private $quoteAddressFactory;

/**
* @var GetCustomer
*/
private $getCustomer;

/**
* @var AssignBillingAddressToCart
*/
private $assignBillingAddressToCart;

/**
* @param QuoteAddressFactory $quoteAddressFactory
* @param GetCustomer $getCustomer
* @param AssignBillingAddressToCart $assignBillingAddressToCart
*/
public function __construct(
QuoteAddressFactory $quoteAddressFactory,
GetCustomer $getCustomer,
AssignBillingAddressToCart $assignBillingAddressToCart
) {
$this->quoteAddressFactory = $quoteAddressFactory;
$this->getCustomer = $getCustomer;
$this->assignBillingAddressToCart = $assignBillingAddressToCart;
}

Expand Down Expand Up @@ -89,10 +80,10 @@ public function execute(ContextInterface $context, CartInterface $cart, array $b
if (null === $customerAddressId) {
$billingAddress = $this->quoteAddressFactory->createBasedOnInputData($addressInput);
} else {
if (true === $context->isGuest()) {
$customer = $context->getExtensionAttributes()->getCustomer();
if (null === $customer) {
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
}
$customer = $this->getCustomer->execute($context->getUserId());
$billingAddress = $this->quoteAddressFactory->createBasedOnCustomerAddress(
(int)$customerAddressId,
(int)$customer->getId()
Expand Down
Loading

0 comments on commit 1bf1312

Please sign in to comment.