feat: add webshop offline maintenance mode with admin toggle and red maintenance screen
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use App\Models\Setting;
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class CheckWebshopOffline
|
||||
{
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param Closure(Request): (Response) $next
|
||||
*/
|
||||
public function handle(Request $request, Closure $next): Response
|
||||
{
|
||||
// If the webshop is not offline, continue normally
|
||||
if (Setting::get('shop_offline', '0') !== '1') {
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
// If the user is authenticated as an admin, allow full access
|
||||
if ($request->user() && $request->user()->isAdmin()) {
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
// Keep admin, auth, health check and asset routes accessible
|
||||
if ($this->isExemptRoute($request)) {
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
// Otherwise, render the large red offline maintenance screen
|
||||
$title = Setting::get('shop_offline_title', 'Aan deze webshop wordt hard gewerkt! 🛠️');
|
||||
$message = Setting::get('shop_offline_message', 'Onze 3D printers worden momenteel opnieuw gekalibreerd en we bouwen aan nieuwe toffe prints en functies. We zijn heel snel weer online!');
|
||||
|
||||
return response()->view('errors.offline', [
|
||||
'offlineTitle' => $title,
|
||||
'offlineMessage' => $message,
|
||||
], 503);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the current route is exempt from offline mode.
|
||||
*/
|
||||
protected function isExemptRoute(Request $request): bool
|
||||
{
|
||||
$exemptPatterns = [
|
||||
'admin',
|
||||
'admin/*',
|
||||
'inloggen',
|
||||
'login',
|
||||
'uitloggen',
|
||||
'logout',
|
||||
'registreren',
|
||||
'register',
|
||||
'wachtwoord-*',
|
||||
'up',
|
||||
'health-debug',
|
||||
'images/*',
|
||||
'storage/*',
|
||||
'build/*',
|
||||
'favicon.ico',
|
||||
];
|
||||
|
||||
foreach ($exemptPatterns as $pattern) {
|
||||
if ($request->is($pattern)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user