mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 17:44:04 +00:00
Tenant-specific maintenance mode
This commit is contained in:
parent
ac58f6992b
commit
2fedd5ce88
3 changed files with 96 additions and 0 deletions
18
src/Database/Concerns/MaintenanceMode.php
Normal file
18
src/Database/Concerns/MaintenanceMode.php
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Database\Concerns;
|
||||
|
||||
use Carbon\Carbon;
|
||||
|
||||
trait MaintenanceMode
|
||||
{
|
||||
public function putDownForMaintenance($data = [])
|
||||
{
|
||||
$this->update(['maintenance_mode' => [
|
||||
'time' => $data['time'] ?? Carbon::now()->getTimestamp(),
|
||||
'message' => $data['message'] ?? null,
|
||||
'retry' => $data['retry'] ?? null,
|
||||
'allowed' => $data['allowed'] ?? [],
|
||||
]]);
|
||||
}
|
||||
}
|
||||
36
src/Middleware/CheckTenantForMaintenanceMode.php
Normal file
36
src/Middleware/CheckTenantForMaintenanceMode.php
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Middleware;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Foundation\Http\Exceptions\MaintenanceModeException;
|
||||
use Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode;
|
||||
use Illuminate\Http\Request;
|
||||
use Stancl\Tenancy\Exceptions\TenancyNotInitializedException;
|
||||
use Symfony\Component\HttpFoundation\IpUtils;
|
||||
|
||||
class CheckTenantForMaintenanceMode extends CheckForMaintenanceMode
|
||||
{
|
||||
public function handle(Request $request, Closure $next)
|
||||
{
|
||||
if (! tenant()) {
|
||||
throw new TenancyNotInitializedException;
|
||||
}
|
||||
|
||||
if (tenant('maintenance_mode')) {
|
||||
$data = tenant('maintenance_mode');
|
||||
|
||||
if (isset($data['allowed']) && IpUtils::checkIp($request->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);
|
||||
}
|
||||
}
|
||||
42
tests/MaintenanceModeTest.php
Normal file
42
tests/MaintenanceModeTest.php
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Tests;
|
||||
|
||||
use Illuminate\Foundation\Http\Exceptions\MaintenanceModeException;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Stancl\Tenancy\Database\Concerns\MaintenanceMode;
|
||||
use Stancl\Tenancy\Middleware\CheckTenantForMaintenanceMode;
|
||||
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
|
||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||
|
||||
class MaintenanceModeTest extends TestCase
|
||||
{
|
||||
/** @test */
|
||||
public function tenant_can_be_in_maintenance_mode()
|
||||
{
|
||||
Route::get('/foo', function () {
|
||||
return 'bar';
|
||||
})->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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue