<?php
declare(strict_types=1);
namespace Iwv\IwvTwoFactorAuthentication;
use Doctrine\DBAL\Connection;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
if (file_exists(dirname(__DIR__) . '/vendor/autoload.php')) {
require_once dirname(__DIR__) . '/vendor/autoload.php';
}
class IwvTwoFactorAuthentication extends Plugin
{
private function deleteUserData(Context $context){
/** @var EntityRepositoryInterface $userRepository */
$updateUsers = [];
$userRepository = $this->container->get('user.repository');
foreach ($userRepository->search(
(new Criteria())->addFilter(new NotFilter(NotFilter::CONNECTION_OR,
[
new EqualsFilter('customFields.iwvTwoFactor', NULL)
])), $context) as $userDB) {
$customFields = (array) $userDB->getCustomFields();
unset($customFields['iwvTwoFactor']);
unset($customFields['iwvTwoFactorCookie']);
unset($customFields['iwvTwoFactorSession']);
$updateUsers[] = [
'id' => $userDB->getId(),
'customFields' => $customFields
];
}
if ($updateUsers) {
$userRepository->update($updateUsers, $context);
}
}
private function deleteCustomerData(Context $context){
/** @var EntityRepositoryInterface $customerRepository */
$updateCustomers = [];
$customerRepository = $this->container->get('customer.repository');
foreach ($customerRepository->search(
(new Criteria())->addFilter(new NotFilter(NotFilter::CONNECTION_OR,
[
new EqualsFilter('customFields.iwvTwoFactor', NULL)
])), $context) as $userDB) {
$customFields = (array) $userDB->getCustomFields();
unset($customFields['iwvTwoFactor']);
unset($customFields['iwvTwoFactorCookie']);
unset($customFields['iwvTwoFactorSession']);
$updateCustomers[] = [
'id' => $userDB->getId(),
'customFields' => $customFields
];
}
if ($updateCustomers) {
$customerRepository->update($updateCustomers, $context);
}
}
private function deleteEmailTemplate(){
$connection = $this->container->get(Connection::class);
$connection->exec('
SET FOREIGN_KEY_CHECKS = 0;
DELETE mail_template_type, mail_template_type_translation, mail_template, mail_template_translation
FROM mail_template_type
JOIN mail_template_type_translation ON mail_template_type.id = mail_template_type_translation.mail_template_type_id
JOIN mail_template ON mail_template_type.id = mail_template.mail_template_type_id
JOIN mail_template_translation ON mail_template.id = mail_template_translation.mail_template_id
WHERE mail_template_type.technical_name = "iwvtwofa_email_confirmation";
SET FOREIGN_KEY_CHECKS = 1;
');
}
/**
* @param UninstallContext $uninstallContext
*/
public function uninstall(UninstallContext $uninstallContext): void
{
parent::uninstall($uninstallContext);
if ($uninstallContext->keepUserData()) {
return;
}
$uninstallContext->getContext()->scope(Context::SYSTEM_SCOPE, function (Context $context): void {
$this->deleteUserData($context);
$this->deleteCustomerData($context);
$this->deleteEmailTemplate();
});
}
}