TokensDialog.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Integration\Controller\Adminhtml\Integration;
  8. use Magento\Framework\App\Action\HttpGetActionInterface as HttpGetActionInterface;
  9. use Magento\Integration\Model\Integration as IntegrationModel;
  10. class TokensDialog extends \Magento\Integration\Controller\Adminhtml\Integration implements HttpGetActionInterface
  11. {
  12. /**
  13. * Set success message based on Integration activation or re-authorization.
  14. *
  15. * @param boolean $isReauthorize Is a re-authorization flow
  16. * @param string $integrationName Integration name
  17. * @return void
  18. */
  19. protected function _setActivationSuccessMsg($isReauthorize, $integrationName)
  20. {
  21. $integrationName = $this->escaper->escapeHtml($integrationName);
  22. $successMsg = $isReauthorize ? __(
  23. "The integration '%1' has been re-authorized.",
  24. $integrationName
  25. ) : __(
  26. "The integration '%1' has been activated.",
  27. $integrationName
  28. );
  29. $this->messageManager->addSuccess($successMsg);
  30. }
  31. /**
  32. * Show tokens popup for simple tokens
  33. *
  34. * @return void
  35. */
  36. public function execute()
  37. {
  38. try {
  39. $integrationId = $this->getRequest()->getParam(self::PARAM_INTEGRATION_ID);
  40. $integration = $this->_integrationService->get($integrationId);
  41. $clearExistingToken = (int)$this->getRequest()->getParam(self::PARAM_REAUTHORIZE, 0);
  42. if ($this->_oauthService->createAccessToken($integration->getConsumerId(), $clearExistingToken)) {
  43. $integration->setStatus(IntegrationModel::STATUS_ACTIVE)->save();
  44. }
  45. // Important to call get() once again - that will pull newly generated token
  46. $this->_registry->register(
  47. self::REGISTRY_KEY_CURRENT_INTEGRATION,
  48. $this->_integrationService->get($integrationId)->getData()
  49. );
  50. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  51. $this->messageManager->addError($e->getMessage());
  52. $this->_redirect('*/*');
  53. return;
  54. } catch (\Exception $e) {
  55. $this->_logger->critical($e);
  56. $this->messageManager->addError(__('Internal error. Check exception log for details.'));
  57. $this->_redirect('*/*');
  58. return;
  59. }
  60. $this->_view->loadLayout(false);
  61. //This cannot precede loadlayout(false) else the messages will be removed
  62. $this->_setActivationSuccessMsg($clearExistingToken, $integration->getName());
  63. $this->_view->renderLayout();
  64. }
  65. }