mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 16:54:05 +00:00
Complying to Laravel maintenance code and parameters
This commit is contained in:
parent
829e149e18
commit
f430cd7afc
3 changed files with 46 additions and 20 deletions
|
|
@ -18,10 +18,11 @@ class Down extends Command
|
||||||
*/
|
*/
|
||||||
|
|
||||||
protected $signature = 'tenancy:down
|
protected $signature = 'tenancy:down
|
||||||
{--time= : The time when the app has been set to maintenance mode}
|
{--redirect= : The path that users should be redirected to}
|
||||||
{--message= : Message to display}
|
|
||||||
{--retry= : The number of seconds after which the request may be retried}
|
{--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.
|
* The console command description.
|
||||||
|
|
@ -40,10 +41,11 @@ class Down extends Command
|
||||||
tenancy()->runForMultiple($this->option('tenants'), function ($tenant) {
|
tenancy()->runForMultiple($this->option('tenants'), function ($tenant) {
|
||||||
$this->line("Tenant: {$tenant['id']}");
|
$this->line("Tenant: {$tenant['id']}");
|
||||||
$tenant->putDownForMaintenance([
|
$tenant->putDownForMaintenance([
|
||||||
'time' => $this->option('time'),
|
'redirect' => $this->option('redirect'),
|
||||||
'message' => $this->option('message'),
|
|
||||||
'retry' => $this->option('retry'),
|
'retry' => $this->option('retry'),
|
||||||
'allowed' => $this->option('allowed'),
|
'refresh' => $this->option('refresh'),
|
||||||
|
'secret' => $this->option('secret'),
|
||||||
|
'status' => $this->option('status'),
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,18 +4,19 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Database\Concerns;
|
namespace Stancl\Tenancy\Database\Concerns;
|
||||||
|
|
||||||
use Carbon\Carbon;
|
|
||||||
|
|
||||||
trait MaintenanceMode
|
trait MaintenanceMode
|
||||||
{
|
{
|
||||||
public function putDownForMaintenance($data = [])
|
public function putDownForMaintenance($data = [])
|
||||||
{
|
{
|
||||||
$this->update(['maintenance_mode' => [
|
$this->update([
|
||||||
'time' => $data['time'] ?? Carbon::now()->getTimestamp(),
|
'maintenance_mode' => [
|
||||||
'message' => $data['message'] ?? null,
|
'redirect' => $data['redirect'] ?? null,
|
||||||
'retry' => $data['retry'] ?? null,
|
'retry' => $data['retry'] ?? null,
|
||||||
'allowed' => $data['allowed'] ?? [],
|
'refresh' => $data['refresh'] ?? null,
|
||||||
]]);
|
'secret' => $data['secret'] ?? null,
|
||||||
|
'status' => $data['status'] ?? 503,
|
||||||
|
]
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function bringUpFromMaintenance()
|
public function bringUpFromMaintenance()
|
||||||
|
|
|
||||||
|
|
@ -5,10 +5,9 @@ declare(strict_types=1);
|
||||||
namespace Stancl\Tenancy\Middleware;
|
namespace Stancl\Tenancy\Middleware;
|
||||||
|
|
||||||
use Closure;
|
use Closure;
|
||||||
use Illuminate\Foundation\Http\Exceptions\MaintenanceModeException;
|
|
||||||
use Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode;
|
use Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode;
|
||||||
use Stancl\Tenancy\Exceptions\TenancyNotInitializedException;
|
use Stancl\Tenancy\Exceptions\TenancyNotInitializedException;
|
||||||
use Symfony\Component\HttpFoundation\IpUtils;
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||||
|
|
||||||
class CheckTenantForMaintenanceMode extends CheckForMaintenanceMode
|
class CheckTenantForMaintenanceMode extends CheckForMaintenanceMode
|
||||||
{
|
{
|
||||||
|
|
@ -21,15 +20,39 @@ class CheckTenantForMaintenanceMode extends CheckForMaintenanceMode
|
||||||
if (tenant('maintenance_mode')) {
|
if (tenant('maintenance_mode')) {
|
||||||
$data = 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);
|
return $next($request);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->inExceptArray($request)) {
|
if (isset($data['redirect'])) {
|
||||||
return $next($request);
|
$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);
|
return $next($request);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue