Delete.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\User\Controller\Adminhtml\User;
  8. use Magento\User\Block\User\Edit\Tab\Main as UserEdit;
  9. use Magento\Framework\Exception\AuthenticationException;
  10. class Delete extends \Magento\User\Controller\Adminhtml\User
  11. {
  12. /**
  13. * @return void
  14. */
  15. public function execute()
  16. {
  17. /** @var \Magento\User\Model\User */
  18. $currentUser = $this->_objectManager->get(\Magento\Backend\Model\Auth\Session::class)->getUser();
  19. $userId = (int)$this->getRequest()->getPost('user_id');
  20. if ($userId) {
  21. if ($currentUser->getId() == $userId) {
  22. $this->messageManager->addError(__('You cannot delete your own account.'));
  23. $this->_redirect('adminhtml/*/edit', ['user_id' => $userId]);
  24. return;
  25. }
  26. try {
  27. $currentUserPassword = (string)$this->getRequest()->getPost(UserEdit::CURRENT_USER_PASSWORD_FIELD);
  28. if (empty($currentUserPassword)) {
  29. throw new AuthenticationException(
  30. __('The password entered for the current user is invalid. Verify the password and try again.')
  31. );
  32. }
  33. $currentUser->performIdentityCheck($currentUserPassword);
  34. /** @var \Magento\User\Model\User $model */
  35. $model = $this->_userFactory->create();
  36. $model->setId($userId);
  37. $model->delete();
  38. $this->messageManager->addSuccess(__('You deleted the user.'));
  39. $this->_redirect('adminhtml/*/');
  40. return;
  41. } catch (\Exception $e) {
  42. $this->messageManager->addError($e->getMessage());
  43. $this->_redirect('adminhtml/*/edit', ['user_id' => $this->getRequest()->getParam('user_id')]);
  44. return;
  45. }
  46. }
  47. $this->messageManager->addError(__('We can\'t find a user to delete.'));
  48. $this->_redirect('adminhtml/*/');
  49. }
  50. }