/** * Initiate session if not available then reads the SecurityContext from it. * * @param GetResponseEvent $event A GetResponseEvent instance */ public function handle(GetResponseEvent $event) { $request = $event->getRequest(); $application = $event->getKernel()->getApplication(); if (null !== $application && false === $request->hasSession()) { $container = $application->getContainer(); $event->getRequest()->setSession($container->get('bb_session')); } parent::handle($event); }
/** * Initiate session if not available then reads the SecurityContext from it. * * @param GetResponseEvent $event A GetResponseEvent instance */ public function handle(GetResponseEvent $event) { $request = $event->getRequest(); $application = $event->getKernel()->getApplication(); if (null !== $application && false === $request->hasSession()) { // Don't need to check if container has service with id `bb_session` cause we declared it as synthetic if (null === $application->getContainer()->get('bb_session')) { $application->getContainer()->set('bb_session', $application->getSession()); } $application->getContainer()->get('bb_session')->start(); $application->info("Session started"); $event->getRequest()->setSession($application->getContainer()->get('bb_session')); } parent::handle($event); }
public function testHandleRemovesTokenIfNoPreviousSessionWasFound() { $request = $this->getMock('Symfony\\Component\\HttpFoundation\\Request'); $request->expects($this->any())->method('hasPreviousSession')->will($this->returnValue(false)); $event = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\Event\\GetResponseEvent')->disableOriginalConstructor()->getMock(); $event->expects($this->any())->method('getRequest')->will($this->returnValue($request)); $tokenStorage = $this->getMock('Symfony\\Component\\Security\\Core\\Authentication\\Token\\Storage\\TokenStorageInterface'); $tokenStorage->expects($this->once())->method('setToken')->with(null); $listener = new ContextListener($tokenStorage, array(), 'key123'); $listener->handle($event); }
/** * @dataProvider provideInvalidToken */ public function testInvalidTokenInSession($token) { $context = $this->getMock('Symfony\\Component\\Security\\Core\\SecurityContextInterface'); $event = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\Event\\GetResponseEvent')->disableOriginalConstructor()->getMock(); $request = $this->getMock('Symfony\\Component\\HttpFoundation\\Request'); $session = $this->getMockBuilder('Symfony\\Component\\HttpFoundation\\Session\\Session')->disableOriginalConstructor()->getMock(); $event->expects($this->any())->method('getRequest')->will($this->returnValue($request)); $request->expects($this->any())->method('hasPreviousSession')->will($this->returnValue(true)); $request->expects($this->any())->method('getSession')->will($this->returnValue($session)); $session->expects($this->any())->method('get')->with('_security_key123')->will($this->returnValue(serialize($token))); $context->expects($this->once())->method('setToken')->with(null); $listener = new ContextListener($context, array(), 'key123'); $listener->handle($event); }