123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Integration\Controller\Adminhtml\Integration;
- use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
- use Magento\Integration\Block\Adminhtml\Integration\Edit\Tab\Info;
- use Magento\Framework\Exception\IntegrationException;
- use Magento\Framework\Exception\LocalizedException;
- use Magento\Integration\Model\Integration as IntegrationModel;
- use Magento\Framework\Exception\State\UserLockedException;
- use Magento\Security\Model\SecurityCookie;
- /**
- * Integration Save controller
- *
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class Save extends \Magento\Integration\Controller\Adminhtml\Integration implements HttpPostActionInterface
- {
- /**
- * @var SecurityCookie
- */
- private $securityCookie;
- /**
- * Get security cookie
- *
- * @return SecurityCookie
- * @deprecated 100.1.0
- */
- private function getSecurityCookie()
- {
- if (!($this->securityCookie instanceof SecurityCookie)) {
- return \Magento\Framework\App\ObjectManager::getInstance()->get(SecurityCookie::class);
- } else {
- return $this->securityCookie;
- }
- }
- /**
- * Save integration action.
- *
- * @return void
- */
- public function execute()
- {
- /** @var array $integrationData */
- $integrationData = [];
- try {
- $integrationId = (int)$this->getRequest()->getParam(self::PARAM_INTEGRATION_ID);
- if ($integrationId) {
- $integrationData = $this->getIntegration($integrationId);
- if (!$integrationData) {
- return;
- }
- if ($integrationData[Info::DATA_SETUP_TYPE] == IntegrationModel::TYPE_CONFIG) {
- throw new LocalizedException(__("The integrations created in the config file can't be edited."));
- }
- }
- $this->validateUser();
- $this->processData($integrationData);
- } catch (UserLockedException $e) {
- $this->_auth->logout();
- $this->getSecurityCookie()->setLogoutReasonCookie(
- \Magento\Security\Model\AdminSessionsManager::LOGOUT_REASON_USER_LOCKED
- );
- $this->_redirect('*');
- } catch (\Magento\Framework\Exception\AuthenticationException $e) {
- $this->messageManager->addError($e->getMessage());
- $this->_getSession()->setIntegrationData($this->getRequest()->getPostValue());
- $this->_redirectOnSaveError();
- } catch (IntegrationException $e) {
- $this->messageManager->addError($this->escaper->escapeHtml($e->getMessage()));
- $this->_getSession()->setIntegrationData($integrationData);
- $this->_redirectOnSaveError();
- } catch (\Magento\Framework\Exception\LocalizedException $e) {
- $this->messageManager->addError($this->escaper->escapeHtml($e->getMessage()));
- $this->_redirectOnSaveError();
- } catch (\Exception $e) {
- $this->_logger->critical($e);
- $this->messageManager->addError($this->escaper->escapeHtml($e->getMessage()));
- $this->_redirectOnSaveError();
- }
- }
- /**
- * Validate current user password
- *
- * @return $this
- * @throws UserLockedException
- * @throws \Magento\Framework\Exception\AuthenticationException
- */
- protected function validateUser()
- {
- $password = $this->getRequest()->getParam(
- \Magento\Integration\Block\Adminhtml\Integration\Edit\Tab\Info::DATA_CONSUMER_PASSWORD
- );
- $user = $this->_auth->getUser();
- $user->performIdentityCheck($password);
- return $this;
- }
- /**
- * Get Integration entity
- *
- * @param int $integrationId
- * @return \Magento\Integration\Model\Integration|null
- */
- protected function getIntegration($integrationId)
- {
- try {
- $integrationData = $this->_integrationService->get($integrationId)->getData();
- } catch (IntegrationException $e) {
- $this->messageManager->addError($this->escaper->escapeHtml($e->getMessage()));
- $this->_redirect('*/*/');
- return null;
- } catch (\Exception $e) {
- $this->_logger->critical($e);
- $this->messageManager->addError(__('Internal error. Check exception log for details.'));
- $this->_redirect('*/*');
- return null;
- }
- return $integrationData;
- }
- /**
- * Redirect merchant to 'Edit integration' or 'New integration' if error happened during integration save.
- *
- * @return void
- */
- protected function _redirectOnSaveError()
- {
- $integrationId = $this->getRequest()->getParam(self::PARAM_INTEGRATION_ID);
- if ($integrationId) {
- $this->_redirect('*/*/edit', ['id' => $integrationId]);
- } else {
- $this->_redirect('*/*/new');
- }
- }
- /**
- * Save integration data.
- *
- * @param array $integrationData
- * @return void
- */
- private function processData($integrationData)
- {
- /** @var array $data */
- $data = $this->getRequest()->getPostValue();
- if (!empty($data)) {
- if (!isset($data['resource'])) {
- $integrationData['resource'] = [];
- }
- $integrationData = array_merge($integrationData, $data);
- if (!isset($integrationData[Info::DATA_ID])) {
- $integration = $this->_integrationService->create($integrationData);
- } else {
- $integration = $this->_integrationService->update($integrationData);
- }
- if (!$this->getRequest()->isXmlHttpRequest()) {
- $this->messageManager->addSuccess(
- __(
- 'The integration \'%1\' has been saved.',
- $this->escaper->escapeHtml($integration->getName())
- )
- );
- $this->_redirect('*/*/');
- } else {
- $isTokenExchange = $integration->getEndpoint() && $integration->getIdentityLinkUrl() ? '1' : '0';
- $this->getResponse()->representJson(
- $this->jsonHelper->jsonEncode(
- ['integrationId' => $integration->getId(), 'isTokenExchange' => $isTokenExchange]
- )
- );
- }
- } else {
- $this->messageManager->addError(__('The integration was not saved.'));
- }
- }
- }
|