diff options
author | Jeremy Benoist <jeremy.benoist@gmail.com> | 2016-01-21 16:39:13 +0100 |
---|---|---|
committer | Jeremy Benoist <jeremy.benoist@gmail.com> | 2016-01-21 16:39:13 +0100 |
commit | 0f0e8eb82a3374e20453fb1f8046325ee306b036 (patch) | |
tree | 4570046b6aa6b29c5ac0fd9289b433d80e69c207 | |
parent | a0d6ccc5ca0dc0082467cc65b006150aff2488c4 (diff) | |
download | wallabag-0f0e8eb82a3374e20453fb1f8046325ee306b036.tar.gz wallabag-0f0e8eb82a3374e20453fb1f8046325ee306b036.tar.zst wallabag-0f0e8eb82a3374e20453fb1f8046325ee306b036.zip |
Use FOSUserEvents instead of c/p a controller
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.
3 files changed, 48 insertions, 75 deletions
diff --git a/src/Wallabag/UserBundle/Controller/ResettingController.php b/src/Wallabag/UserBundle/Controller/ResettingController.php deleted file mode 100644 index 62e27d00..00000000 --- a/src/Wallabag/UserBundle/Controller/ResettingController.php +++ /dev/null | |||
@@ -1,75 +0,0 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\UserBundle\Controller; | ||
4 | |||
5 | use FOS\UserBundle\Event\FilterUserResponseEvent; | ||
6 | use FOS\UserBundle\Event\FormEvent; | ||
7 | use FOS\UserBundle\Event\GetResponseUserEvent; | ||
8 | use FOS\UserBundle\FOSUserEvents; | ||
9 | use Symfony\Component\HttpFoundation\RedirectResponse; | ||
10 | use Symfony\Component\HttpFoundation\Request; | ||
11 | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
12 | |||
13 | class ResettingController extends \FOS\UserBundle\Controller\ResettingController | ||
14 | { | ||
15 | /** | ||
16 | * Extends ResettingController to change the redirection after success. | ||
17 | * | ||
18 | * @param Request $request | ||
19 | * @param $token | ||
20 | * | ||
21 | * @return null|RedirectResponse|\Symfony\Component\HttpFoundation\Response | ||
22 | */ | ||
23 | public function resetAction(Request $request, $token) | ||
24 | { | ||
25 | /** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */ | ||
26 | $formFactory = $this->get('fos_user.resetting.form.factory'); | ||
27 | /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */ | ||
28 | $userManager = $this->get('fos_user.user_manager'); | ||
29 | /** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */ | ||
30 | $dispatcher = $this->get('event_dispatcher'); | ||
31 | |||
32 | $user = $userManager->findUserByConfirmationToken($token); | ||
33 | |||
34 | if (null === $user) { | ||
35 | throw new NotFoundHttpException(sprintf('The user with "confirmation token" does not exist for value "%s"', $token)); | ||
36 | } | ||
37 | |||
38 | $event = new GetResponseUserEvent($user, $request); | ||
39 | $dispatcher->dispatch(FOSUserEvents::RESETTING_RESET_INITIALIZE, $event); | ||
40 | |||
41 | if (null !== $event->getResponse()) { | ||
42 | return $event->getResponse(); | ||
43 | } | ||
44 | |||
45 | $form = $formFactory->createForm(); | ||
46 | $form->setData($user); | ||
47 | |||
48 | $form->handleRequest($request); | ||
49 | |||
50 | if ($form->isValid()) { | ||
51 | $event = new FormEvent($form, $request); | ||
52 | $dispatcher->dispatch(FOSUserEvents::RESETTING_RESET_SUCCESS, $event); | ||
53 | |||
54 | $userManager->updateUser($user); | ||
55 | |||
56 | if (null === $response = $event->getResponse()) { | ||
57 | $this->get('session')->getFlashBag()->add( | ||
58 | 'notice', | ||
59 | 'Password updated' | ||
60 | ); | ||
61 | $url = $this->generateUrl('homepage'); | ||
62 | $response = new RedirectResponse($url); | ||
63 | } | ||
64 | |||
65 | $dispatcher->dispatch(FOSUserEvents::RESETTING_RESET_COMPLETED, new FilterUserResponseEvent($user, $request, $response)); | ||
66 | |||
67 | return $response; | ||
68 | } | ||
69 | |||
70 | return $this->render('FOSUserBundle:Resetting:reset.html.twig', array( | ||
71 | 'token' => $token, | ||
72 | 'form' => $form->createView(), | ||
73 | )); | ||
74 | } | ||
75 | } | ||
diff --git a/src/Wallabag/UserBundle/EventListener/PasswordResettingListener.php b/src/Wallabag/UserBundle/EventListener/PasswordResettingListener.php new file mode 100644 index 00000000..2fdcb441 --- /dev/null +++ b/src/Wallabag/UserBundle/EventListener/PasswordResettingListener.php | |||
@@ -0,0 +1,41 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\UserBundle\EventListener; | ||
4 | |||
5 | use FOS\UserBundle\FOSUserEvents; | ||
6 | use FOS\UserBundle\Event\FormEvent; | ||
7 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
8 | use Symfony\Component\HttpFoundation\RedirectResponse; | ||
9 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
10 | |||
11 | /** | ||
12 | * Listener responsible to change the redirection at the end of the password resetting | ||
13 | * | ||
14 | * @see http://symfony.com/doc/current/bundles/FOSUserBundle/controller_events.html | ||
15 | */ | ||
16 | class PasswordResettingListener implements EventSubscriberInterface | ||
17 | { | ||
18 | private $router; | ||
19 | |||
20 | public function __construct(UrlGeneratorInterface $router) | ||
21 | { | ||
22 | $this->router = $router; | ||
23 | } | ||
24 | |||
25 | /** | ||
26 | * {@inheritDoc} | ||
27 | */ | ||
28 | public static function getSubscribedEvents() | ||
29 | { | ||
30 | return array( | ||
31 | FOSUserEvents::RESETTING_RESET_SUCCESS => 'onPasswordResettingSuccess', | ||
32 | ); | ||
33 | } | ||
34 | |||
35 | public function onPasswordResettingSuccess(FormEvent $event) | ||
36 | { | ||
37 | $url = $this->router->generate('homepage'); | ||
38 | |||
39 | $event->setResponse(new RedirectResponse($url)); | ||
40 | } | ||
41 | } | ||
diff --git a/src/Wallabag/UserBundle/Resources/config/services.yml b/src/Wallabag/UserBundle/Resources/config/services.yml index 93e04d59..bf9e036a 100644 --- a/src/Wallabag/UserBundle/Resources/config/services.yml +++ b/src/Wallabag/UserBundle/Resources/config/services.yml | |||
@@ -8,3 +8,10 @@ services: | |||
8 | - "%scheb_two_factor.email.sender_name%" | 8 | - "%scheb_two_factor.email.sender_name%" |
9 | - "%wallabag_support_url%" | 9 | - "%wallabag_support_url%" |
10 | - "%wallabag_url%" | 10 | - "%wallabag_url%" |
11 | |||
12 | wallabag_user.password_resetting: | ||
13 | class: Wallabag\UserBundle\EventListener\PasswordResettingListener | ||
14 | arguments: | ||
15 | - "@router" | ||
16 | tags: | ||
17 | - { name: kernel.event_subscriber } | ||