PHRAS-3442_optimize-list-notifications_4.1-bis

WIP
restore /session/update route to pass tests, THIS ROUTE IS UNUSED
This commit is contained in:
jygaulier
2021-06-17 20:05:57 +02:00
parent 354636150e
commit 0c58dd2af2
4 changed files with 94 additions and 4 deletions

View File

@@ -11,6 +11,8 @@ namespace Alchemy\Phrasea\Controller\Root;
use Alchemy\Phrasea\Application\Helper\EntityManagerAware; use Alchemy\Phrasea\Application\Helper\EntityManagerAware;
use Alchemy\Phrasea\Controller\Controller; use Alchemy\Phrasea\Controller\Controller;
use Alchemy\Phrasea\Model\Entities\Session;
use Alchemy\Phrasea\Model\Entities\SessionModule;
use Alchemy\Phrasea\Model\Repositories\SessionRepository; use Alchemy\Phrasea\Model\Repositories\SessionRepository;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\RedirectResponse;
@@ -20,6 +22,85 @@ class SessionController extends Controller
{ {
use EntityManagerAware; 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 * Deletes identified session
* *
@@ -57,6 +138,14 @@ class SessionController extends Controller
return $this->app->redirectPath('account_sessions'); return $this->app->redirectPath('account_sessions');
} }
/**
* @return Session
*/
private function getSession()
{
return $this->app['session'];
}
/** /**
* @return SessionRepository * @return SessionRepository
*/ */

View File

@@ -41,6 +41,10 @@ class Session implements ControllerProviderInterface, ServiceProviderInterface
{ {
$controllers = $this->createCollection($app); $controllers = $this->createCollection($app);
/** @uses SessionController::updateSession() */
$controllers->post('/update/', 'controller.session:updateSession')
->bind('update_session');
/** @uses SessionController::deleteSession() */ /** @uses SessionController::deleteSession() */
// used in admin/connected_users to kill a session // used in admin/connected_users to kill a session
$controller = $controllers->post('/delete/{id}', 'controller.session:deleteSession') $controller = $controllers->post('/delete/{id}', 'controller.session:deleteSession')

View File

@@ -3,7 +3,6 @@
namespace Alchemy\Tests\Phrasea\Controller\Root; namespace Alchemy\Tests\Phrasea\Controller\Root;
use Alchemy\Phrasea\Model\Entities\User; use Alchemy\Phrasea\Model\Entities\User;
use Symfony\Component\HttpKernel\Client;
/** /**
* @group functional * @group functional
@@ -90,8 +89,6 @@ class SessionTest extends \PhraseanetAuthenticatedWebTestCase
{ {
$this->assertObjectHasAttribute('status', $data); $this->assertObjectHasAttribute('status', $data);
$this->assertObjectHasAttribute('message', $data); $this->assertObjectHasAttribute('message', $data);
$this->assertObjectHasAttribute('notifications', $data);
$this->assertObjectHasAttribute('changed', $data);
} }
public function testDeleteSession() public function testDeleteSession()

View File

@@ -15,7 +15,7 @@ define([
var TaskCollection = Backbone.Collection.extend({ var TaskCollection = Backbone.Collection.extend({
model: TaskModel, model: TaskModel,
url: function () { url: function () {
return "/admin/task-manager/tasks?update_session=0"; return "/admin/task-manager/tasks";
} }
}); });