custom/plugins/IwvTwoFactorAuthentication/src/IwvTwoFactorAuthentication.php line 19

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Iwv\IwvTwoFactorAuthentication;
  4. use Doctrine\DBAL\Connection;
  5. use Shopware\Core\Framework\Plugin;
  6. use Shopware\Core\Framework\Context;
  7. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
  12. if (file_exists(dirname(__DIR__) . '/vendor/autoload.php')) {
  13.     require_once dirname(__DIR__) . '/vendor/autoload.php';
  14. }
  15. class IwvTwoFactorAuthentication extends Plugin
  16. {
  17.     private function deleteUserData(Context $context){
  18.         /** @var EntityRepositoryInterface $userRepository */
  19.         $updateUsers = [];
  20.         $userRepository $this->container->get('user.repository');
  21.         foreach ($userRepository->search(
  22.             (new Criteria())->addFilter(new NotFilter(NotFilter::CONNECTION_OR,
  23.                     [
  24.                     new EqualsFilter('customFields.iwvTwoFactor'NULL)
  25.                 ])), $context) as $userDB) {
  26.             $customFields = (array) $userDB->getCustomFields();
  27.             unset($customFields['iwvTwoFactor']);
  28.             unset($customFields['iwvTwoFactorCookie']);
  29.             unset($customFields['iwvTwoFactorSession']);
  30.             $updateUsers[] = [
  31.                 'id' => $userDB->getId(),
  32.                 'customFields' => $customFields
  33.             ];
  34.         }
  35.         if ($updateUsers) {
  36.             $userRepository->update($updateUsers$context);
  37.         }
  38.     }
  39.     private function deleteCustomerData(Context $context){
  40.         /** @var EntityRepositoryInterface $customerRepository */
  41.         $updateCustomers = [];
  42.         $customerRepository $this->container->get('customer.repository');
  43.         foreach ($customerRepository->search(
  44.             (new Criteria())->addFilter(new NotFilter(NotFilter::CONNECTION_OR,
  45.                     [
  46.                     new EqualsFilter('customFields.iwvTwoFactor'NULL)
  47.                 ])), $context) as $userDB) {
  48.             $customFields = (array) $userDB->getCustomFields();
  49.             unset($customFields['iwvTwoFactor']);
  50.             unset($customFields['iwvTwoFactorCookie']);
  51.             unset($customFields['iwvTwoFactorSession']);
  52.             $updateCustomers[] = [
  53.                 'id' => $userDB->getId(),
  54.                 'customFields' => $customFields
  55.             ];
  56.         }
  57.         if ($updateCustomers) {
  58.             $customerRepository->update($updateCustomers$context);
  59.         }
  60.     }
  61.     private function deleteEmailTemplate(){
  62.         $connection $this->container->get(Connection::class);
  63.         $connection->exec('
  64.             SET FOREIGN_KEY_CHECKS = 0;
  65.             DELETE mail_template_type, mail_template_type_translation, mail_template, mail_template_translation
  66.             FROM mail_template_type
  67.             JOIN mail_template_type_translation ON mail_template_type.id = mail_template_type_translation.mail_template_type_id
  68.             JOIN mail_template ON mail_template_type.id = mail_template.mail_template_type_id
  69.             JOIN mail_template_translation ON mail_template.id = mail_template_translation.mail_template_id
  70.             WHERE mail_template_type.technical_name = "iwvtwofa_email_confirmation"; 
  71.             
  72.             SET FOREIGN_KEY_CHECKS = 1;
  73.         ');
  74.     }
  75.     
  76.     /**
  77.      * @param UninstallContext $uninstallContext
  78.      */
  79.     public function uninstall(UninstallContext $uninstallContext): void
  80.     {
  81.         parent::uninstall($uninstallContext);
  82.         if ($uninstallContext->keepUserData()) {
  83.             return;
  84.         }
  85.         $uninstallContext->getContext()->scope(Context::SYSTEM_SCOPE, function (Context $context): void {
  86.             $this->deleteUserData($context);
  87.             $this->deleteCustomerData($context);
  88.             $this->deleteEmailTemplate();
  89.         });
  90.     }
  91. }