Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Delete.php
Go to the documentation of this file.
1 <?php
9 
12 
14 {
18  public function execute()
19  {
21  $currentUser = $this->_objectManager->get(\Magento\Backend\Model\Auth\Session::class)->getUser();
22  $userId = (int)$this->getRequest()->getPost('user_id');
23 
24  if ($userId) {
25  if ($currentUser->getId() == $userId) {
26  $this->messageManager->addError(__('You cannot delete your own account.'));
27  $this->_redirect('adminhtml/*/edit', ['user_id' => $userId]);
28  return;
29  }
30  try {
31  $currentUserPassword = (string)$this->getRequest()->getPost(UserEdit::CURRENT_USER_PASSWORD_FIELD);
32  if (empty($currentUserPassword)) {
33  throw new AuthenticationException(
34  __('The password entered for the current user is invalid. Verify the password and try again.')
35  );
36  }
37  $currentUser->performIdentityCheck($currentUserPassword);
39  $model = $this->_userFactory->create();
40  $model->setId($userId);
41  $model->delete();
42  $this->messageManager->addSuccess(__('You deleted the user.'));
43  $this->_redirect('adminhtml/*/');
44  return;
45  } catch (\Exception $e) {
46  $this->messageManager->addError($e->getMessage());
47  $this->_redirect('adminhtml/*/edit', ['user_id' => $this->getRequest()->getParam('user_id')]);
48  return;
49  }
50  }
51  $this->messageManager->addError(__('We can\'t find a user to delete.'));
52  $this->_redirect('adminhtml/*/');
53  }
54 }
__()
Definition: __.php:13