CluPilotCloud/app/Http/Middleware/EnsureAdmin.php

33 lines
1.0 KiB
PHP

<?php
namespace App\Http\Middleware;
use App\Models\User;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
class EnsureAdmin
{
/**
* Only operators (a user holding an RBAC role) may reach the admin console.
* A legacy is_admin account with no role is self-healed into Owner on access
* — migrating it into RBAC rather than permanently trusting the old flag, so
* such accounts are never locked out yet the role boundary still governs.
*/
public function handle(Request $request, Closure $next): Response
{
$user = $request->user();
// Only a genuinely un-migrated legacy admin (is_admin, but NO roles at
// all) is promoted — never override an intentional demotion/other role.
if ($user instanceof User && $user->is_admin && $user->roles->isEmpty()) {
$user->assignRole('Owner');
}
abort_unless((bool) $user?->can('console.view'), 403);
return $next($request);
}
}