InvalidateToken.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Customer\Controller\Adminhtml\Customer;
  8. use Magento\Framework\App\Action\HttpGetActionInterface;
  9. use Magento\Integration\Api\CustomerTokenServiceInterface;
  10. use Magento\Customer\Api\AccountManagementInterface;
  11. use Magento\Customer\Api\AddressRepositoryInterface;
  12. use Magento\Customer\Api\CustomerRepositoryInterface;
  13. use Magento\Customer\Api\Data\AddressInterfaceFactory;
  14. use Magento\Customer\Api\Data\CustomerInterfaceFactory;
  15. use Magento\Customer\Model\Address\Mapper;
  16. use Magento\Framework\DataObjectFactory;
  17. use Magento\Framework\Api\DataObjectHelper;
  18. /**
  19. * Class to invalidate tokens for customers
  20. *
  21. * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
  22. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  23. * @SuppressWarnings(PHPMD.TooManyFields)
  24. * @SuppressWarnings(PHPMD.NumberOfChildren)
  25. */
  26. class InvalidateToken extends \Magento\Customer\Controller\Adminhtml\Index implements HttpGetActionInterface
  27. {
  28. /**
  29. * Authorization level of a basic admin session
  30. *
  31. * @see _isAllowed()
  32. */
  33. const ADMIN_RESOURCE = 'Magento_Customer::invalidate_tokens';
  34. /**
  35. * @var CustomerTokenServiceInterface
  36. */
  37. protected $tokenService;
  38. /**
  39. * @param \Magento\Backend\App\Action\Context $context
  40. * @param \Magento\Framework\Registry $coreRegistry
  41. * @param \Magento\Framework\App\Response\Http\FileFactory $fileFactory
  42. * @param \Magento\Customer\Model\CustomerFactory $customerFactory
  43. * @param \Magento\Customer\Model\AddressFactory $addressFactory
  44. * @param \Magento\Customer\Model\Metadata\FormFactory $formFactory
  45. * @param \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory
  46. * @param \Magento\Customer\Helper\View $viewHelper
  47. * @param \Magento\Framework\Math\Random $random
  48. * @param CustomerRepositoryInterface $customerRepository
  49. * @param \Magento\Framework\Api\ExtensibleDataObjectConverter $extensibleDataObjectConverter
  50. * @param Mapper $addressMapper
  51. * @param AccountManagementInterface $customerAccountManagement
  52. * @param AddressRepositoryInterface $addressRepository
  53. * @param CustomerInterfaceFactory $customerDataFactory
  54. * @param AddressInterfaceFactory $addressDataFactory
  55. * @param \Magento\Customer\Model\Customer\Mapper $customerMapper
  56. * @param \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor
  57. * @param DataObjectHelper $dataObjectHelper
  58. * @param DataObjectFactory $objectFactory
  59. * @param \Magento\Framework\View\LayoutFactory $layoutFactory
  60. * @param \Magento\Framework\View\Result\LayoutFactory $resultLayoutFactory
  61. * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
  62. * @param \Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory
  63. * @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
  64. * @param CustomerTokenServiceInterface $tokenService
  65. *
  66. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  67. */
  68. public function __construct(
  69. \Magento\Backend\App\Action\Context $context,
  70. \Magento\Framework\Registry $coreRegistry,
  71. \Magento\Framework\App\Response\Http\FileFactory $fileFactory,
  72. \Magento\Customer\Model\CustomerFactory $customerFactory,
  73. \Magento\Customer\Model\AddressFactory $addressFactory,
  74. \Magento\Customer\Model\Metadata\FormFactory $formFactory,
  75. \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory,
  76. \Magento\Customer\Helper\View $viewHelper,
  77. \Magento\Framework\Math\Random $random,
  78. CustomerRepositoryInterface $customerRepository,
  79. \Magento\Framework\Api\ExtensibleDataObjectConverter $extensibleDataObjectConverter,
  80. Mapper $addressMapper,
  81. AccountManagementInterface $customerAccountManagement,
  82. AddressRepositoryInterface $addressRepository,
  83. CustomerInterfaceFactory $customerDataFactory,
  84. AddressInterfaceFactory $addressDataFactory,
  85. \Magento\Customer\Model\Customer\Mapper $customerMapper,
  86. \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor,
  87. DataObjectHelper $dataObjectHelper,
  88. DataObjectFactory $objectFactory,
  89. \Magento\Framework\View\LayoutFactory $layoutFactory,
  90. \Magento\Framework\View\Result\LayoutFactory $resultLayoutFactory,
  91. \Magento\Framework\View\Result\PageFactory $resultPageFactory,
  92. \Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory,
  93. \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
  94. CustomerTokenServiceInterface $tokenService
  95. ) {
  96. $this->tokenService = $tokenService;
  97. parent::__construct(
  98. $context,
  99. $coreRegistry,
  100. $fileFactory,
  101. $customerFactory,
  102. $addressFactory,
  103. $formFactory,
  104. $subscriberFactory,
  105. $viewHelper,
  106. $random,
  107. $customerRepository,
  108. $extensibleDataObjectConverter,
  109. $addressMapper,
  110. $customerAccountManagement,
  111. $addressRepository,
  112. $customerDataFactory,
  113. $addressDataFactory,
  114. $customerMapper,
  115. $dataObjectProcessor,
  116. $dataObjectHelper,
  117. $objectFactory,
  118. $layoutFactory,
  119. $resultLayoutFactory,
  120. $resultPageFactory,
  121. $resultForwardFactory,
  122. $resultJsonFactory
  123. );
  124. }
  125. /**
  126. * Reset customer's tokens handler
  127. *
  128. * @return \Magento\Backend\Model\View\Result\Redirect
  129. */
  130. public function execute()
  131. {
  132. $resultRedirect = $this->resultRedirectFactory->create();
  133. if ($customerId = $this->getRequest()->getParam('customer_id')) {
  134. try {
  135. $this->tokenService->revokeCustomerAccessToken($customerId);
  136. $this->messageManager->addSuccess(__('You have revoked the customer\'s tokens.'));
  137. $resultRedirect->setPath('customer/index/edit', ['id' => $customerId, '_current' => true]);
  138. } catch (\Exception $e) {
  139. $this->messageManager->addError($e->getMessage());
  140. $resultRedirect->setPath('customer/index/edit', ['id' => $customerId, '_current' => true]);
  141. }
  142. } else {
  143. $this->messageManager->addError(__('We can\'t find a customer to revoke.'));
  144. $resultRedirect->setPath('customer/index/index');
  145. }
  146. return $resultRedirect;
  147. }
  148. }