Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable automatic login after registration in Symfony's FOSUserbundle

Using latest Symfony and FOSUserbundle, after successfully registering a new user, the user is automatically logged in. I want to prevent this. My reason is that only a special user should be able to register new users.

I guess I have to override the registerAction in the RegisterController of the bundle, but I don't know how.

I tried: http://symfony.com/doc/current/bundles/FOSUserBundle/overriding_controllers.html, but it seems to be outdated, no user is created with this method.

Any hints are appreciated.

Edit:

I found out that I did not create the child bundle correctly. I also had to create my own EventListener. It works now when I overwrite the FOSUserEvents::REGISTRATION_SUCCESS event.

Strange thing is that when I use the FOSUserEvents::REGISTRATION_COMPLETEDevent, both events are dispatched, my bundle's and the FOSUserbundle's, so that the user is redirected to the correct site, but logged in as the new user.

Edit 2:

So this is in my listener:

public static function getSubscribedEvents()
{
    return array(
        FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess',
        FOSUserEvents::REGISTRATION_COMPLETED => 'onRegistrationCompleted',
    );
}

public function onRegistrationSuccess(FormEvent  $event)
{
    $url = $this->router->generate('admin');

    $event->setResponse(new RedirectResponse($url));
}

public function onRegistrationCompleted(FilterUserResponseEvent  $event)
{

}

I set the redirection in the REGISTRATION_SUCCESSevent and the REGISTRATION_COMPLETEDis empty. With the debugger I can verify that my own listener's event is called, but the original event is also called.

like image 783
dev0 Avatar asked Jan 21 '16 14:01

dev0


1 Answers

Actually, there is no need to do any of these. The fos_user.listener.authentication service is removed from the container if use_authentication_listener is set to false.

See line 74-76 in FOS\UserBundle\DependencyInjection\FOSUserExtension.

This information is also included in document FOS UserBundle Configuration.

like image 143
twang Avatar answered Nov 09 '22 23:11

twang