feat: add webshop offline maintenance mode with admin toggle and red maintenance screen
This commit is contained in:
@@ -85,6 +85,10 @@ class ContentController extends Controller
|
||||
'footer_shipping_text',
|
||||
'footer_copyright',
|
||||
'footer_payment_text',
|
||||
|
||||
// Offline / Maintenance Mode
|
||||
'shop_offline_title',
|
||||
'shop_offline_message',
|
||||
];
|
||||
|
||||
// Handle checkbox toggles that aren't present if unchecked
|
||||
@@ -92,6 +96,10 @@ class ContentController extends Controller
|
||||
Setting::set('announcement_enabled', '0');
|
||||
}
|
||||
|
||||
if ($request->has('shop_offline_submitted')) {
|
||||
Setting::set('shop_offline', $request->boolean('shop_offline') ? '1' : '0');
|
||||
}
|
||||
|
||||
foreach ($fields as $field) {
|
||||
if ($request->has($field)) {
|
||||
$value = $request->input($field);
|
||||
@@ -168,4 +176,20 @@ class ContentController extends Controller
|
||||
->with('error', "Fout bij verzenden van e-mail via {$defaultMailer}: ".$e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggle the webshop offline / online status.
|
||||
*/
|
||||
public function toggleOffline(Request $request): RedirectResponse
|
||||
{
|
||||
$current = Setting::get('shop_offline', '0') === '1';
|
||||
$new = ! $current;
|
||||
Setting::set('shop_offline', $new ? '1' : '0');
|
||||
Setting::flushCache();
|
||||
|
||||
$statusText = $new ? 'OFFLINE gezet' : 'weer ONLINE gezet';
|
||||
|
||||
return redirect()->back()
|
||||
->with('success', "De webshop is direct {$statusText}!");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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