Permalink
Browse files

Change uimessages flow such that it doesn't needlessly create sessions

  • Loading branch information...
flack committed Feb 1, 2019
1 parent 471b68b commit f7736793291f9abfbb45af825489750476d9f90c
@@ -118,7 +118,7 @@ public function initialize()
$this->request->attributes->set('context', $context);
// Initialize the UI message stack from session
$this->uimessages->initialize();
$this->uimessages->initialize($this->request);
$this->dispatcher->addListener(KernelEvents::REQUEST, [$this->cache->content, 'on_request'], 10);
$this->dispatcher->addListener(KernelEvents::RESPONSE, [$this->cache->content, 'on_response'], 10);
@@ -59,13 +59,18 @@ public function __construct()
&& $_SERVER['HTTPS'] !== 'off'
&& midcom::get()->config->get('auth_backend_simple_cookie_secure'));
$storage = new NativeSessionStorage([
$storage = $this->prepare_storage($cookie_path, $cookie_secure);
parent::__construct($storage, new NamespacedAttributeBag('midcom_session_data'));
}
protected function prepare_storage($cookie_path, $cookie_secure)
{
return new NativeSessionStorage([
'cookie_path' => $cookie_path,
'cookie_secure' => $cookie_secure,
'cookie_domain' => midcom::get()->config->get('auth_backend_simple_cookie_domain'),
'cookie_httponly' => true
]);
parent::__construct($storage, new NamespacedAttributeBag('midcom_session_data'));
}
/**
@@ -6,6 +6,8 @@
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
*/
use Symfony\Component\HttpFoundation\Request;
/**
* User interface messaging service
*
@@ -70,17 +72,26 @@ class midcom_services_uimessages
*/
public $uimessage_holder = 'body';
public function __construct()
/**
* @return \Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface
*/
private function get_message_stack()
{
$this->_message_stack = midcom::get()->session->getFlashBag();
if (!$this->_message_stack) {
$this->_message_stack = midcom::get()->session->getFlashBag();
}
return $this->_message_stack;
}
/**
* Initialize the message stack on service start-up. Reads older unshown
* messages from user session.
*/
public function initialize()
public function initialize(Request $request)
{
if ($request->hasPreviousSession()) {
$this->get_message_stack();
}
if (midcom::get()->auth->can_user_do('midcom:ajax', null, static::class)) {
midcom::get()->head->enable_jquery();
midcom::get()->head->add_jsfile(MIDCOM_STATIC_URL . '/midcom.services.uimessages/jquery.midcom_services_uimessages.js');
@@ -124,16 +135,18 @@ public function add($title, $message, $type = 'info')
'type' => $type,
];
// Append to message stack
$this->_message_stack->add($type, json_encode($msg));
$this->get_message_stack()->add($type, json_encode($msg));
return true;
}
public function get_messages()
{
$result = [];
foreach ($this->_message_stack->all() as $messages) {
foreach ($messages as $message) {
$result[] = $message;
if ($this->_message_stack) {
foreach ($this->_message_stack->all() as $messages) {
foreach ($messages as $message) {
$result[] = $message;
}
}
}
return $result;
@@ -146,6 +159,9 @@ public function get_messages()
*/
public function show($show_simple = false)
{
if (!$this->_message_stack) {
return;
}
if ( $show_simple
|| !midcom::get()->auth->can_user_do('midcom:ajax', null, static::class)) {
$this->show_simple();
@@ -177,7 +193,7 @@ public function show($show_simple = false)
*/
public function show_simple()
{
if (!empty($this->_message_stack->peekAll())) {
if ($this->_message_stack && !empty($this->_message_stack->peekAll())) {
echo "<div id=\"midcom_services_uimessages_wrapper\">\n";
foreach ($this->get_messages() as $message) {
@@ -96,6 +96,8 @@ function openpsa_test_create_dir($dir)
'SCRIPT_NAME' => 'unittest-run'
];
midcom::register_service_class('session', mock_sessioning::class);
// This is a bit awkward, but makes life simpler until we've transitioned more fully to the
// httpkernel infrastructure
$GLOBALS['kernel'] = midcom::init();
@@ -0,0 +1,10 @@
<?php
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
class mock_sessioning extends midcom_services__sessioning
{
protected function prepare_storage($cookie_path, $cookie_secure)
{
return new MockArraySessionStorage();
}
}

0 comments on commit f773679

Please sign in to comment.