From 831b715608cff89ebc4aeda987fec1de85379140 Mon Sep 17 00:00:00 2001 From: Ferdinand Kuhl Date: Thu, 28 Nov 2024 17:05:22 +0100 Subject: [PATCH 1/4] implementing maintenance mode --- Classes/AllowModules/AllowModuleInterface.php | 12 +++++ Classes/AllowModules/ByCookieAllowModule.php | 35 +++++++++++++++ Classes/Exception.php | 9 ++++ Classes/Http/MaintenanceModeMiddleware.php | 44 +++++++++++++++++++ .../Http/MaintenanceModeMiddlewareFactory.php | 40 +++++++++++++++++ Classes/MaintenanceModeActiveException.php | 10 +++++ ...MaintenanceModeDistributionFileManager.php | 16 +++++++ Classes/MaintenanceModeManagerInterface.php | 10 +++++ Configuration/Objects.yaml | 11 +++++ Configuration/Settings.Flow.yaml | 20 +++++++++ Configuration/Settings.Main.yaml | 8 ++++ composer.json | 21 +++++++++ 12 files changed, 236 insertions(+) create mode 100644 Classes/AllowModules/AllowModuleInterface.php create mode 100644 Classes/AllowModules/ByCookieAllowModule.php create mode 100644 Classes/Exception.php create mode 100644 Classes/Http/MaintenanceModeMiddleware.php create mode 100644 Classes/Http/MaintenanceModeMiddlewareFactory.php create mode 100644 Classes/MaintenanceModeActiveException.php create mode 100644 Classes/MaintenanceModeDistributionFileManager.php create mode 100644 Classes/MaintenanceModeManagerInterface.php create mode 100644 Configuration/Objects.yaml create mode 100644 Configuration/Settings.Flow.yaml create mode 100644 Configuration/Settings.Main.yaml create mode 100644 composer.json diff --git a/Classes/AllowModules/AllowModuleInterface.php b/Classes/AllowModules/AllowModuleInterface.php new file mode 100644 index 0000000..959d323 --- /dev/null +++ b/Classes/AllowModules/AllowModuleInterface.php @@ -0,0 +1,12 @@ +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; + } +} diff --git a/Classes/Exception.php b/Classes/Exception.php new file mode 100644 index 0000000..cd10935 --- /dev/null +++ b/Classes/Exception.php @@ -0,0 +1,9 @@ + + */ + 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); + } +} diff --git a/Classes/Http/MaintenanceModeMiddlewareFactory.php b/Classes/Http/MaintenanceModeMiddlewareFactory.php new file mode 100644 index 0000000..cc52efb --- /dev/null +++ b/Classes/Http/MaintenanceModeMiddlewareFactory.php @@ -0,0 +1,40 @@ +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 + ); + } +} diff --git a/Classes/MaintenanceModeActiveException.php b/Classes/MaintenanceModeActiveException.php new file mode 100644 index 0000000..48b1824 --- /dev/null +++ b/Classes/MaintenanceModeActiveException.php @@ -0,0 +1,10 @@ + Date: Thu, 28 Nov 2024 17:25:02 +0100 Subject: [PATCH 2/4] adding some information and legal stuff --- LICENSE | 19 +++++++++++++++++++ README.md | 19 +++++++++++++++++++ composer.json | 5 +++++ 3 files changed, 43 insertions(+) create mode 100644 LICENSE create mode 100644 README.md diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..7c439ae --- /dev/null +++ b/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2024 Ferdinand Kuhl + +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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..b91cdcf --- /dev/null +++ b/README.md @@ -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 handled by positional array sorter. You can use position keys to order them, or set their value to null to exclude them from being used. diff --git a/composer.json b/composer.json index 4e92536..aa57ae7 100644 --- a/composer.json +++ b/composer.json @@ -11,10 +11,15 @@ "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": [ ] } -- 2.48.1 From 61a604567dced3b4a00e142625139e37ce1df47d Mon Sep 17 00:00:00 2001 From: Ferdinand Kuhl Date: Thu, 28 Nov 2024 17:30:16 +0100 Subject: [PATCH 3/4] minor description improvement --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b91cdcf..22bd216 100644 --- a/README.md +++ b/README.md @@ -16,4 +16,4 @@ DigiComp: 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 handled by positional array sorter. You can use position keys to order them, or set their value to null to exclude them from being used. +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. -- 2.48.1 From 6b964683497b79997a4324857f956553c5577d54 Mon Sep 17 00:00:00 2001 From: Ferdinand Kuhl Date: Tue, 10 Dec 2024 17:33:39 +0100 Subject: [PATCH 4/4] made the maintenance marking file configurable and changed its default to be visible --- ...DistributionFileMaintenanceModeManager.php | 21 +++++++++++++++++++ ...MaintenanceModeDistributionFileManager.php | 16 -------------- Configuration/Settings.Main.yaml | 2 ++ 3 files changed, 23 insertions(+), 16 deletions(-) create mode 100644 Classes/DistributionFileMaintenanceModeManager.php delete mode 100644 Classes/MaintenanceModeDistributionFileManager.php diff --git a/Classes/DistributionFileMaintenanceModeManager.php b/Classes/DistributionFileMaintenanceModeManager.php new file mode 100644 index 0000000..c813073 --- /dev/null +++ b/Classes/DistributionFileMaintenanceModeManager.php @@ -0,0 +1,21 @@ +pathToCheck)) { + return true; + } + return false; + } +} diff --git a/Classes/MaintenanceModeDistributionFileManager.php b/Classes/MaintenanceModeDistributionFileManager.php deleted file mode 100644 index b38af1a..0000000 --- a/Classes/MaintenanceModeDistributionFileManager.php +++ /dev/null @@ -1,16 +0,0 @@ -