Sunday, June 16, 2013

Symfony 2 FOSUserBundle - overriding the registration success event

I was trying the change the registration flow of FOSUserBundle the other day. The goal is to let new users to use the site without activating their accounts while an activation email can still be sent. Every time these users log in to the site, a flash message will show up and ask them to activate their accounts.

The first thing I did was creating a EmailConfirmationListener. You can copy this file from the FOSUserBundle in the vender's folder. All I did was 1) setting user isEnabled flag to true and 2) changing the route (if needed).

Note that the EmailConfirmationListener registers the following event:
FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess'
When I registered a new user, I registered two activation emails were be sent.

The trace log shows that two onRegistrationSuccess events were fired. One is the one from the vendor, the other one is the one I just created.

The listener service is declared in email_confirmation.xml.

Doing a quick search on the project root folder for the term "email_confirmation.xml" reveals the following:
if ($config['confirmation']['enabled']) {
         $loader->load('email_confirmation.xml');
}
Now turn the confirmation setting to false in config.yml

fos_user:
    registration:
        confirmation:
            enabled: true

Then only one onRegistrationSuccess event would be fired.

No comments:

Post a Comment