<?php declare(strict_types=1);
namespace BartCTwoFactorAuth\Subscriber;
use PragmaRX\Google2FA\Google2FA;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\System\User\UserEntity;
use Shopware\Core\System\User\UserEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class UserEdit implements EventSubscriberInterface
{
private EntityRepositoryInterface $tfaRepository;
public function __construct(EntityRepositoryInterface $tfaRepository)
{
$this->tfaRepository = $tfaRepository;
}
public static function getSubscribedEvents(): array
{
// Return the events to listen to as array like this: <event to listen to> => <method to execute>
return [
UserEvents::USER_LOADED_EVENT => 'onUserLoaded',
];
}
public function onUserLoaded(EntityLoadedEvent $event)
{
$entities = $event->getEntities();
if (count($entities) > 0) {
if (!($entities[0] instanceof UserEntity)) {
return;
}
$user = $entities[0];
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('userId', $user->getId()));
$criteria->addAssociation('tfaExtension');
$tfaExtension = $this->tfaRepository->search($criteria, $event->getContext())->first();
if ($tfaExtension === null) {
$this->tfaRepository->create([
[
'userId' => $user->getId(),
'tfaStatus' => false,
]
], $event->getContext());
}
}
}
}