ResetPassword.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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\HttpGetActionInterface;
  8. use Magento\Framework\Exception\NoSuchEntityException;
  9. use Magento\Framework\Exception\SecurityViolationException;
  10. /**
  11. * Reset password controller
  12. *
  13. * @package Magento\Customer\Controller\Adminhtml\Index
  14. */
  15. class ResetPassword extends \Magento\Customer\Controller\Adminhtml\Index implements HttpGetActionInterface
  16. {
  17. /**
  18. * Authorization level of a basic admin session
  19. *
  20. * @see _isAllowed()
  21. */
  22. const ADMIN_RESOURCE = 'Magento_Customer::reset_password';
  23. /**
  24. * Reset password handler
  25. *
  26. * @return \Magento\Backend\Model\View\Result\Redirect
  27. */
  28. public function execute()
  29. {
  30. $resultRedirect = $this->resultRedirectFactory->create();
  31. $customerId = (int)$this->getRequest()->getParam('customer_id', 0);
  32. if (!$customerId) {
  33. $resultRedirect->setPath('customer/index');
  34. return $resultRedirect;
  35. }
  36. try {
  37. $customer = $this->_customerRepository->getById($customerId);
  38. $this->customerAccountManagement->initiatePasswordReset(
  39. $customer->getEmail(),
  40. \Magento\Customer\Model\AccountManagement::EMAIL_REMINDER,
  41. $customer->getWebsiteId()
  42. );
  43. $this->messageManager->addSuccess(__('The customer will receive an email with a link to reset password.'));
  44. } catch (NoSuchEntityException $exception) {
  45. $resultRedirect->setPath('customer/index');
  46. return $resultRedirect;
  47. } catch (\Magento\Framework\Validator\Exception $exception) {
  48. $messages = $exception->getMessages(\Magento\Framework\Message\MessageInterface::TYPE_ERROR);
  49. if (!count($messages)) {
  50. $messages = $exception->getMessage();
  51. }
  52. $this->_addSessionErrorMessages($messages);
  53. } catch (SecurityViolationException $exception) {
  54. $this->messageManager->addErrorMessage($exception->getMessage());
  55. } catch (\Exception $exception) {
  56. $this->messageManager->addException(
  57. $exception,
  58. __('Something went wrong while resetting customer password.')
  59. );
  60. }
  61. $resultRedirect->setPath(
  62. 'customer/*/edit',
  63. ['id' => $customerId, '_current' => true]
  64. );
  65. return $resultRedirect;
  66. }
  67. }