Skip to content

Commit

Permalink
Merge pull request #80 from okazy/feature/traverse-maintenance
Browse files Browse the repository at this point in the history
Feature/traverse maintenance
  • Loading branch information
okazy authored Apr 5, 2021
2 parents 33e70c1 + 28bf606 commit e203454
Show file tree
Hide file tree
Showing 16 changed files with 248 additions and 47 deletions.
1 change: 1 addition & 0 deletions app/config/eccube/packages/security.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ security:
logout:
path: admin_logout
target: admin_login
success_handler: eccube.security.logout.success_handler
customer:
pattern: ^/
anonymous: true
Expand Down
3 changes: 3 additions & 0 deletions app/config/eccube/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ services:
eccube.security.failure_handler:
class: Eccube\Security\Http\Authentication\EccubeAuthenticationFailureHandler

eccube.security.logout.success_handler:
class: Eccube\Security\Http\Authentication\EccubeLogoutSuccessHandler

# Autowiring can't guess the constructor arguments that are not type-hinted with
# classes (e.g. container parameters) so you must define those arguments explicitly
# Eccube\Command\ListUsersCommand:
Expand Down
22 changes: 22 additions & 0 deletions html/template/default/assets/css/style.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion html/template/default/assets/css/style.css.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion html/template/default/assets/css/style.min.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion html/template/default/assets/css/style.min.css.map

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions html/template/default/assets/scss/project/_11.2.header.scss
Original file line number Diff line number Diff line change
Expand Up @@ -826,3 +826,30 @@ Styleguide 11.2.7
.ec-itemNavAccordion {
display: none;
}

.ec-maintenanceAlert {
background: steelblue;
height: 5rem;
position: fixed;
top: 0;
width: 100%;
color: white;
z-index: 9999;
display: flex;
font-weight: bold;
& > * {
margin: auto;
}
& &__icon {
display: inline-block;
margin-right: 1rem;
width: 20px;
height: 20px;
color: #fff;
fill: #fff;
vertical-align: top;
}
& +* {
margin-top: 5rem;
}
}
20 changes: 13 additions & 7 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Eccube\Kernel;
use Eccube\Service\SystemService;
use Symfony\Component\Debug\Debug;
use Dotenv\Dotenv;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -62,13 +63,18 @@
$adminPath = env('ECCUBE_ADMIN_ROUTE', 'admin');
$adminPath = '/'.\trim($adminPath, '/').'/';
if (\strpos($pathInfo, $adminPath) !== 0) {
$locale = env('ECCUBE_LOCALE');
$templateCode = env('ECCUBE_TEMPLATE_CODE');
$baseUrl = \htmlspecialchars(\rawurldecode($request->getBaseUrl()), ENT_QUOTES);

header('HTTP/1.1 503 Service Temporarily Unavailable');
require __DIR__.'/maintenance.php';
return;
$maintenanceContents = file_get_contents($maintenanceFile);
$maintenanceToken = explode(':', $maintenanceContents)[1] ?? null;
$tokenInCookie = $request->cookies->get(SystemService::MAINTENANCE_TOKEN_KEY);
if ($tokenInCookie === null || $tokenInCookie !== $maintenanceToken) {
$locale = env('ECCUBE_LOCALE');
$templateCode = env('ECCUBE_TEMPLATE_CODE');
$baseUrl = \htmlspecialchars(\rawurldecode($request->getBaseUrl()), ENT_QUOTES);

header('HTTP/1.1 503 Service Temporarily Unavailable');
require __DIR__.'/maintenance.php';
return;
}
}
}

Expand Down
6 changes: 2 additions & 4 deletions src/Eccube/Controller/Admin/Content/MaintenanceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,16 @@ public function index(Request $request)

if ($form->isSubmitted() && $form->isValid()) {
$changeTo = $request->request->get('maintenance');
$path = $this->container->getParameter('eccube_content_maintenance_file_path');

if ($isMaintenance === false && $changeTo == 'on') {
// 現在メンテナンスモードではない かつ メンテナンスモードを有効 にした場合
// メンテナンスモードを有効にする
file_put_contents($path, null);

$this->systemService->enableMaintenance('', true);
$this->addSuccess('admin.content.maintenance_switch__on_message', 'admin');
} elseif ($isMaintenance && $changeTo == 'off') {
// 現在メンテナンスモード かつ メンテナンスモードを無効 にした場合
// メンテナンスモードを無効にする
unlink($path);
$this->systemService->disableMaintenanceNow('', true);

$this->addSuccess('admin.content.maintenance_switch__off_message', 'admin');
}
Expand Down
64 changes: 64 additions & 0 deletions src/Eccube/EventListener/MaintenanceListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Eccube\EventListener;

use Eccube\Entity;
use Eccube\Request\Context;
use Eccube\Service\SystemService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;

class MaintenanceListener implements EventSubscriberInterface
{

/** @var Context */
protected $requestContext;

/** @var SystemService */
protected $systemService;

public function __construct(Context $requestContext, SystemService $systemService)
{
$this->requestContext = $requestContext;
$this->systemService = $systemService;
}

public static function getSubscribedEvents()
{
return [
KernelEvents::RESPONSE => ['onResponse'],
];
}

public function onResponse(FilterResponseEvent $event)
{
$response = $event->getResponse();

if (!$this->systemService->isMaintenanceMode()) {
$response->headers->clearCookie(SystemService::MAINTENANCE_TOKEN_KEY);
return;
}

$user = $this->requestContext->getCurrentUser();
if ($user instanceof Entity\Member && $this->requestContext->isAdmin()) {
$cookie = new Cookie(
SystemService::MAINTENANCE_TOKEN_KEY,
$this->systemService->getMaintenanceToken()
);
$response->headers->setCookie($cookie);
}
}
}
35 changes: 12 additions & 23 deletions src/Eccube/EventListener/TwigInitializeListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@
use Eccube\Entity\PageLayout;
use Eccube\Repository\AuthorityRoleRepository;
use Eccube\Repository\BaseInfoRepository;
use Eccube\Repository\BlockPositionRepository;
use Eccube\Repository\LayoutRepository;
use Eccube\Repository\Master\DeviceTypeRepository;
use Eccube\Repository\PageRepository;
use Eccube\Repository\PageLayoutRepository;
use Eccube\Repository\BlockPositionRepository;
use Eccube\Repository\PageRepository;
use Eccube\Request\Context;
use Eccube\Service\SystemService;
use SunCat\MobileDetectBundle\DeviceDetector\MobileDetector;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
Expand Down Expand Up @@ -103,21 +104,13 @@ class TwigInitializeListener implements EventSubscriberInterface
*/
private $layoutRepository;

/**
* @var SystemService
*/
protected $systemService;

/**
* TwigInitializeListener constructor.
*
* @param Environment $twig
* @param BaseInfoRepository $baseInfoRepository
* @param PageRepository $pageRepository
* @param PageLayoutRepository $pageLayoutRepository
* @param BlockPositionRepository $blockPositionRepository
* @param DeviceTypeRepository $deviceTypeRepository
* @param AuthorityRoleRepository $authorityRoleRepository
* @param EccubeConfig $eccubeConfig
* @param Context $context
* @param MobileDetector $mobileDetector
* @param UrlGeneratorInterface $router
* @param LayoutRepository $layoutRepository
*/
public function __construct(
Environment $twig,
Expand All @@ -131,7 +124,8 @@ public function __construct(
Context $context,
MobileDetector $mobileDetector,
UrlGeneratorInterface $router,
LayoutRepository $layoutRepository
LayoutRepository $layoutRepository,
SystemService $systemService
) {
$this->twig = $twig;
$this->baseInfoRepository = $baseInfoRepository;
Expand All @@ -145,11 +139,10 @@ public function __construct(
$this->mobileDetector = $mobileDetector;
$this->router = $router;
$this->layoutRepository = $layoutRepository;
$this->systemService = $systemService;
}

/**
* @param GetResponseEvent $event
*
* @throws NoResultException
* @throws \Doctrine\ORM\NonUniqueResultException
*/
Expand All @@ -171,8 +164,6 @@ public function onKernelRequest(GetResponseEvent $event)
}

/**
* @param GetResponseEvent $event
*
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function setFrontVariables(GetResponseEvent $event)
Expand Down Expand Up @@ -243,11 +234,9 @@ public function setFrontVariables(GetResponseEvent $event)
$this->twig->addGlobal('Layout', $Layout);
$this->twig->addGlobal('Page', $Page);
$this->twig->addGlobal('title', $Page->getName());
$this->twig->addGlobal('isMaintenance', $this->systemService->isMaintenanceMode());
}

/**
* @param GetResponseEvent $event
*/
public function setAdminGlobals(GetResponseEvent $event)
{
// メニュー表示用配列.
Expand Down
1 change: 1 addition & 0 deletions src/Eccube/Resource/locale/messages.en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ front.shopping.payment_total_invalid: The total amount is invalid.
front.shopping.different_payment_methods: Sorry, your order includes items with different purchase methods, which are unable to be processed in one order.
front.shopping.payment_method_unselected: Please select the payment method.
front.shopping.payment_method_not_fount: Sorry, you have no payment options. If you have selected multiple delivery methods, you can only select the same payment option for them all.
front.under_maintenance: The site is currently under maintenance.

#====================================================================================
# Admin Console
Expand Down
1 change: 1 addition & 0 deletions src/Eccube/Resource/locale/messages.ja.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ front.shopping.payment_total_invalid: 合計金額が不正です。
front.shopping.different_payment_methods: 支払い方法が異なる商品が含まれているため、同時に購入することはできません。
front.shopping.payment_method_unselected: お支払い方法を選択してください。
front.shopping.payment_method_not_fount: 選択できるお支払い方法がありません。配送方法が異なる場合は同じ配送方法を選んでください。
front.under_maintenance: メンテナンスモードが有効になっています。

#====================================================================================
# 管理画面
Expand Down
9 changes: 9 additions & 0 deletions src/Eccube/Resource/template/default/default_frame.twig
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ file that was distributed with this source code.
{{ include('block.twig', {'Blocks': Layout.BodyAfter}) }}
{% endif %}

{% if isMaintenance %}
<div class="ec-maintenanceAlert">
<div>
<div class="ec-maintenanceAlert__icon"><img src="{{ asset('assets/icon/exclamation-white.svg') }}"/></div>
{{ 'front.under_maintenance'|trans }}
</div>
</div>
{% endif %}

<div class="ec-layoutRole">
{# Layout: HEADER #}
{% if Layout.Header %}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Eccube\Security\Http\Authentication;

use Eccube\Request\Context;
use Eccube\Service\SystemService;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Http\HttpUtils;
use Symfony\Component\Security\Http\Logout\DefaultLogoutSuccessHandler;

class EccubeLogoutSuccessHandler extends DefaultLogoutSuccessHandler
{
/** @var Context */
protected $context;

public function __construct(HttpUtils $httpUtils, Context $context, $targetUrl = 'admin_login')
{
parent::__construct($httpUtils, $targetUrl);
$this->context = $context;
}

public function onLogoutSuccess(Request $request)
{
$response = parent::onLogoutSuccess($request);

if ($this->context->isAdmin()) {
$response->headers->clearCookie(SystemService::MAINTENANCE_TOKEN_KEY);
}

return $response;
}
}
Loading

0 comments on commit e203454

Please sign in to comment.