Skip to content

Instantly share code, notes, and snippets.

  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ifdattic/21a0575dc4878633430e to your computer and use it in GitHub Desktop.
Source code for article http://ifdattic.com/symfony-events-require-account-information-after-registration
The first line provides the full file path, remove it from real files!
{# in src/Ifdattic/UserBundle/Resources/views/Profile/_disabled_user_message.html.twig #}
{% trans_default_domain 'FOSUserBundle' %}
{% if app.user.isEnabled == false %}
<div>
<h3>{{ 'profile.edit.message_for_disabled_user.header'|trans }}</h3>
<p>{{ 'profile.edit.message_for_disabled_user.message'|trans }}</p>
</div>
{% endif %}
#src/Ifdattic/UserBundle/EventListener/AccountInformationListener.php
<?php
namespace Ifdattic\UserBundle\EventListener;
use FOS\UserBundle\Event\FormEvent;
use FOS\UserBundle\FOSUserEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
class AccountInformationListener implements EventSubscriberInterface
{
/**
* Route to redirect to
*
* @var string
*/
private $redirectRoute;
/**
* @param string $redirectRoute Route to redirect to if conditions not met
*/
public function __construct($redirectRoute)
{
$this->redirectRoute = $redirectRoute;
}
/**
* {@inheritDoc}
*/
public static function getSubscribedEvents()
{
return [
'kernel.controller' => 'onKernelController',
FOSUserEvents::PROFILE_EDIT_SUCCESS => 'onProfileEdit',
];
}
/**
* If user is not enabled (haven't provided all information) make him to
* finish it before using the application.
*
* @param FilterControllerEvent $event
* @return mixed
*/
public function onKernelController(FilterControllerEvent $event)
{
if ($this->redirectRoute === $event->getRequest()->attributes->get('_route')) {
return;
}
$controller = $event->getController();
if (!is_array($controller) || !method_exists($controller[0], 'get')) {
return;
}
$security = $controller[0]->get('security.context');
if (!$security->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
return;
}
$user = $security->getToken()->getUser();
if ($user->isEnabled()) {
return;
}
$redirectUrl = $controller[0]->generateUrl($this->redirectRoute);
$event->setController(function () use ($redirectUrl) {
return new RedirectResponse($redirectUrl);
});
}
/**
* Enable user after he filled all the required account information.
*
* @param FormEvent $event
* @return void
*/
public function onProfileEdit(FormEvent $event)
{
$user = $event->getForm()->getData();
if (false === $user->isEnabled()) {
$user->setEnabled(true);
}
}
}
{# in src/Ifdattic/UserBundle/Resources/views/Profile/edit_content.html.twig #}
{% include "IfdatticUserBundle:Profile:_disabled_user_message.html.twig" %}
#src/Ifdattic/UserBundle/Resources/config/services.yml
services:
ifdattic_user.account_information.listener:
class: Ifdattic\UserBundle\EventListener\AccountInformationListener
arguments: ["fos_user_profile_edit"]
tags:
- { name: kernel.event_subscriber }
@fsmeier
Copy link

fsmeier commented Aug 24, 2015

thx, you helped me a lot!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment