Delete.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Controller\Adminhtml\Index;
  7. use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
  8. use Magento\Framework\Controller\ResultFactory;
  9. /**
  10. * Delete customer action.
  11. */
  12. class Delete extends \Magento\Customer\Controller\Adminhtml\Index implements HttpPostActionInterface
  13. {
  14. /**
  15. * Authorization level of a basic admin session
  16. *
  17. * @see _isAllowed()
  18. */
  19. const ADMIN_RESOURCE = 'Magento_Customer::delete';
  20. /**
  21. * Delete customer action
  22. *
  23. * @return \Magento\Backend\Model\View\Result\Redirect
  24. */
  25. public function execute()
  26. {
  27. $resultRedirect = $this->resultRedirectFactory->create();
  28. $formKeyIsValid = $this->_formKeyValidator->validate($this->getRequest());
  29. $isPost = $this->getRequest()->isPost();
  30. if (!$formKeyIsValid || !$isPost) {
  31. $this->messageManager->addError(__('Customer could not be deleted.'));
  32. return $resultRedirect->setPath('customer/index');
  33. }
  34. $customerId = $this->initCurrentCustomer();
  35. if (!empty($customerId)) {
  36. try {
  37. $this->_customerRepository->deleteById($customerId);
  38. $this->messageManager->addSuccess(__('You deleted the customer.'));
  39. } catch (\Exception $exception) {
  40. $this->messageManager->addError($exception->getMessage());
  41. }
  42. }
  43. /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
  44. $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
  45. return $resultRedirect->setPath('customer/index');
  46. }
  47. }