Save.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Controller\Adminhtml\System\Account;
  7. use Magento\Framework\Validator\Exception as ValidatorException;
  8. use Magento\Framework\Exception\AuthenticationException;
  9. use Magento\Framework\Exception\LocalizedException;
  10. use Magento\Framework\Controller\ResultFactory;
  11. use Magento\Framework\Exception\State\UserLockedException;
  12. use Magento\Security\Model\SecurityCookie;
  13. /**
  14. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  15. */
  16. class Save extends \Magento\Backend\Controller\Adminhtml\System\Account
  17. {
  18. /**
  19. * @var SecurityCookie
  20. */
  21. private $securityCookie;
  22. /**
  23. * Get security cookie
  24. *
  25. * @return SecurityCookie
  26. * @deprecated 100.1.0
  27. */
  28. private function getSecurityCookie()
  29. {
  30. if (!($this->securityCookie instanceof SecurityCookie)) {
  31. return \Magento\Framework\App\ObjectManager::getInstance()->get(SecurityCookie::class);
  32. }
  33. return $this->securityCookie;
  34. }
  35. /**
  36. * Saving edited user information
  37. *
  38. * @return \Magento\Backend\Model\View\Result\Redirect
  39. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  40. */
  41. public function execute()
  42. {
  43. $userId = $this->_objectManager->get(\Magento\Backend\Model\Auth\Session::class)->getUser()->getId();
  44. $password = (string)$this->getRequest()->getParam('password');
  45. $passwordConfirmation = (string)$this->getRequest()->getParam('password_confirmation');
  46. $interfaceLocale = (string)$this->getRequest()->getParam('interface_locale', false);
  47. /** @var $user \Magento\User\Model\User */
  48. $user = $this->_objectManager->create(\Magento\User\Model\User::class)->load($userId);
  49. $user->setId($userId)
  50. ->setUserName($this->getRequest()->getParam('username', false))
  51. ->setFirstName($this->getRequest()->getParam('firstname', false))
  52. ->setLastName($this->getRequest()->getParam('lastname', false))
  53. ->setEmail(strtolower($this->getRequest()->getParam('email', false)));
  54. if ($this->_objectManager->get(\Magento\Framework\Validator\Locale::class)->isValid($interfaceLocale)) {
  55. $user->setInterfaceLocale($interfaceLocale);
  56. /** @var \Magento\Backend\Model\Locale\Manager $localeManager */
  57. $localeManager = $this->_objectManager->get(\Magento\Backend\Model\Locale\Manager::class);
  58. $localeManager->switchBackendInterfaceLocale($interfaceLocale);
  59. }
  60. /** Before updating admin user data, ensure that password of current admin user is entered and is correct */
  61. $currentUserPasswordField = \Magento\User\Block\User\Edit\Tab\Main::CURRENT_USER_PASSWORD_FIELD;
  62. $currentUserPassword = $this->getRequest()->getParam($currentUserPasswordField);
  63. try {
  64. $user->performIdentityCheck($currentUserPassword);
  65. if ($password !== '') {
  66. $user->setPassword($password);
  67. $user->setPasswordConfirmation($passwordConfirmation);
  68. }
  69. $errors = $user->validate();
  70. if ($errors !== true && !empty($errors)) {
  71. foreach ($errors as $error) {
  72. $this->messageManager->addErrorMessage($error);
  73. }
  74. } else {
  75. $user->save();
  76. $user->sendNotificationEmailsIfRequired();
  77. $this->messageManager->addSuccessMessage(__('You saved the account.'));
  78. }
  79. } catch (UserLockedException $e) {
  80. $this->_auth->logout();
  81. $this->getSecurityCookie()->setLogoutReasonCookie(
  82. \Magento\Security\Model\AdminSessionsManager::LOGOUT_REASON_USER_LOCKED
  83. );
  84. } catch (ValidatorException $e) {
  85. $this->messageManager->addMessages($e->getMessages());
  86. if ($e->getMessage()) {
  87. $this->messageManager->addErrorMessage($e->getMessage());
  88. }
  89. } catch (LocalizedException $e) {
  90. $this->messageManager->addErrorMessage($e->getMessage());
  91. } catch (\Exception $e) {
  92. $this->messageManager->addErrorMessage(__('An error occurred while saving account.'));
  93. }
  94. /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
  95. $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
  96. return $resultRedirect->setPath("*/*/");
  97. }
  98. }