Move User event listeners to Core

This commit is contained in:
Yassine Guedidi
2023-12-30 23:54:25 +01:00
parent d8f2d3c7ee
commit 615adc81ff
7 changed files with 10 additions and 10 deletions

View File

@ -1,37 +0,0 @@
<?php
namespace Wallabag\UserBundle\EventListener;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\AuthenticationEvents;
class AuthenticationFailureListener implements EventSubscriberInterface
{
private $requestStack;
private $logger;
public function __construct(RequestStack $requestStack, LoggerInterface $logger)
{
$this->requestStack = $requestStack;
$this->logger = $logger;
}
public static function getSubscribedEvents()
{
return [
AuthenticationEvents::AUTHENTICATION_FAILURE => 'onAuthenticationFailure',
];
}
/**
* On failure, add a custom error in log so server admin can configure fail2ban to block IP from people who try to login too much.
*/
public function onAuthenticationFailure()
{
$request = $this->requestStack->getMasterRequest();
$this->logger->error('Authentication failure for user "' . $request->request->get('_username') . '", from IP "' . $request->getClientIp() . '", with UA: "' . $request->server->get('HTTP_USER_AGENT') . '".');
}
}

View File

@ -1,66 +0,0 @@
<?php
namespace Wallabag\UserBundle\EventListener;
use Doctrine\ORM\EntityManagerInterface;
use FOS\UserBundle\Event\UserEvent;
use FOS\UserBundle\FOSUserEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Wallabag\CoreBundle\Entity\Config;
/**
* This listener will create the associated configuration when a user register.
* This configuration will be created right after the registration (no matter if it needs an email validation).
*/
class CreateConfigListener implements EventSubscriberInterface
{
private $em;
private $itemsOnPage;
private $feedLimit;
private $language;
private $readingSpeed;
private $actionMarkAsRead;
private $listMode;
private $session;
private $displayThumbnails;
public function __construct(EntityManagerInterface $em, $itemsOnPage, $feedLimit, $language, $readingSpeed, $actionMarkAsRead, $listMode, $displayThumbnails, SessionInterface $session)
{
$this->em = $em;
$this->itemsOnPage = $itemsOnPage;
$this->feedLimit = $feedLimit;
$this->language = $language;
$this->readingSpeed = $readingSpeed;
$this->actionMarkAsRead = $actionMarkAsRead;
$this->listMode = $listMode;
$this->session = $session;
$this->displayThumbnails = $displayThumbnails;
}
public static function getSubscribedEvents()
{
return [
// when a user register using the normal form
FOSUserEvents::REGISTRATION_COMPLETED => 'createConfig',
// when we manually create a user using the command line
// OR when we create it from the config UI
FOSUserEvents::USER_CREATED => 'createConfig',
];
}
public function createConfig(UserEvent $event)
{
$config = new Config($event->getUser());
$config->setItemsPerPage($this->itemsOnPage);
$config->setFeedLimit($this->feedLimit);
$config->setLanguage($this->session->get('_locale', $this->language));
$config->setReadingSpeed($this->readingSpeed);
$config->setActionMarkAsRead($this->actionMarkAsRead);
$config->setListMode($this->listMode);
$config->setDisplayThumbnails($this->displayThumbnails);
$this->em->persist($config);
$this->em->flush();
}
}

View File

@ -1,38 +0,0 @@
<?php
namespace Wallabag\UserBundle\EventListener;
use FOS\UserBundle\Event\FormEvent;
use FOS\UserBundle\FOSUserEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
/**
* Listener responsible to change the redirection at the end of the password resetting.
*
* @see http://symfony.com/doc/current/bundles/FOSUserBundle/controller_events.html
*/
class PasswordResettingListener implements EventSubscriberInterface
{
private $router;
public function __construct(UrlGeneratorInterface $router)
{
$this->router = $router;
}
public static function getSubscribedEvents()
{
return [
FOSUserEvents::RESETTING_RESET_SUCCESS => 'onPasswordResettingSuccess',
];
}
public function onPasswordResettingSuccess(FormEvent $event)
{
$url = $this->router->generate('homepage');
$event->setResponse(new RedirectResponse($url));
}
}

View File

@ -1,44 +0,0 @@
<?php
namespace Wallabag\UserBundle\EventListener;
use FOS\UserBundle\Event\GetResponseUserEvent;
use FOS\UserBundle\FOSUserEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class RegistrationListener implements EventSubscriberInterface
{
/**
* @var bool
*/
private $registrationEnabled;
/**
* @var UrlGeneratorInterface
*/
private $urlGenerator;
public function __construct($registrationEnabled, UrlGeneratorInterface $urlGenerator)
{
$this->registrationEnabled = $registrationEnabled;
$this->urlGenerator = $urlGenerator;
}
public static function getSubscribedEvents()
{
return [
FOSUserEvents::REGISTRATION_INITIALIZE => 'onRegistrationInitialize',
];
}
public function onRegistrationInitialize(GetResponseUserEvent $event)
{
if ($this->registrationEnabled) {
return;
}
$event->setResponse(new RedirectResponse($this->urlGenerator->generate('fos_user_security_login'), 301));
}
}