From 2fedd5ce88e8eac4c4277e058b7f773992896d87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Wed, 20 May 2020 21:50:10 +0200 Subject: [PATCH] Tenant-specific maintenance mode --- src/Database/Concerns/MaintenanceMode.php | 18 ++++++++ .../CheckTenantForMaintenanceMode.php | 36 ++++++++++++++++ tests/MaintenanceModeTest.php | 42 +++++++++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 src/Database/Concerns/MaintenanceMode.php create mode 100644 src/Middleware/CheckTenantForMaintenanceMode.php create mode 100644 tests/MaintenanceModeTest.php diff --git a/src/Database/Concerns/MaintenanceMode.php b/src/Database/Concerns/MaintenanceMode.php new file mode 100644 index 00000000..f3f3f339 --- /dev/null +++ b/src/Database/Concerns/MaintenanceMode.php @@ -0,0 +1,18 @@ +update(['maintenance_mode' => [ + 'time' => $data['time'] ?? Carbon::now()->getTimestamp(), + 'message' => $data['message'] ?? null, + 'retry' => $data['retry'] ?? null, + 'allowed' => $data['allowed'] ?? [], + ]]); + } +} \ No newline at end of file diff --git a/src/Middleware/CheckTenantForMaintenanceMode.php b/src/Middleware/CheckTenantForMaintenanceMode.php new file mode 100644 index 00000000..1fccdc9d --- /dev/null +++ b/src/Middleware/CheckTenantForMaintenanceMode.php @@ -0,0 +1,36 @@ +ip(), (array) $data['allowed'])) { + return $next($request); + } + + if ($this->inExceptArray($request)) { + return $next($request); + } + + throw new MaintenanceModeException($data['time'], $data['retry'], $data['message']); + } + + return $next($request); + } +} \ No newline at end of file diff --git a/tests/MaintenanceModeTest.php b/tests/MaintenanceModeTest.php new file mode 100644 index 00000000..cd962c00 --- /dev/null +++ b/tests/MaintenanceModeTest.php @@ -0,0 +1,42 @@ +middleware([InitializeTenancyByDomain::class, CheckTenantForMaintenanceMode::class]); + + $tenant = MaintenanceTenant::create(); + $tenant->domains()->create([ + 'domain' => 'acme.localhost', + ]); + + $this->get('http://acme.localhost/foo') + ->assertSuccessful(); + + tenancy()->end(); // flush stored tenant instance + + $tenant->putDownForMaintenance(); + + $this->expectException(MaintenanceModeException::class); + $this->withoutExceptionHandling() + ->get('http://acme.localhost/foo'); + } +} + +class MaintenanceTenant extends Tenant +{ + use MaintenanceMode; +} \ No newline at end of file