2021-03-24 12:27:07 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Core\EventSuscriber\Account;
|
|
|
|
|
|
|
|
use App\Core\Event\Account\PasswordRequestEvent;
|
|
|
|
use App\Core\Manager\EntityManager;
|
|
|
|
use App\Core\Notification\MailNotifier;
|
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
|
|
|
use Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface;
|
2021-03-24 17:20:46 +01:00
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
2021-03-24 12:27:07 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* class EventListener.
|
|
|
|
*
|
|
|
|
* @author Simon Vieille <simon@deblan.fr>
|
|
|
|
*/
|
|
|
|
class PasswordRequestEventSubscriber implements EventSubscriberInterface
|
|
|
|
{
|
|
|
|
protected MailNotifier $notifier;
|
|
|
|
protected UrlGeneratorInterface $urlGenerator;
|
|
|
|
protected EntityManager $entityManager;
|
|
|
|
protected TokenGeneratorInterface $tokenGenerator;
|
2021-03-24 17:20:02 +01:00
|
|
|
protected TranslatorInterface $translator;
|
2021-03-24 12:27:07 +01:00
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
MailNotifier $notifier,
|
|
|
|
UrlGeneratorInterface $urlGenerator,
|
|
|
|
EntityManager $entityManager,
|
2021-03-24 17:20:02 +01:00
|
|
|
TokenGeneratorInterface $tokenGenerator,
|
|
|
|
TranslatorInterface $translator
|
2021-03-24 12:27:07 +01:00
|
|
|
) {
|
|
|
|
$this->notifier = $notifier;
|
|
|
|
$this->urlGenerator = $urlGenerator;
|
|
|
|
$this->entityManager = $entityManager;
|
|
|
|
$this->tokenGenerator = $tokenGenerator;
|
2021-03-24 17:20:02 +01:00
|
|
|
$this->translator = $translator;
|
2021-03-24 12:27:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function getSubscribedEvents()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
PasswordRequestEvent::EVENT => 'onRequest',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onRequest(PasswordRequestEvent $event)
|
|
|
|
{
|
|
|
|
$user = $event->getUser();
|
|
|
|
$user->setConfirmationToken($this->tokenGenerator->generateToken());
|
|
|
|
$user->setPasswordRequestedAt(new \DateTime('now'));
|
|
|
|
|
|
|
|
$this->entityManager->update($user);
|
|
|
|
|
|
|
|
$this->notifier
|
2021-03-24 17:20:02 +01:00
|
|
|
->setSubject($translator->trans('Mot de passe perdu'))
|
2021-03-24 12:27:07 +01:00
|
|
|
->addRecipient($user->getEmail())
|
|
|
|
->notify('@Core/mail/account/resetting_request.html.twig', [
|
|
|
|
'reseting_update_link' => $this->urlGenerator->generate(
|
|
|
|
'auth_resetting_update',
|
|
|
|
[
|
|
|
|
'token' => $user->getConfirmationToken(),
|
|
|
|
],
|
|
|
|
UrlGeneratorInterface::ABSOLUTE_URL
|
|
|
|
),
|
|
|
|
])
|
|
|
|
;
|
|
|
|
}
|
|
|
|
}
|