Spamworldpro Mini Shell
Spamworldpro


Server : Apache
System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64
User : corals ( 1002)
PHP Version : 7.4.33
Disable Function : exec,passthru,shell_exec,system
Directory :  /home/corals/old/app/code/Soon/CustomerDelete/Controller/Account/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/old/app/code/Soon/CustomerDelete/Controller/Account/DeletePost.php
<?php
/**
 * @license http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 * @author Hervé Guétin <[email protected]> <@herveguetin>
 * @copyright Copyright (c) 2018 Agence Soon (http://www.agence-soon.fr)
 */

namespace Soon\CustomerDelete\Controller\Account;

use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Controller\AbstractAccount;
use Magento\Customer\Model\AuthenticationInterface;
use Magento\Customer\Model\Session;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Data\Form\FormKey\Validator;
use Magento\Framework\Event\Manager;
use Magento\Framework\Exception\InvalidEmailOrPasswordException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\SessionException;
use Magento\Framework\Exception\State\UserLockedException;
use Magento\Framework\Registry;

class DeletePost extends AbstractAccount
{
    /**
     * @var Validator
     */
    private $formKeyValidator;
    /**
     * @var Session
     */
    private $session;
    /**
     * @var AuthenticationInterface
     */
    private $authentication;
    /**
     * @var CustomerRepositoryInterface
     */
    private $customerRepository;
    /**
     * @var \Magento\Customer\Model\Account\Redirect
     */
    private $accountRedirect;
    /**
     * @var Registry
     */
    private $registry;
    /**
     * @var Manager
     */
    private $eventManager;

    public function __construct(
        Context $context,
        Validator $formKeyValidator,
        AuthenticationInterface $authentication,
        Session $session,
        CustomerRepositoryInterface $customerRepository,
        \Magento\Customer\Model\Account\Redirect $accountRedirect,
        Registry $registry
    ) {
        parent::__construct($context);
        $this->formKeyValidator = $formKeyValidator;
        $this->session = $session;
        $this->authentication = $authentication;
        $this->customerRepository = $customerRepository;
        $this->accountRedirect = $accountRedirect;
        $this->registry = $registry;
        $this->eventManager = $context->getEventManager();
    }

    /**
     * Execute action based on request and return result
     *
     * Note: Request will be added as operation argument in future
     *
     * @return ResultInterface|ResponseInterface
     * @throws LocalizedException
     * @throws NoSuchEntityException
     * @throws SessionException
     * @throws UserLockedException
     */
    public function execute()
    {
        /** @var Redirect $resultRedirect */
        $resultRedirect = $this->resultRedirectFactory->create();
        $validFormKey = $this->formKeyValidator->validate($this->getRequest());

        if ($validFormKey && $this->getRequest()->isPost()) {
            $password = $this->getRequest()->getPost('password');
            $customerId = $this->session->getCustomerId();
            if ($password) {
                try {
                    $this->authentication->authenticate($customerId, $password);
                    $this->deleteById($customerId);

                    return $resultRedirect->setPath('customer/account/login');
                } catch (InvalidEmailOrPasswordException $e) {
                    $this->messageManager->addErrorMessage(__('Invalid password.'));
                }
            }
        }

        return $this->accountRedirect->getRedirect();
    }

    /**
     * @param $customerId
     * @throws LocalizedException
     * @throws NoSuchEntityException
     * @throws SessionException
     */
    private function deleteById($customerId)
    {
        $this->eventManager->dispatch('soon_customer_delete_before_delete', ['customer_id' => $customerId]);

        $this->session->logout();

        // @see \Magento\Framework\Model\ActionValidator\RemoveAction::isAllowed
        $this->registry->register('isSecureArea', true);
        $this->customerRepository->deleteById($customerId);

        $this->session->start();
        $this->messageManager->addSuccessMessage(__('Your account has been deleted.'));

        $this->eventManager->dispatch('soon_customer_delete_after_delete', ['customer_id' => $customerId]);
    }
}

Spamworldpro Mini