diff --git a/lib/Alchemy/Phrasea/Controller/Root/SessionController.php b/lib/Alchemy/Phrasea/Controller/Root/SessionController.php index f0c8d29e05..2de5b511ac 100644 --- a/lib/Alchemy/Phrasea/Controller/Root/SessionController.php +++ b/lib/Alchemy/Phrasea/Controller/Root/SessionController.php @@ -11,6 +11,8 @@ namespace Alchemy\Phrasea\Controller\Root; use Alchemy\Phrasea\Application\Helper\EntityManagerAware; use Alchemy\Phrasea\Controller\Controller; +use Alchemy\Phrasea\Model\Entities\Session; +use Alchemy\Phrasea\Model\Entities\SessionModule; use Alchemy\Phrasea\Model\Repositories\SessionRepository; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\RedirectResponse; @@ -20,6 +22,85 @@ class SessionController extends Controller { use EntityManagerAware; + /** + * @param Request $request + * @return JsonResponse + * @throws \Exception in case "new \DateTime()" fails ? + */ + public function updateSession(Request $request) + { + if (!$request->isXmlHttpRequest()) { + $this->app->abort(400); + } + + $ret = [ + 'status' => 'unknown', + 'message' => '', + ]; + + $authenticator = $this->getAuthenticator(); + if ($authenticator->isAuthenticated()) { + $usr_id = $authenticator->getUser()->getId(); + if ($usr_id != $request->request->get('usr')) { // I logged with another user + $ret['status'] = 'disconnected'; + + return $this->app->json($ret); + } + } + else { + $ret['status'] = 'disconnected'; + + return $this->app->json($ret); + } + + try { + $this->getApplicationBox()->get_connection(); + } + catch (\Exception $e) { + return $this->app->json($ret); + } + + if (1 > $moduleId = (int) $request->request->get('module')) { + $ret['message'] = 'Missing or Invalid `module` parameter'; + + return $this->app->json($ret); + } + + /** @var Session $session */ + $now = new \DateTime(); + $session = $this->getSessionRepository()->find($this->getSession()->get('session_id')); + $session->setUpdated($now); + + $manager = $this->getEntityManager(); + if (!$session->hasModuleId($moduleId)) { + $module = new SessionModule(); + $module->setModuleId($moduleId); + $module->setSession($session); + $manager->persist($module); + } + else { + $manager->persist($session->getModuleById($moduleId)->setUpdated($now)); + } + + $manager->persist($session); + $manager->flush(); + + $ret['status'] = 'ok'; + + if (in_array($this->getSession()->get('phraseanet.message'), ['1', null])) { + $conf = $this->getConf(); + if ($conf->get(['main', 'maintenance'])) { + $ret['message'] .= $this->app->trans('The application is going down for maintenance, please logout.'); + } + + if ($conf->get(['registry', 'maintenance', 'enabled'])) { + $ret['message'] .= strip_tags($conf->get(['registry', 'maintenance', 'message'])); + } + } + + return $this->app->json($ret); + } + /** * Deletes identified session * @@ -57,6 +138,14 @@ class SessionController extends Controller return $this->app->redirectPath('account_sessions'); } + /** + * @return Session + */ + private function getSession() + { + return $this->app['session']; + } + /** * @return SessionRepository */ diff --git a/lib/Alchemy/Phrasea/ControllerProvider/Root/Session.php b/lib/Alchemy/Phrasea/ControllerProvider/Root/Session.php index fdfa1a389f..0f13231a22 100644 --- a/lib/Alchemy/Phrasea/ControllerProvider/Root/Session.php +++ b/lib/Alchemy/Phrasea/ControllerProvider/Root/Session.php @@ -41,6 +41,10 @@ class Session implements ControllerProviderInterface, ServiceProviderInterface { $controllers = $this->createCollection($app); + /** @uses SessionController::updateSession() */ + $controllers->post('/update/', 'controller.session:updateSession') + ->bind('update_session'); + /** @uses SessionController::deleteSession() */ // used in admin/connected_users to kill a session $controller = $controllers->post('/delete/{id}', 'controller.session:deleteSession') diff --git a/tests/Alchemy/Tests/Phrasea/Controller/Root/SessionTest.php b/tests/Alchemy/Tests/Phrasea/Controller/Root/SessionTest.php index 1c0f34f26f..dfda0973b7 100644 --- a/tests/Alchemy/Tests/Phrasea/Controller/Root/SessionTest.php +++ b/tests/Alchemy/Tests/Phrasea/Controller/Root/SessionTest.php @@ -3,7 +3,6 @@ namespace Alchemy\Tests\Phrasea\Controller\Root; use Alchemy\Phrasea\Model\Entities\User; -use Symfony\Component\HttpKernel\Client; /** * @group functional @@ -90,8 +89,6 @@ class SessionTest extends \PhraseanetAuthenticatedWebTestCase { $this->assertObjectHasAttribute('status', $data); $this->assertObjectHasAttribute('message', $data); - $this->assertObjectHasAttribute('notifications', $data); - $this->assertObjectHasAttribute('changed', $data); } public function testDeleteSession() diff --git a/www/scripts/apps/admin/tasks-manager/collections/tasks.js b/www/scripts/apps/admin/tasks-manager/collections/tasks.js index 63253dc376..9cac331e81 100644 --- a/www/scripts/apps/admin/tasks-manager/collections/tasks.js +++ b/www/scripts/apps/admin/tasks-manager/collections/tasks.js @@ -15,7 +15,7 @@ define([ var TaskCollection = Backbone.Collection.extend({ model: TaskModel, url: function () { - return "/admin/task-manager/tasks?update_session=0"; + return "/admin/task-manager/tasks"; } });