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