N°2060 - Fix session messages and SCSS compilation services being cached

This commit is contained in:
Molkobain
2020-01-16 11:28:25 +01:00
parent a6fe564a95
commit 53adb37f43
6 changed files with 184 additions and 56 deletions

View File

@@ -48,6 +48,9 @@ $oListsCompat = new Lists($oModuleDesign);
$oListsCompat->Process($container);
// Generating CSS files
// Note: We do this here as it is not user dependent and therefore can be cached for everyone.
// A dedicated listener 'CssFromSassCompiler' exists to compile files again when by-passing HTTP cache.
// This is to keep developers comfort when tuning the SCSS files.
$aImportPaths = array($_ENV['COMBODO_PORTAL_BASE_ABSOLUTE_PATH'].'css/');
$aPortalConf = $container->getParameter('combodo.portal.instance.conf');
foreach ($aPortalConf['properties']['themes'] as $sKey => $value)
@@ -68,43 +71,4 @@ foreach ($aPortalConf['properties']['themes'] as $sKey => $value)
$aPortalConf['properties']['themes'][$sKey] = $aValues;
}
}
$container->setParameter('combodo.portal.instance.conf', $aPortalConf);
//TODO: The following needs to be refactored
// Session messages
// Note: We keep this system instead of following the Symfony system to make it simpler for extension developers to use them accross the admin. console and the portal.
$aAllMessages = array();
if ((array_key_exists('obj_messages', $_SESSION)) && (!empty($_SESSION['obj_messages'])))
{
foreach ($_SESSION['obj_messages'] as $sMessageKey => $aMessageObjectData)
{
$aObjectMessages = array();
$aRanks = array();
foreach ($aMessageObjectData as $sMessageId => $aMessageData)
{
$sMsgClass = 'alert alert-dismissible alert-';
switch ($aMessageData['severity'])
{
case 'info':
$sMsgClass .= 'info';
break;
case 'error':
$sMsgClass .= 'danger';
break;
case 'ok':
default:
$sMsgClass .= 'success';
break;
}
$aObjectMessages[] = array('css_classes' => $sMsgClass, 'message' => $aMessageData['message']);
$aRanks[] = $aMessageData['rank'];
}
//unset($_SESSION['obj_messages'][$sMessageKey]);
array_multisort($aRanks, $aObjectMessages);
foreach ($aObjectMessages as $aObjectMessage)
{
$aAllMessages[] = $aObjectMessage;
}
}
}
$container->setParameter('combodo.current_user.session_messages', $aAllMessages);
$container->setParameter('combodo.portal.instance.conf', $aPortalConf);

View File

@@ -0,0 +1,21 @@
# Copyright (C) 2010-2020 Combodo SARL
#
# This file is part of iTop.
#
# iTop is free software; you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# iTop is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with iTop. If not, see <http://www.gnu.org/licenses/>
p_session_message_add:
path: '/session-message/add'
defaults:
_controller: 'Combodo\iTop\Portal\Controller\SessionMessageController::AddMessageAction'

View File

@@ -1,4 +1,4 @@
# Copyright (C) 2010-2018 Combodo SARL
# Copyright (C) 2010-2020 Combodo SARL
#
# This file is part of iTop.
#
@@ -74,6 +74,8 @@ services:
tags: [{ name: 'kernel.event_listener', event: 'kernel.request', priority: 400 }]
Combodo\iTop\Portal\EventListener\ApplicationContextSetPluginPropertyClass:
tags: [{ name: 'kernel.event_listener', event: 'kernel.request', priority: 300 }]
Combodo\iTop\Portal\EventListener\CssFromSassCompiler:
tags: [{ name: 'kernel.event_listener', event: 'kernel.request', priority: 200 }]
# Add more service definitions when explicit configuration is needed
# Please note that last definitions always *replace* previous ones
@@ -141,4 +143,7 @@ services:
public: true
brick_controller_helper:
alias: Combodo\iTop\Portal\Helper\BrickControllerHelper
public: true
session_message_helper:
alias: Combodo\iTop\Portal\Helper\SessionMessageHelper
public: true

View File

@@ -0,0 +1,67 @@
<?php
/**
* Copyright (C) 2013-2020 Combodo SARL
*
* This file is part of iTop.
*
* iTop is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* iTop is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
*/
namespace Combodo\iTop\Portal\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpException;
/**
* Class SessionMessageController
*
* @author Guillaume Lajarige <guillaume.lajarige@combodo.com>
* @package Combodo\iTop\Portal\Controller
* @since 2.7.0
*/
class SessionMessageController extends AbstractController
{
/**
* @param \Symfony\Component\HttpFoundation\Request $oRequest
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function AddMessageAction(Request $oRequest)
{
$aData = array();
/** @var \Combodo\iTop\Portal\Helper\RequestManipulatorHelper $oRequestManipulator */
$oRequestManipulator = $this->get('request_manipulator');
/** @var \Combodo\iTop\Portal\Helper\SessionMessageHelper $oSessionMessageHelper */
$oSessionMessageHelper = $this->get('session_message_helper');
// Retrieve parameters
$sMessageSeverity = $oRequestManipulator->ReadParam('sSeverity');
$sMessageContent = $oRequestManipulator->ReadParam('sContent');
// Check parameters consistency
if (empty($sMessageSeverity) || empty($sMessageContent))
{
throw new HttpException(Response::HTTP_BAD_REQUEST, 'Message must have a severity and a content, make sure both sSeverity & sContent parameters are sent.');
}
// Add message
$oSessionMessageHelper->AddMessage(uniqid(), $sMessageContent, $sMessageSeverity);
return new JsonResponse($aData);
}
}

View File

@@ -0,0 +1,76 @@
<?php
/**
* Copyright (C) 2013-2020 Combodo SARL
*
* This file is part of iTop.
*
* iTop is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* iTop is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
*/
namespace Combodo\iTop\Portal\EventListener;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use utils;
/**
* Class CssFromSassCompiler
*
* This class is mostly here for developers comfort. SCSS files are already compiled when Symfony is creating its cache, but we need them to re-compile
* when tuning the SCSS files during development. To do so, just by pass the HTTP cache by hitting Ctrl + F5
*
* @package Combodo\iTop\Portal\EventListener
* @since 2.7.0
*/
class CssFromSassCompiler
{
/** @var array $aCombodoPortalInstanceConf */
private $aCombodoPortalInstanceConf;
/**
* CssFromSassCompiler constructor.
*
* @param array $aCombodoPortalInstanceConf
*/
public function __construct($aCombodoPortalInstanceConf)
{
$this->aCombodoPortalInstanceConf = $aCombodoPortalInstanceConf;
}
/**
* @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $oGetResponseEvent
*/
public function onKernelRequest(GetResponseEvent $oGetResponseEvent)
{
// Force compilation need only when by-passing cache to limit server load.
if (isset($_SERVER['HTTP_CACHE_CONTROL']) && ($_SERVER['HTTP_CACHE_CONTROL'] !== 'no-cache'))
{
return;
}
$aImportPaths = array($_ENV['COMBODO_PORTAL_BASE_ABSOLUTE_PATH'].'css/');
foreach ($this->aCombodoPortalInstanceConf['properties']['themes'] as $sKey => $value)
{
if (!is_array($value))
{
utils::GetCSSFromSASS('env-'.utils::GetCurrentEnvironment().'/'.$value, $aImportPaths);
}
else
{
foreach ($value as $sSubValue)
{
utils::GetCSSFromSASS('env-'.utils::GetCurrentEnvironment().'/'.$sSubValue, $aImportPaths);
}
}
}
}
}

View File

@@ -325,25 +325,20 @@
<div class="container-fluid" id="main-wrapper">
<div class="row">
<div class="col-xs-12 col-sm-9 col-md-10 col-sm-offset-3 col-md-offset-2">
{% if app['combodo.current_user.session_messages']|length > 0 %}
<section class="row" id="session-messages">
<div class="col-xs-12">
{% for aSessionMessage in app['combodo.current_user.session_messages'] %}
<div class="{{ aSessionMessage['css_classes'] }}">
<button type="button" class="close" data-dismiss="alert" aria-label="X"><span class="fas fa-times"></span></button>
{{ aSessionMessage['message'] }}
</div>
{% endfor %}
</div>
</section>
{% endif %}
<section class="row" id="session-messages">
<div class="col-xs-12">
{% for aSessionMessage in app['session_message_helper'] %}
<div class="{{ aSessionMessage['css_classes'] }}">
<button type="button" class="close" data-dismiss="alert" aria-label="X"><span class="fas fa-times"></span></button>
{{ aSessionMessage['message'] }}
</div>
{% endfor %}
</div>
</section>
<section class="row" id="main-header">
{% block pMainHeader %}
{% endblock %}
</section>
<section class="row" id="main-content">
{% block pMainContent %}
{% endblock %}