Skip to content
This repository was archived by the owner on Dec 19, 2019. It is now read-only.

#486 added customer account validation in Quote operations #714

Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,27 @@ public function execute(ContextParametersInterface $contextParameters): ContextP
if (null !== $currentUserId) {
$currentUserId = (int)$currentUserId;
}
$contextParameters->setUserId($currentUserId);

$currentUserType = $this->userContext->getUserType();
if (null !== $currentUserType) {
$currentUserType = (int)$currentUserType;
}

$contextParameters->setUserId($currentUserId);
$contextParameters->setUserType($currentUserType);

$contextParameters->addExtensionAttribute('is_customer', $this->isCustomer($currentUserId, $currentUserType));
return $contextParameters;
}

/**
* Checking if current user is logged
*
* @param int|null $customerId
* @param int|null $customerType
* @return bool
*/
private function isCustomer(?int $customerId, ?int $customerType): bool
{
return !empty($customerId) && !empty($customerType) && $customerType !== UserContextInterface::USER_TYPE_GUEST;
}
}
23 changes: 1 addition & 22 deletions app/code/Magento/CustomerGraphQl/Model/Customer/GetCustomer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace Magento\CustomerGraphQl\Model\Customer;

use Magento\Authorization\Model\UserContextInterface;
use Magento\Customer\Api\AccountManagementInterface;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Api\Data\CustomerInterface;
Expand All @@ -18,7 +17,7 @@
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
use Magento\GraphQl\Model\Query\ContextInterface;

/**
* Get customer
Expand Down Expand Up @@ -68,11 +67,6 @@ public function __construct(
public function execute(ContextInterface $context): CustomerInterface
{
$currentUserId = $context->getUserId();
$currentUserType = $context->getUserType();

if (true === $this->isUserGuest($currentUserId, $currentUserType)) {
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
}

try {
$customer = $this->customerRepository->getById($currentUserId);
Expand Down Expand Up @@ -100,19 +94,4 @@ public function execute(ContextInterface $context): CustomerInterface
}
return $customer;
}

/**
* Checking if current customer is guest
*
* @param int|null $customerId
* @param int|null $customerType
* @return bool
*/
private function isUserGuest(?int $customerId, ?int $customerType): bool
{
if (null === $customerId || null === $customerType) {
return true;
}
return 0 === (int)$customerId || (int)$customerType === UserContextInterface::USER_TYPE_GUEST;
}
}
13 changes: 10 additions & 3 deletions app/code/Magento/CustomerGraphQl/Model/Resolver/ChangePassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
use Magento\CustomerGraphQl\Model\Customer\GetCustomer;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\GraphQl\Model\Query\ContextInterface;

/**
* Change customer password resolver
Expand Down Expand Up @@ -70,6 +72,11 @@ public function resolve(
array $value = null,
array $args = null
) {
/** @var ContextInterface $context */
if (false === $context->getExtensionAttributes()->getIsCustomer()) {
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
}

if (!isset($args['currentPassword']) || '' == trim($args['currentPassword'])) {
throw new GraphQlInputException(__('Specify the "currentPassword" value.'));
}
Expand All @@ -78,16 +85,16 @@ public function resolve(
throw new GraphQlInputException(__('Specify the "newPassword" value.'));
}

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

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

try {
$this->accountManagement->changePasswordById($customerId, $args['currentPassword'], $args['newPassword']);
} catch (LocalizedException $e) {
throw new GraphQlInputException(__($e->getMessage()), $e);
}

$customer = $this->getCustomer->execute($context);
return $this->extractCustomerData->execute($customer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,18 @@

use Magento\CustomerGraphQl\Model\Customer\Address\CreateCustomerAddress as CreateCustomerAddressModel;
use Magento\CustomerGraphQl\Model\Customer\Address\ExtractCustomerAddressData;
use Magento\CustomerGraphQl\Model\Customer\GetCustomer;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\GraphQl\Model\Query\ContextInterface;

/**
* Customers address create, used for GraphQL request processing
*/
class CreateCustomerAddress implements ResolverInterface
{
/**
* @var GetCustomer
*/
private $getCustomer;

/**
* @var CreateCustomerAddressModel
*/
Expand All @@ -36,16 +32,13 @@ class CreateCustomerAddress implements ResolverInterface
private $extractCustomerAddressData;

/**
* @param GetCustomer $getCustomer
* @param CreateCustomerAddressModel $createCustomerAddress
* @param ExtractCustomerAddressData $extractCustomerAddressData
*/
public function __construct(
GetCustomer $getCustomer,
CreateCustomerAddressModel $createCustomerAddress,
ExtractCustomerAddressData $extractCustomerAddressData
) {
$this->getCustomer = $getCustomer;
$this->createCustomerAddress = $createCustomerAddress;
$this->extractCustomerAddressData = $extractCustomerAddressData;
}
Expand All @@ -60,13 +53,16 @@ public function resolve(
array $value = null,
array $args = null
) {
/** @var ContextInterface $context */
if (false === $context->getExtensionAttributes()->getIsCustomer()) {
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);

$address = $this->createCustomerAddress->execute((int)$customer->getId(), $args['input']);
$address = $this->createCustomerAddress->execute($context->getUserId(), $args['input']);
return $this->extractCustomerAddressData->execute($address);
}
}
8 changes: 7 additions & 1 deletion app/code/Magento/CustomerGraphQl/Model/Resolver/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
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;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\GraphQl\Model\Query\ContextInterface;

/**
* Customers field resolver, used for GraphQL request processing.
Expand Down Expand Up @@ -50,8 +52,12 @@ public function resolve(
array $value = null,
array $args = null
) {
$customer = $this->getCustomer->execute($context);
/** @var ContextInterface $context */
if (false === $context->getExtensionAttributes()->getIsCustomer()) {
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
}

$customer = $this->getCustomer->execute($context);
return $this->extractCustomerData->execute($customer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
namespace Magento\CustomerGraphQl\Model\Resolver;

use Magento\Customer\Model\Customer;
use Magento\CustomerGraphQl\Model\Customer\GetCustomer;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\GraphQl\Config\Element\Field;
Expand All @@ -20,25 +19,17 @@
*/
class CustomerAddresses implements ResolverInterface
{
/**
* @var GetCustomer
*/
private $getCustomer;

/**
* @var ExtractCustomerAddressData
*/
private $extractCustomerAddressData;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,18 @@

use Magento\CustomerGraphQl\Model\Customer\Address\DeleteCustomerAddress as DeleteCustomerAddressModel;
use Magento\CustomerGraphQl\Model\Customer\Address\GetCustomerAddress;
use Magento\CustomerGraphQl\Model\Customer\GetCustomer;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\GraphQl\Model\Query\ContextInterface;

/**
* Customers address delete, used for GraphQL request processing.
*/
class DeleteCustomerAddress implements ResolverInterface
{
/**
* @var GetCustomer
*/
private $getCustomer;

/**
* @var GetCustomerAddress
*/
Expand All @@ -36,16 +32,13 @@ class DeleteCustomerAddress implements ResolverInterface
private $deleteCustomerAddress;

/**
* @param GetCustomer $getCustomer
* @param GetCustomerAddress $getCustomerAddress
* @param DeleteCustomerAddressModel $deleteCustomerAddress
*/
public function __construct(
GetCustomer $getCustomer,
GetCustomerAddress $getCustomerAddress,
DeleteCustomerAddressModel $deleteCustomerAddress
) {
$this->getCustomer = $getCustomer;
$this->getCustomerAddress = $getCustomerAddress;
$this->deleteCustomerAddress = $deleteCustomerAddress;
}
Expand All @@ -60,13 +53,16 @@ public function resolve(
array $value = null,
array $args = null
) {
/** @var ContextInterface $context */
if (false === $context->getExtensionAttributes()->getIsCustomer()) {
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
}

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

$customer = $this->getCustomer->execute($context);
$address = $this->getCustomerAddress->execute((int)$args['id'], (int)$customer->getId());

$address = $this->getCustomerAddress->execute((int)$args['id'], $context->getUserId());
$this->deleteCustomerAddress->execute($address);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,29 @@

namespace Magento\CustomerGraphQl\Model\Resolver;

use Magento\CustomerGraphQl\Model\Customer\GetCustomer;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\GraphQl\Model\Query\ContextInterface;
use Magento\Integration\Api\CustomerTokenServiceInterface;

/**
* Customers Revoke Token resolver, used for GraphQL request processing.
*/
class RevokeCustomerToken implements ResolverInterface
{
/**
* @var GetCustomer
*/
private $getCustomer;

/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;

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

Expand All @@ -50,8 +43,11 @@ public function resolve(
array $value = null,
array $args = null
) {
$customer = $this->getCustomer->execute($context);
/** @var ContextInterface $context */
if (false === $context->getExtensionAttributes()->getIsCustomer()) {
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
}

return ['result' => $this->customerTokenService->revokeCustomerAccessToken((int)$customer->getId())];
return ['result' => $this->customerTokenService->revokeCustomerAccessToken($context->getUserId())];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@

use Magento\CustomerGraphQl\Model\Customer\GetCustomer;
use Magento\CustomerGraphQl\Model\Customer\UpdateCustomerAccount;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\CustomerGraphQl\Model\Customer\ExtractCustomerData;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\GraphQl\Model\Query\ContextInterface;

/**
* Update customer data resolver
Expand Down Expand Up @@ -60,6 +62,11 @@ public function resolve(
array $value = null,
array $args = null
) {
/** @var ContextInterface $context */
if (false === $context->getExtensionAttributes()->getIsCustomer()) {
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'));
}
Expand Down
Loading