|
| 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\Plugin; |
| 9 | + |
| 10 | +use Magento\Authorization\Model\UserContextInterface; |
| 11 | +use Magento\Customer\Model\ResourceModel\CustomerRepository; |
| 12 | +use Magento\Customer\Model\Session as CustomerSession; |
| 13 | +use Magento\Framework\App\ResponseInterface; |
| 14 | +use Magento\Framework\Exception\LocalizedException; |
| 15 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 16 | +use Magento\GraphQl\Controller\GraphQl as GraphQlController; |
| 17 | + |
| 18 | +/** |
| 19 | + * Clear the user data out of the session object before returning the GraphQL response |
| 20 | + */ |
| 21 | +class ClearCustomerSessionAfterRequest |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @var UserContextInterface |
| 25 | + */ |
| 26 | + private $userContext; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var CustomerSession |
| 30 | + */ |
| 31 | + private $customerSession; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var CustomerRepository |
| 35 | + */ |
| 36 | + private $customerRepository; |
| 37 | + |
| 38 | + /** |
| 39 | + * @param UserContextInterface $userContext |
| 40 | + * @param CustomerSession $customerSession |
| 41 | + * @param CustomerRepository $customerRepository |
| 42 | + */ |
| 43 | + public function __construct( |
| 44 | + UserContextInterface $userContext, |
| 45 | + CustomerSession $customerSession, |
| 46 | + CustomerRepository $customerRepository |
| 47 | + ) { |
| 48 | + $this->userContext = $userContext; |
| 49 | + $this->customerSession = $customerSession; |
| 50 | + $this->customerRepository = $customerRepository; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Clear the customer data from the session after business logic has completed |
| 55 | + * |
| 56 | + * @param GraphQlController $controller |
| 57 | + * @param ResponseInterface $response |
| 58 | + * @return ResponseInterface |
| 59 | + * |
| 60 | + * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
| 61 | + */ |
| 62 | + public function afterDispatch(GraphQlController $controller, ResponseInterface $response): ResponseInterface |
| 63 | + { |
| 64 | + $this->customerSession->setCustomerId(null); |
| 65 | + $this->customerSession->setCustomerGroupId(null); |
| 66 | + return $response; |
| 67 | + } |
| 68 | +} |
0 commit comments