Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Save.php
Go to the documentation of this file.
1 <?php
7 
8 use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
15 
22 {
26  private $securityCookie;
27 
34  private function getSecurityCookie()
35  {
36  if (!($this->securityCookie instanceof SecurityCookie)) {
37  return \Magento\Framework\App\ObjectManager::getInstance()->get(SecurityCookie::class);
38  } else {
39  return $this->securityCookie;
40  }
41  }
42 
48  public function execute()
49  {
51  $integrationData = [];
52  try {
53  $integrationId = (int)$this->getRequest()->getParam(self::PARAM_INTEGRATION_ID);
54  if ($integrationId) {
55  $integrationData = $this->getIntegration($integrationId);
56  if (!$integrationData) {
57  return;
58  }
59  if ($integrationData[Info::DATA_SETUP_TYPE] == IntegrationModel::TYPE_CONFIG) {
60  throw new LocalizedException(__("The integrations created in the config file can't be edited."));
61  }
62  }
63  $this->validateUser();
64  $this->processData($integrationData);
65  } catch (UserLockedException $e) {
66  $this->_auth->logout();
67  $this->getSecurityCookie()->setLogoutReasonCookie(
68  \Magento\Security\Model\AdminSessionsManager::LOGOUT_REASON_USER_LOCKED
69  );
70  $this->_redirect('*');
71  } catch (\Magento\Framework\Exception\AuthenticationException $e) {
72  $this->messageManager->addError($e->getMessage());
73  $this->_getSession()->setIntegrationData($this->getRequest()->getPostValue());
74  $this->_redirectOnSaveError();
75  } catch (IntegrationException $e) {
76  $this->messageManager->addError($this->escaper->escapeHtml($e->getMessage()));
77  $this->_getSession()->setIntegrationData($integrationData);
78  $this->_redirectOnSaveError();
79  } catch (\Magento\Framework\Exception\LocalizedException $e) {
80  $this->messageManager->addError($this->escaper->escapeHtml($e->getMessage()));
81  $this->_redirectOnSaveError();
82  } catch (\Exception $e) {
83  $this->_logger->critical($e);
84  $this->messageManager->addError($this->escaper->escapeHtml($e->getMessage()));
85  $this->_redirectOnSaveError();
86  }
87  }
88 
96  protected function validateUser()
97  {
98  $password = $this->getRequest()->getParam(
99  \Magento\Integration\Block\Adminhtml\Integration\Edit\Tab\Info::DATA_CONSUMER_PASSWORD
100  );
101  $user = $this->_auth->getUser();
102  $user->performIdentityCheck($password);
103 
104  return $this;
105  }
106 
113  protected function getIntegration($integrationId)
114  {
115  try {
116  $integrationData = $this->_integrationService->get($integrationId)->getData();
117  } catch (IntegrationException $e) {
118  $this->messageManager->addError($this->escaper->escapeHtml($e->getMessage()));
119  $this->_redirect('*/*/');
120  return null;
121  } catch (\Exception $e) {
122  $this->_logger->critical($e);
123  $this->messageManager->addError(__('Internal error. Check exception log for details.'));
124  $this->_redirect('*/*');
125  return null;
126  }
127 
128  return $integrationData;
129  }
130 
136  protected function _redirectOnSaveError()
137  {
138  $integrationId = $this->getRequest()->getParam(self::PARAM_INTEGRATION_ID);
139  if ($integrationId) {
140  $this->_redirect('*/*/edit', ['id' => $integrationId]);
141  } else {
142  $this->_redirect('*/*/new');
143  }
144  }
145 
152  private function processData($integrationData)
153  {
155  $data = $this->getRequest()->getPostValue();
156  if (!empty($data)) {
157  if (!isset($data['resource'])) {
158  $integrationData['resource'] = [];
159  }
160  $integrationData = array_merge($integrationData, $data);
161  if (!isset($integrationData[Info::DATA_ID])) {
162  $integration = $this->_integrationService->create($integrationData);
163  } else {
164  $integration = $this->_integrationService->update($integrationData);
165  }
166  if (!$this->getRequest()->isXmlHttpRequest()) {
167  $this->messageManager->addSuccess(
168  __(
169  'The integration \'%1\' has been saved.',
170  $this->escaper->escapeHtml($integration->getName())
171  )
172  );
173  $this->_redirect('*/*/');
174  } else {
175  $isTokenExchange = $integration->getEndpoint() && $integration->getIdentityLinkUrl() ? '1' : '0';
176  $this->getResponse()->representJson(
177  $this->jsonHelper->jsonEncode(
178  ['integrationId' => $integration->getId(), 'isTokenExchange' => $isTokenExchange]
179  )
180  );
181  }
182  } else {
183  $this->messageManager->addError(__('The integration was not saved.'));
184  }
185  }
186 }
__()
Definition: __.php:13
$user
Definition: dummy_user.php:13