Commit 36eb9978 authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Possibilité de passer l'appli en mode maintenance (indisponibilité totale)...

Possibilité de passer l'appli en mode maintenance (indisponibilité totale) dans la config, sauf pour certaines adresses IP (repris de OSE, merci Laurent).  Si activé, tous les "route match" sont remplacés pour exécuter l'action ApplicationController::maintenance (pas de vue, mais un layout dédié).
parent 846e0b0c
Loading
Loading
Loading
Loading
+12
Changes for Module.php: 12 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php
namespace UnicaenApp;

use UnicaenApp\Mvc\Listener\MaintenanceListener;
use UnicaenApp\Options\ModuleOptions;
use Zend\EventManager\EventInterface;
use Zend\ServiceManager\ServiceManager;
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
@@ -14,6 +16,8 @@ use Zend\Mvc\I18n\Translator;
use Locale;
use UnicaenApp\Mvc\View\Http\ExceptionStrategy;
use UnicaenApp\Mvc\Listener\ModalListener;
use Zend\View\Helper\Navigation;
use Zend\View\HelperPluginManager;

define('__VENDOR_DIR__', dirname(dirname(__DIR__)));

@@ -64,6 +68,7 @@ class Module implements
     *
     * @param EventInterface $e
     * @see BootstrapListenerInterface
     * @return void
     */
    public function onBootstrap(EventInterface $e) /* @var $e \Zend\Mvc\MvcEvent */
    {
@@ -107,6 +112,13 @@ class Module implements
        
        $eventManager->attach(new ModalListener());

        /** @var ModuleOptions $moduleOptions */
        $moduleOptions = $services->get('unicaen-app_module_options');

        // détection du mode maintenance pour rendre l'appli indisponible
        $maintenanceListener = new MaintenanceListener($moduleOptions->getMaintenance());
        $maintenanceListener->attach($eventManager);

        $this->appendSessionRefreshJs($e);
    }

+26
Changes for config/module.config.php: 26 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -94,6 +94,17 @@ return [
                    ],
                ],
            ],
//            // Rendu du menu secondaire
//            'menu-secondaire' => [
//                'type'          => 'Literal',
//                'options'       => [
//                    'route'       => '/menu-secondaire',
//                    'defaults'    => [
//                        'controller' => 'UnicaenApp\Controller\Application',
//                        'action'     => 'menu-secondaire',
//                    ],
//                ],
//            ],
            // Test d'envoi de mail par l'appli
            'test-envoi-mail' => [
                'type'          => 'Literal',
@@ -105,6 +116,16 @@ return [
                    ],
                ],
            ],
            'maintenance' => [
                'type'          => 'Literal',
                'options'       => [
                    'route'       => '/maintenance',
                    'defaults'    => [
                        'controller' => 'UnicaenApp\Controller\Application',
                        'action'     => 'maintenance',
                    ],
                ],
            ],
            'cache'            => [
                'type'          => 'Zend\Mvc\Router\Http\Literal',
                'options'       => [
@@ -477,6 +498,11 @@ return [
    'bjyauthorize'    => [
        'guards' => [
            'BjyAuthorize\Guard\Controller' => [
                [
                    'controller' => 'UnicaenApp\Controller\Application',
                    'action'     => ['maintenance'],
                    'roles'      => [],
                ],
                [
                    'controller' => 'UnicaenApp\Controller\Cache',
                    'action'     => ['js', 'css', 'printable-css'],
+12
Changes for config/unicaen-app.local.php.dist: 12 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -38,6 +38,18 @@ return array(
            // désactivation totale de l'envoi de mail par l'application
            'do_not_send' => false,
        ),
        /**
         * Mode maintenance (application indisponible)
         */
        'maintenance' => array(
            // activation (true: activé, false: désactivé)
            'enable' => false,
            // liste blanche des adresses IP clientes non concernées
            'white_list' => [
//                ['127.0.0.1'],   // localhost
//                ['10.60.11.88'], // Bertrand
            ],
        ),
    ),
    'asset_manager' => array(
//        'caching' => array( // https://github.com/RWOverdijk/AssetManager/wiki/Caching
+36
Changes for src/UnicaenApp/Controller/ApplicationController.php: 36 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -40,6 +40,35 @@ class ApplicationController extends AbstractActionController
        exit;
    }

//    public function menuSecondaireAction()
//    {
//        $pagesConfig = $this->getServiceLocator()->get('config')['navigation']['default'];
//        $container = new Navigation($pagesConfig);
//
//        $json = $this->params()->fromQuery('route-match');
//        $routeMatchElements = Json::decode($json, Json::TYPE_ARRAY);
//        $routeMatchElements = [
//            '__NAMESPACE__' => 'Application\Controller',
//            'controller' => 'Application\Controller\These',
//            'action' => 'identite',
//            'these' => '28498',
//            '__CONTROLLER__' => 'These',
//            'matchedRouteName' => 'these/identite'
//        ];
//
//        $matchedRouteName = $routeMatchElements['matchedRouteName'];
//        unset($routeMatchElements['matchedRouteName']);
//        $routeMatch = new RouteMatch($routeMatchElements);
//        $routeMatch->setMatchedRouteName($matchedRouteName);
//
//        var_dump($routeMatchElements, $routeMatch);
//
//        return [
//            'routeMatch' => $routeMatch,
//            'container' => $container,
//        ];
//    }

    public function testEnvoiMailAction()
    {
        $app = $this->appInfos()->getNom();
@@ -65,4 +94,11 @@ class ApplicationController extends AbstractActionController

        return $this->redirect()->toRoute('home');
    }

    public function maintenanceAction()
    {
        $this->layout('layout/maintenance');

        return false;
    }
}
 No newline at end of file
+112
Changes for src/UnicaenApp/Mvc/Listener/MaintenanceListener.php: 112 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace UnicaenApp\Mvc\Listener;

use Zend\EventManager\EventManagerInterface;
use Zend\EventManager\ListenerAggregateInterface;
use Zend\EventManager\ListenerAggregateTrait;
use Zend\Http\Response as HttpResponse;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\Router\Http\RouteMatch;
use Zend\Stdlib\ArrayUtils;

class MaintenanceListener implements ListenerAggregateInterface
{
    use ListenerAggregateTrait;

    /**
     * @var array
     */
    protected $config = [
        'enable' => false,
        'white_list' => [
//           ['127.0.0.1'], // localhost
//           ['10.20.30.40'],
        ],
    ];

    /**
     * MaintenanceListener constructor.
     *
     * @param array $config
     */
    public function __construct(array $config = [])
    {
        $this->config = ArrayUtils::merge($this->config, $config);
    }

    /**
     * @return bool
     */
    protected function isMaintenanceMode()
    {
        if (!array_key_exists('enable', $this->config)) {
            return false;
        }

        $enable = in_array($this->config['enable'], [1, true, '1']);
        if (! $enable) {
            return false;
        }

        if (! array_key_exists('white_list', $this->config)) {
            return true;
        }

        $whiteList = $this->config['white_list'];
        $passed = false;
        foreach ($whiteList as $ip) {
            $passed = $ip[0] === $_SERVER['REMOTE_ADDR'];
            if ($passed && isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                $passed = isset($ip[1]) && $ip[1] === $_SERVER['HTTP_X_FORWARDED_FOR'];
            }
            if ($passed) break;
        }
        if ($passed) {
            return false;
        }

        return true;
    }

    /**
     * @param MvcEvent $e
     */
    public function intercept(MvcEvent $e)
    {
        if (! $this->isMaintenanceMode()) {
            return;
        }

        if (php_sapi_name() === 'cli') {
            exit(0);
        }

//        if (in_array($e->getRouteMatch()->getMatchedRouteName(), ['apropos','contact','plan','mentions-legales','il'])) {
//            return;
//        }

        $routeMatch = new RouteMatch([
            'controller' => 'UnicaenApp\Controller\Application',
            'action'     => 'maintenance',
        ], strlen('/maintenance'));
        $routeMatch->setMatchedRouteName('maintenance');

        $e->setRouteMatch($routeMatch);
    }

    /**
     * Attach one or more listeners
     *
     * Implementors may add an optional $priority argument; the EventManager
     * implementation will pass this to the aggregate.
     *
     * @param EventManagerInterface $events
     *
     * @return void
     */
    public function attach(EventManagerInterface $events)
    {
        $events->attach(MvcEvent::EVENT_ROUTE, [$this, 'intercept']);
    }
}
 No newline at end of file
Loading