42 lines
1.3 KiB
PHP
42 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
use Closure;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Artisan;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
class EnsureUserIsAdmin
|
|
{
|
|
/**
|
|
* Handle an incoming request.
|
|
*/
|
|
public function handle(Request $request, Closure $next): Response
|
|
{
|
|
if (! auth()->check()) {
|
|
return redirect()->guest(route('login'))
|
|
->with('error', 'Log eerst in met je beheerder-account om toegang te krijgen tot het admin gedeelte.');
|
|
}
|
|
|
|
if (! auth()->user()->isAdmin()) {
|
|
return redirect()->route('home')
|
|
->with('error', 'Je hebt geen beheerderstoegang tot deze pagina.');
|
|
}
|
|
|
|
// Auto-migrate check: Ensure that new database tables (like communications) are migrated
|
|
// automatically when an admin enters the admin panel, avoiding 500 errors on Coolify/production.
|
|
try {
|
|
if (! Schema::hasTable('communications')) {
|
|
Artisan::call('migrate', ['--force' => true]);
|
|
}
|
|
} catch (\Throwable $e) {
|
|
Log::warning('Auto-migration in EnsureUserIsAdmin failed: '.$e->getMessage());
|
|
}
|
|
|
|
return $next($request);
|
|
}
|
|
}
|