Skip to content

Commit

Permalink
[10.x] Add --generate-secret option to Artisan down command. (#49171
Browse files Browse the repository at this point in the history
)

* Add option to Artisan `down` command to generate a secret phrase.

* Revise sentence.

* Use `Str::random()` helper instead.

* Rename `getSecretPhrase()` to `getSecret()`.

* Fix spelling.

* Move import statement.

* formatting

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
jj15asmr and taylorotwell authored Nov 29, 2023
1 parent d80af24 commit 3d2c03d
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/Illuminate/Foundation/Console/DownCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Console\Command;
use Illuminate\Foundation\Events\MaintenanceModeEnabled;
use Illuminate\Foundation\Exceptions\RegisterErrorViewPaths;
use Illuminate\Support\Str;
use Symfony\Component\Console\Attribute\AsCommand;
use Throwable;

Expand All @@ -23,6 +24,7 @@ class DownCommand extends Command
{--retry= : The number of seconds after which the request may be retried}
{--refresh= : The number of seconds after which the browser may refresh}
{--secret= : The secret phrase that may be used to bypass maintenance mode}
{--with-secret : Generate a random 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}';

/**
Expand All @@ -46,7 +48,9 @@ public function handle()
return 0;
}

$this->laravel->maintenanceMode()->activate($this->getDownFilePayload());
$downFilePayload = $this->getDownFilePayload();

$this->laravel->maintenanceMode()->activate($downFilePayload);

file_put_contents(
storage_path('framework/maintenance.php'),
Expand All @@ -56,6 +60,10 @@ public function handle()
$this->laravel->get('events')->dispatch(new MaintenanceModeEnabled());

$this->components->info('Application is now in maintenance mode.');

if ($downFilePayload['secret'] !== null) {
$this->components->info("You may bypass maintenance mode via [".config('app.url')."/{$downFilePayload['secret']}].");
}
} catch (Exception $e) {
$this->components->error(sprintf(
'Failed to enter maintenance mode: %s.',
Expand All @@ -78,7 +86,7 @@ protected function getDownFilePayload()
'redirect' => $this->redirectPath(),
'retry' => $this->getRetryTime(),
'refresh' => $this->option('refresh'),
'secret' => $this->option('secret'),
'secret' => $this->getSecret(),
'status' => (int) $this->option('status', 503),
'template' => $this->option('render') ? $this->prerenderView() : null,
];
Expand Down Expand Up @@ -137,4 +145,18 @@ protected function getRetryTime()

return is_numeric($retry) && $retry > 0 ? (int) $retry : null;
}

/**
* Get the secret phrase that may be used to bypass maintenance mode.
*
* @return string|null
*/
protected function getSecret()
{
return match (true) {
! is_null($this->option('secret')) => $this->option('secret'),
$this->option('with-secret') => Str::random(),
default => null,
};
}
}

0 comments on commit 3d2c03d

Please sign in to comment.