symfony - FOSUserBundle: Success target after password reset according to roles -


after user did reset password using password reset of fosuserbundle, default redirected fosuserprofile. want redirect different route according role. possible , if yes, how? code redirect kind of users

namespace acme\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 change redirection @ end of password resetting  */ 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));     } } 

and registering service with

services:     acme_user.password_resetting:         class: acme\userbundle\eventlistener\passwordresettinglistener         arguments: [ "@router" ]         tags:             - { name: kernel.event_subscriber } 

based on version of symfony can choose 1 of approaches described in: http://symfony.com/blog/new-in-symfony-2-6-security-component-improvements

for example can use security.authorization_checker service:

inject service:

services:     acme_user.password_resetting:         class: acme\userbundle\eventlistener\passwordresettinglistener         arguments: [ "@router",  "@security.authorization_checker" ]         tags:             - { name: kernel.event_subscriber } 

then in actual service:

use symfony\component\security\core\authorization\authorizationchecker;  /**  * listener responsible change redirection @ end of password resetting  */ class passwordresettinglistener implements eventsubscriberinterface {     private $router;     private $authorizationchecker;      public function __construct(urlgeneratorinterface $router, authorizationchecker $authorizationchecker) {         $this->authorizationchecker = $authorizationchecker;         $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));          if (false === $this->authorizationchecker->isgranted('role_admin')) {             // redirect somewhere         } else {            // redirect elsewhere         }       } } 

Comments