This repository has been archived by the owner on Dec 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#741: Add extension point to set custom parameters to Query Context o…
…bject
- Loading branch information
Showing
14 changed files
with
563 additions
and
185 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
app/code/Magento/CustomerGraphQl/Model/Context/AddUserInfoToContext.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CustomerGraphQl\Model\Context; | ||
|
||
use Magento\Authorization\Model\UserContextInterface; | ||
use Magento\GraphQl\Model\Query\ContextParametersInterface; | ||
use Magento\GraphQl\Model\Query\ContextParametersProcessorInterface; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
class AddUserInfoToContext implements ContextParametersProcessorInterface | ||
{ | ||
/** | ||
* @var UserContextInterface | ||
*/ | ||
private $userContext; | ||
|
||
/** | ||
* @var GetCustomer | ||
*/ | ||
private $getCustomer; | ||
|
||
/** | ||
* @param UserContextInterface $userContext | ||
* @param GetCustomer $getCustomer | ||
*/ | ||
public function __construct( | ||
UserContextInterface $userContext, | ||
GetCustomer $getCustomer | ||
) { | ||
$this->userContext = $userContext; | ||
$this->getCustomer = $getCustomer; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function execute(ContextParametersInterface $contextParameters): ContextParametersInterface | ||
{ | ||
$currentUserId = $this->userContext->getUserId(); | ||
if (null !== $currentUserId) { | ||
$currentUserId = (int)$currentUserId; | ||
} | ||
|
||
$currentUserType = $this->userContext->getUserType(); | ||
if (null !== $currentUserType) { | ||
$currentUserType = (int)$currentUserType; | ||
} | ||
|
||
if (false === $this->isUserGuest($currentUserId, $currentUserType)) { | ||
$customer = $this->getCustomer->execute($currentUserId); | ||
$contextParameters->addExtensionAttribute('customer', $customer); | ||
} | ||
|
||
$contextParameters->setUserId($currentUserId); | ||
$contextParameters->setUserType($currentUserType); | ||
return $contextParameters; | ||
} | ||
|
||
/** | ||
* 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; | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
app/code/Magento/CustomerGraphQl/Model/Context/GetCustomer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CustomerGraphQl\Model\Context; | ||
|
||
use Magento\Customer\Api\AccountManagementInterface; | ||
use Magento\Customer\Api\CustomerRepositoryInterface; | ||
use Magento\Customer\Api\Data\CustomerInterface; | ||
use Magento\Customer\Model\AuthenticationInterface; | ||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\Framework\Exception\NoSuchEntityException; | ||
use Magento\Framework\GraphQl\Exception\GraphQlAuthenticationException; | ||
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException; | ||
use Magento\Framework\GraphQl\Exception\GraphQlInputException; | ||
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; | ||
|
||
/** | ||
* Get customer | ||
*/ | ||
class GetCustomer | ||
{ | ||
/** | ||
* @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; | ||
} | ||
|
||
/** | ||
* Get customer | ||
* | ||
* @param int $customerId | ||
* @return void | ||
* @throws GraphQlAuthenticationException | ||
* @throws GraphQlAuthorizationException | ||
* @throws GraphQlInputException | ||
* @throws GraphQlNoSuchEntityException | ||
*/ | ||
public function execute(int $customerId): CustomerInterface | ||
{ | ||
try { | ||
$customer = $this->customerRepository->getById($customerId); | ||
} catch (NoSuchEntityException $e) { | ||
throw new GraphQlNoSuchEntityException( | ||
__('Customer with id "%customer_id" does not exist.', ['customer_id' => $customerId]), | ||
$e | ||
); | ||
} catch (LocalizedException $e) { | ||
throw new GraphQlInputException(__($e->getMessage())); | ||
} | ||
|
||
if (true === $this->authentication->isLocked($customerId)) { | ||
throw new GraphQlAuthenticationException(__('The account is locked.')); | ||
} | ||
|
||
try { | ||
$confirmationStatus = $this->accountManagement->getConfirmationStatus($customerId); | ||
} catch (LocalizedException $e) { | ||
throw new GraphQlInputException(__($e->getMessage())); | ||
} | ||
|
||
if ($confirmationStatus === AccountManagementInterface::ACCOUNT_CONFIRMATION_REQUIRED) { | ||
throw new GraphQlAuthenticationException(__("This account isn't confirmed. Verify and try again.")); | ||
} | ||
return $customer; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\GraphQl\Model\Query; | ||
|
||
/** | ||
* Concrete implementation for @see ContextInterface | ||
* | ||
* The purpose for this that GraphQL specification wants to make use of such object where multiple modules can | ||
* participate with data through extension attributes. | ||
*/ | ||
class Context implements ContextInterface | ||
{ | ||
/** | ||
* @var int|null | ||
*/ | ||
private $userType; | ||
|
||
/** | ||
* @var int|null | ||
*/ | ||
private $userId; | ||
|
||
/** | ||
* @var ContextExtensionInterface | ||
*/ | ||
private $extensionAttributes; | ||
|
||
/** | ||
* @param int|null $userType | ||
* @param int|null $userId | ||
* @param ContextExtensionInterface $extensionAttributes | ||
*/ | ||
public function __construct( | ||
?int $userType, | ||
?int $userId, | ||
ContextExtensionInterface $extensionAttributes | ||
) { | ||
$this->userType = $userType; | ||
$this->userId = $userId; | ||
$this->extensionAttributes = $extensionAttributes; | ||
} | ||
|
||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getUserType(): ?int | ||
{ | ||
return $this->userType; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getUserId(): ?int | ||
{ | ||
return $this->userId; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function getExtensionAttributes(): ContextExtensionInterface | ||
{ | ||
return $this->extensionAttributes; | ||
} | ||
} |
Oops, something went wrong.