]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Use FOSUserEvents instead of c/p a controller
authorJeremy Benoist <jeremy.benoist@gmail.com>
Thu, 21 Jan 2016 15:39:13 +0000 (16:39 +0100)
committerJeremy Benoist <jeremy.benoist@gmail.com>
Thu, 21 Jan 2016 15:39:13 +0000 (16:39 +0100)
The `resetAction` was overriden to redirect user to the homepage instead of `fos_user_profile_show`.
Instead of copying the whole method we can simply use FOSUserEvents to handle that.

src/Wallabag/UserBundle/Controller/ResettingController.php [deleted file]
src/Wallabag/UserBundle/EventListener/PasswordResettingListener.php [new file with mode: 0644]
src/Wallabag/UserBundle/Resources/config/services.yml

diff --git a/src/Wallabag/UserBundle/Controller/ResettingController.php b/src/Wallabag/UserBundle/Controller/ResettingController.php
deleted file mode 100644 (file)
index 62e27d0..0000000
+++ /dev/null
@@ -1,75 +0,0 @@
-<?php
-
-namespace Wallabag\UserBundle\Controller;
-
-use FOS\UserBundle\Event\FilterUserResponseEvent;
-use FOS\UserBundle\Event\FormEvent;
-use FOS\UserBundle\Event\GetResponseUserEvent;
-use FOS\UserBundle\FOSUserEvents;
-use Symfony\Component\HttpFoundation\RedirectResponse;
-use Symfony\Component\HttpFoundation\Request;
-use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
-
-class ResettingController extends \FOS\UserBundle\Controller\ResettingController
-{
-    /**
-     * Extends ResettingController to change the redirection after success.
-     *
-     * @param Request $request
-     * @param $token
-     *
-     * @return null|RedirectResponse|\Symfony\Component\HttpFoundation\Response
-     */
-    public function resetAction(Request $request, $token)
-    {
-        /** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
-        $formFactory = $this->get('fos_user.resetting.form.factory');
-        /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
-        $userManager = $this->get('fos_user.user_manager');
-        /** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
-        $dispatcher = $this->get('event_dispatcher');
-
-        $user = $userManager->findUserByConfirmationToken($token);
-
-        if (null === $user) {
-            throw new NotFoundHttpException(sprintf('The user with "confirmation token" does not exist for value "%s"', $token));
-        }
-
-        $event = new GetResponseUserEvent($user, $request);
-        $dispatcher->dispatch(FOSUserEvents::RESETTING_RESET_INITIALIZE, $event);
-
-        if (null !== $event->getResponse()) {
-            return $event->getResponse();
-        }
-
-        $form = $formFactory->createForm();
-        $form->setData($user);
-
-        $form->handleRequest($request);
-
-        if ($form->isValid()) {
-            $event = new FormEvent($form, $request);
-            $dispatcher->dispatch(FOSUserEvents::RESETTING_RESET_SUCCESS, $event);
-
-            $userManager->updateUser($user);
-
-            if (null === $response = $event->getResponse()) {
-                $this->get('session')->getFlashBag()->add(
-                    'notice',
-                    'Password updated'
-                );
-                $url = $this->generateUrl('homepage');
-                $response = new RedirectResponse($url);
-            }
-
-            $dispatcher->dispatch(FOSUserEvents::RESETTING_RESET_COMPLETED, new FilterUserResponseEvent($user, $request, $response));
-
-            return $response;
-        }
-
-        return $this->render('FOSUserBundle:Resetting:reset.html.twig', array(
-            'token' => $token,
-            'form' => $form->createView(),
-        ));
-    }
-}
diff --git a/src/Wallabag/UserBundle/EventListener/PasswordResettingListener.php b/src/Wallabag/UserBundle/EventListener/PasswordResettingListener.php
new file mode 100644 (file)
index 0000000..2fdcb44
--- /dev/null
@@ -0,0 +1,41 @@
+<?php
+
+namespace Wallabag\UserBundle\EventListener;
+
+use FOS\UserBundle\FOSUserEvents;
+use FOS\UserBundle\Event\FormEvent;
+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;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public static function getSubscribedEvents()
+    {
+        return array(
+            FOSUserEvents::RESETTING_RESET_SUCCESS => 'onPasswordResettingSuccess',
+        );
+    }
+
+    public function onPasswordResettingSuccess(FormEvent $event)
+    {
+        $url = $this->router->generate('homepage');
+
+        $event->setResponse(new RedirectResponse($url));
+    }
+}
index 93e04d59253e23c6cc50c675a55ba06839f275b8..bf9e036ae64a9b1dea8997afe1e3168486cfc596 100644 (file)
@@ -8,3 +8,10 @@ services:
             - "%scheb_two_factor.email.sender_name%"
             - "%wallabag_support_url%"
             - "%wallabag_url%"
+
+    wallabag_user.password_resetting:
+        class: Wallabag\UserBundle\EventListener\PasswordResettingListener
+        arguments:
+            - "@router"
+        tags:
+            - { name: kernel.event_subscriber }