Save.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\User\Controller\Adminhtml\User;
  7. use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
  8. use Magento\Framework\Exception\AuthenticationException;
  9. use Magento\Framework\Exception\State\UserLockedException;
  10. use Magento\Security\Model\SecurityCookie;
  11. use Magento\User\Model\Spi\NotificationExceptionInterface;
  12. /**
  13. * Save admin user.
  14. *
  15. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  16. */
  17. class Save extends \Magento\User\Controller\Adminhtml\User implements HttpPostActionInterface
  18. {
  19. /**
  20. * @var SecurityCookie
  21. */
  22. private $securityCookie;
  23. /**
  24. * Get security cookie
  25. *
  26. * @return SecurityCookie
  27. * @deprecated 100.1.0
  28. */
  29. private function getSecurityCookie()
  30. {
  31. if (!($this->securityCookie instanceof SecurityCookie)) {
  32. return \Magento\Framework\App\ObjectManager::getInstance()->get(SecurityCookie::class);
  33. } else {
  34. return $this->securityCookie;
  35. }
  36. }
  37. /**
  38. * @inheritDoc
  39. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  40. * @SuppressWarnings(PHPMD.NPathComplexity)
  41. */
  42. public function execute()
  43. {
  44. $userId = (int)$this->getRequest()->getParam('user_id');
  45. $data = $this->getRequest()->getPostValue();
  46. if (array_key_exists('form_key', $data)) {
  47. unset($data['form_key']);
  48. }
  49. if (!$data) {
  50. $this->_redirect('adminhtml/*/');
  51. return;
  52. }
  53. /** @var $model \Magento\User\Model\User */
  54. $model = $this->_userFactory->create()->load($userId);
  55. if ($userId && $model->isObjectNew()) {
  56. $this->messageManager->addError(__('This user no longer exists.'));
  57. $this->_redirect('adminhtml/*/');
  58. return;
  59. }
  60. $model->setData($this->_getAdminUserData($data));
  61. $userRoles = $this->getRequest()->getParam('roles', []);
  62. if (count($userRoles)) {
  63. $model->setRoleId($userRoles[0]);
  64. }
  65. /** @var $currentUser \Magento\User\Model\User */
  66. $currentUser = $this->_objectManager->get(\Magento\Backend\Model\Auth\Session::class)->getUser();
  67. if ($userId == $currentUser->getId()
  68. && $this->_objectManager->get(\Magento\Framework\Validator\Locale::class)
  69. ->isValid($data['interface_locale'])
  70. ) {
  71. $this->_objectManager->get(
  72. \Magento\Backend\Model\Locale\Manager::class
  73. )->switchBackendInterfaceLocale(
  74. $data['interface_locale']
  75. );
  76. }
  77. /** Before updating admin user data, ensure that password of current admin user is entered and is correct */
  78. $currentUserPasswordField = \Magento\User\Block\User\Edit\Tab\Main::CURRENT_USER_PASSWORD_FIELD;
  79. $isCurrentUserPasswordValid = isset($data[$currentUserPasswordField])
  80. && !empty($data[$currentUserPasswordField]) && is_string($data[$currentUserPasswordField]);
  81. try {
  82. if (!($isCurrentUserPasswordValid)) {
  83. throw new AuthenticationException(
  84. __('The password entered for the current user is invalid. Verify the password and try again.')
  85. );
  86. }
  87. $currentUser->performIdentityCheck($data[$currentUserPasswordField]);
  88. $model->save();
  89. $this->messageManager->addSuccess(__('You saved the user.'));
  90. $this->_getSession()->setUserData(false);
  91. $this->_redirect('adminhtml/*/');
  92. $model->sendNotificationEmailsIfRequired();
  93. } catch (UserLockedException $e) {
  94. $this->_auth->logout();
  95. $this->getSecurityCookie()->setLogoutReasonCookie(
  96. \Magento\Security\Model\AdminSessionsManager::LOGOUT_REASON_USER_LOCKED
  97. );
  98. $this->_redirect('adminhtml/*/');
  99. } catch (NotificationExceptionInterface $exception) {
  100. $this->messageManager->addErrorMessage($exception->getMessage());
  101. } catch (\Magento\Framework\Exception\AuthenticationException $e) {
  102. $this->messageManager->addError(
  103. __('The password entered for the current user is invalid. Verify the password and try again.')
  104. );
  105. $this->redirectToEdit($model, $data);
  106. } catch (\Magento\Framework\Validator\Exception $e) {
  107. $messages = $e->getMessages();
  108. $this->messageManager->addMessages($messages);
  109. $this->redirectToEdit($model, $data);
  110. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  111. if ($e->getMessage()) {
  112. $this->messageManager->addError($e->getMessage());
  113. }
  114. $this->redirectToEdit($model, $data);
  115. }
  116. }
  117. /**
  118. * Redirect to Edit form.
  119. *
  120. * @param \Magento\User\Model\User $model
  121. * @param array $data
  122. * @return void
  123. */
  124. protected function redirectToEdit(\Magento\User\Model\User $model, array $data)
  125. {
  126. $this->_getSession()->setUserData($data);
  127. $arguments = $model->getId() ? ['user_id' => $model->getId()] : [];
  128. $arguments = array_merge($arguments, ['_current' => true, 'active_tab' => '']);
  129. $this->_redirect('adminhtml/*/edit', $arguments);
  130. }
  131. }