{"id":1485,"date":"2015-08-24T07:16:03","date_gmt":"2015-08-24T07:16:03","guid":{"rendered":"https:\/\/intelligentbee.com\/blog\/?p=1485"},"modified":"2024-10-25T09:11:20","modified_gmt":"2024-10-25T09:11:20","slug":"how-to-enable-email-confirmation-on-fosuserbundle-profile-edit","status":"publish","type":"post","link":"https:\/\/intelligentbee.com\/blog\/how-to-enable-email-confirmation-on-fosuserbundle-profile-edit\/","title":{"rendered":"How To Enable Email Confirmation On Fosuserbundle Profile Edit"},"content":{"rendered":"<p>We all know and use FOSUserBundle in our Symfony applications, so much it became kind of a standard. It provides everything you need for user management: login, registration, email confirmation and much more control over the access of\u00a0the user in your application. But we\u00a0found a thing missing from this awesome package: email confirmation after the initial email address has been changed through a profile edit. In the following lines we will show you how to extend the FOSUserBundle to implement this.<\/p>\n<h2>How To Enable Email Confirmation On Fosuserbundle Profile Edit<\/h2>\n<p>This post assumes you are familiar (even advanced) with the Symfony framework and FOSUserBundle.<\/p>\n<p>To get started we will need a listener to be triggered when a profile edit has happened, FOSUserBundle fires\u00a0two events that we are interested in: <code>FOSUserEvents::PROFILE_EDIT_INITIALIZE<\/code> and\u00a0<code>FOSUserEvents::PROFILE_EDIT_SUCCESS<\/code>. The first one is triggered before the actual profile data is changed so we will use that to get a hold on the original\u00a0email address. When the second event is fired, we will compare the initial email address with the current\u00a0one and, if they are not the same, we will start the confirmation process:<\/p>\n<pre class=\"lang:php decode:true \">&lt;?php\r\n\/\/ src\/AppBundle\/EventListener\/ProfileEditListener.php\r\n\r\nnamespace AppBundle\\EventListener;\r\n\r\nuse FOS\\UserBundle\\FOSUserEvents;\r\nuse FOS\\UserBundle\\Event\\FormEvent;\r\nuse FOS\\UserBundle\\Event\\GetResponseUserEvent;\r\nuse FOS\\UserBundle\\Mailer\\MailerInterface;\r\nuse FOS\\UserBundle\\Util\\TokenGeneratorInterface;\r\nuse Symfony\\Component\\EventDispatcher\\EventSubscriberInterface;\r\nuse Symfony\\Component\\HttpFoundation\\RedirectResponse;\r\nuse Symfony\\Component\\Routing\\Generator\\UrlGeneratorInterface;\r\nuse Symfony\\Component\\HttpFoundation\\Session\\SessionInterface;\r\n\r\nclass ProfileEditListener implements EventSubscriberInterface\r\n{\r\n    private $oldEmail;\r\n    private $mailer;\r\n    private $tokenGenerator;\r\n    private $router;\r\n    private $session;\r\n    private $tokenStorage;\r\n\r\n    public function __construct(MailerInterface $mailer, TokenGeneratorInterface $tokenGenerator, UrlGeneratorInterface $router, SessionInterface $session, TokenStorageInterface $tokenStorage)\r\n    {\r\n        $this-&gt;mailer = $mailer;\r\n        $this-&gt;tokenGenerator = $tokenGenerator;\r\n        $this-&gt;router = $router;\r\n        $this-&gt;session = $session;\r\n        $this-&gt;tokenStorage = $tokenStorage;\r\n    }\r\n\r\n    public static function getSubscribedEvents()\r\n    {\r\n        return array(\r\n            FOSUserEvents::PROFILE_EDIT_INITIALIZE =&gt; 'onProfileEditInitialize',\r\n            FOSUserEvents::PROFILE_EDIT_SUCCESS =&gt; 'onProfileEditSuccess'\r\n        );\r\n    }\r\n\r\n    public function onProfileEditInitialize(GetResponseUserEvent $event)\r\n    {\r\n        $this-&gt;oldEmail = $event-&gt;getUser()-&gt;getEmail();\r\n    }\r\n    \r\n    public function onProfileEditSuccess(FormEvent $event)\r\n    {\r\n        $user = $event-&gt;getForm()-&gt;getData();\r\n        if ($user-&gt;getEmail() !== $this-&gt;oldEmail)\r\n        {\r\n            \/\/ disable user\r\n            $user-&gt;setEnabled(false);\r\n\r\n            \/\/ send confirmation token to new email\r\n            $user-&gt;setConfirmationToken($this-&gt;tokenGenerator-&gt;generateToken());\r\n            $this-&gt;mailer-&gt;sendConfirmationEmailMessage($user);\r\n\r\n            \/\/ force user to log-out\r\n            $this-&gt;tokenStorage-&gt;setToken();\r\n\r\n            \/\/ redirect user to check email page\r\n            $this-&gt;session-&gt;set('fos_user_send_confirmation_email\/email', $user-&gt;getEmail());\r\n            $url = $this-&gt;router-&gt;generate('fos_user_registration_check_email');\r\n            $event-&gt;setResponse(new RedirectResponse($url));\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>Now, add this to your <code>services.yml<\/code> file and you&#8217;re good to go:<\/p>\n<pre class=\"lang:default decode:true \">    app.profile_edit_listener:\r\n        class: AppBundle\\EventListener\\ProfileEditListener\r\n        arguments: [@fos_user.mailer, @fos_user.util.token_generator, @router, @session, @security.token_storage]\r\n        tags:\r\n            - { name: kernel.event_subscriber }<\/pre>\n<h3>How To Enable Email Confirmation On Fosuserbundle Profile Edit<\/h3>\n<p>One last thing: you will probably want to change the email template that is sent to the user with the confirmation link. You can overwrite it by\u00a0creating <code>app\/Resources\/FOSUserBundle\/views\/Registration\/email.txt.twig<\/code> and put what you need in there (use the original one from <code>vendor\/friendsofsymfony\/user-bundle\/Resources\/views\/Registration\/email.txt.twig<\/code> to see how to get the confirmation link).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We all know and use FOSUserBundle in our Symfony applications, so much it became kind of a standard. It provides [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":1499,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[73,82],"tags":[194,238],"yst_prominent_words":[365],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts\/1485"}],"collection":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/comments?post=1485"}],"version-history":[{"count":5,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts\/1485\/revisions"}],"predecessor-version":[{"id":133276,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts\/1485\/revisions\/133276"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/media\/1499"}],"wp:attachment":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/media?parent=1485"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/categories?post=1485"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/tags?post=1485"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/yst_prominent_words?post=1485"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}