Compare commits

..

4 commits

14 changed files with 286 additions and 0 deletions

View file

@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
namespace DigiComp\FlowMaintenanceMode\AllowModules;
use Psr\Http\Message\ServerRequestInterface;
interface AllowModuleInterface
{
public function isRequestAllowed(ServerRequestInterface $request): bool;
}

View file

@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace DigiComp\FlowMaintenanceMode\AllowModules;
use Psr\Http\Message\ServerRequestInterface;
class ByCookieAllowModule implements AllowModuleInterface
{
protected ?string $cookieName;
protected ?string $cookieValue;
/**
* @param string|null $cookieName
* @param string|null $cookieValue
*/
public function __construct(?string $cookieName, ?string $cookieValue)
{
$this->cookieName = $cookieName;
$this->cookieValue = $cookieValue;
}
public function isRequestAllowed(ServerRequestInterface $request): bool
{
$cookies = $request->getCookieParams();
if ($this->cookieName === null || !isset($cookies[$this->cookieName])) {
return false;
}
if ($this->cookieValue !== null && $cookies[$this->cookieName] !== $this->cookieValue) {
return false;
}
return true;
}
}

View file

@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace DigiComp\FlowMaintenanceMode;
use Neos\Flow\Annotations as Flow;
class DistributionFileMaintenanceModeManager implements MaintenanceModeManagerInterface
{
#[Flow\InjectConfiguration(path: 'maintenanceManager.pathToCheck')]
protected string $pathToCheck;
public function isMaintenanceMode(): bool
{
if (\file_exists($this->pathToCheck)) {
return true;
}
return false;
}
}

9
Classes/Exception.php Normal file
View file

@ -0,0 +1,9 @@
<?php
declare(strict_types=1);
namespace DigiComp\FlowMaintenanceMode;
class Exception extends \Neos\Flow\Exception
{
}

View file

@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
namespace DigiComp\FlowMaintenanceMode\Http;
use DigiComp\FlowMaintenanceMode\AllowModules\AllowModuleInterface;
use DigiComp\FlowMaintenanceMode\MaintenanceModeActiveException;
use DigiComp\FlowMaintenanceMode\MaintenanceModeManagerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
class MaintenanceModeMiddleware implements MiddlewareInterface
{
protected MaintenanceModeManagerInterface $maintenanceModeManager;
/**
* @var array<array-key, AllowModuleInterface>
*/
protected array $allowModules;
public function __construct(
MaintenanceModeManagerInterface $maintenanceModeManager,
AllowModuleInterface ...$allowModules
) {
$this->maintenanceModeManager = $maintenanceModeManager;
$this->allowModules = $allowModules;
}
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
if ($this->maintenanceModeManager->isMaintenanceMode()) {
foreach ($this->allowModules as $allowModule) {
if ($allowModule->isRequestAllowed($request)) {
return $handler->handle($request);
}
}
throw new MaintenanceModeActiveException('Maintenance is active');
}
return $handler->handle($request);
}
}

View file

@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace DigiComp\FlowMaintenanceMode\Http;
use DigiComp\FlowMaintenanceMode\MaintenanceModeManagerInterface;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\ObjectManagement\ObjectManagerInterface;
use Neos\Utility\PositionalArraySorter;
class MaintenanceModeMiddlewareFactory
{
protected ObjectManagerInterface $objectManager;
#[Flow\InjectConfiguration(path: 'allowModules')]
protected array $allowModuleNames;
/**
* @param ObjectManagerInterface $objectManager
*/
public function __construct(ObjectManagerInterface $objectManager)
{
$this->objectManager = $objectManager;
}
public function create(): MaintenanceModeMiddleware
{
$allowModules = [];
$sorter = new PositionalArraySorter($this->allowModuleNames);
foreach ($sorter->getSortedKeys() as $moduleName) {
$allowModules[] = $this->objectManager->get($moduleName);
}
return new MaintenanceModeMiddleware(
$this->objectManager->get(MaintenanceModeManagerInterface::class),
...$allowModules
);
}
}

View file

@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
namespace DigiComp\FlowMaintenanceMode;
class MaintenanceModeActiveException extends Exception
{
protected $statusCode = 503;
}

View file

@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
namespace DigiComp\FlowMaintenanceMode;
interface MaintenanceModeManagerInterface
{
public function isMaintenanceMode(): bool;
}

View file

@ -0,0 +1,11 @@
DigiComp.FlowMaintenance:AllowByCookieModule:
className: DigiComp\FlowMaintenanceMode\AllowModules\ByCookieAllowModule
arguments:
1:
setting: DigiComp.FlowMaintenanceMode.allowCookie.name
2:
setting: DigiComp.FlowMaintenanceMode.allowCookie.value
DigiComp.FlowMaintenance:MaintenanceModeMiddleware:
className: DigiComp\FlowMaintenanceMode\Http\MaintenanceModeMiddleware
factoryObjectName: DigiComp\FlowMaintenanceMode\Http\MaintenanceModeMiddlewareFactory

View file

@ -0,0 +1,20 @@
Neos:
Flow:
http:
middlewares:
maintenanceMode:
position: "after trustedProxies"
middleware: "DigiComp.FlowMaintenance:MaintenanceModeMiddleware"
error:
exceptionHandler:
renderingGroups:
maintenanceErrors:
matchingStatusCodes:
- 503
options:
error:
options:
logException: false
templatePathAndFilename: "resource://Neos.Flow/Private/Templates/Error/Default.html"
variables:
errorDescription: "The system is currently under maintenance. Try again later."

View file

@ -0,0 +1,10 @@
DigiComp:
FlowMaintenanceMode:
allowCookie:
name: ~
value: ~
allowModules:
DigiComp.FlowMaintenance:AllowByCookieModule:
position: start
maintenanceManager:
pathToCheck: "%FLOW_PATH_ROOT%/MAINTENANCE"

19
LICENSE Normal file
View file

@ -0,0 +1,19 @@
Copyright (c) 2024 Ferdinand Kuhl <f.kuhl@digital-competence.de>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

19
README.md Normal file
View file

@ -0,0 +1,19 @@
# DigiComp.FlowMaintenanceMode
This package assists in presenting each visitor of your system a page showing working on maintenance.
It allows you to use usual error page rendering for that purpose - the idea is, that those layouts, should exist anyway.
If you want to bypass the maintenance mode, you can do so by configuring a cookie based exception:
```yaml
DigiComp:
FlowMaintenanceMode:
allowCookie:
name: name
value: value
```
That would allow you to bypass the maintenance answer, if you had a cookie named 'name' with a value of 'value'. If you set 'value' to `null` only the name is matched.
You can extend the exception behavior, by implementing the `AllowModuleInterface` and add it to `DigiComp.FlowMaintenanceMode.allowModules`. This list is handled by `PositionalArraySorter`. You can use position keys to order them, or set their value to null to exclude them from being used.

26
composer.json Normal file
View file

@ -0,0 +1,26 @@
{
"name": "digicomp/flow-maintenance-mode",
"description": "Maintenance mode for Flow based systems",
"type": "neos-package",
"require": {
"neos/flow": "^7.3",
"php": "^8.1"
},
"autoload": {
"psr-4": {
"DigiComp\\FlowMaintenanceMode\\": "Classes"
}
},
"homepage": "https://git.digital-competence.de/Packages/DigiComp.FlowMaintenanceMode",
"license": "MIT",
"extra": {
"neos": {
"package-key": "DigiComp.FlowMaintenanceMode"
},
"branch-alias": {
"dev-develop": "1.0.x-dev"
},
"applied-flow-migrations": [
]
}
}