Execute functions before each url and then redirect to Symfony 2.6
I want to check some user metadata before each url. During this process, I want to skip some special urls like login, login and of course need to get the current user as well.
NOTE. I want to test other functions and redirect before it goes to the controller. Don't want to change the answer, but ask for it. I hope this is the right event.
I am currently connecting to an event kernel.request
and doing something like this
Create subscriber
profile_check_subscriber:
class: BBT\MainBundle\EventListener\ProfileCheckSubscriber
arguments: ['@user_manager', '@router.default']
tags:
- { name: kernel.event_subscriber }
And then in the ProfileCheckSubscriber class
class ProfileCheckSubscriber implements EventSubscriberInterface {
/**
* @var UserManager
*/
private $userManager;
/**
* @var Router
*/
private $router;
public function __construct(UserManager $userManager, Router $router)
{
$this->userManager = $userManager;
$this->router = $router;
}
public static function getSubscribedEvents()
{
return array(
'kernel.request' => 'onKernelRequest'
);
}
public function onKernelRequest( GetResponseEvent $event)
{
$request_uri = $event->getRequest()->getRequestUri();
$logout_url = $this->router->generate('fos_user_security_logout');
//don't proceed on these urls
$check_url_array = array(
$logout_url,
$this->router->generate('user_welcome'),
$this->router->generate('fos_user_registration_confirmed'),
$this->router->generate('fos_user_registration_register'),
$this->router->generate('fos_user_security_check'),
$this->router->generate('fos_user_security_login')
);
if( $event->getRequest()->getMethod() !== 'GET' )
return;
if( $event->getRequest()->isXmlHttpRequest() )
return;
if( in_array( $request_uri, $check_url_array ) )
return;
if( false !== strpos( $request_uri, 'f/f_edit' ) )
return;
if( false !== strpos( $request_uri, 'register' ) )
return;
if( 'anon.' == $this->userManager->current_user || false == $this->userManager->current_user )
return;
//good to process
$forced_profile_page_url = $this->router->generate('user_forced_pages', array('page_type' => 'profile_picture'));
$profile_fields_url = $this->router->generate('user_profile_edit');
$profile_image_check = $this->userManager->getSingleMeta('force_profile_image_set');
$profile_fields_check = $this->userManager->getSingleMeta('force_profile_fields_set');
if( true == $profile_image_check && $request_uri != $forced_profile_page_url ){
$event->setResponse(new RedirectResponse( $forced_profile_page_url ));
} elseif( true == $profile_fields_check && $request_uri != $profile_fields_url && $request_uri !== $forced_profile_page_url ) {
$event->setResponse(new RedirectResponse( $profile_fields_url ));
}
}
}
Here userManager
is my client, which current_user
is retrieved by security token.
This code works fine if I use kernel.response
and use FilterResponseEvent
instead GetResponseEvent
, but it doesn't run on kernel.request
because it can't find the current user. I'm not sure how to kernel.request
mess up the session.
UPDATE: Sorry guys, go back to the drawing board. Setting the priority did not trigger the user. So it is not possible to get the user yet Tried priority 0 and 50. No luck!
source to share
Have you tried changing the event listening on kernel.controller
?
public function onKernelController(FilterControllerEvent $event)
{
$user = $this->tokenStorage->getToken()->getUser();
/* all your logic */
$redirectUrl = /** something **/
$event->setController(function() use ($redirectUrl) {
return new RedirectResponse($redirectUrl);
});
}
http://symfony.com/doc/current/components/http_kernel/introduction.html#the-kernel-controller-event
source to share