CluPilotCloud/app/Http/Middleware/EnsureAdmin.php

31 lines
859 B
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();
if ($user instanceof User && $user->is_admin && ! $user->isOperator()) {
$user->assignRole('Owner');
}
abort_unless((bool) $user?->can('console.view'), 403);
return $next($request);
}
}