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
8 
9 use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
14 
21 {
25  private $securityCookie;
26 
33  private function getSecurityCookie()
34  {
35  if (!($this->securityCookie instanceof SecurityCookie)) {
36  return \Magento\Framework\App\ObjectManager::getInstance()->get(SecurityCookie::class);
37  } else {
38  return $this->securityCookie;
39  }
40  }
41 
47  public function execute()
48  {
49  $userId = (int)$this->getRequest()->getParam('user_id');
50  $data = $this->getRequest()->getPostValue();
51  if (array_key_exists('form_key', $data)) {
52  unset($data['form_key']);
53  }
54  if (!$data) {
55  $this->_redirect('adminhtml/*/');
56  return;
57  }
58 
60  $model = $this->_userFactory->create()->load($userId);
61  if ($userId && $model->isObjectNew()) {
62  $this->messageManager->addError(__('This user no longer exists.'));
63  $this->_redirect('adminhtml/*/');
64  return;
65  }
66  $model->setData($this->_getAdminUserData($data));
67  $userRoles = $this->getRequest()->getParam('roles', []);
68  if (count($userRoles)) {
69  $model->setRoleId($userRoles[0]);
70  }
71 
73  $currentUser = $this->_objectManager->get(\Magento\Backend\Model\Auth\Session::class)->getUser();
74  if ($userId == $currentUser->getId()
75  && $this->_objectManager->get(\Magento\Framework\Validator\Locale::class)
76  ->isValid($data['interface_locale'])
77  ) {
78  $this->_objectManager->get(
79  \Magento\Backend\Model\Locale\Manager::class
80  )->switchBackendInterfaceLocale(
81  $data['interface_locale']
82  );
83  }
84 
86  $currentUserPasswordField = \Magento\User\Block\User\Edit\Tab\Main::CURRENT_USER_PASSWORD_FIELD;
87  $isCurrentUserPasswordValid = isset($data[$currentUserPasswordField])
88  && !empty($data[$currentUserPasswordField]) && is_string($data[$currentUserPasswordField]);
89  try {
90  if (!($isCurrentUserPasswordValid)) {
91  throw new AuthenticationException(
92  __('The password entered for the current user is invalid. Verify the password and try again.')
93  );
94  }
95  $currentUser->performIdentityCheck($data[$currentUserPasswordField]);
96  $model->save();
97 
98  $this->messageManager->addSuccess(__('You saved the user.'));
99  $this->_getSession()->setUserData(false);
100  $this->_redirect('adminhtml/*/');
101 
102  $model->sendNotificationEmailsIfRequired();
103  } catch (UserLockedException $e) {
104  $this->_auth->logout();
105  $this->getSecurityCookie()->setLogoutReasonCookie(
106  \Magento\Security\Model\AdminSessionsManager::LOGOUT_REASON_USER_LOCKED
107  );
108  $this->_redirect('adminhtml/*/');
109  } catch (NotificationExceptionInterface $exception) {
110  $this->messageManager->addErrorMessage($exception->getMessage());
111  } catch (\Magento\Framework\Exception\AuthenticationException $e) {
112  $this->messageManager->addError(
113  __('The password entered for the current user is invalid. Verify the password and try again.')
114  );
115  $this->redirectToEdit($model, $data);
116  } catch (\Magento\Framework\Validator\Exception $e) {
117  $messages = $e->getMessages();
118  $this->messageManager->addMessages($messages);
119  $this->redirectToEdit($model, $data);
120  } catch (\Magento\Framework\Exception\LocalizedException $e) {
121  if ($e->getMessage()) {
122  $this->messageManager->addError($e->getMessage());
123  }
124  $this->redirectToEdit($model, $data);
125  }
126  }
127 
135  protected function redirectToEdit(\Magento\User\Model\User $model, array $data)
136  {
137  $this->_getSession()->setUserData($data);
138  $arguments = $model->getId() ? ['user_id' => $model->getId()] : [];
139  $arguments = array_merge($arguments, ['_current' => true, 'active_tab' => '']);
140  $this->_redirect('adminhtml/*/edit', $arguments);
141  }
142 }
__()
Definition: __.php:13
$arguments
redirectToEdit(\Magento\User\Model\User $model, array $data)
Definition: Save.php:135