1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 11:14:03 +00:00

Complying to Laravel maintenance code and parameters

This commit is contained in:
j.stein 2021-12-27 12:11:49 +01:00
parent 829e149e18
commit f430cd7afc
3 changed files with 46 additions and 20 deletions

View file

@ -18,10 +18,11 @@ class Down extends Command
*/
protected $signature = 'tenancy:down
{--time= : The time when the app has been set to maintenance mode}
{--message= : Message to display}
{--redirect= : The path that users should be redirected to}
{--retry= : The number of seconds after which the request may be retried}
{--allowed=* : List of IPs allowed}';
{--refresh= : The number of seconds after which the browser may refresh}
{--secret= : The secret phrase that may be used to bypass maintenance mode}
{--status=503 : The status code that should be used when returning the maintenance mode response}';
/**
* The console command description.
@ -40,10 +41,11 @@ class Down extends Command
tenancy()->runForMultiple($this->option('tenants'), function ($tenant) {
$this->line("Tenant: {$tenant['id']}");
$tenant->putDownForMaintenance([
'time' => $this->option('time'),
'message' => $this->option('message'),
'redirect' => $this->option('redirect'),
'retry' => $this->option('retry'),
'allowed' => $this->option('allowed'),
'refresh' => $this->option('refresh'),
'secret' => $this->option('secret'),
'status' => $this->option('status'),
]);
});

View file

@ -4,18 +4,19 @@ declare(strict_types=1);
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'] ?? [],
]]);
$this->update([
'maintenance_mode' => [
'redirect' => $data['redirect'] ?? null,
'retry' => $data['retry'] ?? null,
'refresh' => $data['refresh'] ?? null,
'secret' => $data['secret'] ?? null,
'status' => $data['status'] ?? 503,
]
]);
}
public function bringUpFromMaintenance()

View file

@ -5,10 +5,9 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Middleware;
use Closure;
use Illuminate\Foundation\Http\Exceptions\MaintenanceModeException;
use Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode;
use Stancl\Tenancy\Exceptions\TenancyNotInitializedException;
use Symfony\Component\HttpFoundation\IpUtils;
use Symfony\Component\HttpKernel\Exception\HttpException;
class CheckTenantForMaintenanceMode extends CheckForMaintenanceMode
{
@ -21,15 +20,39 @@ class CheckTenantForMaintenanceMode extends CheckForMaintenanceMode
if (tenant('maintenance_mode')) {
$data = tenant('maintenance_mode');
if (isset($data['allowed']) && IpUtils::checkIp($request->ip(), (array) $data['allowed'])) {
if (isset($data['secret']) && $request->path() === $data['secret']) {
return $this->bypassResponse($data['secret']);
}
if ($this->hasValidBypassCookie($request, $data) ||
$this->inExceptArray($request)) {
return $next($request);
}
if ($this->inExceptArray($request)) {
return $next($request);
if (isset($data['redirect'])) {
$path = $data['redirect'] === '/'
? $data['redirect']
: trim($data['redirect'], '/');
if ($request->path() !== $path) {
return redirect($path);
}
}
throw new MaintenanceModeException($data['time'], $data['retry'], $data['message']);
if (isset($data['template'])) {
return response(
$data['template'],
(int) $data['status'] ?? 503,
$this->getHeaders($data)
);
}
throw new HttpException(
(int) $data['status'] ?? 503,
'Service Unavailable',
null,
$this->getHeaders($data)
);
}
return $next($request);