You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
expire-email-validation/EventListener/EmailAddressVerificationLis...

113 lines
4.0 KiB

<?php
namespace vierbergenlars\AuthserverAutoExpireUsersBundle\EventListener;
use App\AppEvents;
use App\Event\UserCheckerEvent;
use App\Entity\EmailAddress;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Exception\AccountExpiredException;
use Symfony\Component\Security\Core\AuthenticationEvents;
use Symfony\Component\Security\Core\Event\AuthenticationEvent;
use vierbergenlars\AuthserverAutoExpireUsersBundle\Entity\ExpiredUser;
use vierbergenlars\AuthserverStatsBundle\Event\StatsEvent;
use Symfony\Component\VarDumper\VarDumper;
use Psr\Log\LoggerInterface;
use App\Mail\PrimedTwigMailer;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Events;
use Doctrine\ORM\Event\PreUpdateEventArgs;
use Doctrine\ORM\Event\PreFlushEventArgs;
use Doctrine\ORM\Event\OnFlushEventArgs;
use Doctrine\ORM\UnitOfWork;
use Doctrine\ORM\EntityManager;
class EmailAddressVerificationListener implements EventSubscriber
{
/**
*
* @var EntityManagerInterface
*/
private $em;
/**
*
* @var LoggerInterface
*/
private $logger;
public function getSubscribedEvents()
{
return [
Events::onFlush
];
}
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
public function onFlush(OnFlushEventArgs $event)
{
$uow = $event->getEntityManager()->getUnitOfWork();
$updates = $uow->getScheduledEntityUpdates();
foreach ($updates as $update) {
$this->processEntity($update, $event->getEntityManager(), $uow);
}
}
private function processEntity($entity, EntityManager $em, UnitOfWork $uow)
{
if (!($entity instanceof EmailAddress))
return;
$changeSet = $uow->getEntityChangeSet($entity);
/* @var $entity EmailAddress */
// verification status of the email address has changed and it is not the primary email address
if (isset($changeSet['verified']) && $entity->isVerified()) {
$expiredUser = $em->find(ExpiredUser::class, $entity->getUser());
$this->logger->debug('User expiration record for user.', [
'user' => $entity->getUser(),
'expired_user' => $expiredUser,
'is_expired' => $expiredUser ? $expiredUser->isExpired() : null
]);
if (!$expiredUser || !$expiredUser->isExpired())
return;
/* @var $expiredUser ExpiredUser */
$this->logger->info('An email address has been verified, clearing expiration for user.', [
'user' => $entity->getUser(),
'expired_user' => $expiredUser
]);
$expiredUserMeta = $em->getMetadataFactory()->getMetadataFor(ExpiredUser::class);
$expiredUser->setExpiredAt(null);
$uow->recomputeSingleEntityChangeSet($expiredUserMeta, $expiredUser);
if (!$entity->isPrimary()) {
$primaryAddress = $entity->getUser()->getPrimaryEmailAddress();
if ($primaryAddress->isVerified())
return;
$this->logger->info('An email address has been verified, but the primary email address has not been verified. Switching primary email address to the verified email address.', [
'user' => $entity->getUser(),
'old_primary_address' => $primaryAddress,
'verified_address' => $entity
]);
$emailAddressMeta = $em->getMetadataFactory()->getMetadataFor(EmailAddress::class);
$primaryAddress->setPrimary(false);
$uow->recomputeSingleEntityChangeSet($emailAddressMeta, $primaryAddress);
$entity->setPrimary(true);
$uow->recomputeSingleEntityChangeSet($emailAddressMeta, $entity);
}
}
}
}