Move init session to session manager subscriber

This commit is contained in:
Nicolas Le Goff
2014-05-23 10:43:46 +02:00
parent 83e63e731a
commit 2cd0f1879d
2 changed files with 34 additions and 29 deletions

View File

@@ -399,7 +399,6 @@ class Application extends SilexApplication
$this['dispatcher'] = $this->share(
$this->extend('dispatcher', function ($dispatcher, Application $app) {
$dispatcher->addListener(KernelEvents::REQUEST, array($app, 'initSession'), 254);
$dispatcher->addListener(KernelEvents::RESPONSE, array($app, 'addUTF8Charset'), -128);
$dispatcher->addSubscriber($app['phraseanet.logout-subscriber']);
$dispatcher->addSubscriber($app['phraseanet.locale-subscriber']);
@@ -511,25 +510,6 @@ class Application extends SilexApplication
return $this->redirect($this->url($route, $parameters));
}
public function initSession(GetResponseEvent $event)
{
if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
return;
}
if (false !== stripos($event->getRequest()->server->get('HTTP_USER_AGENT'), 'flash')
&& $event->getRequest()->getRequestUri() === '/prod/upload/') {
if (null !== $sessionId = $event->getRequest()->request->get('php_session_id')) {
$request = $event->getRequest();
$request->cookies->set($this['session']->getName(), $sessionId);
return $request;
}
}
}
public function addUTF8Charset(FilterResponseEvent $event)
{
if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {

View File

@@ -13,13 +13,14 @@ namespace Alchemy\Phrasea\Core\Event\Subscriber;
use Alchemy\Phrasea\Application;
use Entities\SessionModule;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
/**log real human activity on application, to keep session alive*/
class SessionManagerSubscriber implements EventSubscriberInterface
{
private $app;
@@ -32,10 +33,31 @@ class SessionManagerSubscriber implements EventSubscriberInterface
public static function getSubscribedEvents()
{
return array(
KernelEvents::REQUEST => array('checkSessionActivity', -112),
KernelEvents::REQUEST => array(
array('initSession', Application::EARLY_EVENT),
array('checkSessionActivity', Application::LATE_EVENT)
)
);
}
public function initSession(GetResponseEvent $event)
{
if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
return;
}
if ($this->isFlashUploadRequest($event->getRequest())) {
if (null !== $sessionId = $event->getRequest()->request->get('php_session_id')) {
$request = $event->getRequest();
$request->cookies->set($this->app['session']->getName(), $sessionId);
return $request;
}
}
}
/**log real human activity on application, to keep session alive*/
public function checkSessionActivity(GetResponseEvent $event)
{
$modulesIds = array(
@@ -49,25 +71,25 @@ class SessionManagerSubscriber implements EventSubscriberInterface
$pathInfo = array_filter(explode('/', $event->getRequest()->getPathInfo()));
if(count($pathInfo) < 1) {
if (count($pathInfo) < 1) {
return;
}
$moduleName = strtolower($pathInfo[1]);
if (!array_key_exists($moduleName, $modulesIds) ) {
return;
}
// this route is polled by js in admin/databox to refresh infos (progress bar...)
if (preg_match("#^/admin/databox/[0-9]+/informations/documents/#", $event->getRequest()->getPathInfo()) == 1) {
return;
}
// this route is polled by js in admin/tasks to refresh tasks status
if ($event->getRequest()->getPathInfo() == "/admin/task-manager/tasks/" && $event->getRequest()->getContentType() == 'json') {
return;
}
if ($this->isFlashUploadRequest($event->getRequest())) {
return;
}
// if we are already disconnected (ex. from another window), quit immediatly
if (!($this->app['authentication']->isAuthenticated())) {
if ($event->getRequest()->isXmlHttpRequest()) {
@@ -81,7 +103,6 @@ class SessionManagerSubscriber implements EventSubscriberInterface
return;
}
$session = $this->app['EM']->find('Entities\Session', $this->app['session']->get('session_id'));
$idle = 0;
@@ -104,7 +125,6 @@ class SessionManagerSubscriber implements EventSubscriberInterface
return;
}
$moduleId = $modulesIds[$moduleName];
$session->setUpdated(new \DateTime());
@@ -119,8 +139,13 @@ class SessionManagerSubscriber implements EventSubscriberInterface
} else {
$this->app['EM']->persist($session->getModuleById($moduleId)->setUpdated(new \DateTime()));
}
$this->app['EM']->persist($session);
$this->app['EM']->flush();
}
private function isFlashUploadRequest(Request $request)
{
return false !== stripos($request->server->get('HTTP_USER_AGENT'), 'flash')
&& $request->getRequestUri() === '/prod/upload/';
}
}