Save.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Integration\Controller\Adminhtml\Integration;
  7. use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
  8. use Magento\Integration\Block\Adminhtml\Integration\Edit\Tab\Info;
  9. use Magento\Framework\Exception\IntegrationException;
  10. use Magento\Framework\Exception\LocalizedException;
  11. use Magento\Integration\Model\Integration as IntegrationModel;
  12. use Magento\Framework\Exception\State\UserLockedException;
  13. use Magento\Security\Model\SecurityCookie;
  14. /**
  15. * Integration Save controller
  16. *
  17. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  18. */
  19. class Save extends \Magento\Integration\Controller\Adminhtml\Integration implements HttpPostActionInterface
  20. {
  21. /**
  22. * @var SecurityCookie
  23. */
  24. private $securityCookie;
  25. /**
  26. * Get security cookie
  27. *
  28. * @return SecurityCookie
  29. * @deprecated 100.1.0
  30. */
  31. private function getSecurityCookie()
  32. {
  33. if (!($this->securityCookie instanceof SecurityCookie)) {
  34. return \Magento\Framework\App\ObjectManager::getInstance()->get(SecurityCookie::class);
  35. } else {
  36. return $this->securityCookie;
  37. }
  38. }
  39. /**
  40. * Save integration action.
  41. *
  42. * @return void
  43. */
  44. public function execute()
  45. {
  46. /** @var array $integrationData */
  47. $integrationData = [];
  48. try {
  49. $integrationId = (int)$this->getRequest()->getParam(self::PARAM_INTEGRATION_ID);
  50. if ($integrationId) {
  51. $integrationData = $this->getIntegration($integrationId);
  52. if (!$integrationData) {
  53. return;
  54. }
  55. if ($integrationData[Info::DATA_SETUP_TYPE] == IntegrationModel::TYPE_CONFIG) {
  56. throw new LocalizedException(__("The integrations created in the config file can't be edited."));
  57. }
  58. }
  59. $this->validateUser();
  60. $this->processData($integrationData);
  61. } catch (UserLockedException $e) {
  62. $this->_auth->logout();
  63. $this->getSecurityCookie()->setLogoutReasonCookie(
  64. \Magento\Security\Model\AdminSessionsManager::LOGOUT_REASON_USER_LOCKED
  65. );
  66. $this->_redirect('*');
  67. } catch (\Magento\Framework\Exception\AuthenticationException $e) {
  68. $this->messageManager->addError($e->getMessage());
  69. $this->_getSession()->setIntegrationData($this->getRequest()->getPostValue());
  70. $this->_redirectOnSaveError();
  71. } catch (IntegrationException $e) {
  72. $this->messageManager->addError($this->escaper->escapeHtml($e->getMessage()));
  73. $this->_getSession()->setIntegrationData($integrationData);
  74. $this->_redirectOnSaveError();
  75. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  76. $this->messageManager->addError($this->escaper->escapeHtml($e->getMessage()));
  77. $this->_redirectOnSaveError();
  78. } catch (\Exception $e) {
  79. $this->_logger->critical($e);
  80. $this->messageManager->addError($this->escaper->escapeHtml($e->getMessage()));
  81. $this->_redirectOnSaveError();
  82. }
  83. }
  84. /**
  85. * Validate current user password
  86. *
  87. * @return $this
  88. * @throws UserLockedException
  89. * @throws \Magento\Framework\Exception\AuthenticationException
  90. */
  91. protected function validateUser()
  92. {
  93. $password = $this->getRequest()->getParam(
  94. \Magento\Integration\Block\Adminhtml\Integration\Edit\Tab\Info::DATA_CONSUMER_PASSWORD
  95. );
  96. $user = $this->_auth->getUser();
  97. $user->performIdentityCheck($password);
  98. return $this;
  99. }
  100. /**
  101. * Get Integration entity
  102. *
  103. * @param int $integrationId
  104. * @return \Magento\Integration\Model\Integration|null
  105. */
  106. protected function getIntegration($integrationId)
  107. {
  108. try {
  109. $integrationData = $this->_integrationService->get($integrationId)->getData();
  110. } catch (IntegrationException $e) {
  111. $this->messageManager->addError($this->escaper->escapeHtml($e->getMessage()));
  112. $this->_redirect('*/*/');
  113. return null;
  114. } catch (\Exception $e) {
  115. $this->_logger->critical($e);
  116. $this->messageManager->addError(__('Internal error. Check exception log for details.'));
  117. $this->_redirect('*/*');
  118. return null;
  119. }
  120. return $integrationData;
  121. }
  122. /**
  123. * Redirect merchant to 'Edit integration' or 'New integration' if error happened during integration save.
  124. *
  125. * @return void
  126. */
  127. protected function _redirectOnSaveError()
  128. {
  129. $integrationId = $this->getRequest()->getParam(self::PARAM_INTEGRATION_ID);
  130. if ($integrationId) {
  131. $this->_redirect('*/*/edit', ['id' => $integrationId]);
  132. } else {
  133. $this->_redirect('*/*/new');
  134. }
  135. }
  136. /**
  137. * Save integration data.
  138. *
  139. * @param array $integrationData
  140. * @return void
  141. */
  142. private function processData($integrationData)
  143. {
  144. /** @var array $data */
  145. $data = $this->getRequest()->getPostValue();
  146. if (!empty($data)) {
  147. if (!isset($data['resource'])) {
  148. $integrationData['resource'] = [];
  149. }
  150. $integrationData = array_merge($integrationData, $data);
  151. if (!isset($integrationData[Info::DATA_ID])) {
  152. $integration = $this->_integrationService->create($integrationData);
  153. } else {
  154. $integration = $this->_integrationService->update($integrationData);
  155. }
  156. if (!$this->getRequest()->isXmlHttpRequest()) {
  157. $this->messageManager->addSuccess(
  158. __(
  159. 'The integration \'%1\' has been saved.',
  160. $this->escaper->escapeHtml($integration->getName())
  161. )
  162. );
  163. $this->_redirect('*/*/');
  164. } else {
  165. $isTokenExchange = $integration->getEndpoint() && $integration->getIdentityLinkUrl() ? '1' : '0';
  166. $this->getResponse()->representJson(
  167. $this->jsonHelper->jsonEncode(
  168. ['integrationId' => $integration->getId(), 'isTokenExchange' => $isTokenExchange]
  169. )
  170. );
  171. }
  172. } else {
  173. $this->messageManager->addError(__('The integration was not saved.'));
  174. }
  175. }
  176. }